{"id":170,"date":"2014-05-02T11:08:11","date_gmt":"2014-05-02T11:08:11","guid":{"rendered":"http:\/\/www.dimlucas.com\/?p=170"},"modified":"2017-02-22T11:12:05","modified_gmt":"2017-02-22T11:12:05","slug":"post-order-traversal-of-a-binary-tree-in-c","status":"publish","type":"post","link":"https:\/\/www.dimlucas.com\/index.php\/2014\/05\/02\/post-order-traversal-of-a-binary-tree-in-c\/","title":{"rendered":"Post-order Traversal of a Binary Tree in C++"},"content":{"rendered":"<p>If you know about <a href=\"https:\/\/www.dimlucas.com\/index.php\/2014\/04\/03\/pre-order-traversal-of-a-binary-tree-in-c\/\">pre-order traversal<\/a> you know that it visits the root first and the subtrees later. If you know about <a href=\"https:\/\/www.dimlucas.com\/index.php\/2014\/04\/14\/in-order-traversal-of-a-binary-tree-in-c\/\">in-order traversal<\/a> you know that it visits the nodes in-order (duh), first the left subtree , then the root and finally the right subtree. The remaining combination is to operate on the root when you&#8217;re done with the subtrees. That&#8217;s what post-order traversal does.<\/p>\n<p>Let&#8217;s see an example of printing all the nodes of a tree using post-order traversal:<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/www.dimlucas.com\/wp-content\/uploads\/2017\/02\/binarytree.png\" alt=\"binarytree\" width=\"315\" height=\"246\" class=\"alignnone size-full wp-image-158\" \/><\/p>\n<p>Starting from the root the post-order traversal algorithm will visit the left subtree, the right subtree and then return to the root node when it&#8217;s done.<\/p>\n<p><strong>Output so far<\/strong>: <empty><\/p>\n<p>We visit the left subtree on node-9 and move on its own subtrees until we find the tree&#8217;s leaves.<\/p>\n<p><strong>Output so far<\/strong>: <empty><\/p>\n<p>When we reach node-2 there are no left and right subtrees so we print it<\/p>\n<p><strong>Output so far<\/strong>: 2<\/p>\n<p>Same way with node-5<\/p>\n<p><strong>Output so far<\/strong>: 2, 5<\/p>\n<p>We return to node-9 and since we&#8217;re done with its subtrees we print the value 9<\/p>\n<p><strong>Output so far<\/strong>: 2, 5, 9<\/p>\n<p>Back to node-6. We haven&#8217;t yet visited its right subtree so on we go<\/p>\n<p><strong>Output so far<\/strong>: 2, 5, 9<\/p>\n<p>Node-4 has no left subtree, so we visit its right-hand one. <\/p>\n<p><strong>Output so far<\/strong>: 2, 5, 9<\/p>\n<p>We visit node-8&#8217;s left and right subtrees which are empty and then return to node-8 and print its value<\/p>\n<p><strong>Output so far<\/strong>: 2, 5, 9, 8<\/p>\n<p>Now that we&#8217;re done with node-4&#8217;s subtrees we print its value<\/p>\n<p><strong>Output so far<\/strong>: 2, 5, 9, 8, 4<\/p>\n<p>We&#8217;re done with both subtrees of our tree&#8217;s root, node-6. It&#8217;s time to print its value too.<\/p>\n<p><strong>Output so far<\/strong>: 2, 5, 9, 8, 4, 6<\/p>\n<p>And that&#8217;s it. We&#8217;ve traversed the tree In a post-order fashion and have printed its nodes.<\/p>\n<p>The algorithm that achieves that in C++ is the following:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/dimlucas\/300d71a42eed85def30f5c78e167ce07.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you know about pre-order traversal you know that it visits the root first and the subtrees later. If you know about in-order traversal you know that it visits the nodes in-order (duh), first the left subtree , then the root and finally the right subtree. The remaining combination is to operate on the root [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[77],"tags":[91,57,88,89,90],"_links":{"self":[{"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/posts\/170"}],"collection":[{"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":1,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dimlucas.com\/index.php\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}