{"id":27145,"date":"2018-01-03T21:52:56","date_gmt":"2018-01-03T16:22:56","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27145"},"modified":"2018-01-03T21:52:56","modified_gmt":"2018-01-03T16:22:56","slug":"python-program-level-order-tree-traversal","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-program-level-order-tree-traversal\/","title":{"rendered":"Python program &#8211; Level Order Tree Traversal"},"content":{"rendered":"<p>Level order traversal of a tree is breadth first traversal for the tree.<\/p>\n<p>Level order traversal of the above tree is 1 2 3 4 5<\/p>\n<div id=\"practice\">\n<h2 id=\"recommended-please-solve-it-on-practice-first\">Recommended: Please solve it on &#8220;<b><i><u>PRACTICE<\/u><\/i><\/b>&#8221; first,<\/h2>\n<h2 id=\"before-moving-on-to-the-solution\">before moving on to the solution.<\/h2>\n<\/div>\n<p><strong>METHOD 1 (Use function to print a given level)<\/strong><\/p>\n<p><strong>Algorithm:<\/strong><br \/>\nThere are basically two functions in this method. One is to print all nodes at a given level (printGivenLevel), and other is to print level order traversal of the tree (printLevelorder). printLevelorder makes use of printGivenLevel to print nodes at all levels one by one starting from root.<\/p>\n<pre>\/*Function to print level order traversal of tree*\/\r\n<strong>printLevelorder(tree)<\/strong>\r\nfor d = 1 to height(tree)\r\n   printGivenLevel(tree, d);\r\n\r\n\/*Function to print all nodes at a given level*\/\r\n<strong>printGivenLevel(tree, level)<\/strong>\r\nif tree is NULL then return;\r\nif level is 1, then\r\n    print(tree-&gt;data);\r\nelse if level greater than 1, then\r\n    printGivenLevel(tree-&gt;left, level-1);\r\n    printGivenLevel(tree-&gt;right, level-1);<\/pre>\n[ad type=&#8221;banner&#8221;]\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\"># Recursive Python program for level order traversal of Binary Tree<br\/> <br\/># A node structure<br\/>class Node:<br\/> <br\/>    # A utility function to create a new node<br\/>    def __init__(self, key):<br\/>        self.data = key <br\/>        self.left = None<br\/>        self.right = None<br\/> <br\/> <br\/># Function to  print level order traversal of tree<br\/>def printLevelOrder(root):<br\/>    h = height(root)<br\/>    for i in range(1, h+1):<br\/>        printGivenLevel(root, i)<br\/> <br\/> <br\/># Print nodes at a given level<br\/>def printGivenLevel(root , level):<br\/>    if root is None:<br\/>        return<br\/>    if level == 1:<br\/>        print &quot;%d&quot; %(root.data),<br\/>    elif level &gt; 1 :<br\/>        printGivenLevel(root.left , level-1)<br\/>        printGivenLevel(root.right , level-1)<br\/> <br\/> <br\/>&quot;&quot;&quot; Compute the height of a tree--the number of nodes<br\/>    along the longest path from the root node down to<br\/>    the farthest leaf node<br\/>&quot;&quot;&quot;<br\/>def height(node):<br\/>    if node is None:<br\/>        return 0<br\/>    else :<br\/>        # Compute the height of each subtree <br\/>        lheight = height(node.left)<br\/>        rheight = height(node.right)<br\/> <br\/>        #Use the larger one<br\/>        if lheight &gt; rheight :<br\/>            return lheight+1<br\/>        else:<br\/>            return rheight+1<br\/> <br\/># Driver program to test above function<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\/> <br\/>print &quot;Level order traversal of binary tree is -&quot;<br\/>printLevelOrder(root)<br\/> <br\/>#This code is contributed by Nikhil Kumar Singh(nickzuck_007)<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Level order traversal of binary tree is - \r\n1 2 3 4 5<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Python Program &#8211; Level Order Tree Traversal &#8211; Level order traversal of a tree is breadth first traversal for the tree. There are basically two functions <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80140,1,4148],"tags":[80969,80907,80905,80909,80903,80906,80962,80910],"class_list":["post-27145","post","type-post","status-publish","format-standard","hentry","category-binay-tree","category-coding","category-python","tag-breadth-first-traversal-of-a-tree-in-c","tag-level-order-traversal-in-spiral-form","tag-level-order-traversal-java","tag-level-order-traversal-leetcode","tag-level-order-traversal-of-a-binary-tree-in-c-code","tag-level-order-traversal-of-binary-tree-using-queue-in-c","tag-level-order-traversal-using-queue","tag-print-tree-level-by-level-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27145","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=27145"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27145\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}