{"id":27144,"date":"2018-01-03T21:50:24","date_gmt":"2018-01-03T16:20:24","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27144"},"modified":"2018-01-03T21:50:24","modified_gmt":"2018-01-03T16:20:24","slug":"java-program-level-order-tree-traversal","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-program-level-order-tree-traversal\/","title":{"rendered":"java 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-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Recursive Java program for level order traversal of Binary Tree<br\/> <br\/>\/* Class containing left and right child of current <br\/>   node and key value*\/<br\/>class Node<br\/>{<br\/>    int data;<br\/>    Node left, right;<br\/>    public Node(int item)<br\/>    {<br\/>        data = item;<br\/>        left = right = null;<br\/>    }<br\/>}<br\/> <br\/>class BinaryTree<br\/>{<br\/>    \/\/ Root of the Binary Tree<br\/>    Node root;<br\/> <br\/>    public BinaryTree()<br\/>    {<br\/>        root = null;<br\/>    }<br\/> <br\/>    \/* function to print level order traversal of tree*\/<br\/>    void printLevelOrder()<br\/>    {<br\/>        int h = height(root);<br\/>        int i;<br\/>        for (i=1; i&lt;=h; i++)<br\/>            printGivenLevel(root, i);<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(Node root)<br\/>    {<br\/>        if (root == null)<br\/>           return 0;<br\/>        else<br\/>        {<br\/>            \/* compute  height of each subtree *\/<br\/>            int lheight = height(root.left);<br\/>            int rheight = height(root.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\/>    \/* Print nodes at the given level *\/<br\/>    void printGivenLevel (Node root ,int level)<br\/>    {<br\/>        if (root == null)<br\/>            return;<br\/>        if (level == 1)<br\/>            System.out.print(root.data + &quot; &quot;);<br\/>        else if (level &gt; 1)<br\/>        {<br\/>            printGivenLevel(root.left, level-1);<br\/>            printGivenLevel(root.right, level-1);<br\/>        }<br\/>    }<br\/>     <br\/>    \/* Driver program to test above functions *\/<br\/>    public static void main(String args[])<br\/>    {<br\/>       BinaryTree tree = new BinaryTree();<br\/>       tree.root= new Node(1);<br\/>       tree.root.left= new Node(2);<br\/>       tree.root.right= new Node(3);<br\/>       tree.root.left.left= new Node(4);<br\/>       tree.root.left.right= new Node(5);<br\/>        <br\/>       System.out.println(&quot;Level order traversal of binary tree is &quot;);<br\/>       tree.printLevelOrder();<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>java program &#8211; Level Order Tree Traversal &#8211; Level order traversal of 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,2139],"tags":[80965,80963,80964,80908,80903,80906,80962,80966],"class_list":["post-27144","post","type-post","status-publish","format-standard","hentry","category-binay-tree","category-coding","category-java","tag-binary-tree-level-order-traversal-ii","tag-binary-tree-level-order-traversal-java","tag-binary-tree-level-order-traversal-leetcode","tag-level-order-traversal-example","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-zigzag-level-order-traversal"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27144","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=27144"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27144\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}