Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.57 KB | None | 0 0
  1. package eg.edu.alexu.csd.filestructure.redblacktree;
  2.  
  3. import javax.management.RuntimeErrorException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class RedBlackTree<T extends Comparable<T>, V> implements IRedBlackTree<T, V> {
  8.     private INode<T,V>root=new Node<>();
  9.     private INode<T,V> current=root;
  10.     @Override
  11.     public INode<T,V> getRoot() {
  12.         if(root.isNull())
  13.             return null;
  14.         return root;
  15.     }
  16.  
  17.     @Override
  18.     public boolean isEmpty() {
  19.         return root.isNull();
  20.     }
  21.  
  22.     @Override
  23.     public void clear() {
  24.         root=new Node<>();
  25.         current =root;
  26.     }
  27.  
  28.     @Override
  29.     public V search(T key) {
  30.         if(key==null)
  31.             throw new RuntimeErrorException(new Error("null key inserted"));
  32.         if(root.isNull()|| current.isNull())
  33.         {
  34.             current=root;
  35.             return null;
  36.         }
  37.         if(current.getKey().compareTo(key)==0)
  38.         {
  39.             V answer= current.getValue();
  40.             current =root;
  41.             return answer;
  42.         }
  43.         if(current.getKey().compareTo(key)>0)
  44.             current = current.getLeftChild();
  45.         else
  46.             current = current.getRightChild();
  47.         return search(key);
  48.     }
  49.  
  50.     @Override
  51.     public boolean contains(T key) {
  52.         V value= search(key);
  53.         return value!=null;
  54.     }
  55.  
  56.     @Override
  57.     public void insert(T key, V value) {
  58.         if(key==null)
  59.             throw new RuntimeErrorException(new Error("null key inserted"));
  60.         if(value==null)
  61.             throw new RuntimeErrorException(new Error("null value inserted"));
  62.         if(root.isNull())
  63.         {
  64.             root.setColor(false);
  65.             root.setKey(key);
  66.             root.setValue(value);
  67.             root.setRightChild(new Node<>());
  68.             root.setLeftChild(new Node<>());
  69.             return;
  70.         }
  71.         if(contains(key))
  72.         {
  73.             Replace(key,value);
  74.             return;
  75.         }
  76.         INode<T,V>parent=root;
  77.         while(true)
  78.         {
  79.             if(current.isNull())
  80.             {
  81.                 current =new Node<>();
  82.                 current.setColor(true);
  83.                 current.setKey(key);
  84.                 current.setValue(value);
  85.                 current.setParent(parent);
  86.                 current.setLeftChild(new Node<>());
  87.                 current.setRightChild(new Node<>());
  88.                 if(key.compareTo(parent.getKey())>0)
  89.                     parent.setRightChild(current);
  90.                 else
  91.                     parent.setLeftChild(current);
  92.                 //Balance(current);
  93.                 current=root;
  94.                 break;
  95.             }
  96.             if(current.getKey().compareTo(key)<0)
  97.             {
  98.                 parent=current;
  99.                 current= current.getRightChild();
  100.             }
  101.             else
  102.             {
  103.                 parent=current;
  104.                 current=current.getLeftChild();
  105.             }
  106.         }
  107.     }
  108.     private void Replace(T key,V value)
  109.     {
  110.         if(current.getKey().compareTo(key)==0)
  111.         {
  112.             current.setValue(value);
  113.             current =root;
  114.             return;
  115.         }
  116.         if(current.getKey().compareTo(key)>0)
  117.             current = current.getLeftChild();
  118.         else
  119.             current = current.getRightChild();
  120.         Replace(key,value);
  121.     }
  122.     private void LeftRotate(INode<T,V> x){
  123.         INode<T,V> y=x.getRightChild();
  124.         x.setRightChild(y.getLeftChild());
  125.         if( (!y.getLeftChild().getLeftChild().isNull()) || (!y.getLeftChild().getRightChild().isNull()) ){
  126.             y.getLeftChild().setParent(x);
  127.         }
  128.         y.setParent(x.getParent());
  129.         if( (x.getParent().getLeftChild().isNull()) || (x.getParent().getRightChild().isNull())||x.getParent().isNull() ){
  130.             this.root=y;
  131.         }else if(x==x.getParent().getLeftChild()){
  132.             x.getParent().setLeftChild(y);
  133.         }else{
  134.             x.getParent().setRightChild(y);
  135.         }
  136.         y.setLeftChild(x);
  137.         x.setParent(y);
  138.     }
  139.     private void RightRotate(INode<T,V> y){
  140.         INode<T,V> x;
  141.         x=y.getLeftChild();
  142.         y.setLeftChild(x.getRightChild());
  143.         if( (!x.getRightChild().getLeftChild().isNull()) || (!x.getRightChild().getRightChild().isNull()) ){
  144.             x.getRightChild().setParent(y);
  145.         }
  146.         x.setParent(y.getParent());
  147.         if( (y.getParent().getLeftChild().isNull()) && (y.getParent().getRightChild().isNull()) ){
  148.             this.root=x;
  149.         }else if(y==y.getParent().getRightChild()){
  150.             y.getParent().setRightChild(x);
  151.         }else{
  152.             y.getParent().setLeftChild(x);
  153.         }
  154.         x.setRightChild(y);
  155.         y.setParent(x);
  156.     }
  157.     private void Balance(INode<T,V> node) {
  158.         while(node.getParent().getColor()){
  159.             if(node.getParent()==node.getParent().getParent().getLeftChild()){
  160.                 INode<T,V> y=node.getParent().getParent().getRightChild();
  161.                 if(y.getColor()){
  162.                     node.getParent().setColor(false);
  163.                     y.setColor(false);
  164.                     node.getParent().getParent().setColor(true);
  165.                     node=node.getParent().getParent();
  166.                 }else if(node==node.getParent().getRightChild()){
  167.                     node=node.getParent();
  168.                     LeftRotate(node);
  169.                     node.getParent().setColor(false);
  170.                     node.getParent().getParent().setColor(true);
  171.                     RightRotate(node.getParent().getParent());
  172.                 }
  173.             }else if(node.getParent()==node.getParent().getParent().getRightChild()){
  174.                 node=node.getParent();
  175.                 RightRotate(node);
  176.                 node.getParent().setColor(false);
  177.                 node.getParent().getParent().setColor(true);
  178.                 LeftRotate(node);
  179.             }
  180.         }
  181.         this.root.setColor(false);
  182.     }
  183.  
  184.     @Override
  185.     public boolean delete(T key) {
  186.         if(key==null)
  187.             throw new RuntimeErrorException(new Error("null key inserted"));
  188.         if(root.isNull()||!contains(key))
  189.             return false;
  190.         else
  191.         {
  192.             List<INode<T,V>> nodes=BSTDeletion(key);
  193.             if(nodes.get(0).getColor()!=nodes.get(1).getColor())
  194.             {
  195.                 INode<T,V>u=nodes.get(0);
  196.                 INode<T,V>v=nodes.get(1);
  197.                 v.setColor(false);
  198.                 v.setKey(u.getKey());
  199.                 v.setValue(u.getValue());
  200.                 v.setLeftChild(new Node<>());
  201.                 v.setRightChild(new Node<>());
  202.             }
  203.             else
  204.             {
  205.                 Node<T,V>u= (Node<T, V>) nodes.get(0);
  206.                 Node<T,V>v= (Node<T, V>) nodes.get(1);
  207.                 u.setDoubleBlack(true);
  208.                 if (u.IsDoubleBlack()&&u!=root)
  209.                 {
  210.                     INode<T,V>s;
  211.                     if(RightChild(u))
  212.                     {
  213.                         s=u.getParent().getLeftChild();
  214.                     }
  215.                     else
  216.                     {
  217.                         s=u.getParent().getRightChild();
  218.                         if(!s.getColor())
  219.                         {
  220.                             INode<T,V>r;
  221.                             //s has a red child
  222.                             if(s.getRightChild().getColor())
  223.                             {
  224.                                 r=s.getRightChild();
  225.                                 v.setKey(u.getKey());
  226.                                 v.setValue(u.getValue());
  227.                                 u=new Node<>();
  228.                             }
  229.                         }
  230.                     }
  231.                     /*if(!s.getColor())
  232.                     {
  233.                         INode<T,V>r;
  234.                         if(s.getLeftChild().getColor())
  235.                             r=s.getLeftChild();
  236.                         else
  237.                             r=s.getRightChild();
  238.  
  239.                     }*/
  240.                 }
  241.             }
  242.         }
  243.         return true;
  244.     }
  245.     //first element is leaf,second element is to be deleted
  246.     private List<INode<T,V>> BSTDeletion(T key)
  247.     {
  248.         List<INode<T,V>> container=new ArrayList<>();
  249.         while(current.getKey().compareTo(key)!=0)
  250.         {
  251.             if(current.getKey().compareTo(key)>0)
  252.                 current=current.getLeftChild();
  253.             else
  254.                 current=current.getRightChild();
  255.         }//No children
  256.         if(current.getLeftChild().isNull()&&current.getRightChild().isNull())
  257.         {
  258.             container.add(current.getRightChild());
  259.             container.add(current);
  260.         }//One child Only
  261.         else if(current.getLeftChild().isNull())
  262.         {
  263.             INode<T,V> tempNode=current.getRightChild();
  264.             container.add(tempNode);
  265.             container.add(current);
  266.         }
  267.         else if(current.getRightChild().isNull())
  268.         {
  269.             INode<T,V>tempNode=current.getLeftChild();
  270.             container.add(tempNode);
  271.             container.add(current);
  272.         }//Both children
  273.         else
  274.         {
  275.             INode<T,V>tempNode=current.getRightChild();
  276.             while(!tempNode.getLeftChild().isNull())
  277.                 tempNode=tempNode.getLeftChild();
  278.             container.add(tempNode);
  279.             container.add(current);
  280.         }
  281.         return container;
  282.     }
  283.     private boolean RightChild(INode<T,V>node)
  284.     {
  285.         //right = true ...... left = false
  286.         return node.getParent().getRightChild() == node;
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement