0% found this document useful (0 votes)
59 views

Binary Tree Traversal Methods: Welcome To CS221: Programming & Data Structures

The document discusses different methods for traversing binary trees, including pre-order, in-order, post-order, and level-order traversal, and provides examples and algorithms for each traversal method. It also notes that while traversal sequences can provide information about a binary tree, the exact binary tree structure cannot always be uniquely reconstructed from a given traversal sequence alone.

Uploaded by

PRITAM RAJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Binary Tree Traversal Methods: Welcome To CS221: Programming & Data Structures

The document discusses different methods for traversing binary trees, including pre-order, in-order, post-order, and level-order traversal, and provides examples and algorithms for each traversal method. It also notes that while traversal sequences can provide information about a binary tree, the exact binary tree structure cannot always be uniquely reconstructed from a given traversal sequence alone.

Uploaded by

PRITAM RAJ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Welcome to CS221: Programming & Data Structures

Binary Tree traversal methods


• Topics covered are: Binary tree traversal methods, binary tree construction
• Note:
1. The slides are adapted from the textbook “Data Structures Using C” by Reema Theraja, Oxford University Press and from online materials.
2. Online materials from the home page of Prof. Sartaj Sahni, Distinguished Professor of Computer and Information Sciences and Engineering at the University of Florida
Traversal

In a traversal of a binary tree, each element of the binary tree is


visited exactly once.
During the visit of an element, all action (make a clone, display,
evaluate the operator, etc.) with respect to this element is
taken.
Binary Tree Traversal Methods
• Pre-order
• In-order
• Post-order
• Level-order
Pre-order Traversal
Recursive steps: Algorithm
1. Visit the root node,
2. Traverse the left sub-tree, and finally
3. Traverse the right sub-tree.
Pre-order Example
1. Print the value: a
a 20. Visit right subtree
2. Visit left subtree

3. Print the value: b


b 14. Visit right subtree
c 21. Print the value: c

4. Visit left subtree 22. Visit left subtree

f 23. Print the value: f


5. Print the value: d
d 10. Visit right subtree
e 15. Print the value: e
24. Visit right subtree
6. Visit left subtree 16. Visit right subtree

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

12. Print the value: b


b 13. Visit right subtree
c 28. Print the value: c

2. Visit left subtree 21. Visit left subtree

f 22. Left subtree is null


7. Print the value: d
d 8. Visit right subtree
e 14. Left subtree is null
15. Print the value: e
23. Print the value: f
24. Visit right subtree
3. Visit left subtree
16. Visit right 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

18. Print the value: b


b 12. Visit right subtree
c 26. Print the value: c

2. Visit left subtree 20. Visit left subtree

f 25. Print the value: f


11. Print the value: d
d 7. Visit right subtree
e 17. Print the value: e
21. Visit right subtree
3. Visit left subtree
13. Visit right 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

2. Print the value: b


b c 3. Print the value: c

f 6. Print the value: f


4. Print the value: d
d e 5. Print the value: e

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

• Can you construct the binary tree,


given two traversal sequences?
• Depends on which two sequences are
given.
Pre-order And Post-order

pre-order = ab a a
post-order = ba b b

• pre-order and post-order do not uniquely define a binary tree.


• Nor do pre-order and level-order (same example).
• Nor do post-order and level-order (same example).
In-order And Pre-order
• in-order = g d h b e i a f j c
• pre-order = a b d g h e i c f j
• Scan the pre-order left to right using the in-order
to separate left and right subtrees.
• a is the root of the tree; gdhbei are in the left
subtree; fjc are in the right subtree.

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

• Scan post-order from right to left using in-order to


separate left and right subtrees.
• in-order = g d h b e i a f j c
• post-order = g h d i e b j f c a
• Tree root is a; gdhbei are in left subtree; fjc are in
right subtree.
In-order And Level-Order

• Scan level-order from left to right using in-order to


separate left and right subtrees.
• in-order = g d h b e i a f j c
• level-order = a b c d e f g h i j
• Tree root is a; gdhbei are in left subtree; fjc are in
right subtree.
Solve:
1. Given, in-order traversal = g d h b e i a f j c, post-order traversal= g h d i e b j f c a.
Construct a binary tree completely.
2. Given, in-order traversal = g d h b e i a f j c, level-order traversal= a b c d e f g h i j.
Construct a binary tree completely.
3. Given below is a binary tree. Visit the nodes in pre-order, in-order, post-order and level-order and print the desired values.

You might also like