Binary Tree Traversal Methods: Welcome To CS221: Programming & Data Structures
Binary Tree Traversal Methods: Welcome To CS221: Programming & Data Structures
g h i j
7. Print the value: g 11. Print the value: h 17. Print the value: i 25. Print the value: j
8. Left subtree is null 12. Left subtree is null 18. Left subtree is null 26. Left subtree is null
9. Right subtree is null 13. Right subtree is null 19. Right subtree is null 27. Right subtree is null
Pre-order traversal: a b d g h e i c f j
In-order Traversal
Recursive steps: Algorithm
1. Traverse the left sub-tree,
2. Visit the root node, and finally
3. Traverse the right sub-tree.
In-order Example
19. Print the value: a
a 20. Visit right subtree
1. Visit left subtree
g h i j
4. Left subtree is null 9. Left subtree is null 17. Left subtree is null 25. Left subtree is null
5. Print the value: g 10. Print the value: h 18. Print the value: i 26. Print the value: j
6. Right subtree is null 11. Right subtree is null 19. Right subtree is null 27. Right subtree is null
In-order traversal: g d h b e i a f j c
Post-order Traversal
Recursive steps: Algorithm
1. Traverse the left sub-tree,
2. Traverse the right sub-tree, and finally
3. Visit the root node
Post-order Example
27. Print the value: a
a 19. Visit right subtree
1. Visit left subtree
g h i j
4. Left subtree is null 8. Left subtree is null 14. Left subtree is null 22. Left subtree is null
5. Right subtree is null 9. Right subtree is null 15. Right subtree is null 23. Right subtree is null
6. Print the value: g 10. Print the value: h 16. Print the value: i 24. Print the value: j
Post-order traversal: g h d i e b j f c a
Level-order Traversal
In level-order traversal, all the nodes at a level Algorithm: Apply a Queue for Level-order traversal
are accessed before going to the next level. This
algorithm is also called as the breadth-first • Create empty queue and push root node to
traversal algorithm it.
• Do the following when queue is not empty
• Pop a node from queue and print it
• Push left child of popped node
to queue if not null
• Push right child of popped node
to queue if not null
Level-order Example
1. Print the value: a
a
g h i j
7. Print the value: g 8. Print the value: h 9. Print the value: i 10. Print the value: j
Level-order traversal: a b c d e f g h i j
Binary Tree Construction
• Suppose that the elements in a binary tree are
distinct.
• Can you construct the binary tree from which a
given traversal sequence came?
• When a traversal sequence has more than one
element, the binary tree is not uniquely defined.
• Therefore, the tree from which the sequence was
obtained cannot be reconstructed uniquely.
Some Examples
pre-order a a
= ab
b b
in-order b a
= ab a b
post-order b b
= ab a a
level-order a a
= ab b b
Binary Tree Construction
pre-order = ab a a
post-order = ba b b
gdhbei fjc
In-order And Pre-order
a
gdhbei fjc
• pre-order = a b d g h e i c f j
• b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a
b fjc
gdh ei
In-order And Pre-order
a
b fjc
gdh ei
• pre-order = a b d g h e i c f j
• d is the next root; g is in the left
subtree; h is in the right subtree.
a
b fjc
d ei
g h
In-order And Post-order