Professional Documents
Culture Documents
Q.2 What is tree traversal? What are different methods of tree traversal? Write an
algorithm for PREORDER traversal and explain.
Ans: The traversal is a method of processing every node in the tree exactly once in a systematic manner.
Types of traversal are:
- Pre order traversal - In order traversal - Post order traversal
Algorithm: PREORDER (T)
- This function traverses the tree in pre-order
- T is a pointer which points to the root node of the tree
Step1. [process the root node]
If T != NULL
then write (DATA (T))
else write (‘EMPTY TREE’)
return
step2. [process the left sub tree]
if LPTR (T) != NULL
then call PREORDER (LPTR(T))
step3. [process the right sub tree]
if RPTR (T) != NULL
then call PREORDER (RPTR(T))
step4. [finished]
return
- In this figure of binary tree, node 1 is root node which has node 3 as left
sub tree and node 6 as right sub tree.
- Node 1, 3, 6 are non terminal nodes as they do not have out degree = 0
- Node 5, 9 and 8 have out degree = 0 so they are terminal nodes or leaf
nodes.
68
45
- Now, if we want to insert the node 35, its value is less than root
value. So it is inserted at the right of the root, like: 35 68
45
- Now, do the same process for inserting all other remaining nodes as 35 68
describes above. Finally we get
15 42
3) If the node which is to be deleted has left and right sub tree., then
we have to do following steps:
- Find in order successor of the deleted node
- Append the right sub tree of the in order successor to its grand
parent
- Replace the node to be deleted with its in order successor
node
- Suppose in this tree if we want to delete node 7 which has non
empty left and right sub tree. Then find out the in order
successor for the node 7 which is node 5
- Now replace node 7 with node 5 (remove the link of node 5)
and now pointer of node 2 is pointing to node 5.
Q.6 Explain list representation of binary tree with suitable example
Ans: We can represent binary tree in memory by two methods.
- Array representation - Linked implementation
1) Array representation
- In array representation of binary tree, consider two cases
1. Array implantation of complete binary tree
- A complete binary tree has one node at root level, two nodes at level two, four at
level 3 and so on.
Index 1 2 3 4 5 6 7
Info A B C D E F G
- In this type of implementation, location of left child of node I is given by 2*I and the
location of right child of node I is given by 2*I+1. i.e in this tree the left child of node
C (index 3) is 3 * 2 = 6, that is node F
- Similarly the location of the parent node j is given by TRUNC (j/2). i.e. parent of node
D (index 4) is 4/2=2 (that is node B)
2. Array implantation of incomplete binary tree
Index 1 2 3 4 5 6 7 8 9 10 11 12 13
Info F B G A D - I - - C E H -
- In this type of implementation, location of left child of node I is given by 2*I and the
location of right child of node I is given by 2*I+1. i.e in this tree the left child of node
B (index 2) is 2 * 2 = 4, that is node A
- But here, for node A (index 4) has left child 2*4=8, but index 8 is empty. So wastage
of memory is there in this case.
- Similarly the location of the parent node j is given by TRUNC (j/2). i.e. parent of node
D (index 4) is 4/2=2 (that is node B)
- Array implementation is not efficient for this method.
2) Linked representation
- in binary tree there is a node called root node, this root node again has two parts: left sub tree and
right sub tree.
- Thus a node in binary tree is represented as below
LPTR INFO RPTR
- INFO contain the actual value of the node
- LPTR is a pointer points to left child node
- RPTR is a pointer points to right child node
- If a node which do not have either of left or right child empty then LPTR or RPTR is NULL
- Binary tree using linked list is shown below
Binary Tree Linked representation
A
B C
D E F G
Q.7 Write Tree Order Traversal (In order, Preorder, Post order) of the given tree
(C) (D)
(A) (B)
For tree A:
- Pre order: G D B F G C M L P
- In order: BDEFGCLMP
- Post order: B E F D L P M C G
For tree B:
- Pre order: A B D G C E H I F
- In order: DGBAHEICF
- Post order: G D B H I E F C A
For tree C:
- Pre order: M E B A D L P N V T Z
- In order: ABDELMNPTVZ
- Post order: A D B L E N T Z V P M
For tree D:
- Pre order: A B C E I F J D G H K L
- In order: E I C J F B G D K H L A
- Post order: I E J F C G K L H D B A