Java Program to Print the Nodes at Odd Levels of a Tree

«
»
This is a Java Program to implement a Binary Tree and print the level order traversal of the same, such that only odd levels are considered. At current level check nodes in next to next level and put them in Queue, dequeue them one by one and print them.

Here is the source code of the Java Program to Print only Odd Numbered Levels of a Tree. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to print only odd levels of the given tree
  2. import java.util.Queue;
  3. import java.util.LinkedList;
  4.  
  5. public class BinaryTreePrintOddLevels
  6. {
  7.     private static class Node<T>
  8.     {
  9.         public Node<T> left;
  10.         public Node<T> right;
  11.         public T       data;
  12.  
  13.         public Node(T data)
  14.         {
  15.             this.data = data;
  16.         }
  17.  
  18.         public Node<T> getLeft()
  19.         {
  20.             return this.left;
  21.         }
  22.  
  23.         public void setLeft(Node<T> left)
  24.         {
  25.             this.left = left;
  26.         }
  27.  
  28.         public Node<T> getRight()
  29.         {
  30.             return this.right;
  31.         }
  32.  
  33.         public void setRight(Node<T> right)
  34.         {
  35.             this.right = right;
  36.         }
  37.     }
  38.  
  39.     public static void preorder(Node<?> n)
  40.     {
  41.         if (n != null)
  42.         {
  43.             System.out.print(n.data + " ");
  44.             preorder(n.getLeft());
  45.             preorder(n.getRight());
  46.         }
  47.     }
  48.  
  49.     public static void inorder(Node<?> n)
  50.     {
  51.         if (n != null)
  52.         {
  53.             inorder(n.getLeft());
  54.             System.out.print(n.data + " ");
  55.             inorder(n.getRight());
  56.         }
  57.     }
  58.  
  59.     public static void postorder(Node<?> n)
  60.     {
  61.         if (n != null)
  62.         {
  63.             postorder(n.getLeft());
  64.             postorder(n.getRight());
  65.             System.out.print(n.data + " ");
  66.         }
  67.     }
  68.  
  69.     public static void levelorder(Node<?> n)
  70.     {
  71.         Queue<Node<?>> nodequeue = new LinkedList<Node<?>>();
  72.         if (n != null)
  73.             nodequeue.add(n);
  74.         while (!nodequeue.isEmpty())
  75.         {
  76.             Node<?> next = nodequeue.remove();
  77.             System.out.print(next.data + " ");
  78.             if (next.getLeft() != null)
  79.             {
  80.                 if (next.getLeft().getLeft() != null)
  81.                     nodequeue.add(next.getLeft().getLeft());
  82.                 if (next.getLeft().getRight() != null)
  83.                     nodequeue.add(next.getLeft().getRight());
  84.             }
  85.             if (next.getRight() != null)
  86.             {
  87.                 if (next.getRight().getLeft() != null)
  88.                     nodequeue.add(next.getRight().getLeft());
  89.                 if (next.getRight().getRight() != null)
  90.                     nodequeue.add(next.getRight().getRight());
  91.             }
  92.         }
  93.     }
  94.  
  95.     public static void main(final String[] args)
  96.     {
  97.         Node<Integer> one = new Node<Integer>(1);
  98.         Node<Integer> two = new Node<Integer>(2);
  99.         Node<Integer> three = new Node<Integer>(3);
  100.         Node<Integer> four = new Node<Integer>(4);
  101.         Node<Integer> five = new Node<Integer>(5);
  102.         Node<Integer> six = new Node<Integer>(6);
  103.         Node<Integer> seven = new Node<Integer>(7);
  104.         Node<Integer> eight = new Node<Integer>(8);
  105.         Node<Integer> nine = new Node<Integer>(9);
  106.  
  107.         one.setLeft(two);
  108.         one.setRight(three);
  109.         two.setLeft(four);
  110.         two.setRight(five);
  111.         three.setLeft(six);
  112.         four.setLeft(seven);
  113.         six.setLeft(eight);
  114.         six.setRight(nine);
  115.  
  116.         System.out.println("\nPre-Order of the Tree");
  117.         preorder(one);
  118.         System.out.println("\nIn-Order of the Tree");
  119.         inorder(one);
  120.         System.out.println("\nPost-Order of the Tree");
  121.         postorder(one);
  122.         System.out.println("\nLevel-Order Odd Levels of the Tree");
  123.         levelorder(one);
  124.  
  125.     }
  126. }

Output:

advertisement
$ javac BinaryTreePrintOddLevels.java
$ java BinaryTreePrintOddLevels
 
Pre-Order of the Tree
1 2 4 7 5 3 6 8 9 
In-Order of the Tree
7 4 2 5 1 8 6 9 3 
Post-Order of the Tree
7 4 5 2 8 9 6 3 1 
Level-Order Odd Levels of the Tree
1 4 5 6

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.