Inorder Tree Traversal in Binary Tree In C++
Inorder Tree Traversal
Inorder traversal is a depth first algorithm. There are three types of traversals in tree: Preorder, Inorder, Postorder. In this article, inorder tree traversal is performed using recursion.
Steps To Find Inorder Traversal:
- Leftmost node is 5 so print 5
- Move back, print 8 and look for right child.
- As there is no right child, print 12 and move to the right subtree.
- 13 is printed and whole subtree is covered.
- Print 15 and move to the right subtree.
- 18 and 54 are printed and tree is covered.
What is Inorder generally used for?
We generally use Inorder traversal technique on Binary Tress , as it fetches the values from the underlying set in order. Using Post-order traversal is also an option, but during post order traversal while delete or freeing nodes it can even delete or free an entire binary tree, which is not a favorable condition.
Algorithm To Find Inorder Traversal:
- If root is NULL, return NULL.
- Visit the left subtree.
- Print the node.
- Visit the right subtree.
5 8 12 13 15 18 54
Time Complexity To Find Inorder Traversal in Binary Tree