Java Program to Implement Treap

«
»
This is a Java Program to implement Treap. Treap is a form of binary search tree data structure that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of insertions and deletions of keys, the shape of the tree is a random variable with the same probability distribution as a random binary tree; in particular, with high probability its height is proportional to the logarithm of the number of keys, so that each search, insertion, or deletion operation takes logarithmic time to perform.

Here is the source code of the Java program to implement Treap. 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 Treap
  3.  **/
  4.  
  5.  import java.util.Scanner;
  6.  import java.util.Random;
  7.  
  8.  /** Class TreapNode **/
  9.  class TreapNode
  10.  {
  11.      TreapNode left, right;
  12.      int priority, element;
  13.  
  14.      /** Constructor **/    
  15.      public TreapNode()
  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 TreapNode(int ele)
  25.      {
  26.          this(ele, null, null);
  27.      } 
  28.  
  29.      /** Constructor **/
  30.      public TreapNode(int ele, TreapNode left, TreapNode 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 TreapTree **/
  40.  class TreapTree
  41.  {
  42.      private TreapNode root;
  43.      private static TreapNode nil = new TreapNode();
  44.  
  45.      /** Constructor **/
  46.      public TreapTree()
  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 TreapNode insert(int X, TreapNode T)
  69.      {
  70.          if (T == nil)
  71.              return new TreapNode(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.                   TreapNode 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.                  TreapNode 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(TreapNode 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(TreapNode 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(TreapNode 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(TreapNode 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(TreapNode 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 TreapTest **/
  187. public class TreapTest
  188. {
  189.     public static void main(String[] args)
  190.     {            
  191.         Scanner scan = new Scanner(System.in);
  192.         /** Creating object of Treap **/
  193.         TreapTree trpt = new TreapTree(); 
  194.         System.out.println("Treap Test\n");          
  195.         char ch;
  196.         /**  Perform tree operations  **/
  197.         do    
  198.         {
  199.             System.out.println("\nTreap 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.                 trpt.insert( scan.nextInt() );                     
  212.                 break;                          
  213.             case 2 : 
  214.                 System.out.println("Enter integer element to search");
  215.                 System.out.println("Search result : "+ trpt.search( scan.nextInt() ));
  216.                 break;                                          
  217.             case 3 : 
  218.                 System.out.println("Nodes = "+ trpt.countNodes());
  219.                 break;     
  220.             case 4 : 
  221.                 System.out.println("Empty status = "+ trpt.isEmpty());
  222.                 break;
  223.             case 5 : 
  224.                 System.out.println("\nTreap Cleared");
  225.                 trpt.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.             trpt.postorder();
  234.             System.out.print("\nPre order : ");
  235.             trpt.preorder();    
  236.             System.out.print("\nIn order : ");
  237.             trpt.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. }

advertisement
Treap Test
 
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
24
 
Post order : 24
Pre order : 24
In order : 24
Do you want to continue (Type y or n)
 
y
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
6
 
Post order : 6 24
Pre order : 24 6
In order : 6 24
Do you want to continue (Type y or n)
 
y
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
94
 
Post order : 6 94 24
Pre order : 24 6 94
In order : 6 24 94
Do you want to continue (Type y or n)
 
y
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
19
 
Post order : 6 94 24 19
Pre order : 19 6 24 94
In order : 6 19 24 94
Do you want to continue (Type y or n)
 
y
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
28
 
Post order : 6 24 19 94 28
Pre order : 28 19 6 24 94
In order : 6 19 24 28 94
Do you want to continue (Type y or n)
 
y
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
5
 
Post order : 6 5 24 19 94 28
Pre order : 28 19 5 6 24 94
In order : 5 6 19 24 28 94
Do you want to continue (Type y or n)
 
y
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
63
 
Post order : 6 5 24 19 28 94 63
Pre order : 63 28 19 5 6 24 94
In order : 5 6 19 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
Treap 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 : 6 5 24 19 28 94 63
Pre order : 63 28 19 5 6 24 94
In order : 5 6 19 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
3
Nodes = 7
 
Post order : 6 5 24 19 28 94 63
Pre order : 63 28 19 5 6 24 94
In order : 5 6 19 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
Treap Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
5
 
Treap Cleared
 
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
 
y
 
Treap 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
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

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.