Trees (chapter 8) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Define a tree in terms of models and structure

A

A tree is an abstract model that stores element hierarchically

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a tree consist of?

A

consists of nodes that have a parent-child relationship with the exception of the root which is a special node that has no parent. Along with possible subtrees

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define internal and external nodes

A

an internal node is a node that has a child/children. An external node is a node with no children, also known as a leaf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define an edge

A

an edge is pair of nodes, (u,v) such that u,v have a parent-child relationship.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define a path

A

a path is a sequence of nodes situated in such a manner that any 2 consecutive nodes in the sequence form an edge

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In what 3 areas can trees be applied?

A
  1. Organization charts (CEO and employees under him/her)
  2. File systems. There is a root folder, I go to documents, then CSC, then lectures but there are many other paths that I can take
  3. Programming environments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define a sub-tree

A

A tree consists of a node and its descendants. A tree can be a subtree of itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe the depth of a tree.

A
  • depth is described as the number of edges present from the root of a tree to the particular NODE in question (excluding the node)
  • also defined as the number of ancestors a node has (excluding the node)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give code on how to find the depth of a tree recursively

A
int depth(node v){
if v is root
return 0 
else
return 1 + depth(parent(v))
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the height of a tree (hint, an external node has a height of 0)

A

The height of a tree is the largest depth of a node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give code to find the height of a node

A
int height(Node v){
if v isExternal
return 0
else
return 1 + max(height(child(v)))
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What ADT do we use to define a tree why is it useful

A

We use position ADT, why? Beats me

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is a single node in a tree represented?

A

They are represented by an object that stores an element, a parent of the node, and a sequence of children of the node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

3 types of tree traversals?

A

Post order, pre-order and inorder traversal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a pre-order traversal and where can it be applied?

A

in a pre-order, a node is visited before its descendants.

the application would be printing a structured document, the headings and subheadings must be printed before the text can be printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Provide code for pre-order traversal

A

visit(v)
for each child of v, w
preOrder(w)

  • Pre-order is called on each child of the node
17
Q

What happens in a post-order traversal and what are the applications?

A

the descendants of a node are visited before the node itself

the application would be a computation of the size of files and their subdirectories. You have to first calculate the size of the deepest files and tally them up as you go, only at the end do you get the total size of the folder

18
Q

code for post-order traversals?

A

for each child w, of v
postOrder(w)
visit(v)

all descendants visited before the node in question is visited

19
Q

What is a binary tree and what are its properties(concerning children)

A
  • A binary tree is a data structure with a parent node having at most 2 children nodes (0,1,2)
  • The children of binary trees form an order per and can be distinguished as the left and right child
20
Q

What is a proper binary tree?

A

It is a binary tree that has the restriction that an internal node must strictly have either 0 or 2 children

21
Q

Define a binary tree in terms of recursion

A

A tree consisting of a single node, or the root has an ordered pair of children, each of which are a binary tree (being itself or having 2 children, thus a binary tree)

22
Q

What are the applications of a binary tree?

A
  1. Decision-making. A yes or no tree
  2. Searching (?)
  3. Arithmetic expressions
23
Q

What are the internal and external nodes of an arithmetic binary tree? Decision making?

A
  • internal nodes are operands and external nodes are

* questions are the internal nodes(having yes/no answer), external nodes are the yes/no

24
Q

How does a linked binary tree operate?

A

An object will store a reference to the element, the parent, the left child and the right child

25
Q

Define a binary tree recursively

A

A tree with a single node, the root. Or whose root consists of children who form an ordered pair. Each of which form a tree

26
Q

How is an inorder tree implemented and what are its applications?

A

Left subtree is visited before the parent, the right subtree is visited thereafter.

Application is drawing a binary tree

27
Q

Provide psuedo-code for the inorder traversal

A

inOrder(Node v){

if(hasLeft(v)){
inOrder(v.left)
}
visit(v)
if(hasRight){
inOrder(v.right)
}
}
28
Q

What is the Euler tour? How many times is each node visited?

A

Euler tour is a kind of traversal that includes special cases of the pre, in and post traverals. Each node is visited 3 times

29
Q

What is a template method pattern? (3)

A
  • generic algorithm that can be specialized by redefining certain steps
  • implemented using an abstract java class
  • visit methods are redefined by the subclasses