Javatpoint Logo
Javatpoint Logo

Traversing Binary Trees

Traversing means to visit all the nodes of the tree. There are three standard methods to traverse the binary trees. These are as follows:

  1. Preorder Traversal
  2. Postorder Traversal
  3. Inorder Traversal

1. Preorder Traversal: The preorder traversal of a binary tree is a recursive process. The preorder traversal of a tree is

  • Visit the root of the tree.
  • Traverse the left subtree in preorder.
  • Traverse the right subtree in preorder.

2. Postorder Traversal: The postorder traversal of a binary tree is a recursive process. The postorder traversal of a tree is

  • Traverse the left subtree in postorder.
  • Traverse the right subtree in postorder.
  • Visit the root of the tree.

3. Inorder Traversal: The inorder traversal of a binary tree is a recursive process. The inorder traversal of a tree is

  • Traverse in inorder the left subtree.
  • Visit the root of the tree.
  • Traverse in inorder the right subtree.

Example: Determine the preorder, postorder and inorder traversal of the binary tree as shown in fig:

Discrete Mathematics Traversing Binary Trees

Solution: The preorder, postorder and inorder traversal of the tree is as follows:

Preorder 1 2 3 4 5 6 7 8 9 10 11
Postorder 3 5 4 2 7 10 9 11 8 6 1
Inorder 3 2 5 4 1 7 6 9 10 8 11

Algorithms:

(a)Algorithm to draw a Unique Binary Tree when Inorder and Preorder Traversal of the tree is Given:

  1. We know that the root of the binary tree is the first node in its preorder. Draw the root of the tree.
  2. To find the left child of the root node, first, use the inorder traversal to find the nodes in the left subtree of the binary tree. (All the nodes that are left to the root node in the inorder traversal are the nodes of the left subtree). After that, the left child of the root is obtained by selecting the first node in the preorder traversal of the left subtree. Draw the left child.
  3. In the same way, use the inorder traversal to find the nodes in the right subtree of the binary tree. Then the right child is obtained by selecting the first node in the preorder traversal of the right subtree. Draw the right child.
  4. Repeat the steps 2 and 3 with each new node until every node is not visited in the preorder. Finally, we obtain a unique tree.

Example: Draw the unique binary tree when the inorder and preorder traversal is given as follows:

Inorder B A D C F E J H K G I
Preorder A B C D E F G H J K I

Solution: We know that the root of the binary tree is the first node in preorder traversal. Now, check A, in the inorder traversal, all the nodes that are of left A, are nodes of left subtree and all the nodes that are right of A, are nodes of right subtree. Read the next node in the preorder and check its position against the root node, if its left of the root node, then draw it as a left child, otherwise draw it a right child. Repeat the above process for each new node until all the nodes of the preorder traversal are read and finally we obtain the binary tree as shown in fig:

Discrete Mathematics Traversing Binary Trees

(b) Algorithm to draw a Unique Binary Tree when Inorder and Postorder Traversal of the tree is Given:

  1. We know that the root of the binary tree is the last node in its postorder. Draw the root of the tree.
  2. To find the right child of the root node, first, use the inorder traversal to find the nodes in the right subtree of the binary tree. (All the nodes that are right to the root node in the inorder traversal are the nodes of the right subtree). After that, the right child of the root is obtained by selecting the last node in the postorder traversal of the right subtree. Draw the right child.
  3. In the same way, use the inorder traversal to find the nodes in the left subtree of the binary tree. Then the left child is obtained by selecting the last node in the postorder traversal of the left subtree. Draw the left child.
  4. Repeat the steps 2 and 3 with each new node until every node is not visited in the postorder. After visiting the last node, we obtain a unique tree.

Example: Draw the unique binary tree for the given Inorder and Postorder traversal is given as follows:

Inorder 4 6 10 12 8 2 1 5 7 11 13 9 3
Postorder 12 10 8 6 4 2 13 11 9 7 5 3 1

Solution: We know that the root of the binary tree is the last node in the postorder traversal. Hence, one in the root node.

Now, check the inorder traversal, we know that root is at the center, hence all the nodes that are left to the root node in inorder traversal are the nodes of left subtree and, all that are right to the root node are the nodes of the right subtree.

      Now, visit the next node from back in postorder traversal and check its position in inorder traversal, if it is on the left of root then draw it as left child and if it is on the right, then draw it as the right child.

Repeat the above process for each new node, and we obtain the binary tree as shown in fig:

Discrete Mathematics Traversing Binary Trees

(c)Algorithm to convert General Tree into the binary tree

  1. Starting from the root node, the root of the tree is also the root of the binary tree.
  2. The first child C1(from left) of the root node in the tree is the left child C1 of the root node in the binary tree, and the sibling of the C1 is the right child of C1 and so on.
  3. Repeat the step2 for each new node.

Example: Convert the following tree as shown in fig into a binary tree.

Discrete Mathematics Traversing Binary Trees

Solution: The root of the tree is the root of the binary tree. Hence A is the root of the binary tree. Now B becomes the left child of A in a binary tree, C becomes the right child of B, D becomes right child of C, and E becomes the right child of D in the binary tree and similarly applying the algorithm we obtain the binary tree as shown in fig:

Discrete Mathematics Traversing Binary Trees




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA