Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.42 KB | None | 0 0
  1.  
  2.  
  3. public class RedBlackTree <E extends Comparable<E>>{
  4.  
  5.     private Node<E> root;
  6.     private static final boolean RED = false;
  7.     private static final boolean BLACK = true;
  8.    
  9.    
  10.     public RedBlackTree() {
  11.  
  12.     }
  13.    
  14.     public boolean insert(E element) throws NullPointerException{
  15.  
  16.         Node<E> current = root;
  17.         Node<E> newnode = new Node<E>(element);
  18.        
  19.         //iterate through loop to find a spot for the new node. break loop if we reach a null child.
  20.         while(current != null) {
  21.            
  22.             //step right
  23.             if(current.element.compareTo(element) < 0) {           
  24.                 if(current.rightChild == null)
  25.                     break;
  26.                
  27.                 current = current.rightChild;
  28.                
  29.             //step left
  30.             }else if(current.element.compareTo(element) > 0) {
  31.                 if(current.leftChild == null)
  32.                     break;
  33.                
  34.                 current = current.leftChild;
  35.                
  36.             //equals current element
  37.             }else {
  38.                 return false;
  39.                
  40.             }
  41.         }
  42.        
  43.         //link newnode to parent node.
  44.         newnode.parent = current;
  45.        
  46.         //if current is null tree is empty, insert first node
  47.         if(current == null) {
  48.             root = newnode;
  49.             root.color = BLACK;
  50.            
  51.         }else {
  52.             if(current.element.compareTo(element) < 0) {
  53.                 current.rightChild = newnode;
  54.                 newnode.parent = current;
  55.                 insertUtil(newnode);
  56.                
  57.             }else if(current.element.compareTo(element) > 0) {
  58.                 current.leftChild = newnode;
  59.                 newnode.parent = current;
  60.                 insertUtil(newnode);
  61.                
  62.             }
  63.         }
  64.        
  65.        
  66.        
  67.         return true;
  68.     }
  69.    
  70.    
  71.     private void insertUtil(Node<E> current) {
  72.        
  73.  
  74.         while(current.parent.color == RED) {
  75.            
  76.             Node<E> parent = current.parent, grandparent = current.parent.parent, uncle;
  77.            
  78.             if(parent != null && grandparent != null) {
  79.                
  80.                
  81.                 if(current == parent.leftChild) {
  82.                    
  83.                     //left-left case
  84.                     if(parent == grandparent.leftChild) {
  85.    
  86.                         uncle = grandparent.rightChild;
  87.                        
  88.                         //ll case black uncle
  89.                         if(uncle == null || uncle.color == BLACK) {
  90.                             System.out.println("llbu");
  91.                             rightRotate(current.parent.parent);
  92.                             current.parent.rightChild.color = RED;
  93.                            
  94.                         //ll case red uncle
  95.                         }else {
  96.                             System.out.println("llru");
  97.                             recolor(current.parent);
  98.                            
  99.                         }
  100.                        
  101.                     //right-left case
  102.                     }else if(parent == grandparent.rightChild) {
  103.    
  104.                         uncle = grandparent.leftChild;
  105.                        
  106.                         //rl case black uncle
  107.                         if(uncle == null || uncle.color == BLACK) {
  108.                             System.out.println("rlbu");
  109.                             rightRotate(current.parent);
  110.                             leftRotate(current.parent);
  111.                         //rl case red uncle
  112.                         }else {
  113.                             System.out.println("rlru");
  114.                             recolor(current.parent);
  115.                            
  116.                         }
  117.                     }
  118.                    
  119.    
  120.                 }else if(current == parent.rightChild) {
  121.                    
  122.                     //right-right case
  123.                     if(parent == grandparent.rightChild) {
  124.                    
  125.                         uncle = grandparent.leftChild;
  126.                         //rr case black uncle
  127.                         if(uncle == null || uncle.color == BLACK) {
  128.                             System.out.println("rrbu");
  129.                             leftRotate(current.parent.parent);
  130.                             current.parent.leftChild.color = RED;
  131.                        
  132.                         //rr case red uncle
  133.                         }else {
  134.                             System.out.println("rrru");
  135.                             recolor(current.parent);
  136.    
  137.                         }
  138.                        
  139.                     //left-right case
  140.                     }else if(parent == grandparent.leftChild) {
  141.    
  142.                         uncle = grandparent.rightChild;
  143.                        
  144.                         //lr case black uncle
  145.                         if(uncle == null || uncle.color == BLACK) {
  146.                            
  147.                             System.out.println("lrbu");
  148.                            
  149.                             leftRotate(current.parent);
  150.                             rightRotate(current.parent);
  151.                            
  152.                        
  153.                         //lr case red uncle
  154.                         }else {
  155.                             System.out.println("lrru");
  156.                            
  157.                             recolor(current.parent);
  158.    
  159.                         }
  160.                     }
  161.                 }
  162.                
  163.                 root.color = BLACK;
  164.                
  165.                 //handle exception cases while trying to step to the next node
  166.                 try {
  167.                     current = current.parent.parent;
  168.                    
  169.                     if(current == root || current == null)
  170.                         break;
  171.                    
  172.                 }catch(Exception e) {
  173.                     break;
  174.                 }
  175.                
  176.             }
  177.            
  178.         }      
  179.        
  180.     }
  181.    
  182.    
  183.    
  184.     private void leftRotate(Node<E> node) {
  185.         Node<E>  rightChild = node.rightChild;
  186.         Node<E>  rLeftChild = rightChild.leftChild;
  187.        
  188.         rightChild.leftChild = node;
  189.         node.rightChild = rLeftChild;
  190.        
  191.         if(rLeftChild != null)
  192.             rLeftChild.parent = node;
  193.        
  194.         //case where node was a subtree
  195.         if(node.parent != null) {
  196.             node.parent.leftChild = rightChild;
  197.             node.parent.color = RED;
  198.         }
  199.         //case where node was the root
  200.         else
  201.             root = rightChild;
  202.        
  203.         rightChild.color = BLACK;
  204.        
  205.         rightChild.parent = node.parent;
  206.         node.parent = rightChild;
  207.    
  208.        
  209.  
  210.            
  211.     }
  212.    
  213.     private void rightRotate(Node<E> node) {
  214.        
  215.         Node<E>  leftChild = node.leftChild;
  216.         Node<E>  lRightChild = leftChild.rightChild;
  217.        
  218.         leftChild.rightChild = node;
  219.         node.leftChild = lRightChild;
  220.        
  221.         if(lRightChild != null)
  222.             lRightChild.parent = node;
  223.        
  224.         //case where node was a subtree
  225.         if(node.parent != null) {
  226.             node.parent.rightChild = leftChild;
  227.             node.parent.color = RED;
  228.         }
  229.         //case where node was the root
  230.         else
  231.             root = leftChild;
  232.        
  233.        
  234.         leftChild.color = BLACK;
  235.        
  236.         leftChild.parent = node.parent;
  237.         node.parent = leftChild;
  238.        
  239.            
  240.     }
  241.    
  242.     private void recolor(Node<E> node) {
  243.         node.color = BLACK;
  244.        
  245.         if(node == node.parent.rightChild) {
  246.             if(node.parent.leftChild != null)
  247.                 node.parent.leftChild.color = BLACK;
  248.            
  249.         }else if(node == node.parent.leftChild) {
  250.             if(node.parent.rightChild != null)
  251.                 node.parent.rightChild.color = BLACK;
  252.         }
  253.        
  254.         if(node.parent != root)
  255.             node.parent.color = RED;
  256.     }
  257.  
  258.     public boolean contains(E object) {
  259.         return true;
  260.     }
  261.    
  262.     public String toString() {
  263.         return toString(root);
  264.     }
  265.    
  266.     private String toString(Node current) {
  267.         String s = "";
  268.        
  269.         if(current == null)
  270.             return "";
  271.        
  272.         if(current.color == RED) {
  273.             s += "*" + current.element + " ";
  274.            
  275.         }else {
  276.             s += current.element + " ";
  277.            
  278.         }
  279.  
  280.         if(current.leftChild != null) {
  281.             s += toString(current.leftChild);
  282.            
  283.         }
  284.  
  285.         if(current.rightChild != null) {
  286.             s += toString(current.rightChild);
  287.            
  288.         }
  289.  
  290.         return s;
  291.     }
  292.    
  293.     private static class Node <E extends Comparable<E>>{
  294.         private E element;
  295.         private Node leftChild;
  296.         private Node rightChild;
  297.         private Node parent;
  298.         private boolean color;
  299.        
  300.         public Node(){
  301.             element = null;
  302.             leftChild = null;
  303.             rightChild = null;
  304.             parent = null;
  305.             color = RED;
  306.         }
  307.        
  308.         public Node(E element) {
  309.             this.element = element;
  310.             leftChild = null;
  311.             rightChild = null;
  312.             parent = null;
  313.             color = RED;
  314.         }
  315.  
  316.     }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement