Tree Traversals Flashcards

(8 cards)

1
Q

What is graph traversal?

A

The process of systematically visiting each node in a graph.

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

What is breadth first traversal?

A

Nodes closest to the start node are visited first.
E.g. finding the shortest path through an unweighted graph (Dijkstra).

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

What is depth first traversal?

A

Exploring as far as possible away from the start node.
E.g. solving puzzles like mazes.

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

What is the time complexity of tree traversals?

A

O(n) because every node is visited once.

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

What are the three types of tree traversal?

A

Pre-order, in-order and post-order.
All tree traversals are depth first traversals.

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

Explain pre-order traversal.

A

Pattern: visit, left, right
Use: copying a tree

In pre-order traversals, the node is visited before traversing its left and right subtrees.
That’s why the root is the first node outputted.

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

Explain in-order traversal.

A

They are used for binary search trees only.

Pattern: left, visit, right
Use: outputting a binary search tree in ascending order

In-order traversals will output the contents of a binary search tree in order. It only outputs the node once all the nodes on its left have been output.

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

Explain post-order traversal.

A

Pattern: left, right, visit
Use: converting infix to RPN (RPN is post-fix)

Post-order only outputs the node once it has output all the nodes on its left and right. The root is the last node to be output/visited.

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