Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.81 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.         if(x==root)
  125.             root=y;
  126.         MoveDown(y,x);
  127.         x.setRightChild(y.getLeftChild());
  128.         if(!y.getLeftChild().isNull())
  129.             y.getLeftChild().setParent(x);
  130.         y.setLeftChild(x);
  131.     }
  132.     private void RightRotate(INode<T,V> x){
  133.         INode<T,V> y=x.getLeftChild();
  134.         if(x==root)
  135.             root=y;
  136.         MoveDown(y,x);
  137.         x.setLeftChild(y.getRightChild());
  138.         if(!y.getRightChild().isNull())
  139.             y.getRightChild().setParent(x);
  140.         y.setRightChild(x);
  141.     }
  142.  
  143.     private void MoveDown(INode<T,V>y,INode<T,V>x)
  144.     {
  145.         if(x.getParent()!=null)
  146.         {
  147.             if (!RightChild(x))
  148.                 x.getParent().setLeftChild(y);
  149.             else
  150.                 x.getParent().setRightChild(y);
  151.         }
  152.         y.setParent(x.getParent());
  153.         x.setParent(y);
  154.     }
  155.  
  156.     private void Balance(INode<T,V> node) {
  157.         while(node.getParent().getColor()){
  158.             if(node.getParent()==node.getParent().getParent().getLeftChild()){
  159.                 INode<T,V> y=node.getParent().getParent().getRightChild();
  160.                 if(y.getColor()){
  161.                     node.getParent().setColor(false);
  162.                     y.setColor(false);
  163.                     node.getParent().getParent().setColor(true);
  164.                     node=node.getParent().getParent();
  165.                 }else if(node==node.getParent().getRightChild()){
  166.                     node=node.getParent();
  167.                     LeftRotate(node);
  168.                     node.getParent().setColor(false);
  169.                     node.getParent().getParent().setColor(true);
  170.                     RightRotate(node.getParent().getParent());
  171.                 }
  172.             }else if(node.getParent()==node.getParent().getParent().getRightChild()){
  173.                 node=node.getParent();
  174.                 RightRotate(node);
  175.                 node.getParent().setColor(false);
  176.                 node.getParent().getParent().setColor(true);
  177.                 LeftRotate(node);
  178.             }
  179.         }
  180.         this.root.setColor(false);
  181.     }
  182.  
  183.     @Override
  184.     public boolean delete(T key) {
  185.         if(key==null)
  186.             throw new RuntimeErrorException(new Error("null key inserted"));
  187.         if(root.isNull()||!contains(key))
  188.             return false;
  189.         List<INode<T,V>> nodes=BSTDeletion(key);
  190.         INode<T,V>u=nodes.get(0),v=nodes.get(1);
  191.         boolean BothBlack=((u.isNull()||!u.getColor())&&!v.getColor());
  192.         INode<T,V>parent=v.getParent();
  193.         if(u.isNull())
  194.         {
  195.             if(v==root)
  196.                 root=new Node<>();
  197.             else
  198.             {
  199.                 if(BothBlack)
  200.                     FixDoubleBlack(v);
  201.                 else
  202.                 {
  203.                     if(RightChild(v))
  204.                     {
  205.                         if(!v.getParent().getLeftChild().isNull())
  206.                             v.getParent().getLeftChild().setColor(true);
  207.                     }
  208.                     else
  209.                     {
  210.                         if(!v.getParent().getRightChild().isNull())
  211.                             v.getParent().getRightChild().setColor(true);
  212.                     }
  213.                 }
  214.                 if(!RightChild(v))
  215.                     parent.setLeftChild(new Node<>());
  216.                 else
  217.                     parent.setRightChild(new Node<>());
  218.             }
  219.             v=new Node<>();
  220.             return true;
  221.         }
  222.         if(v.getRightChild().isNull()||v.getLeftChild().isNull())
  223.         {
  224.             if(v==root)
  225.             {
  226.                 v.setValue(u.getValue());
  227.                 v.setLeftChild(new Node<>());
  228.                 v.setRightChild(new Node<>());
  229.                 u=new Node<>();
  230.             }
  231.             else
  232.             {
  233.                 if(!RightChild(v))
  234.                     parent.setLeftChild(u);
  235.                 else
  236.                     parent.setRightChild(u);
  237.                 v=new Node<>();
  238.                 u.setParent(parent);
  239.                 if(BothBlack)
  240.                     FixDoubleBlack(u);
  241.                 else
  242.                     u.setColor(false);
  243.             }
  244.             return true;
  245.         }
  246.         T Key=u.getKey();V Value=u.getValue();
  247.         u.setValue(v.getValue());u.setKey(v.getKey());
  248.         v.setKey(Key);v.setValue(Value);
  249.         return delete(u.getKey());
  250.     }
  251.     private void FixDoubleBlack(INode<T,V>node)
  252.     {
  253.         if(node==root)
  254.             return;
  255.         INode<T,V>sibling,parent=node.getParent();
  256.         if(RightChild(node))
  257.             sibling=node.getParent().getLeftChild();
  258.         else
  259.             sibling=node.getParent().getRightChild();
  260.         if(sibling.isNull())
  261.             FixDoubleBlack(parent);
  262.         else
  263.         {
  264.             if (sibling.getColor()) {
  265.                 parent.setColor(true);
  266.                 sibling.setColor(false);
  267.                 if (!RightChild(sibling))
  268.                     RightRotate(parent);
  269.                 else
  270.                     LeftRotate(parent);
  271.                 FixDoubleBlack(node);
  272.             }
  273.             else
  274.             {
  275.                 if(sibling.getLeftChild().getColor()||sibling.getRightChild().getColor())
  276.                 {
  277.                     if(!sibling.getLeftChild().isNull()&&sibling.getLeftChild().getColor())
  278.                     {
  279.                         if(!RightChild(sibling))
  280.                         {
  281.                             sibling.getLeftChild().setColor(sibling.getColor());
  282.                             sibling.setColor(parent.getColor());
  283.                             RightRotate(parent);
  284.                         }
  285.                         else
  286.                         {
  287.                             sibling.getLeftChild().setColor(parent.getColor());
  288.                             RightRotate(sibling);
  289.                             LeftRotate(parent);
  290.                         }
  291.                     }
  292.                     else
  293.                     {
  294.                         if(!RightChild(sibling))
  295.                         {
  296.                             sibling.getRightChild().setColor(parent.getColor());
  297.                             LeftRotate(sibling);
  298.                             RightRotate(parent);
  299.                         }
  300.                         else
  301.                         {
  302.                             sibling.getRightChild().setColor(sibling.getColor());
  303.                             sibling.setColor(parent.getColor());
  304.                             LeftRotate(parent);
  305.                         }
  306.                     }
  307.                     parent.setColor(false);
  308.                 }
  309.                 else
  310.                 {
  311.                     sibling.setColor(true);
  312.                     if(!parent.getColor())
  313.                         FixDoubleBlack(parent);
  314.                     else
  315.                         parent.setColor(false);
  316.                 }
  317.             }
  318.         }
  319.     }
  320.     //first element is successor,second element is to be deleted
  321.     private List<INode<T,V>> BSTDeletion(T key)
  322.     {
  323.         List<INode<T,V>> container=new ArrayList<>();
  324.         while(current.getKey().compareTo(key)!=0)
  325.         {
  326.             if(current.getKey().compareTo(key)>0)
  327.                 current=current.getLeftChild();
  328.             else
  329.                 current=current.getRightChild();
  330.         }//No children
  331.         if(current.getLeftChild().isNull()&&current.getRightChild().isNull())
  332.         {
  333.             container.add(current.getRightChild());
  334.             container.add(current);
  335.         }//One child Only
  336.         else if(current.getLeftChild().isNull())
  337.         {
  338.             INode<T,V> tempNode=current.getRightChild();
  339.             container.add(tempNode);
  340.             container.add(current);
  341.         }
  342.         else if(current.getRightChild().isNull())
  343.         {
  344.             INode<T,V>tempNode=current.getLeftChild();
  345.             container.add(tempNode);
  346.             container.add(current);
  347.         }//Both children
  348.         else
  349.         {
  350.             INode<T,V>tempNode=current.getRightChild();
  351.             while(!tempNode.getLeftChild().isNull())
  352.                 tempNode=tempNode.getLeftChild();
  353.             container.add(tempNode);
  354.             container.add(current);
  355.         }
  356.         return container;
  357.     }
  358.     private boolean RightChild(INode<T,V>node)
  359.     {
  360.         //right = true ...... left = false
  361.         return node.getParent().getRightChild() == node;
  362.     }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement