Binary Tree Constructionusing given tree traversal
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
Binary Tree Constructionusing given tree traversal
Can you construct the binary tree, given two
traversal sequences?
Depends on which two sequences are given
Inorder And Preorder
Inorder And Postorder
5.
Binary Tree Constructionusing given tree traversal
By given preorder and postorder we will come to
know which is the root node.
Ones we learn about the root node we can learn
about the LST (Left SubTree) and RST (Right
SubTree)
Repeat these 2 steps until the end.
6.
Example
Given preorder:ABCDE
Given Inoder: BADCE
Step1: By given preorder we can identify A is root
node.
Step 2: Find that root node in given inorder sequence.
BADCE
7.
Cont…
Result ofabove steps are:
Next root node is B but there is no left and right child
Repeat step 1 and 2 again
Inorder And Preorder
preorder = a b d g h e i c f j
inorder = g d h b e i a f j c
Scan the preorder left to right
using the inorder to separate
left and right subtrees
1. a is the root of the tree;
gdhbei are in the left subtree;
fjc are in the right subtree
2. b is the next root;
gdh are in the left subtree;
ei are in the right subtree
3. d is the next root;
g is in the left subtree;
h is in the right subtree
a
gdhbei fjc
a
gdh
fjc
b
ei
a
g
fjc
b
ei
d
h
11.
Inorder And Preorder
preorder = a b d g h e i c f j
inorder = g d h b e i a f j c
Scan the preorder left to right
using the inorder to separate
left and right subtrees
1. g is the next root;
NULL are in the left subtree;
NULL are in the right subtree
2. h is the next root;
NULL is in the left subtree;
NULL is in the right subtree
3. e is the next root;
NULL is in the left subtree;
i is in the right subtree
a
g
fjc
b
ei
d
h
a
g
fjc
b
d
h
e
i
12.
Inorder And Preorder
preorder = a b d g h e i c f j
inorder = g d h b e i a f j c
Scan the preorder left to right
using the inorder to separate
left and right subtrees
1. i is the next root;
NULL are in the left subtree;
NULL are in the right subtree
2. c is the next root;
J F is in the left subtree;
NULL is in the right subtree
3. f is the next root;
NULL is in the left subtree;
j is in the right subtree
a
g
fjc
b
d
h
e
i
a
g
b
d
h
e
i
c
fj