Professional Documents
Culture Documents
Outline
Breadth-first traversal
Depth-first traversal
Tree Traversals
Unlike an array, its not always obvious how
Breadth-first traversal
Starting from the root
Breadth-first Traversal:
A,B,C,D,E,F,G,H,I
order of nodes
Pseudocode:
function bft(root):
//Input: root node of tree
//Output: None
Q = new Queue()
enqueue root
while Q is not empty:
node = Q.dequeue()
visit(node)
enqueue nodes left & right children
Depth-first traversal
Starting from the root
Depth-first Traversal:
A,C,G,F,B,E,I,H,D
or
A,B,D,E,H,I,C,F,G
order of nodes
Pseudocode:
function dft(root):
//Input: root node of tree
//Output: none
S = new Stack()
push root onto S
while S is not empty:
node = S.pop()
visit(node)
push nodes left and right children
Visualizations
BFT
DFT
Pre-order traversal
function preorder(node):
//Input: root node of tree
//Output: None
visit(node)
if node has left child
preorder(node.left)
if node has right child
preorder(node.right)
Pre-order Traversal:
A,B,D,E,H,I,C,F,G
Note: this is similar to iterative DFT
Post-order traversal
function postorder(node):
//Input: root node of tree
//Output: None
if node has left child
postorder(node.left)
if node has right child
postorder(node.right)
visit(node)
Post-order Traversal:
D,H,I,E,B,F,G,C,A
In-order traversal
function inorder(node):
//Input: root node of tree
//Output: None
if node has left child
inorder(node.left)
visit(node)
if node has right child
inorder(node.right)
In-order Traversal:
D,B,H,E,I,A,F,C,G
Visualizations
Pre-Order
Think of the
prefix as when
the root is visited!
In-order
Post-order
Preorder gif: http://ceadserv1.nku.edu/longa//classes/mat385_resources/docs/traversal_files/PreOrderTrav.gif
Inorder gif: http://ceadserv1.nku.edu/longa//classes/mat385_resources/docs/traversal_files/InorderTrav.gif
Postorder gif: http://ceadserv1.nku.edu/longa//classes/mat385_resources/docs/traversal_files/PostorderTrav.gif
binary tree
Includes as special
cases the pre-order,
post-order, and in-order
traversals
Walk around the tree
and visit each node
three times:
On the left (pre-order)
From below (in-order)
On the right (post-order)
nodes
27
17
visitLeft(node) // Pre-order
15
H 10 12 I
9
19
20
11
26
22
6
3
visitBelow(node) // In-order
18
16
13
14
21 23 G
24
25
Aside: Decoration
Often youll be asked to decorate each
Practice Problem 1
Decorate each node with the number of its
descendants
Best traversal: post-order
Its easy to calculate the number descendants
of a node if you already know how many
descendants each of its children has
Since post-order traversal visits both children
before the parent, this is the traversal we want
Try writing some pseudocode for this!
Practice Problem 2
Given the root of a tree, determine if the tree
is perfect
Best traversal: breadth-first
This problem is easiest solved by traversing the
tree level-by-level
We can keep track of the current level, and at each
level count the number of nodes we see
Each level should have exactly twice as many
nodes as the last
There are other ways to solve this problem too can you think of any?
Practice Problem 3
Given an arithmetic expression tree, evaluate the
expression
+
/
(7 (4 + 3)) + (9 / 3)
7
+
4
Practice Problem 4
Given an arithmetic expression tree, print
74+3+9/3
7
+
4
Practice Problem 5
Given an arithmetic expression tree, print out the
+
4