{"id":27135,"date":"2018-01-03T21:38:52","date_gmt":"2018-01-03T16:08:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27135"},"modified":"2018-01-03T21:38:52","modified_gmt":"2018-01-03T16:08:52","slug":"level-order-tree-traversal","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/level-order-tree-traversal\/","title":{"rendered":"C program -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-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Recursive C program for level order traversal of Binary Tree<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/> <br\/>\/* A binary tree node has data, pointer to left child<br\/>   and a pointer to right child *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node* left, *right;<br\/>};<br\/> <br\/>\/* Function protoypes *\/<br\/>void printGivenLevel(struct node* root, int level);<br\/>int height(struct node* node);<br\/>struct node* newNode(int data);<br\/> <br\/>\/* Function to print level order traversal a tree*\/<br\/>void printLevelOrder(struct node* root)<br\/>{<br\/>    int h = height(root);<br\/>    int i;<br\/>    for (i=1; i&lt;=h; i++)<br\/>        printGivenLevel(root, i);<br\/>}<br\/> <br\/>\/* Print nodes at a given level *\/<br\/>void printGivenLevel(struct node* root, int level)<br\/>{<br\/>    if (root == NULL)<br\/>        return;<br\/>    if (level == 1)<br\/>        printf(&quot;%d &quot;, root-&gt;data);<br\/>    else if (level &gt; 1)<br\/>    {<br\/>        printGivenLevel(root-&gt;left, level-1);<br\/>        printGivenLevel(root-&gt;right, level-1);<br\/>    }<br\/>}<br\/> <br\/>\/* Compute the &quot;height&quot; of a tree -- the number of<br\/>    nodes along the longest path from the root node<br\/>    down to the farthest leaf node.*\/<br\/>int height(struct node* node)<br\/>{<br\/>    if (node==NULL)<br\/>        return 0;<br\/>    else<br\/>    {<br\/>        \/* compute the height of each subtree *\/<br\/>        int lheight = height(node-&gt;left);<br\/>        int rheight = height(node-&gt;right);<br\/> <br\/>        \/* use the larger one *\/<br\/>        if (lheight &gt; rheight)<br\/>            return(lheight+1);<br\/>        else return(rheight+1);<br\/>    }<br\/>}<br\/> <br\/>\/* Helper function that allocates a new node with the<br\/>   given data and NULL left and right pointers. *\/<br\/>struct node* newNode(int data)<br\/>{<br\/>    struct node* node = (struct node*)<br\/>                        malloc(sizeof(struct node));<br\/>    node-&gt;data = data;<br\/>    node-&gt;left = NULL;<br\/>    node-&gt;right = NULL;<br\/> <br\/>    return(node);<br\/>}<br\/> <br\/>\/* Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>    struct node *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\/> <br\/>    printf(&quot;Level Order traversal of binary tree is \\n&quot;);<br\/>    printLevelOrder(root);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Level Order Tree Traversal &#8211; Level Order Tree Travers &#8211; level order tree travesal &#8211; here are basically two functions in this method. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80140,83838],"tags":[80908,80907,80905,80909,80903,80906,80910,80904],"class_list":["post-27135","post","type-post","status-publish","format-standard","hentry","category-binay-tree","category-level-order-tree","tag-level-order-traversal-example","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-print-tree-level-by-level-java","tag-reverse-level-order-traversal"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27135","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=27135"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27135\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}