{"id":27636,"date":"2018-04-09T20:16:08","date_gmt":"2018-04-09T14:46:08","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27636"},"modified":"2018-09-16T13:24:10","modified_gmt":"2018-09-16T07:54:10","slug":"c-algorithm-iterative-postorder-traversal-set-2-using-one-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-iterative-postorder-traversal-set-2-using-one-stack\/","title":{"rendered":"C Algorithm &#8211; Iterative Postorder Traversal | Set 2 (Using One Stack)"},"content":{"rendered":"<p>We have discussed a simple iterative postorder traversal using two stacks in the previous post. In this post, an approach with only one stack is discussed.<span id=\"more-114897\"><\/span><\/p>\n<p>The idea is to move down to leftmost node using left pointer. While moving down, push root and root\u2019s right child to stack. Once we reach leftmost node, print it if it doesn\u2019t have a right child. If it has a right child, then change root so that the right child is processed before.<\/p>\n<p>Following is detailed algorithm.<\/p>\n<pre>1.1 Create an empty stack\r\n2.1 Do following while root is not NULL\r\n    a) Push root's right child and then root to stack.\r\n    b) Set root as root's left child.\r\n2.2 Pop an item from stack and set it as root.\r\n    a) If the popped item has a right child and the right child \r\n       is at top of stack, then remove the right child from stack,\r\n       push the root back and set root as root's right child.\r\n    b) Else print root's data and set root as NULL.\r\n2.3 Repeat steps 2.1 and 2.2 while stack is not empty.<\/pre>\n<p>Let us consider the following tree<\/p>\n<p>Following are the steps to print postorder traversal of the above tree using one stack.<\/p>\n<pre>1. Right child of 1 exists. \r\n   Push 3 to stack. Push 1 to stack. Move to left child.\r\n        Stack: 3, 1\r\n\r\n2. Right child of 2 exists. \r\n   Push 5 to stack. Push 2 to stack. Move to left child.\r\n        Stack: 3, 1, 5, 2\r\n\r\n3. Right child of 4 doesn't exist. '\r\n   Push 4 to stack. Move to left child.\r\n        Stack: 3, 1, 5, 2, 4\r\n\r\n4. Current node is NULL. \r\n   Pop 4 from stack. Right child of 4 doesn't exist. \r\n   Print 4. Set current node to NULL.\r\n        Stack: 3, 1, 5, 2\r\n\r\n5. Current node is NULL. \r\n    Pop 2 from stack. Since right child of 2 equals stack top element, \r\n    pop 5 from stack. Now push 2 to stack.     \r\n    Move current node to right child of 2 i.e. 5\r\n        Stack: 3, 1, 2\r\n\r\n6. Right child of 5 doesn't exist. Push 5 to stack. Move to left child.\r\n        Stack: 3, 1, 2, 5\r\n\r\n7. Current node is NULL. Pop 5 from stack. Right child of 5 doesn't exist. \r\n   Print 5. Set current node to NULL.\r\n        Stack: 3, 1, 2\r\n\r\n8. Current node is NULL. Pop 2 from stack. \r\n   Right child of 2 is not equal to stack top element. \r\n   Print 2. Set current node to NULL.\r\n        Stack: 3, 1\r\n\r\n9. Current node is NULL. Pop 1 from stack. \r\n   Since right child of 1 equals stack top element, pop 3 from stack. \r\n   Now push 1 to stack. Move current node to right child of 1 i.e. 3\r\n        Stack: 1\r\n\r\n10. Repeat the same as above steps and Print 6, 7 and 3. \r\n    Pop 1 and Print 1.<\/pre>\n<p><strong>C Programming:<\/strong><\/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\">\/\/ C program for iterative postorder traversal using one stack<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/> <br\/>\/\/ Maximum stack size<br\/>#define MAX_SIZE 100<br\/> <br\/>\/\/ A tree node<br\/>struct Node<br\/>{<br\/>    int data;<br\/>    struct Node *left, *right;<br\/>};<br\/> <br\/>\/\/ Stack type<br\/>struct Stack<br\/>{<br\/>    int size;<br\/>    int top;<br\/>    struct Node* *array;<br\/>};<br\/> <br\/>\/\/ A utility function to create a new tree node<br\/>struct Node* newNode(int data)<br\/>{<br\/>    struct Node* node = (struct Node*) malloc(sizeof(struct Node));<br\/>    node-&gt;data = data;<br\/>    node-&gt;left = node-&gt;right = NULL;<br\/>    return node;<br\/>}<br\/> <br\/>\/\/ A utility function to create a stack of given size<br\/>struct Stack* createStack(int size)<br\/>{<br\/>    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));<br\/>    stack-&gt;size = size;<br\/>    stack-&gt;top = -1;<br\/>    stack-&gt;array = (struct Node**) malloc(stack-&gt;size * sizeof(struct Node*));<br\/>    return stack;<br\/>}<br\/> <br\/>\/\/ BASIC OPERATIONS OF STACK<br\/>int isFull(struct Stack* stack)<br\/>{  return stack-&gt;top - 1 == stack-&gt;size; }<br\/> <br\/>int isEmpty(struct Stack* stack)<br\/>{  return stack-&gt;top == -1; }<br\/> <br\/>void push(struct Stack* stack, struct Node* node)<br\/>{<br\/>    if (isFull(stack))<br\/>        return;<br\/>    stack-&gt;array[++stack-&gt;top] = node;<br\/>}<br\/> <br\/>struct Node* pop(struct Stack* stack)<br\/>{<br\/>    if (isEmpty(stack))<br\/>        return NULL;<br\/>    return stack-&gt;array[stack-&gt;top--];<br\/>}<br\/> <br\/>struct Node* peek(struct Stack* stack)<br\/>{<br\/>    if (isEmpty(stack))<br\/>        return NULL;<br\/>    return stack-&gt;array[stack-&gt;top];<br\/>}<br\/> <br\/>\/\/ An iterative function to do postorder traversal of a given binary tree<br\/>void postOrderIterative(struct Node* root)<br\/>{<br\/>    \/\/ Check for empty tree<br\/>    if (root == NULL)<br\/>        return;<br\/>     <br\/>    struct Stack* stack = createStack(MAX_SIZE);<br\/>    do<br\/>    {<br\/>        \/\/ Move to leftmost node<br\/>        while (root)<br\/>        {<br\/>            \/\/ Push root&#039;s right child and then root to stack.<br\/>            if (root-&gt;right)<br\/>                push(stack, root-&gt;right);<br\/>            push(stack, root);<br\/> <br\/>            \/\/ Set root as root&#039;s left child  <br\/>            root = root-&gt;left;<br\/>        }<br\/> <br\/>        \/\/ Pop an item from stack and set it as root    <br\/>        root = pop(stack);<br\/> <br\/>        \/\/ If the popped item has a right child and the right child is not<br\/>        \/\/ processed yet, then make sure right child is processed before root<br\/>        if (root-&gt;right &amp;&amp; peek(stack) == root-&gt;right)<br\/>        {<br\/>            pop(stack);  \/\/ remove right child from stack<br\/>            push(stack, root);  \/\/ push root back to stack<br\/>            root = root-&gt;right; \/\/ change root so that the right <br\/>                                \/\/ child is processed next<br\/>        }<br\/>        else  \/\/ Else print root&#039;s data and set root as NULL<br\/>        {<br\/>            printf(&quot;%d &quot;, root-&gt;data);<br\/>            root = NULL;<br\/>        }<br\/>    } while (!isEmpty(stack));<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    \/\/ Let us construct the tree shown in above figure<br\/>    struct Node* root = NULL;<br\/>    root = newNode(1);<br\/>    root-&gt;left = newNode(2);<br\/>    root-&gt;right = newNode(3);<br\/>    root-&gt;left-&gt;left = newNode(4);<br\/>    root-&gt;left-&gt;right = newNode(5);<br\/>    root-&gt;right-&gt;left = newNode(6);<br\/>    root-&gt;right-&gt;right = newNode(7);<br\/>    printf(&quot;Post order traversal of binary tree is :\\n&quot;);<br\/>    printf(&quot;[&quot;);<br\/>    postOrderIterative(root);<br\/>    printf(&quot;]&quot;);<br\/>     <br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Post Order traversal of binary tree is\r\n[4, 5, 2, 6, 7, 3, 1]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Iterative Postorder Traversal | Set 2 (Using One Stack) &#8211; Stack &#8211; We have discussed a simple iterative postorder traversal using two stacks <\/p>\n","protected":false},"author":1,"featured_media":31284,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73012,79607],"tags":[81802,81938,81942,81937,81940,81804,81939,81941],"class_list":["post-27636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures","category-stack","tag-inorder-preorder-postorder-traversal-without-recursion-in-c","tag-iterative-inorder-traversal","tag-iterative-inorder-traversal-without-stack","tag-iterative-preorder-traversal","tag-postorder-traversal-algorithm-using-stack","tag-postorder-traversal-without-recursion-and-stack","tag-postorder-traversal-without-recursion-geeksforgeeks","tag-preorder-traversal-without-recursion-without-stack"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27636","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=27636"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27636\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31284"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}