{"id":27638,"date":"2018-04-09T20:17:42","date_gmt":"2018-04-09T14:47:42","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27638"},"modified":"2018-09-14T19:08:19","modified_gmt":"2018-09-14T13:38:19","slug":"python-algorithm-iterative-postorder-traversal-set-2-using-one-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-algorithm-iterative-postorder-traversal-set-2-using-one-stack\/","title":{"rendered":"Python  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>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20for%20iterative%20postorder%20traversal%0A%23%20using%20one%20stack%0A%20%0A%23%20Stores%20the%20answer%0Aans%20%3D%20%5B%5D%0A%20%0A%23%20A%20Binary%20tree%20node%0Aclass%20Node%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Constructor%20to%20create%20a%20new%20node%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%0A%20%20%20%20%20%20%20%20self.left%20%3D%20None%0A%20%20%20%20%20%20%20%20self.right%20%3D%20None%0A%20%0Adef%20peek(stack)%3A%0A%20%20%20%20if%20len(stack)%20%3E%200%3A%0A%20%20%20%20%20%20%20%20return%20stack%5B-1%5D%0A%20%20%20%20return%20None%0A%23%20A%20iterative%20function%20to%20do%20postorder%20traversal%20of%20%0A%23%20a%20given%20binary%20tree%0Adef%20postOrderIterative(root)%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%23%20Check%20for%20empty%20tree%0A%20%20%20%20if%20root%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20stack%20%3D%20%5B%5D%0A%20%20%20%20%20%0A%20%20%20%20while(True)%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20while%20(root)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Push%20root\u2019s%20right%20child%20and%20then%20root%20to%20stack%0A%20%20%20%20%20%20%20%20%20%20%20%20%20if%20root.right%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20stack.append(root.right)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20stack.append(root)%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Set%20root%20as%20root\u2019s%20left%20child%0A%20%20%20%20%20%20%20%20%20%20%20%20%20root%20%3D%20root.left%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Pop%20an%20item%20from%20stack%20and%20set%20it%20as%20root%0A%20%20%20%20%20%20%20%20root%20%3D%20stack.pop()%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20the%20popped%20item%20has%20a%20right%20child%20and%20the%0A%20%20%20%20%20%20%20%20%23%20right%20child%20is%20not%20processed%20yet%2C%20then%20make%20sure%0A%20%20%20%20%20%20%20%20%23%20right%20child%20is%20processed%20before%20root%0A%20%20%20%20%20%20%20%20if%20(root.right%20is%20not%20None%20and%0A%20%20%20%20%20%20%20%20%20%20%20%20peek(stack)%20%3D%3D%20root.right)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20stack.pop()%20%23%20Remove%20right%20child%20from%20stack%20%0A%20%20%20%20%20%20%20%20%20%20%20%20stack.append(root)%20%23%20Push%20root%20back%20to%20stack%0A%20%20%20%20%20%20%20%20%20%20%20%20root%20%3D%20root.right%20%23%20change%20root%20so%20that%20the%20%0A%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%23%20righ%20childis%20processed%20next%0A%20%0A%20%20%20%20%20%20%20%20%23%20Else%20print%20root\u2019s%20data%20and%20set%20root%20as%20None%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ans.append(root.data)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20root%20%3D%20None%0A%20%0A%20%20%20%20%20%20%20%20if%20(len(stack)%20%3C%3D%200)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%0A%23%20Driver%20pogram%20to%20test%20above%20function%0Aroot%20%3D%20Node(1)%0Aroot.left%20%3D%20Node(2)%0Aroot.right%20%3D%20Node(3)%0Aroot.left.left%20%3D%20Node(4)%0Aroot.left.right%20%3D%20Node(5)%0Aroot.right.left%20%3D%20Node(6)%0Aroot.right.right%20%3D%20Node(7)%0A%20%0Aprint%20%22Post%20Order%20traversal%20of%20binary%20tree%20is%22%0ApostOrderIterative(root)%0Aprint%20ans%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Nikhil%20Kumar%20Singh(nickzuck_007)\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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>Python  Algorithm &#8211; Iterative Postorder Traversal | Set 2 (Using One Stack) &#8211; Stack &#8211; We have discussed a simple iterative postorder traversal <\/p>\n","protected":false},"author":1,"featured_media":31275,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73012,79607],"tags":[81802,81938,81942,81937,81953,81940,81804,81941],"class_list":["post-27638","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-non-recursive-postorder-traversal-algorithm","tag-postorder-traversal-algorithm-using-stack","tag-postorder-traversal-without-recursion-and-stack","tag-preorder-traversal-without-recursion-without-stack"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27638","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=27638"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27638\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31275"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}