Programming Tree Traversals (Inorder, Preorder and Postorder)

Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Following are the generally used ways for traversing trees.

Depth First Traversals:

(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1

Breadth First or Level Order Traversal : 1 2 3 4 5
Please see this post for Breadth First Traversal.

Inorder Traversal:

Algorithm Inorder(tree)
   1. Traverse the left subtree, i.e., call Inorder(left-subtree)
   2. Visit the root.
   3. Traverse the right subtree, i.e., call Inorder(right-subtree)

Uses of Inorder

In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder itraversal s reversed, can be used.
Example: Inorder traversal for the above given figure is 4 2 5 1 3.

[ad type=”banner”]

Postorder Traversal:

Algorithm Postorder(tree)
   1. Traverse the left subtree, i.e., call Postorder(left-subtree)
   2. Traverse the right subtree, i.e., call Postorder(right-subtree)
   3. Visit the root.

Uses of Postorder

Postorder traversal is used to delete the tree. . Postorder traversal is also useful to get the postfix expression of an expression tree. Please see http://en.wikipedia.org/wiki/Reverse_Polish_notation to for the usage of postfix expression.

Example: Postorder traversal for the above given figure is 4 5 2 3 1.

[ad type=”banner”]

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20for%20tree%20traversals%0A%20%0A%23%20A%20class%20that%20represents%20an%20individual%20node%20in%20a%0A%23%20Binary%20Tree%0Aclass%20Node%3A%0A%20%20%20%20def%20__init__(self%2Ckey)%3A%0A%20%20%20%20%20%20%20%20self.left%20%3D%20None%0A%20%20%20%20%20%20%20%20self.right%20%3D%20None%0A%20%20%20%20%20%20%20%20self.val%20%3D%20key%0A%20%0A%20%0A%23%20A%20function%20to%20do%20inorder%20tree%20traversal%0Adef%20printInorder(root)%3A%0A%20%0A%20%20%20%20if%20root%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20First%20recur%20on%20left%20child%0A%20%20%20%20%20%20%20%20printInorder(root.left)%0A%20%0A%20%20%20%20%20%20%20%20%23%20then%20print%20the%20data%20of%20node%0A%20%20%20%20%20%20%20%20print(root.val)%2C%0A%20%0A%20%20%20%20%20%20%20%20%23%20now%20recur%20on%20right%20child%0A%20%20%20%20%20%20%20%20printInorder(root.right)%0A%20%0A%20%0A%20%0A%23%20A%20function%20to%20do%20postorder%20tree%20traversal%0Adef%20printPostorder(root)%3A%0A%20%0A%20%20%20%20if%20root%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20First%20recur%20on%20left%20child%0A%20%20%20%20%20%20%20%20printPostorder(root.left)%0A%20%0A%20%20%20%20%20%20%20%20%23%20the%20recur%20on%20right%20child%0A%20%20%20%20%20%20%20%20printPostorder(root.right)%0A%20%0A%20%20%20%20%20%20%20%20%23%20now%20print%20the%20data%20of%20node%0A%20%20%20%20%20%20%20%20print(root.val)%2C%0A%20%0A%20%0A%23%20A%20function%20to%20do%20postorder%20tree%20traversal%0Adef%20printPreorder(root)%3A%0A%20%0A%20%20%20%20if%20root%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20First%20print%20the%20data%20of%20node%0A%20%20%20%20%20%20%20%20print(root.val)%2C%0A%20%0A%20%20%20%20%20%20%20%20%23%20Then%20recur%20on%20left%20child%0A%20%20%20%20%20%20%20%20printPreorder(root.left)%0A%20%0A%20%20%20%20%20%20%20%20%23%20Finally%20recur%20on%20right%20child%0A%20%20%20%20%20%20%20%20printPreorder(root.right)%0A%20%0A%20%0A%23%20Driver%20code%0Aroot%20%3D%20Node(1)%0Aroot.left%20%20%20%20%20%20%3D%20Node(2)%0Aroot.right%20%20%20%20%20%3D%20Node(3)%0Aroot.left.left%20%20%3D%20Node(4)%0Aroot.left.right%20%20%3D%20Node(5)%0Aprint%20%22Preorder%20traversal%20of%20binary%20tree%20is%22%0AprintPreorder(root)%0A%20%0Aprint%20%22%5CnInorder%20traversal%20of%20binary%20tree%20is%22%0AprintInorder(root)%0A%20%0Aprint%20%22%5CnPostorder%20traversal%20of%20binary%20tree%20is%22%0AprintPostorder(root)” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

Preorder traversal of binary tree is
1 2 4 5 3 
Inorder traversal of binary tree is
4 2 5 1 3 
Postorder traversal of binary tree is
4 5 2 3 1

Categorized in: