Java Program to Implement Double Order Traversal of a Binary Tree

This is a Java Program to perform Double Order traversal over binary tree.
Recurse through:
1. Visit root of (sub)tree.
2. Visit left sub-tree.
3. Revisit root of (sub)tree.
4. Visit right sub-tree.

Here is the source code of the Java Program to Implement Double Order Traversal of a Binary 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 implement doubleorder traversal of the Binary Search Tree
  2. import java.util.Scanner;
  3.  
  4. class BinarySearchTreeNodes
  5. {
  6.     BinarySearchTreeNodes left, right;
  7.     int      data;
  8.  
  9.     public BinarySearchTreeNodes()
  10.     {
  11.         left = null;
  12.         right = null;
  13.         data = 0;
  14.     }
  15.  
  16.     public BinarySearchTreeNodes(int n)
  17.     {
  18.         left = null;
  19.         right = null;
  20.         data = n;
  21.     }
  22.  
  23.     public void setLeft(BinarySearchTreeNodes n)
  24.     {
  25.         left = n;
  26.     }
  27.  
  28.     public void setRight(BinarySearchTreeNodes n)
  29.     {
  30.         right = n;
  31.     }
  32.  
  33.     public BinarySearchTreeNodes getLeft()
  34.     {
  35.         return left;
  36.     }
  37.  
  38.     public BinarySearchTreeNodes getRight()
  39.     {
  40.         return right;
  41.     }
  42.  
  43.     public void setData(int d)
  44.     {
  45.         data = d;
  46.     }
  47.  
  48.     public int getData()
  49.     {
  50.         return data;
  51.     }
  52. }
  53.  
  54. class BinarySearchTree
  55. {
  56.     private BinarySearchTreeNodes root;
  57.  
  58.     public BinarySearchTree()
  59.     {
  60.         root = null;
  61.     }
  62.  
  63.     public boolean isEmpty()
  64.     {
  65.         return root == null;
  66.     }
  67.  
  68.     public void insert(int data)
  69.     {
  70.         root = insert(root, data);
  71.     }
  72.  
  73.     private BinarySearchTreeNodes insert(BinarySearchTreeNodes node, int data)
  74.     {
  75.         if (node == null)
  76.             node = new BinarySearchTreeNodes(data);
  77.         else
  78.         {
  79.             if (data <= node.getData())
  80.                 node.left = insert(node.left, data);
  81.             else
  82.                 node.right = insert(node.right, data);
  83.         }
  84.         return node;
  85.     }
  86.  
  87. 	public void doubleorder()
  88. 	{
  89. 		doubleorder(root);
  90. 	}
  91.  
  92. 	private void doubleorder(BinarySearchTreeNodes r)
  93. 	{
  94. 		if(r != null)
  95. 		{
  96. 			System.out.print(r.getData() + " ");
  97. 			doubleorder(r.getLeft());
  98. 			System.out.print(r.getData() + " ");
  99. 			doubleorder(r.getRight());
  100. 		}
  101. 	}
  102. }
  103.  
  104. public class Doubleorder_Traversal
  105. {
  106.     public static void main(String[] args)
  107.     {
  108.         Scanner scan = new Scanner(System.in);
  109.         BinarySearchTree bst = new BinarySearchTree();
  110.         System.out.println("Enter the first 10 elements of the tree\n");
  111.         int N = 10;
  112.         for (int i = 0; i < N; i++)
  113.             bst.insert(scan.nextInt());
  114.  
  115.         System.out.print("\nDouble-order   : ");
  116.         bst.doubleorder();
  117.  
  118.         scan.close();
  119.     }
  120. }

Output:

$ javac Doubleorder_Traversal.java
$ java Doubleorder_Traversal
 
Enter the first 10 elements of the tree
 
12 10 11 03 15 19 02 01 04 70
 
Double-order   : 12 10 3 2 1 1 2 3 4 4 10 11 11 12 15 15 19 19 70 70

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.