{"id":27601,"date":"2018-04-04T19:36:35","date_gmt":"2018-04-04T14:06:35","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27601"},"modified":"2018-09-16T14:46:40","modified_gmt":"2018-09-16T09:16:40","slug":"inorder-tree-traversal-without-recursion","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/inorder-tree-traversal-without-recursion\/","title":{"rendered":"C Program &#8211; Inorder Tree Traversal without Recursion"},"content":{"rendered":"<p>Using <a href=\"http:\/\/en.wikipedia.org\/wiki\/Stack_%28data_structure%29\" target=\"_blank\" rel=\"noopener\">Stack <\/a>is the obvious way to traverse tree without recursion. Below is an algorithm for traversing binary tree using stack. See <a href=\"http:\/\/neural.cs.nthu.edu.tw\/jang\/courses\/cs2351\/slide\/animation\/Iterative%20Inorder%20Traversal.pps\" target=\"_blank\" rel=\"noopener\">this <\/a>for step wise step execution of the algorithm.<\/p>\n<pre>1) Create an empty stack S.\r\n2) Initialize current node as root\r\n3) Push the current node to S and set current = current->left until current is NULL\r\n4) If current is NULL and stack is not empty then \r\n     a) Pop the top item from stack.\r\n     b) Print the popped item, set current = popped_item->right \r\n     c) Go to step 3.\r\n5) If current is NULL and stack is empty then we are done.\r\n<\/pre>\n<p>Let us consider the below tree for example<\/p>\n<pre>            1\r\n          \/   \\\r\n        2      3\r\n      \/  \\\r\n    4     5\r\n\r\nStep 1 Creates an empty stack: S = NULL\r\n\r\nStep 2 sets current as address of root: current -> 1\r\n\r\nStep 3 Pushes the current node and set current = current->left until current is NULL\r\n     current -> 1\r\n     push 1: Stack S -> 1\r\n     current -> 2\r\n     push 2: Stack S -> 2, 1\r\n     current -> 4\r\n     push 4: Stack S -> 4, 2, 1\r\n     current = NULL\r\n\r\nStep 4 pops from S\r\n     a) Pop 4: Stack S -> 2, 1\r\n     b) print \"4\"\r\n     c) current = NULL \/*right of 4 *\/ and go to step 3\r\nSince current is NULL step 3 doesn't do anything. \r\n\r\nStep 4 pops again.\r\n     a) Pop 2: Stack S -> 1\r\n     b) print \"2\"\r\n     c) current -> 5\/*right of 2 *\/ and go to step 3\r\n\r\nStep 3 pushes 5 to stack and makes current NULL\r\n     Stack S -> 5, 1\r\n     current = NULL\r\n\r\nStep 4 pops from S\r\n     a) Pop 5: Stack S -> 1\r\n     b) print \"5\"\r\n     c) current = NULL \/*right of 5 *\/ and go to step 3\r\nSince current is NULL step 3 doesn't do anything\r\n\r\nStep 4 pops again.\r\n     a) Pop 1: Stack S -> NULL\r\n     b) print \"1\"\r\n     c) current -> 3 \/*right of 5 *\/  \r\n\r\nStep 3 pushes 3 to stack and makes current NULL\r\n     Stack S -> 3\r\n     current = NULL\r\n\r\nStep 4 pops from S\r\n     a) Pop 3: Stack S -> NULL\r\n     b) print \"3\"\r\n     c) current = NULL \/*right of 3 *\/<\/pre>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%23define%20bool%20int%0A%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%0Astruct%20tNode%0A%7B%0A%20%20%20int%20data%3B%0A%20%20%20struct%20tNode*%20left%3B%0A%20%20%20struct%20tNode*%20right%3B%0A%7D%3B%0A%20%0A%2F*%20Structure%20of%20a%20stack%20node.%20Linked%20List%20implementation%20is%20used%20for%20%0A%20%20%20stack.%20A%20stack%20node%20contains%20a%20pointer%20to%20tree%20node%20and%20a%20pointer%20to%20%0A%20%20%20next%20stack%20node%20*%2F%0Astruct%20sNode%0A%7B%0A%20%20struct%20tNode%20*t%3B%0A%20%20struct%20sNode%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Stack%20related%20functions%20*%2F%0Avoid%20push(struct%20sNode**%20top_ref%2C%20struct%20tNode%20*t)%3B%0Astruct%20tNode%20*pop(struct%20sNode**%20top_ref)%3B%0Abool%20isEmpty(struct%20sNode%20*top)%3B%0A%20%0A%2F*%20Iterative%20function%20for%20inorder%20tree%20traversal%20*%2F%0Avoid%20inOrder(struct%20tNode%20*root)%0A%7B%0A%20%20%2F*%20set%20current%20to%20root%20of%20binary%20tree%20*%2F%0A%20%20struct%20tNode%20*current%20%3D%20root%3B%0A%20%20struct%20sNode%20*s%20%3D%20NULL%3B%20%20%2F*%20Initialize%20stack%20s%20*%2F%0A%20%20bool%20done%20%3D%200%3B%0A%20%0A%20%20while%20(!done)%0A%20%20%7B%0A%20%20%20%20%2F*%20Reach%20the%20left%20most%20tNode%20of%20the%20current%20tNode%20*%2F%0A%20%20%20%20if(current%20!%3D%20%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%2F*%20place%20pointer%20to%20a%20tree%20node%20on%20the%20stack%20before%20traversing%20%0A%20%20%20%20%20%20%20%20the%20node\u2019s%20left%20subtree%20*%2F%0A%20%20%20%20%20%20push(%26s%2C%20current)%3B%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%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20current%20%3D%20current-%3Eleft%3B%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F*%20backtrack%20from%20the%20empty%20subtree%20and%20visit%20the%20tNode%20%0A%20%20%20%20%20%20%20at%20the%20top%20of%20the%20stack%3B%20however%2C%20if%20the%20stack%20is%20empty%2C%0A%20%20%20%20%20%20you%20are%20done%20*%2F%0A%20%20%20%20else%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%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%0A%20%20%20%20%7B%0A%20%20%20%20%20%20if%20(!isEmpty(s))%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20current%20%3D%20pop(%26s)%3B%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20current-%3Edata)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20we%20have%20visited%20the%20node%20and%20its%20left%20subtree.%0A%20%20%20%20%20%20%20%20%20%20Now%2C%20it\u2019s%20right%20subtree\u2019s%20turn%20*%2F%0A%20%20%20%20%20%20%20%20current%20%3D%20current-%3Eright%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20done%20%3D%201%3B%20%0A%20%20%20%20%7D%0A%20%20%7D%20%2F*%20end%20of%20while%20*%2F%20%0A%7D%20%20%20%20%20%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20*%2F%0A%2F*%20Function%20to%20push%20an%20item%20to%20sNode*%2F%0Avoid%20push(struct%20sNode**%20top_ref%2C%20struct%20tNode%20*t)%0A%7B%0A%20%20%2F*%20allocate%20tNode%20*%2F%0A%20%20struct%20sNode*%20new_tNode%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20sNode*)%20malloc(sizeof(struct%20sNode))%3B%0A%20%0A%20%20if(new_tNode%20%3D%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20%20printf(%22Stack%20Overflow%20%5Cn%22)%3B%0A%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20exit(0)%3B%0A%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%0A%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20new_tNode-%3Et%20%20%3D%20t%3B%0A%20%0A%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20tNode%20*%2F%0A%20%20new_tNode-%3Enext%20%3D%20(*top_ref)%3B%20%20%20%0A%20%0A%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20tNode%20*%2F%0A%20%20(*top_ref)%20%20%20%20%3D%20new_tNode%3B%0A%7D%0A%20%0A%2F*%20The%20function%20returns%20true%20if%20stack%20is%20empty%2C%20otherwise%20false%20*%2F%0Abool%20isEmpty(struct%20sNode%20*top)%0A%7B%0A%20%20%20return%20(top%20%3D%3D%20NULL)%3F%201%20%3A%200%3B%0A%7D%20%20%20%0A%20%0A%2F*%20Function%20to%20pop%20an%20item%20from%20stack*%2F%0Astruct%20tNode%20*pop(struct%20sNode**%20top_ref)%0A%7B%0A%20%20struct%20tNode%20*res%3B%0A%20%20struct%20sNode%20*top%3B%0A%20%0A%20%20%2F*If%20sNode%20is%20empty%20then%20error%20*%2F%0A%20%20if(isEmpty(*top_ref))%0A%20%20%7B%0A%20%20%20%20%20printf(%22Stack%20Underflow%20%5Cn%22)%3B%0A%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20exit(0)%3B%0A%20%20%7D%0A%20%20else%0A%20%20%7B%0A%20%20%20%20%20top%20%3D%20*top_ref%3B%0A%20%20%20%20%20res%20%3D%20top-%3Et%3B%0A%20%20%20%20%20*top_ref%20%3D%20top-%3Enext%3B%0A%20%20%20%20%20free(top)%3B%0A%20%20%20%20%20return%20res%3B%0A%20%20%7D%0A%7D%0A%20%0A%2F*%20Helper%20function%20that%20allocates%20a%20new%20tNode%20with%20the%0A%20%20%20given%20data%20and%20NULL%20left%20and%20right%20pointers.%20*%2F%0Astruct%20tNode*%20newtNode(int%20data)%0A%7B%0A%20%20struct%20tNode*%20tNode%20%3D%20(struct%20tNode*)%0A%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%20tNode))%3B%0A%20%20tNode-%3Edata%20%3D%20data%3B%0A%20%20tNode-%3Eleft%20%3D%20NULL%3B%0A%20%20tNode-%3Eright%20%3D%20NULL%3B%0A%20%0A%20%20return(tNode)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions*%2F%0Aint%20main()%0A%7B%0A%20%0A%20%20%2F*%20Constructed%20binary%20tree%20is%0A%20%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%20%20%20%2F%20%20%20%5C%0A%20%20%20%20%20%20%20%202%20%20%20%20%20%203%0A%20%20%20%20%20%20%2F%20%20%5C%0A%20%20%20%204%20%20%20%20%205%0A%20%20*%2F%0A%20%20struct%20tNode%20*root%20%3D%20newtNode(1)%3B%0A%20%20root-%3Eleft%20%20%20%20%20%20%20%20%3D%20newtNode(2)%3B%0A%20%20root-%3Eright%20%20%20%20%20%20%20%3D%20newtNode(3)%3B%0A%20%20root-%3Eleft-%3Eleft%20%20%3D%20newtNode(4)%3B%0A%20%20root-%3Eleft-%3Eright%20%3D%20newtNode(5)%3B%20%0A%20%0A%20%20inOrder(root)%3B%0A%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Time Complexity: O(n)<\/p>\n<p>Output:<\/p>\n<pre> 4 2 5 1 3<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>inorder tree travesal without recursion  &#8211; learn in 30 secfrom microsoft awarded MVP,Using Stack is the obvious way to traverse tree without recursion. Below is an algorithm for traversing binary tree using stack. See this for step wise step execution of the algorithm.<\/p>\n","protected":false},"author":1,"featured_media":31294,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80125,80140],"tags":[81806,81802,81803,81805,81800,81801,81804,81799],"class_list":["post-27601","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-binary-tree","category-binay-tree","tag-binary-tree-inorder-traversal-leetcode","tag-inorder-preorder-postorder-traversal-without-recursion-in-c","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","tag-preorder-traversal-without-recursion-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27601","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=27601"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27601\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31294"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}