## Traversing Binary TreesTraversing means to visit all the nodes of the tree. There are three standard methods to traverse the binary trees. These are as follows: - Preorder Traversal
- Postorder Traversal
- Inorder Traversal
- Visit the root of the tree.
- Traverse the left subtree in preorder.
- Traverse the right subtree in preorder.
- Traverse the left subtree in postorder.
- Traverse the right subtree in postorder.
- Visit the root of the tree.
- Traverse in inorder the left subtree.
- Visit the root of the tree.
- Traverse in inorder the right subtree.
## Algorithms:
- We know that the root of the binary tree is the first node in its preorder. Draw the root of the tree.
- 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.
- 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.
- 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.
- We know that the root of the binary tree is the last node in its postorder. Draw the root of the tree.
- 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.
- 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.
- 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.
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:
- Starting from the root node, the root of the tree is also the root of the binary tree.
- The first child C
_{1}(from left) of the root node in the tree is the left child C_{1}of the root node in the binary tree, and the sibling of the C_{1}is the right child of C_{1}and so on. - Repeat the step2 for each new node.
Next TopicBinary Search Trees |