Java Program to Implement Randomized Binary Search Tree

This is a Java Program to implement Randomized Binary Search Tree. The randomized binary search tree stores the same nodes with the same random distribution of tree shape, but maintains different information within the nodes of the tree in order to maintain its randomized structure. The implementation of randomized binary search tree is similar to that of a Treap data structure.

Here is the source code of the Java program to implement Randomized Binary Search Tree. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  *  Java Program to Implement RandomizedBinarySearchTree
  3.  **/
  4.  
  5.  import java.util.Scanner;
  6.  import java.util.Random;
  7.  
  8.  /** Class RBSTNode **/
  9.  class RBSTNode
  10.  {
  11.      RBSTNode left, right;
  12.      int priority, element;
  13.  
  14.      /** Constructor **/    
  15.      public RBSTNode()
  16.      {
  17.          this.element = 0;
  18.          this.left = this;
  19.          this.right = this;
  20.          this.priority = Integer.MAX_VALUE;
  21.      }    
  22.  
  23.      /** Constructor **/    
  24.      public RBSTNode(int ele)
  25.      {
  26.          this(ele, null, null);
  27.      } 
  28.  
  29.      /** Constructor **/
  30.      public RBSTNode(int ele, RBSTNode left, RBSTNode right)
  31.      {
  32.          this.element = ele;
  33.          this.left = left;
  34.          this.right = right;
  35.          this.priority = new Random().nextInt( );
  36.      }    
  37.  }
  38.  
  39.  /** Class RandomizedBinarySearchTree **/
  40.  class RandomizedBinarySearchTree
  41.  {
  42.      private RBSTNode root;
  43.      private static RBSTNode nil = new RBSTNode();
  44.  
  45.      /** Constructor **/
  46.      public RandomizedBinarySearchTree()
  47.      {
  48.          root = nil;
  49.      }
  50.  
  51.      /** Function to check if tree is empty **/
  52.      public boolean isEmpty()
  53.      {
  54.          return root == nil;
  55.      }
  56.  
  57.      /** Make the tree logically empty **/
  58.      public void makeEmpty()
  59.      {
  60.          root = nil;
  61.      }
  62.  
  63.      /** Functions to insert data **/
  64.      public void insert(int X)
  65.      {
  66.          root = insert(X, root);
  67.      }
  68.      private RBSTNode insert(int X, RBSTNode T)
  69.      {
  70.          if (T == nil)
  71.              return new RBSTNode(X, nil, nil);
  72.          else if (X < T.element)
  73.          {
  74.              T.left = insert(X, T.left);
  75.              if (T.left.priority < T.priority)
  76.              {
  77.                   RBSTNode L = T.left;
  78.                   T.left = L.right;
  79.                   L.right = T;
  80.                   return L;
  81.               }    
  82.          }
  83.          else if (X > T.element)
  84.          {
  85.              T.right = insert(X, T.right);
  86.              if (T.right.priority < T.priority)
  87.              {
  88.                  RBSTNode R = T.right;
  89.                   T.right = R.left;
  90.                   R.left = T;
  91.                   return R;
  92.              }
  93.          }
  94.          return T;
  95.      }
  96.  
  97.      /** Functions to count number of nodes **/
  98.      public int countNodes()
  99.      {
  100.          return countNodes(root);
  101.      }
  102.      private int countNodes(RBSTNode r)
  103.      {
  104.          if (r == nil)
  105.              return 0;
  106.          else
  107.          {
  108.              int l = 1;
  109.              l += countNodes(r.left);
  110.              l += countNodes(r.right);
  111.              return l;
  112.          }
  113.      }
  114.  
  115.      /** Functions to search for an element **/
  116.      public boolean search(int val)
  117.      {
  118.          return search(root, val);
  119.      }
  120.      private boolean search(RBSTNode r, int val)
  121.      {
  122.          boolean found = false;
  123.          while ((r != nil) && !found)
  124.          {
  125.              int rval = r.element;
  126.              if (val < rval)
  127.                  r = r.left;
  128.              else if (val > rval)
  129.                  r = r.right;
  130.              else
  131.              {
  132.                  found = true;
  133.                  break;
  134.              }
  135.              found = search(r, val);
  136.          }
  137.          return found;
  138.      }
  139.  
  140.      /** Function for inorder traversal **/
  141.      public void inorder()
  142.      {
  143.          inorder(root);
  144.      }
  145.      private void inorder(RBSTNode r)
  146.      {
  147.          if (r != nil)
  148.          {
  149.              inorder(r.left);
  150.              System.out.print(r.element +" ");
  151.              inorder(r.right);
  152.          }
  153.      }
  154.  
  155.      /** Function for preorder traversal **/
  156.      public void preorder()
  157.      {
  158.          preorder(root);
  159.      }
  160.      private void preorder(RBSTNode r)
  161.      {
  162.          if (r != nil)
  163.          {
  164.              System.out.print(r.element +" ");
  165.              preorder(r.left);             
  166.              preorder(r.right);
  167.          }
  168.      }
  169.  
  170.      /** Function for postorder traversal **/
  171.      public void postorder()
  172.      {
  173.          postorder(root);
  174.      }
  175.      private void postorder(RBSTNode r)
  176.      {
  177.          if (r != nil)
  178.          {
  179.              postorder(r.left);             
  180.              postorder(r.right);
  181.              System.out.print(r.element +" ");
  182.          }
  183.      }         
  184.  }
  185.  
  186. /** Class RandomizedBinarySearchTreeTest **/
  187. public class RandomizedBinarySearchTreeTest
  188. {
  189.     public static void main(String[] args)
  190.     {            
  191.         Scanner scan = new Scanner(System.in);
  192.         /** Creating object of RandomizedBinarySearchTree **/
  193.         RandomizedBinarySearchTree rbst = new RandomizedBinarySearchTree(); 
  194.         System.out.println("Randomized Binary SearchTree Test\n");          
  195.         char ch;
  196.         /**  Perform tree operations  **/
  197.         do    
  198.         {
  199.             System.out.println("\nRandomized Binary SearchTree Operations\n");
  200.             System.out.println("1. insert ");
  201.             System.out.println("2. search");
  202.             System.out.println("3. count nodes");
  203.             System.out.println("4. check empty");
  204.             System.out.println("5. clear");
  205.  
  206.             int choice = scan.nextInt();            
  207.             switch (choice)
  208.             {
  209.             case 1 : 
  210.                 System.out.println("Enter integer element to insert");
  211.                 rbst.insert( scan.nextInt() );                     
  212.                 break;                          
  213.             case 2 : 
  214.                 System.out.println("Enter integer element to search");
  215.                 System.out.println("Search result : "+ rbst.search( scan.nextInt() ));
  216.                 break;                                          
  217.             case 3 : 
  218.                 System.out.println("Nodes = "+ rbst.countNodes());
  219.                 break;     
  220.             case 4 : 
  221.                 System.out.println("Empty status = "+ rbst.isEmpty());
  222.                 break;
  223.             case 5 : 
  224.                 System.out.println("\nRandomizedBinarySearchTree Cleared");
  225.                 rbst.makeEmpty();
  226.                 break;            
  227.             default : 
  228.                 System.out.println("Wrong Entry \n ");
  229.                 break;   
  230.             }
  231.             /**  Display tree  **/ 
  232.             System.out.print("\nPost order : ");
  233.             rbst.postorder();
  234.             System.out.print("\nPre order : ");
  235.             rbst.preorder();    
  236.             System.out.print("\nIn order : ");
  237.             rbst.inorder();
  238.  
  239.             System.out.println("\nDo you want to continue (Type y or n) \n");
  240.             ch = scan.next().charAt(0);                        
  241.         } while (ch == 'Y'|| ch == 'y');               
  242.     }
  243. }

 
Randomized Binary SearchTree Test
 
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
28
 
Post order : 28
Pre order : 28
In order : 28
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
5
 
Post order : 5 28
Pre order : 28 5
In order : 5 28
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
63
 
Post order : 5 28 63
Pre order : 63 28 5
In order : 5 28 63
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
24
 
Post order : 5 24 28 63
Pre order : 63 28 24 5
In order : 5 24 28 63
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
64
 
Post order : 5 24 28 64 63
Pre order : 63 28 24 5 64
In order : 5 24 28 63 64
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
19
 
Post order : 5 24 28 19 64 63
Pre order : 63 19 5 28 24 64
In order : 5 19 24 28 63 64
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
94
 
Post order : 5 24 28 19 64 94 63
Pre order : 63 19 5 28 24 94 64
In order : 5 19 24 28 63 64 94
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
2
Enter integer element to search
24
Search result : true
 
Post order : 5 24 28 19 64 94 63
Pre order : 63 19 5 28 24 94 64
In order : 5 19 24 28 63 64 94
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
2
Enter integer element to search
25
Search result : false
 
Post order : 5 24 28 19 64 94 63
Pre order : 63 19 5 28 24 94 64
In order : 5 19 24 28 63 64 94
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
5
 
RandomizedBinarySearchTree Cleared
 
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
 
y
 
Randomized Binary SearchTree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
4
Empty status = true
 
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

👉 For weekly data structure practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels
advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations