{"id":27070,"date":"2018-01-02T22:17:20","date_gmt":"2018-01-02T16:47:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27070"},"modified":"2018-10-31T16:50:30","modified_gmt":"2018-10-31T11:20:30","slug":"python-programming-tree-traversals-inorder-preorder-postorder","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-tree-traversals-inorder-preorder-postorder\/","title":{"rendered":"Programming Tree Traversals (Inorder, Preorder and Postorder)"},"content":{"rendered":"<p><span style=\"color: #003300;\"><strong>Programming Tree Traversals (Inorder, Preorder and Postorder)<\/strong><\/span><\/p>\n<p>Unlike <a href=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/what-is-data-structure\" target=\"_blank\" rel=\"noopener\">linear data structures<\/a> (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Following are the generally used ways for traversing trees.<\/p>\n<h3 id=\"depth-first-traversals\"><span style=\"color: #333300;\">Depth First Traversals:<\/span><\/h3>\n<p>(a)<strong> Inorder<\/strong> (Left, Root, Right) : 4 2 5 1 3<br \/>\n(b) <strong>Preorder<\/strong> (Root, Left, Right) : 1 2 4 5 3<br \/>\n(c) <strong>Postorder<\/strong> (Left, Right, Root) : 4 5 2 3 1<\/p>\n<p>Breadth First or Level Order Traversal : 1 2 3 4 5<br \/>\nPlease see this post for <a href=\"https:\/\/www.wikitechy.com\/technology\/25857-2\/\" target=\"_blank\" rel=\"noopener\">Breadth First Traversal.<\/a><\/p>\n<h3 id=\"inorder-traversal\"><span style=\"color: #003366;\"><strong>Inorder Traversal:<\/strong><\/span><\/h3>\n<pre>Algorithm Inorder(tree)\r\n   1. Traverse the left subtree, i.e., call Inorder(left-subtree)\r\n   2. Visit the root.\r\n   3. Traverse the right subtree, i.e., call Inorder(right-subtree)\r\n<\/pre>\n<h3 id=\"uses-of-inorder\"><span style=\"color: #008080;\"><strong>Uses of Inorder<\/strong><\/span><\/h3>\n<p>In case of <a href=\"https:\/\/www.wikitechy.com\/technology\/python-program-inorder-successor-binary-search-tree\/\" target=\"_blank\" rel=\"noopener\">binary search trees<\/a> (BST), Inorder traversal gives nodes in non-decreasing order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder itraversal s reversed, can be used.<br \/>\n<span style=\"color: #993300;\"><strong>Example:<\/strong><\/span> Inorder traversal for the above given figure is 4 2 5 1 3.<\/p>\n[ad type=&#8221;banner&#8221;]<strong><br \/>\n<\/strong><\/p>\n<h3 id=\"postorder-traversal\"><span style=\"color: #003366;\"><strong>Postorder Traversal:<\/strong><\/span><\/h3>\n<pre>Algorithm Postorder(tree)\r\n   1. Traverse the left subtree, i.e., call Postorder(left-subtree)\r\n   2. Traverse the right subtree, i.e., call Postorder(right-subtree)\r\n   3. Visit the root.\r\n<\/pre>\n<h3 id=\"uses-of-postorder\"><span style=\"color: #008080;\"><strong>Uses of Postorder<\/strong><\/span><\/h3>\n<p>Postorder traversal is used to delete the tree. . <a href=\"https:\/\/www.wikitechy.com\/technology\/python-algorithm-iterative-postorder-traversal-set-1-using-two-stacks\/\" target=\"_blank\" rel=\"noopener\">Postorder traversal<\/a> is also useful to get the <a href=\"https:\/\/www.wikitechy.com\/technology\/python-algorithm-reverse-string-using-stack\/\" target=\"_blank\" rel=\"noopener\">postfix expression<\/a> of an expression tree. Please see<a href=\"http:\/\/en.wikipedia.org\/wiki\/Reverse_Polish_notation\" target=\"_blank\" rel=\"noopener\"> http:\/\/en.wikipedia.org\/wiki\/Reverse_Polish_notation<\/a> to for the usage of postfix expression.<\/p>\n<p><span style=\"color: #993300;\"><strong>Example:<\/strong> <\/span>Postorder traversal for the above given figure is 4 5 2 3 1.<\/p>\n[ad type=&#8221;banner&#8221;]\n<strong><br \/>\n<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to for tree traversals<br\/> <br\/># A class that represents an individual node in a<br\/># Binary Tree<br\/>class Node:<br\/>    def __init__(self,key):<br\/>        self.left = None<br\/>        self.right = None<br\/>        self.val = key<br\/> <br\/> <br\/># A function to do inorder tree traversal<br\/>def printInorder(root):<br\/> <br\/>    if root:<br\/> <br\/>        # First recur on left child<br\/>        printInorder(root.left)<br\/> <br\/>        # then print the data of node<br\/>        print(root.val),<br\/> <br\/>        # now recur on right child<br\/>        printInorder(root.right)<br\/> <br\/> <br\/> <br\/># A function to do postorder tree traversal<br\/>def printPostorder(root):<br\/> <br\/>    if root:<br\/> <br\/>        # First recur on left child<br\/>        printPostorder(root.left)<br\/> <br\/>        # the recur on right child<br\/>        printPostorder(root.right)<br\/> <br\/>        # now print the data of node<br\/>        print(root.val),<br\/> <br\/> <br\/># A function to do postorder tree traversal<br\/>def printPreorder(root):<br\/> <br\/>    if root:<br\/> <br\/>        # First print the data of node<br\/>        print(root.val),<br\/> <br\/>        # Then recur on left child<br\/>        printPreorder(root.left)<br\/> <br\/>        # Finally recur on right child<br\/>        printPreorder(root.right)<br\/> <br\/> <br\/># Driver code<br\/>root = Node(1)<br\/>root.left      = Node(2)<br\/>root.right     = Node(3)<br\/>root.left.left  = Node(4)<br\/>root.left.right  = Node(5)<br\/>print &quot;Preorder traversal of binary tree is&quot;<br\/>printPreorder(root)<br\/> <br\/>print &quot;\\nInorder traversal of binary tree is&quot;<br\/>printInorder(root)<br\/> <br\/>print &quot;\\nPostorder traversal of binary tree is&quot;<br\/>printPostorder(root)<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h3 id=\"output\"><span style=\"color: #339966;\">Output:<\/span><\/h3>\n<pre>Preorder traversal of binary tree is\r\n1 2 4 5 3 \r\nInorder traversal of binary tree is\r\n4 2 5 1 3 \r\nPostorder traversal of binary tree is\r\n4 5 2 3 1<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python &#8211;  Programming Tree Traversals (Inorder, Preorder and Postorder) &#8211; Tree &#8211; Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) <\/p>\n","protected":false},"author":1,"featured_media":31291,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80140],"tags":[80710,80711,80801,80799,80797,80714,80798,80800],"class_list":["post-27070","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-binay-tree","tag-binary-tree-traversal-program-in-c","tag-inorder-preorder-postorder-traversal-examples-pdf","tag-inorder-preorder-postorder-traversal-examples-ppt","tag-inorder-traversal-algorithm","tag-inorder-traversal-example","tag-inorder-traversal-without-recursion","tag-tree-traversal-in-data-structure","tag-tree-traversal-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27070","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=27070"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27070\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31291"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}