Binary Tree Traversal - Inorder, Preorder
and Postorder
Binary Tree Traversal can be done in the following ways.
Inorder traversal
Preorder traversal
Postorder traversal
Consider the given binary tree,
Inorder Traversal: 7 9 4 2 5 1 3 6 8
Preorder Traversal: 1 2 4 7 9 5 3 6 8
Postorder Traversal: 9 7 4 5 2 8 6 3 1
Inorder Traversal: For binary search trees (BST), Inorder Traversal specifies the nodes
in non-descending order. In order to obtain nodes from BST in non-increasing order, a
variation of inorder traversal may be used where inorder traversal is reversed.
Preorder Traversal: Preorder traversal will create a copy of the tree. Preorder
Traversal is also used to get the prefix expression of an expression.
Postorder Traversal: Postorder traversal is used to get the postfix expression of an
expression given
Algorithm for binary tree traversal
Inorder(root)
Traverse the left sub-tree, (recursively call inorder(root -> left).
Visit and print the root node.
Traverse the right sub-tree, (recursively call inorder(root -> right).
Preorder(root)
Visit and print the root node.
Traverse the left sub-tree, (recursively call inorder(root -> left).
Traverse the right sub-tree, (recursively call inorder(root -> right).
Postorder(root)
Traverse the left sub-tree, (recursively call inorder(root -> left).
Traverse the right sub-tree, (recursively call inorder(root -> right).
Visit and print the root node.
_________________________________________________________________________
Preorder Traversal-
100 , 20 , 10 , 30 , 200 , 150 , 300
Inorder Traversal-
10 , 20 , 30 , 100 , 150 , 200 , 300
Postorder Traversal-
10 , 30 , 20 , 150 , 300 , 200 , 100