Indian Institute of Information Technology, Nagpur: Course: Data Structures (CSL 102, Core)
Indian Institute of Information Technology, Nagpur: Course: Data Structures (CSL 102, Core)
Nagpur
Ans: d
• Problem 03:
• If the binary tree in figure is traversed in
inorder, then the order in which the nodes will
be visited is ____?
Ans:
• Problem 04:
• Which of the following sequences denotes the
postorder traversal sequence of the tree
shown in figure?
a) FEGCBDBA
b) GCBDAFE
c) GCDBFEA
d) FDEGCBA
Ans: c
• Problem 05:
• Which of the following binary trees has its
inorder and preorder traversals as BCAD and
ABCD respectively.
• Ans: d
• Problem 06:
• Traverse the following binary tree in pre, post,
inorder and level order.
• Ans:
• Preorder
• P, F , B, H, G , S, R, Y, T, W, Z
• Postorder
• B, G , H, F , R, W, T, Z, Y, S, P
• Inorder
• B, F , G , H, P, R, S, T, W, Y, Z
• Level order
• P, F , S, B, H, R, Y, G , T, Z, W
• Problem 07:
• Traverse the following binary tree in pre, post,
inorder and level order.
• Ans:
• Preorder
• A, B, D, G , K, H, L, M , C , E
• Postorder
• K, G , L, M , H, D, B, E, C , A
• Inorder
• K, G , D, L, H, M , B, A, E, C
• Level order
• A, B, C , D, E, G , H, K, L, M
• Problem 08:
• Construct a binary tree from a given preorder and
inorder sequence:
Preorder: A B D G C E H I F
Inorder: D G B A H E I C F
Ans:
• From Preorder sequence A B D G C E H I F, the root is: A
• From Inorder sequence D G B A H E I C F, we get the
left and right sub trees:
• Left sub tree is: D G B
• Right sub tree is: H E I C F
• To find the root, left and right sub trees for D G
B:
• From the preorder sequence B D G, the root of
tree is: B
• From the inorder sequence D G B, we can find
that D and G are to the left of B.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for D G:
• From the preorder sequence D G, the root of the
tree is: D
• From the inorder sequence D G, we can find that
there is no left node to D and G is at the right of D.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for H E I C
F:
• From the preorder sequence C E H I F, the root of
the left sub tree is: C
• From the inorder sequence H E I C F, we can find
that H E I are at the left of C and F is at the right of
C.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for H E I:
• From the preorder sequence E H I, the root of the
tree is: E
• From the inorder sequence H E I, we can find that
H is at the left of E and I is at the right of E.
• The Binary tree upto this point looks like:
• Problem 09:
• Construct a binary tree from a given postorder and inorder
sequence:
Inorder: D G B A H E I C F
Postorder: G D B H I E F C A
Ans:
• From Postorder sequence G D B H I E F C A, the root is: A
• From Inorder sequence D G B A H E I C F, we get the left and
right sub trees:
• Left sub tree is: D G B
• Right sub tree is: H E I C F
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for D G B:
• From the postorder sequence G D B, the root of
tree is: B
• From the inorder sequence D G B, we can find
that D G are to the left of B and there is no right
subtree for B.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for D G:
• From the postorder sequence G D, the root of the
tree is: D
• From the inorder sequence D G, we can find that
is no left subtree for D and G is to the right of D.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for H E I C
F:
• From the postorder sequence H I E F C, the root of
the left sub tree is: C
• From the inorder sequence H E I C F, we can find
that H E I are to the left of C and F is the right
subtree for C.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for H E I:
• From the postorder sequence H I E, the root of the
tree is: E
• From the inorder sequence H E I, we can find that
H is left subtree for E and I is to the right of E.
• The Binary tree upto this point looks like: