Trees Flashcards

1
Q

What are the two ways you could view a tree as?

A

Generalisation of a linked list

Specialisation of a graph

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

Define a tree

A

Abstract data type that simulates a hierarchical tree structure, with a root value and subtrees with children

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

What makes a binary tree special?

A

Each node has at most two child nodes

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

What three things are stored in a class for a BinaryTreeNode?

A

element
left node
right node

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

Define traversal

A

Systematic way of accessing or visiting all the nodes

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

Complexity for insert in a tree

A

Linear

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

Complexity for contains in a tree

A

Linear

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

Complexity for remove in a tree

A

Linear

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

Complexity for size in a tree

A

Linear

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

What are the four strategies for traversal?

A
Breadth first
Depth first (in order, post order, pre order)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe in order traversal

A

Visit the first child, visit the node, visit the next
node/child
l n r

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

Describe post order traversal

A

Visit the children, visit the node

l r n

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

Describe pre order traversal

A

Visit the node, visit the children

n l r

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

Write a recursive function that does pre order traversal of a tree

A

public void traverse(BinaryTreeNode node){
visit(node.element)
if(node.left !=null) traverse(node.left)
if(node.right !=null) traverse(node.right)
}

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

Write a recursive function that does post order traversal of a tree

A

public void traverse(BinaryTreeNode node){
if(node.left !=null) traverse(node.left)
if(node.right !=null) traverse(node.right)
visit(node.element)
}

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

Write a recursive function that does in order traversal of a tree

A

public void traverse(BinaryTreeNode node){
if(node.left !=null) traverse(node.left)
visit(node.element)
if(node.right !=null) traverse(node.right)
}

17
Q

What is an expression tree?

A

A tree where in each node can hold an operator or an operand
Different traversals generate different notations and results

18
Q

Give the name for this notation and what kind of traversal is used to generate it
+ 2 3

A

Polish notation

Pre-order traversal

19
Q

Give the name for this notation and what kind of traversal is used to generate it
2 + 3

A

Infix notation

In order traversal

20
Q

Give the name for this notation and what kind of traversal is used to generate it
2 3 +

A

Reverse Polish notation

Post order traversal

21
Q

What is another name for breadth first traversal?

A

Level order traversal

22
Q

What are the two types of search that can be performed on trees?

A

Depth first search (DFS)

Breadth first search (BFS)

23
Q

Describe a depth first search

A

Start at the root

Go as far down as possible each branch before backtracking if the goal has not been found

24
Q

Describe a breadth first search

A

Start at the root
Examine all the child nodes
For each child node, examine it’s child nodes
and so on…

25
Q

Desirable features for searching/sorting data structures in terms of constraints

A

Unbounded size

Dynamic

26
Q

Desirable features for searching/sorting data structures in terms of operations

A

Add/remove

Search/sort

27
Q

Desirable features for searching/sorting data structures in terms of complexities

A
Everything O(1) or as close as possible
Keep operations scale-able