Advertisement
Guest User

Treap

a guest
Dec 22nd, 2013
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.71 KB | None | 0 0
  1.  
  2. /* Mark Klingberg
  3.  * m2615919
  4.  * 10/4/2013
  5.  *
  6.  * This program implements the probabilistic treap data structure.
  7.  */
  8.  
  9. import java.util.*;
  10.  
  11. class Node <AnyType extends Comparable<AnyType>>{
  12.    
  13.     AnyType data;
  14.     int priority;
  15.    
  16.     Node<AnyType> left, right;
  17.  
  18.     Node(AnyType data, int priority, Node<AnyType> left, Node<AnyType> right)
  19.     {
  20.         this.data = data;
  21.         this.priority = priority;
  22.         this.left = left;
  23.         this.right = right;
  24.     }
  25. }
  26.  
  27. public class treap<AnyType extends Comparable<AnyType>> {
  28.  
  29.     private Node<AnyType> root = null;
  30.     private LinkedList<Integer> usedProiritiesList = new LinkedList<Integer>(); //used to store already used priorities.
  31.     private int size = 0;
  32.    
  33.     public void add(AnyType data){
  34.    
  35.         int priority  = (int)(Math.ceil(Math.random() * Integer.MAX_VALUE));
  36.        
  37.         while(true){
  38.            
  39.             //generate random number from 0 to Integer.MAX_VALUE
  40.             priority = (int)Math.ceil(Math.random() * Integer.MAX_VALUE);
  41.            
  42.             //if priority is not in L
  43.             if(!(usedProiritiesList.contains(priority))){
  44.            
  45.                 //store it in L
  46.                 usedProiritiesList.add(priority);
  47.                     break;
  48.             }
  49.                
  50.             else{
  51.                 //Do nothing, priority has already been used. Continue loop and generate a new priority.
  52.             }
  53.         }
  54.            
  55.         add(data, priority);
  56.     }
  57.    
  58.     public void add(AnyType data, int priority){
  59.        
  60.          root = add(root, data, priority);
  61.     }
  62.    
  63.     public Node<AnyType> add(Node<AnyType> root, AnyType data, int priority){
  64.        
  65.         //Insert nod BST style:
  66.        
  67.         if(root == null){
  68.             //new node is added here.
  69.             size++;
  70.             return new Node<AnyType>(data, priority, null, null);
  71.         }
  72.        
  73.         else if ((data.compareTo(root.data)) < 0)
  74.         {
  75.             //data < root, go left
  76.             root.left = add(root.left, data, priority);
  77.            
  78.             //Rotate to satisfy heap priority if needed.
  79.             if(root.left.priority < root.priority){
  80.            
  81.                 //swap the nodes
  82.                
  83.                 //set up parent node, prepare it to be the right child of current child node
  84.                 Node<AnyType> temp = new Node<AnyType>(root.data, root.priority, root.left.right, root.right);
  85.                 //replace values in parent node with that of the child
  86.                 root.data = root.left.data;
  87.                 root.priority = root.left.priority;
  88.                 //replace partent.left with child.left
  89.                 root.left = root.left.left;
  90.                 //make its right node the parent node that was set up
  91.                 root.right = temp;
  92.             }
  93.         }
  94.         else if ((data.compareTo(root.data)) > 0)
  95.         {
  96.             //data > root, go right
  97.             root.right = add(root.right, data, priority);
  98.            
  99.             //Rotate to satisfy heap priority if needed.
  100.             if(root.right.priority < root.priority){
  101.                 //swap the nodes
  102.                
  103.                 //set up parent node, prepare it to be the left child of current child node
  104.                 Node<AnyType> temp = new Node<AnyType>(root.data, root.priority, root.left, root.right.left);
  105.                
  106.                 //replace values in parent node with that of the child
  107.                 root.data = root.right.data;
  108.                 root.priority = root.right.priority;
  109.                 //replace partent.right with child.right
  110.                 root.right = root.right.right;
  111.                 //make its right node the parent node that was set up
  112.                 root.left = temp;
  113.             }
  114.         }
  115.         else
  116.         {
  117.             // Stylistically, I have this here to explicitly state that we are
  118.             // disallowing insertion of duplicate values.
  119.         }
  120.            
  121.         return root;   
  122.     }
  123.        
  124.     public void remove(AnyType data){
  125.        
  126.         //if this node exists, remove it
  127.         if(contains(data)){
  128.            
  129.             root = removeNode(root, data);
  130.            
  131.         }
  132.        
  133.         else{
  134.             //The treap did not contain data
  135.         }
  136.     }
  137.    
  138.     //moves root to the node where data is. if it is a leaf node it deletes it
  139.     private Node<AnyType> removeNode(Node<AnyType> root, AnyType data){
  140.        
  141.             if(root.data == data){
  142.                
  143.                 //removing node
  144.                 if((root.left==null)&&(root.right==null)){
  145.                     size--;
  146.                     return null;
  147.                 }
  148.                 else{
  149.                    
  150.                     //node found, needs rotation
  151.                     if((root.left==null)||(root.right==null)){
  152.                        
  153.                         if(root.left==null){
  154.                                    
  155.                             //set up parent node that will be rotated
  156.                             Node<AnyType> temp = new Node<AnyType>(root.data, root.priority, root.left, root.right.left);
  157.                                    
  158.                             //move former child node to parent node position
  159.                             root.data = root.right.data;
  160.                             root.priority = root.right.priority;
  161.                             root.right = root.right.right;
  162.                                    
  163.                             //take former parent node and place as new parent nodes child
  164.                             root.left = temp;
  165.                                    
  166.                             root.left = removeNode(root.left, data);   
  167.                         }                          
  168.                             //temp.right == null
  169.                         else{
  170.                                    
  171.                             //set up parent node that will be rotated
  172.                             Node<AnyType> temp = new Node<AnyType>(root.data, root.priority, root.left.right, root.right);
  173.                                    
  174.                             //move former child node to parent node position
  175.                             root.data = root.left.data;
  176.                             root.priority = root.left.priority;
  177.                             root.left = root.left.left;
  178.                                    
  179.                             //take former parent node and place as new parent nodes child
  180.                             root.right = temp;
  181.                                        
  182.                             root.right = removeNode(root.right, data);
  183.                                
  184.                         }
  185.                     }
  186.                     //2 child nodes, compare for lowest priority
  187.                     else{
  188.                                
  189.                         if(root.left.priority < root.right.priority){
  190.                                    
  191.                             //set up parent node that will be rotated
  192.                             Node<AnyType> temp = new Node<AnyType>(root.data, root.priority, root.left.right, root.right);
  193.                                    
  194.                             //move former child node to parent node position
  195.                             root.data = root.left.data;
  196.                             root.priority = root.left.priority;
  197.                             root.left = root.left.left;
  198.                                    
  199.                             //take former parent node and place as new parent nodes child
  200.                             root.right = temp;
  201.                                    
  202.                             root.right = removeNode(root.right, data);
  203.                                    
  204.                         }
  205.                         //temp.right.priority < temp.left.priority
  206.                         else{
  207.                                
  208.                             //set up parent node that will be rotated
  209.                             Node<AnyType> temp = new Node<AnyType>(root.data, root.priority, root.left, root.right.left);
  210.                                
  211.                             //move former child node to parent node position
  212.                             root.data = root.right.data;
  213.                             root.priority = root.right.priority;
  214.                             root.right = root.right.right;
  215.                                
  216.                             //take former parent node and place as new parent nodes child
  217.                             root.left = temp;
  218.                                
  219.                             root.left = removeNode(root.left, data);
  220.                            
  221.                         }
  222.                     }
  223.                 }
  224.             }
  225.            
  226.             //temp > data
  227.             else if(data.compareTo(root.data) < 0){
  228.                
  229.                 root.left = removeNode(root.left, data);
  230.             }
  231.             //temp < data
  232.             else{
  233.                
  234.                 root.right = removeNode(root.right, data);
  235.             }
  236.            
  237.             return root;
  238.         }
  239.    
  240.     public boolean contains(AnyType data){
  241.        
  242.         Node<AnyType> temp = new Node<AnyType>(root.data, root.priority, root.left, root.right);
  243.        
  244.         while(temp != null){
  245.            
  246.             if(temp.data == data)
  247.                 return true;
  248.             //temp > data
  249.             else if(data.compareTo(temp.data) < 0)
  250.             {
  251.                 temp = temp.left;
  252.             }
  253.             //temp < data
  254.             else{
  255.                 temp = temp.right;
  256.             }
  257.         }
  258.        
  259.         return false;
  260.     }
  261.    
  262.     public int size(){
  263.         return size;
  264.     }
  265.    
  266.     public int height(){
  267.          
  268.         return findHeight(root, -1, -1);
  269.     }
  270.          
  271.          
  272.     private int findHeight(Node<AnyType> root, int i, int max){
  273.          
  274.         if ( root != null ){
  275.            
  276.             ++i;
  277.             if ( i > max )
  278.                 max = i;
  279.          
  280.             //increase i by 1 and check for larger max before traveling to next node
  281.             if(root.left != null){
  282.                
  283.                 max = findHeight(root.left, i, max);
  284.             }
  285.             if(root.right != null){
  286.                
  287.                 max = findHeight(root.right, i, max);
  288.             }
  289.              
  290.             //decrease i by 1 when backtracking
  291.             i--;
  292.            
  293.         }
  294.              
  295.         return max;
  296.     }
  297.    
  298.     public static double difficultyRating(){
  299.        
  300.         return 2.0;
  301.     }
  302.    
  303.     public static double hoursSpent(){
  304.        
  305.         return 8.0;
  306.     }
  307.    
  308.     public void inorder()
  309.     {
  310.         System.out.print("In-order Traversal:");
  311.         inorder(root);
  312.         System.out.println();
  313.     }
  314.  
  315.     private void inorder(Node<AnyType> root)
  316.     {
  317.         if (root == null)
  318.             return;
  319.  
  320.         inorder(root.left);
  321.         System.out.print(" " + root.data);
  322.         inorder(root.right);
  323.     }
  324.    
  325.     public static void main(String[] args) {
  326.        
  327.         treap<String> test = new treap<String>();
  328.        
  329.         test.add("wl",162283549);
  330.         test.add("nx",911094035);
  331.         test.add("dj",742034874);
  332.         test.add("it",1501123419);
  333.         test.add("rz",938870320);
  334.         test.add("je",1118164710);
  335.         test.add("jq",167394538);
  336.         test.add("mx",684413799);
  337.         test.add("kn",1496528339);
  338.         test.add("cl",376379770);
  339.         test.add("kd",359614604);
  340.         test.add("sx",1524460400);
  341.         test.add("tq",760316997);
  342.         test.add("xg",1752446483);
  343.         test.add("ze",1951832343);
  344.         test.add("md",428940290);
  345.         test.add("bu",274689379);
  346.         test.add("po",1954243111);
  347.         test.add("ym",1298570710);
  348.         test.add("im",573274445);
  349.         test.add("kv",736941912);
  350.        
  351.         test.inorder();
  352.        
  353.         test.remove("wl");
  354.         test.remove("nx");
  355.         test.remove("dj");
  356.        
  357.         test.inorder();
  358.        
  359.         System.out.println(test.size());
  360.         System.out.println(test.height());
  361.  
  362.     }
  363.  
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement