Preorder Traversal of a Binary Tree using Recursion in Java

«
»
This is a java program to construct a binary tree and perform preorder traversal of the constructed binary tree.
Nodes visited are in the order:
visit Root node
visit Left node
visit Right node

Here is the source code of the Java Program to Perform Preorder Recursive Traversal of a Given 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 recursive preorder 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 preorder()
  88.     {
  89.         preorder(root);
  90.     }
  91.  
  92.     private void preorder(BinarySearchTreeNodes r)
  93.     {
  94.         if (r != null)
  95.         {
  96.             System.out.print(r.getData() + " ");
  97.             preorder(r.getLeft());
  98.             preorder(r.getRight());
  99.         }
  100.     }
  101. }
  102.  
  103. public class Preorder_Recursive_BST
  104. {
  105.     public static void main(String[] args)
  106.     {
  107.         Scanner scan = new Scanner(System.in);
  108.         BinarySearchTree bst = new BinarySearchTree();
  109.         System.out.println("Enter the first 10 elements of the tree\n");
  110.         int N = 10;
  111.         for (int i = 0; i < N; i++)
  112.             bst.insert(scan.nextInt());
  113.  
  114.         System.out.print("\nPre order  : ");
  115.         bst.preorder();
  116.  
  117.         scan.close();
  118.     }
  119. }

Output:

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

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.