DS Module 4 Trees
DS Module 4 Trees
D H I
B C F
Transforming a forest into binary tree
• If T1, T2, T3, T4, ….., Tn is a forest of trees, then
the binary tree corresponding to this forest,
denoted by B(T1, T2, T3, T4, ….., Tn)
1. Is empty if n=0
2. Has root = root(T1), has left subtree=B(T11, T12,
T13, T14, ….., T1m ) where subtree=B(T11, T12, T13,
T14, ….., T1m are subtrees of root(T1) and has
right subtree = B(T2, T3, T4, ….., Tn)
• Binary tree representation of the forest of
Fig5.37
A
B E
C F G
D H
I
Forest traversals
Forest preorder
1. If F is empty then return
2. Visit the root of the first tree of F
3. Traverse the subtrees of the first tree in forest
preorder
4. Traverse the remaining trees of F in forest preorder
Forest inorder
1. If F is empty then return
2. Traverse the subtrees of the first tree in
forest inorder
3. Visit the root of the first tree of F
4. Traverse the remaining trees of F in forest
inorder
Forest postorder
1. If F is empty then return
2. Traverse the subtrees of the first tree in
forest postorder
3. Traverse the remaining trees of F in forest
postorder
4. Visit the root of the first tree of F