{"id":27069,"date":"2018-01-03T21:17:53","date_gmt":"2018-01-03T15:47:53","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27069"},"modified":"2018-01-03T21:17:53","modified_gmt":"2018-01-03T15:47:53","slug":"java-programming-tree-traversals-inorder-preorder-postorder","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-tree-traversals-inorder-preorder-postorder\/","title":{"rendered":"Java Programming Tree Traversals (Inorder, Preorder and Postorder)"},"content":{"rendered":"<p>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.<\/p>\n<p>Depth First Traversals:<br \/>\n(a) Inorder (Left, Root, Right) : 4 2 5 1 3<br \/>\n(b) Preorder (Root, Left, Right) : 1 2 4 5 3<br \/>\n(c) Postorder (Left, Right, Root) : 4 5 2 3 1<\/p>\n<p>Breadth First or Level Order Traversal : 1 2 3 4 5<br \/>\nPlease see <a href=\"http:\/\/www.geeksforgeeks.org\/?p=2686\">this <\/a>post for Breadth First Traversal.<\/p>\n<p><strong>Inorder Traversal:<\/strong><\/p>\n<pre>Algorithm Inorder(tree)\r\n   1. Traverse the left subtree, i.e., call Inorder(left-subtree)\r\n   2. Visit the root.\r\n   3. Traverse the right subtree, i.e., call Inorder(right-subtree)\r\n<\/pre>\n<p>Uses of Inorder<br \/>\nIn 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.<br \/>\nExample: Inorder traversal for the above given figure is 4 2 5 1 3.<\/p>\n<p><strong><br \/>\n<a href=\"http:\/\/www.practice.geeksforgeeks.org\/problem-page.php?pid=700135\">Practice Inorder Traversal<\/a><\/strong><\/p>\n<p><strong><br \/>\nPreorder Traversal:<\/strong><\/p>\n<pre>Algorithm Preorder(tree)\r\n   1. Visit the root.\r\n   2. Traverse the left subtree, i.e., call Preorder(left-subtree)\r\n   3. Traverse the right subtree, i.e., call Preorder(right-subtree)<\/pre>\n<p>Uses of Preorder<br \/>\nPreorder 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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Polish_notation\">http:\/\/en.wikipedia.org\/wiki\/Polish_notation<\/a> to know why prefix expressions are useful.<br \/>\nExample: Preorder traversal for the above given figure is 1 2 4 5 3.<br \/>\n<strong>[ad type=\u201dbanner\u201d]\n<a href=\"http:\/\/www.practice.geeksforgeeks.org\/problem-page.php?pid=700319\">Practice Preorder Traversal<\/a><\/strong><\/p>\n<p><strong><br \/>\nPostorder Traversal:<\/strong><\/p>\n<pre>Algorithm Postorder(tree)\r\n   1. Traverse the left subtree, i.e., call Postorder(left-subtree)\r\n   2. Traverse the right subtree, i.e., call Postorder(right-subtree)\r\n   3. Visit the root.\r\n<\/pre>\n<p>Uses of Postorder<br \/>\nPostorder traversal is used to delete the tree. Please see <a href=\"http:\/\/www.geeksforgeeks.org\/?p=654\">the question for deletion of tree <\/a>for details. Postorder traversal is also useful to get the postfix expression of an expression tree. Please see<a href=\"http:\/\/en.wikipedia.org\/wiki\/Reverse_Polish_notation\"> http:\/\/en.wikipedia.org\/wiki\/Reverse_Polish_notation<\/a> to for the usage of postfix expression.<\/p>\n<p><strong><a href=\"http:\/\/www.practice.geeksforgeeks.org\/problem-page.php?pid=700199\">Practice Postorder Traversal <\/a><br \/>\n<\/strong><\/p>\n<p>Example: Postorder traversal for the above given figure is 4 5 2 3 1.<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20for%20different%20tree%20traversals%0A%20%0A%2F*%20Class%20containing%20left%20and%20right%20child%20of%20current%0A%20%20%20node%20and%20key%20value*%2F%0Aclass%20Node%0A%7B%0A%20%20%20%20int%20key%3B%0A%20%20%20%20Node%20left%2C%20right%3B%0A%20%0A%20%20%20%20public%20Node(int%20item)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20key%20%3D%20item%3B%0A%20%20%20%20%20%20%20%20left%20%3D%20right%20%3D%20null%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0Aclass%20BinaryTree%0A%7B%0A%20%20%20%20%2F%2F%20Root%20of%20Binary%20Tree%0A%20%20%20%20Node%20root%3B%0A%20%0A%20%20%20%20BinaryTree()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20root%20%3D%20null%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Given%20a%20binary%20tree%2C%20print%20its%20nodes%20according%20to%20the%0A%20%20%20%20%20%20%22bottom-up%22%20postorder%20traversal.%20*%2F%0A%20%20%20%20void%20printPostorder(Node%20node)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(node%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20first%20recur%20on%20left%20subtree%0A%20%20%20%20%20%20%20%20printPostorder(node.left)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20then%20recur%20on%20right%20subtree%0A%20%20%20%20%20%20%20%20printPostorder(node.right)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20now%20deal%20with%20the%20node%0A%20%20%20%20%20%20%20%20System.out.print(node.key%20%2B%20%22%20%22)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Given%20a%20binary%20tree%2C%20print%20its%20nodes%20in%20inorder*%2F%0A%20%20%20%20void%20printInorder(Node%20node)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(node%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20first%20recur%20on%20left%20child%20*%2F%0A%20%20%20%20%20%20%20%20printInorder(node.left)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20then%20print%20the%20data%20of%20node%20*%2F%0A%20%20%20%20%20%20%20%20System.out.print(node.key%20%2B%20%22%20%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20now%20recur%20on%20right%20child%20*%2F%0A%20%20%20%20%20%20%20%20printInorder(node.right)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Given%20a%20binary%20tree%2C%20print%20its%20nodes%20in%20preorder*%2F%0A%20%20%20%20void%20printPreorder(Node%20node)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(node%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20first%20print%20data%20of%20node%20*%2F%0A%20%20%20%20%20%20%20%20System.out.print(node.key%20%2B%20%22%20%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20then%20recur%20on%20left%20sutree%20*%2F%0A%20%20%20%20%20%20%20%20printPreorder(node.left)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20now%20recur%20on%20right%20subtree%20*%2F%0A%20%20%20%20%20%20%20%20printPreorder(node.right)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Wrappers%20over%20above%20recursive%20functions%0A%20%20%20%20void%20printPostorder()%20%20%7B%20%20%20%20%20printPostorder(root)%3B%20%20%7D%0A%20%20%20%20void%20printInorder()%20%20%20%20%7B%20%20%20%20%20printInorder(root)%3B%20%20%20%7D%0A%20%20%20%20void%20printPreorder()%20%20%20%7B%20%20%20%20%20printPreorder(root)%3B%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20BinaryTree%20tree%20%3D%20new%20BinaryTree()%3B%0A%20%20%20%20%20%20%20%20tree.root%20%3D%20new%20Node(1)%3B%0A%20%20%20%20%20%20%20%20tree.root.left%20%3D%20new%20Node(2)%3B%0A%20%20%20%20%20%20%20%20tree.root.right%20%3D%20new%20Node(3)%3B%0A%20%20%20%20%20%20%20%20tree.root.left.left%20%3D%20new%20Node(4)%3B%0A%20%20%20%20%20%20%20%20tree.root.left.right%20%3D%20new%20Node(5)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Preorder%20traversal%20of%20binary%20tree%20is%20%22)%3B%0A%20%20%20%20%20%20%20%20tree.printPreorder()%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22%5CnInorder%20traversal%20of%20binary%20tree%20is%20%22)%3B%0A%20%20%20%20%20%20%20%20tree.printInorder()%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22%5CnPostorder%20traversal%20of%20binary%20tree%20is%20%22)%3B%0A%20%20%20%20%20%20%20%20tree.printPostorder()%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>Preorder traversal of binary tree is\r\n1 2 4 5 3 \r\nInorder traversal of binary tree is\r\n4 2 5 1 3 \r\nPostorder traversal of binary tree is\r\n4 5 2 3 1<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Java Programming Tree Traversals (Inorder, Preorder and Postorder) &#8211; Tree &#8211; Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80125,1,2139],"tags":[80712,80711,80778,80777,80713,80775,80776],"class_list":["post-27069","post","type-post","status-publish","format-standard","hentry","category-binary-tree","category-coding","category-java","tag-binary-tree-traversal-in-data-structure","tag-inorder-preorder-postorder-traversal-examples-pdf","tag-inorder-traversal-iterative","tag-inorder-traversal-java","tag-inorder-tree-traversal","tag-postorder-traversal-java","tag-preorder-traversal-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27069"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27069\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}