{"id":27668,"date":"2018-04-09T20:21:07","date_gmt":"2018-04-09T14:51:07","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27668"},"modified":"2018-09-14T18:31:46","modified_gmt":"2018-09-14T13:01:46","slug":"java-program-inorder-tree-traversal-without-recursion-without-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-program-inorder-tree-traversal-without-recursion-without-stack\/","title":{"rendered":"Program &#8211; Inorder Tree Traversal without recursion and without stack!"},"content":{"rendered":"<p>Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on <a href=\"http:\/\/en.wikipedia.org\/wiki\/Threaded_binary_tree\">Threaded Binary Tree<\/a>. <span id=\"more-6358\"><\/span>In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree.<\/p>\n<pre>1. Initialize current as root \r\n2. While current is not NULL\r\n   If current does not have left child\r\n      a) Print current\u2019s data\r\n      b) Go to the right, i.e., current = current->right\r\n   Else\r\n      a) Make current as right child of the rightmost \r\n         node in current's left subtree\r\n      b) Go to this left child, i.e., current = current->left\r\n<\/pre>\n<p>Although the tree is modified through the traversal, it is reverted back to its original shape after the completion. Unlike <a href=\"http:\/\/www.geeksforgeeks.org\/?p=5592\">Stack based traversal<\/a>, no extra space is required for this traversal.<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20print%20inorder%20traversal%20without%20recursion%20and%20stack%0A%20%20%0A%2F*%20A%20binary%20tree%20tNode%20has%20data%2C%20pointer%20to%20left%20child%0A%20%20%20and%20a%20pointer%20to%20right%20child%20*%2F%0Aclass%20tNode%20%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20tNode%20left%2C%20right%3B%0A%20%20%20%20%20%20%0A%20%20%20%20tNode(int%20item)%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20data%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%20%0Aclass%20BinaryTree%20%0A%7B%0A%20%20%20%20tNode%20root%3B%0A%20%20%0A%20%20%20%20%2F*%20Function%20to%20traverse%20binary%20tree%20without%20recursion%20and%20%0A%20%20%20%20%20%20%20without%20stack%20*%2F%0A%20%20%20%20void%20MorrisTraversal(tNode%20root)%20%7B%0A%20%20%20%20%20%20%20%20tNode%20current%2C%20pre%3B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20(root%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20current%20%3D%20root%3B%0A%20%20%20%20%20%20%20%20while%20(current%20!%3D%20null)%20%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(current.left%20%3D%3D%20null)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(current.data%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.right%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Find%20the%20inorder%20predecessor%20of%20current%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pre%20%3D%20current.left%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(pre.right%20!%3D%20null%20%26%26%20pre.right%20!%3D%20current)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pre%20%3D%20pre.right%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Make%20current%20as%20right%20child%20of%20its%20inorder%20predecessor%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(pre.right%20%3D%3D%20null)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pre.right%20%3D%20current%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.left%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%0A%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Revert%20the%20changes%20made%20in%20if%20part%20to%20restore%20the%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20original%20tree%20i.e.%2Cfix%20the%20right%20child%20of%20predecssor*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pre.right%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(current.data%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.right%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%2F*%20End%20of%20if%20condition%20pre-%3Eright%20%3D%3D%20NULL%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%2F*%20End%20of%20if%20condition%20current-%3Eleft%20%3D%3D%20NULL*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D%20%2F*%20End%20of%20while%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20Constructed%20binary%20tree%20is%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%20%20%20%5C%0A%20%20%20%20%20%20%20%20%20%20%20%202%20%20%20%20%20%203%0A%20%20%20%20%20%20%20%20%20%20%2F%20%20%5C%0A%20%20%20%20%20%20%20%204%20%20%20%20%205%0A%20%20%20%20%20%20%20%20*%2F%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%20tNode(1)%3B%0A%20%20%20%20%20%20%20%20tree.root.left%20%3D%20new%20tNode(2)%3B%0A%20%20%20%20%20%20%20%20tree.root.right%20%3D%20new%20tNode(3)%3B%0A%20%20%20%20%20%20%20%20tree.root.left.left%20%3D%20new%20tNode(4)%3B%0A%20%20%20%20%20%20%20%20tree.root.left.right%20%3D%20new%20tNode(5)%3B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20tree.MorrisTraversal(tree.root)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%20%0A%2F%2F%20This%20code%20has%20been%20contributed%20by%20Mayank%20Jaiswal(mayank_24)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>4 2 5 1 3<\/pre>\n","protected":false},"excerpt":{"rendered":"<p> Inorder Tree Traversal without recursion and without stack! &#8211; Using Morris Traversal, we can traverse the tree without using stack and recursion.<\/p>\n","protected":false},"author":1,"featured_media":31268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80125,80140],"tags":[81943,81802,81944,81803,81805,81800,81801,81804],"class_list":["post-27668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-binary-tree","category-binay-tree","tag-binary-tree-traversal-without-recursion","tag-inorder-preorder-postorder-traversal-without-recursion-in-c","tag-inorder-traversal-of-threaded-binary-tree","tag-inorder-traversal-with-recursion","tag-inorder-traversal-with-recursion-in-c","tag-inorder-traversal-without-recursion-and-stack","tag-postorder-traversal-without-recursion","tag-postorder-traversal-without-recursion-and-stack"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27668","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=27668"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27668\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31268"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}