{"id":27669,"date":"2018-04-09T20:21:05","date_gmt":"2018-04-09T14:51:05","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27669"},"modified":"2018-09-14T18:40:23","modified_gmt":"2018-09-14T13:10:23","slug":"python-program-inorder-tree-traversal-without-recursion-without-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-program-inorder-tree-traversal-without-recursion-without-stack\/","title":{"rendered":"Python 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\" target=\"_blank\" rel=\"noopener\">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-&gt;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-&gt;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\" target=\"_blank\" rel=\"noopener\">Stack based traversal<\/a>, no extra space is required for this traversal.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to do inorder traversal without recursion and <br\/># without stack Morris inOrder Traversal<br\/> <br\/># A binary tree node<br\/>class Node:<br\/>     <br\/>    # Constructor to create a new node<br\/>    def __init__(self, data):<br\/>        self.data = data <br\/>        self.left = None<br\/>        self.right = None<br\/> <br\/># Iterative function for inorder tree traversal<br\/>def MorrisTraversal(root):<br\/>     <br\/>    # Set current to root of binary tree<br\/>    current = root <br\/>     <br\/>    while(current is not None):<br\/>         <br\/>        if current.left is None:<br\/>            print current.data ,<br\/>            current = current.right<br\/>        else:<br\/>            #Find the inorder predecessor of current<br\/>            pre = current.left<br\/>            while(pre.right is not None and pre.right != current):<br\/>                pre = pre.right<br\/>  <br\/>            # Make current as right child of its inorder predecessor<br\/>            if(pre.right is None):<br\/>                pre.right = current<br\/>                current = current.left<br\/>                 <br\/>            # Revert the changes made in if part to restore the <br\/>            # original tree i.e., fix the right child of predecssor<br\/>            else:<br\/>                pre.right = None<br\/>                print current.data ,<br\/>                current = current.right<br\/>             <br\/># Driver program to test above function<br\/>&quot;&quot;&quot; <br\/>Constructed binary tree is<br\/>            1<br\/>          \/   \\<br\/>        2      3<br\/>      \/  \\<br\/>    4     5<br\/>&quot;&quot;&quot;<br\/>root = Node(1)<br\/>root.left = Node(2)<br\/>root.right = Node(3)<br\/>root.left.left = Node(4)<br\/>root.left.right = Node(5)<br\/> <br\/>MorrisTraversal(root)<br\/> <br\/># This code is contributed by Naveen Aili<\/code><\/pre> <\/div>\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":31269,"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-27669","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\/27669","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=27669"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27669\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31269"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}