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”] Practice Inorder Traversal


Preorder Traversal:

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

Uses of Preorder
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree. Please see http://en.wikipedia.org/wiki/Polish_notation to know why prefix expressions are useful.
Example: Preorder traversal for the above given figure is 1 2 4 5 3.

Practice Preorder Traversal


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. Please see the question for deletion of tree for details. 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.

Practice Postorder Traversal

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

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20for%20different%20tree%20traversals%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20binary%20tree%20node%20has%20data%2C%20pointer%20to%20left%20child%0A%20%20%20and%20a%20pointer%20to%20right%20child%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20struct%20node*%20left%3B%0A%20%20%20%20%20struct%20node*%20right%3B%0A%7D%3B%0A%20%0A%2F*%20Helper%20function%20that%20allocates%20a%20new%20node%20with%20the%0A%20%20%20given%20data%20and%20NULL%20left%20and%20right%20pointers.%20*%2F%0Astruct%20node*%20newNode(int%20data)%0A%7B%0A%20%20%20%20%20struct%20node*%20node%20%3D%20(struct%20node*)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20malloc(sizeof(struct%20node))%3B%0A%20%20%20%20%20node-%3Edata%20%3D%20data%3B%0A%20%20%20%20%20node-%3Eleft%20%3D%20NULL%3B%0A%20%20%20%20%20node-%3Eright%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%20return(node)%3B%0A%7D%0A%20%0A%2F*%20Given%20a%20binary%20tree%2C%20print%20its%20nodes%20according%20to%20the%0A%20%20%22bottom-up%22%20postorder%20traversal.%20*%2F%0Avoid%20printPostorder(struct%20node*%20node)%0A%7B%0A%20%20%20%20%20if%20(node%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%2F%2F%20first%20recur%20on%20left%20subtree%0A%20%20%20%20%20printPostorder(node-%3Eleft)%3B%0A%20%0A%20%20%20%20%20%2F%2F%20then%20recur%20on%20right%20subtree%0A%20%20%20%20%20printPostorder(node-%3Eright)%3B%0A%20%0A%20%20%20%20%20%2F%2F%20now%20deal%20with%20the%20node%0A%20%20%20%20%20printf(%22%25d%20%22%2C%20node-%3Edata)%3B%0A%7D%0A%20%0A%2F*%20Given%20a%20binary%20tree%2C%20print%20its%20nodes%20in%20inorder*%2F%0Avoid%20printInorder(struct%20node*%20node)%0A%7B%0A%20%20%20%20%20if%20(node%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%2F*%20first%20recur%20on%20left%20child%20*%2F%0A%20%20%20%20%20printInorder(node-%3Eleft)%3B%0A%20%0A%20%20%20%20%20%2F*%20then%20print%20the%20data%20of%20node%20*%2F%0A%20%20%20%20%20printf(%22%25d%20%22%2C%20node-%3Edata)%3B%20%20%0A%20%0A%20%20%20%20%20%2F*%20now%20recur%20on%20right%20child%20*%2F%0A%20%20%20%20%20printInorder(node-%3Eright)%3B%0A%7D%0A%20%0A%2F*%20Given%20a%20binary%20tree%2C%20print%20its%20nodes%20in%20preorder*%2F%0Avoid%20printPreorder(struct%20node*%20node)%0A%7B%0A%20%20%20%20%20if%20(node%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%2F*%20first%20print%20data%20of%20node%20*%2F%0A%20%20%20%20%20printf(%22%25d%20%22%2C%20node-%3Edata)%3B%20%20%0A%20%0A%20%20%20%20%20%2F*%20then%20recur%20on%20left%20sutree%20*%2F%0A%20%20%20%20%20printPreorder(node-%3Eleft)%3B%20%20%0A%20%0A%20%20%20%20%20%2F*%20now%20recur%20on%20right%20subtree%20*%2F%0A%20%20%20%20%20printPreorder(node-%3Eright)%3B%0A%7D%20%20%20%20%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%20struct%20node%20*root%20%20%3D%20newNode(1)%3B%0A%20%20%20%20%20root-%3Eleft%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20newNode(2)%3B%0A%20%20%20%20%20root-%3Eright%20%20%20%20%20%20%20%20%20%20%20%3D%20newNode(3)%3B%0A%20%20%20%20%20root-%3Eleft-%3Eleft%20%20%20%20%20%3D%20newNode(4)%3B%0A%20%20%20%20%20root-%3Eleft-%3Eright%20%20%20%3D%20newNode(5)%3B%20%0A%20%0A%20%20%20%20%20printf(%22%5CnPreorder%20traversal%20of%20binary%20tree%20is%20%5Cn%22)%3B%0A%20%20%20%20%20printPreorder(root)%3B%0A%20%0A%20%20%20%20%20printf(%22%5CnInorder%20traversal%20of%20binary%20tree%20is%20%5Cn%22)%3B%0A%20%20%20%20%20printInorder(root)%3B%20%20%0A%20%0A%20%20%20%20%20printf(%22%5CnPostorder%20traversal%20of%20binary%20tree%20is%20%5Cn%22)%3B%0A%20%20%20%20%20printPostorder(root)%3B%0A%20%0A%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20return%200%3B%0A%7D” 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