{"id":27659,"date":"2018-04-09T20:17:37","date_gmt":"2018-04-09T14:47:37","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27659"},"modified":"2018-09-14T19:30:16","modified_gmt":"2018-09-14T14:00:16","slug":"27659-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/27659-2\/","title":{"rendered":"C 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-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* A binary tree tNode has data, pointer to left child<br\/>   and a pointer to right child *\/<br\/>struct tNode<br\/>{<br\/>   int data;<br\/>   struct tNode* left;<br\/>   struct tNode* right;<br\/>};<br\/> <br\/>\/* Function to traverse binary tree without recursion and <br\/>   without stack *\/<br\/>void MorrisTraversal(struct tNode *root)<br\/>{<br\/>  struct tNode *current,*pre;<br\/> <br\/>  if(root == NULL)<br\/>     return; <br\/> <br\/>  current = root;<br\/>  while(current != NULL)<br\/>  {                 <br\/>    if(current-&gt;left == NULL)<br\/>    {<br\/>      printf(&quot;%d &quot;, current-&gt;data);<br\/>      current = current-&gt;right;      <br\/>    }    <br\/>    else<br\/>    {<br\/>      \/* Find the inorder predecessor of current *\/<br\/>      pre = current-&gt;left;<br\/>      while(pre-&gt;right != NULL &amp;&amp; pre-&gt;right != current)<br\/>        pre = pre-&gt;right;<br\/> <br\/>      \/* Make current as right child of its inorder predecessor *\/<br\/>      if(pre-&gt;right == NULL)<br\/>      {<br\/>        pre-&gt;right = current;<br\/>        current = current-&gt;left;<br\/>      }<br\/>             <br\/>      \/* Revert the changes made in if part to restore the original <br\/>        tree i.e., fix the right child of predecssor *\/   <br\/>      else <br\/>      {<br\/>        pre-&gt;right = NULL;<br\/>        printf(&quot;%d &quot;,current-&gt;data);<br\/>        current = current-&gt;right;      <br\/>      } \/* End of if condition pre-&gt;right == NULL *\/<br\/>    } \/* End of if condition current-&gt;left == NULL*\/<br\/>  } \/* End of while *\/<br\/>}<br\/> <br\/>\/* UTILITY FUNCTIONS *\/<br\/>\/* Helper function that allocates a new tNode with the<br\/>   given data and NULL left and right pointers. *\/<br\/>struct tNode* newtNode(int data)<br\/>{<br\/>  struct tNode* tNode = (struct tNode*)<br\/>                       malloc(sizeof(struct tNode));<br\/>  tNode-&gt;data = data;<br\/>  tNode-&gt;left = NULL;<br\/>  tNode-&gt;right = NULL;<br\/> <br\/>  return(tNode);<br\/>}<br\/> <br\/>\/* Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/> <br\/>  \/* Constructed binary tree is<br\/>            1<br\/>          \/   \\<br\/>        2      3<br\/>      \/  \\<br\/>    4     5<br\/>  *\/<br\/>  struct tNode *root = newtNode(1);<br\/>  root-&gt;left        = newtNode(2);<br\/>  root-&gt;right       = newtNode(3);<br\/>  root-&gt;left-&gt;left  = newtNode(4);<br\/>  root-&gt;left-&gt;right = newtNode(5); <br\/> <br\/>  MorrisTraversal(root);<br\/> <br\/>  getchar();<br\/>  return 0;<br\/>}<\/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":31279,"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-27659","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\/27659","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=27659"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27659\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31279"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}