Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.71 KB | None | 0 0
  1. package eg.edu.alexu.csd.filestructure.redblacktree;
  2.  
  3. import javax.management.RuntimeErrorException;
  4.  
  5. public class RedBlackTree<T extends Comparable<T>, V> implements IRedBlackTree<T, V> {
  6.     private INode<T,V>root=new Node<>();
  7.     private INode<T,V> current=root;
  8.     @Override
  9.     public INode<T,V> getRoot() {
  10.         return root;
  11.     }
  12.  
  13.     @Override
  14.     public boolean isEmpty() {
  15.         return root.isNull();
  16.     }
  17.  
  18.     @Override
  19.     public void clear() {
  20.         root=new Node<>();
  21.         current =root;
  22.     }
  23.  
  24.     @Override
  25.     public V search(T key) {
  26.         if(key==null)
  27.             throw new RuntimeErrorException(new Error());
  28.         if(current.isNull()||root.isNull())
  29.         {
  30.             current=root;
  31.             return null;
  32.         }
  33.         if(current.getKey().compareTo(key)==0)
  34.         {
  35.             V answer= current.getValue();
  36.             current =root;
  37.             return answer;
  38.         }
  39.         if(current.getKey().compareTo(key)>0)
  40.             current = current.getLeftChild();
  41.         else
  42.             current = current.getRightChild();
  43.         return search(key);
  44.     }
  45.  
  46.     @Override
  47.     public boolean contains(T key) {
  48.         current=root;
  49.         V value= search(key);
  50.         return value!=null;
  51.     }
  52.  
  53.     @Override
  54.     public void insert(T key, V value) {
  55.         if(key==null)
  56.             throw new RuntimeErrorException(new Error("null key inserted"));
  57.         if(value==null)
  58.             throw new RuntimeErrorException(new Error("null value inserted"));
  59.         if(contains(key))
  60.         {
  61.             Replace(key,value);
  62.             return;
  63.         }
  64.         INode<T,V>NewNode=new Node<>();
  65.         NewNode.setLeftChild(new Node<>());NewNode.setRightChild(new Node<>());
  66.         NewNode.setKey(key);NewNode.setValue(value);
  67.         NewNode.setColor(true);
  68.         if(root.isNull())
  69.         {
  70.             NewNode.setColor(false);
  71.             root=NewNode;
  72.         }
  73.         else
  74.         {
  75.             INode<T,V>temp=BSTSearch(key);
  76.             NewNode.setParent(temp);
  77.             if(key.compareTo(temp.getKey())<0)
  78.                 temp.setLeftChild(NewNode);
  79.             else
  80.                 temp.setRightChild(NewNode);
  81.             FixRedRed(NewNode);
  82.         }
  83.     }
  84.     private void FixRedRed(INode<T,V>node)
  85.     {
  86.         if(node==root)
  87.         {
  88.             node.setColor(false);
  89.             return;
  90.         }
  91.         INode<T,V>parent=node.getParent(),grandparent=parent.getParent(),uncle;
  92.         if(grandparent==null)
  93.             uncle=null;
  94.         else
  95.         {
  96.             if(RightChild(parent))
  97.                 uncle=grandparent.getLeftChild();
  98.             else
  99.                 uncle=grandparent.getRightChild();
  100.         }
  101.         if(parent.getColor())
  102.         {
  103.             if(uncle!=null&&uncle.getColor())
  104.             {
  105.                 parent.setColor(false);
  106.                 uncle.setColor(false);
  107.                 grandparent.setColor(true);
  108.                 FixRedRed(grandparent);
  109.             }
  110.             else
  111.             {
  112.                 if(!RightChild(parent))
  113.                 {
  114.                     if(!RightChild(node))
  115.                     {
  116.                         boolean temp=parent.getColor();
  117.                         parent.setColor(grandparent.getColor());
  118.                         grandparent.setColor(temp);
  119.                     }
  120.                     else
  121.                     {
  122.                         LeftRotate(parent);
  123.                         boolean temp=node.getColor();
  124.                         node.setColor(grandparent.getColor());
  125.                         grandparent.setColor(temp);
  126.                     }
  127.                     RightRotate(grandparent);
  128.                 }
  129.                 else
  130.                 {
  131.                     if(!RightChild(node))
  132.                     {
  133.                         RightRotate(parent);
  134.                         boolean temp=node.getColor();
  135.                         node.setColor(grandparent.getColor());
  136.                         grandparent.setColor(temp);
  137.                     }
  138.                     else
  139.                     {
  140.                         boolean temp=parent.getColor();
  141.                         parent.setColor(grandparent.getColor());
  142.                         grandparent.setColor(temp);
  143.                     }
  144.                     LeftRotate(grandparent);
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     private void Replace(T key,V value)
  150.     {
  151.         if(current.getKey().compareTo(key)==0)
  152.         {
  153.             current.setValue(value);
  154.             current =root;
  155.             return;
  156.         }
  157.         if(current.getKey().compareTo(key)>0)
  158.             current = current.getLeftChild();
  159.         else
  160.             current = current.getRightChild();
  161.         Replace(key,value);
  162.     }
  163.     private void LeftRotate(INode<T,V> x){
  164.         INode<T,V> y=x.getRightChild();
  165.         if(x==root)
  166.             root=y;
  167.         MoveDown(y,x);
  168.         x.setRightChild(y.getLeftChild());
  169.         if(!y.getLeftChild().isNull())
  170.             y.getLeftChild().setParent(x);
  171.         y.setLeftChild(x);
  172.     }
  173.     private void RightRotate(INode<T,V> x){
  174.         INode<T,V> y=x.getLeftChild();
  175.         if(x==root)
  176.             root=y;
  177.         MoveDown(y,x);
  178.         x.setLeftChild(y.getRightChild());
  179.         if(!y.getRightChild().isNull())
  180.             y.getRightChild().setParent(x);
  181.         y.setRightChild(x);
  182.     }
  183.  
  184.     private void MoveDown(INode<T,V>y,INode<T,V>x)
  185.     {
  186.         if(x.getParent()!=null)
  187.         {
  188.             if (!RightChild(x))
  189.                 x.getParent().setLeftChild(y);
  190.             else
  191.                 x.getParent().setRightChild(y);
  192.         }
  193.         y.setParent(x.getParent());
  194.         x.setParent(y);
  195.     }
  196.  
  197.     @Override
  198.     public boolean delete(T key) {
  199.         if(key==null)
  200.             throw new RuntimeErrorException(new Error("null key inserted"));
  201.         if(root.isNull()||!contains(key))
  202.         {
  203.             System.out.println(key+" & "+current.getKey());
  204.             return false;
  205.         }
  206.         DeleteNode(BSTSearch(key));
  207.         return true;
  208.     }
  209.     private void DeleteNode(INode<T,V>v)
  210.     {
  211.         INode<T,V>u=BSTReplace(v);
  212.         boolean BothBlack=((u.isNull()||!u.getColor())&&!v.getColor());
  213.         INode<T,V>parent=v.getParent();
  214.         if(u.isNull())
  215.         {
  216.             if(v==root)
  217.                 root=new Node<>();
  218.             else
  219.             {
  220.                 if(BothBlack)
  221.                     FixDoubleBlack(v);
  222.                 else
  223.                 {
  224.                     if(RightChild(v))
  225.                     {
  226.                         if(!v.getParent().getLeftChild().isNull())
  227.                             v.getParent().getLeftChild().setColor(true);
  228.                     }
  229.                     else
  230.                     {
  231.                         if(!v.getParent().getRightChild().isNull())
  232.                             v.getParent().getRightChild().setColor(true);
  233.                     }
  234.                 }
  235.                 if(!RightChild(v))
  236.                     parent.setLeftChild(new Node<>());
  237.                 else
  238.                     parent.setRightChild(new Node<>());
  239.             }
  240.             v.setColor(false);
  241.             v.setKey(null);
  242.             v.setValue(null);
  243.             v.setRightChild(new Node<>());
  244.             v.setLeftChild(new Node<>());
  245.             v.setParent(null);//TODO check
  246.             current=root;
  247.             return;
  248.         }
  249.         if(v.getRightChild().isNull()||v.getLeftChild().isNull())
  250.         {
  251.             if(v==root)
  252.             {
  253.                 v.setValue(u.getValue());
  254.                 v.setKey(u.getKey());
  255.                 v.setLeftChild(new Node<>());
  256.                 v.setRightChild(new Node<>());
  257.                 u.setColor(false);
  258.                 u.setKey(null);
  259.                 u.setValue(null);
  260.                 u.setRightChild(new Node<>());
  261.                 u.setLeftChild(new Node<>());
  262.                 u.setParent(null);//TODO check
  263.             }
  264.             else
  265.             {
  266.                 if(!RightChild(v))
  267.                     parent.setLeftChild(u);
  268.                 else
  269.                     parent.setRightChild(u);
  270.                 v.setColor(false);
  271.                 v.setKey(null);
  272.                 v.setValue(null);
  273.                 v.setRightChild(new Node<>());
  274.                 v.setLeftChild(new Node<>());
  275.                 v.setParent(null);//TODO check
  276.                 u.setParent(parent);
  277.                 if(BothBlack)
  278.                     FixDoubleBlack(u);
  279.                 else
  280.                     u.setColor(false);
  281.             }
  282.             current=root;
  283.             return;
  284.         }
  285.         T TempKey=u.getKey();V TempValue=u.getValue();
  286.         u.setValue(v.getValue());u.setKey(v.getKey());
  287.         v.setValue(TempValue);v.setKey(TempKey);
  288.         current=root;
  289.         DeleteNode(u);
  290.     }
  291.     private void FixDoubleBlack(INode<T,V>node)
  292.     {
  293.         if(node==root)
  294.             return;
  295.         INode<T,V>sibling,parent=node.getParent();
  296.         if(RightChild(node))
  297.             sibling=node.getParent().getLeftChild();
  298.         else
  299.             sibling=node.getParent().getRightChild();
  300.         if(sibling.isNull())
  301.             FixDoubleBlack(parent);
  302.         else
  303.         {
  304.             if (sibling.getColor()) {
  305.                 parent.setColor(true);
  306.                 sibling.setColor(false);
  307.                 if (!RightChild(sibling))
  308.                     RightRotate(parent);
  309.                 else
  310.                     LeftRotate(parent);
  311.                 FixDoubleBlack(node);
  312.             }
  313.             else
  314.             {
  315.                 if(!sibling.getLeftChild().isNull()&&sibling.getLeftChild().getColor()||!sibling.getRightChild().isNull()&&sibling.getRightChild().getColor())
  316.                 {
  317.                     if(!sibling.getLeftChild().isNull()&&sibling.getLeftChild().getColor())
  318.                     {
  319.                         if(!RightChild(sibling))
  320.                         {
  321.                             sibling.getLeftChild().setColor(sibling.getColor());
  322.                             sibling.setColor(parent.getColor());
  323.                             RightRotate(parent);
  324.                         }
  325.                         else
  326.                         {
  327.                             sibling.getLeftChild().setColor(parent.getColor());
  328.                             RightRotate(sibling);
  329.                             LeftRotate(parent);
  330.                         }
  331.                     }
  332.                     else
  333.                     {
  334.                         if(!RightChild(sibling))
  335.                         {
  336.                             sibling.getRightChild().setColor(parent.getColor());
  337.                             LeftRotate(sibling);
  338.                             RightRotate(parent);
  339.                         }
  340.                         else
  341.                         {
  342.                             sibling.getRightChild().setColor(sibling.getColor());
  343.                             sibling.setColor(parent.getColor());
  344.                             LeftRotate(parent);
  345.                         }
  346.                     }
  347.                     parent.setColor(false);
  348.                 }
  349.                 else
  350.                 {
  351.                     sibling.setColor(true);
  352.                     if(!parent.getColor())
  353.                         FixDoubleBlack(parent);
  354.                     else
  355.                         parent.setColor(false);
  356.                 }
  357.             }
  358.         }
  359.     }
  360.     private INode<T,V> BSTSearch(T key)
  361.     {
  362.         INode<T,V>temp=root;
  363.         while (!temp.isNull())
  364.         {
  365.             if(key.compareTo(temp.getKey())<0)
  366.             {
  367.                 if(temp.getLeftChild().isNull())
  368.                     break;
  369.                 else
  370.                     temp=temp.getLeftChild();
  371.             }else if (key.compareTo(temp.getKey())==0)
  372.                 break;
  373.             else
  374.             {
  375.                 if(temp.getRightChild().isNull())
  376.                     break;
  377.                 else
  378.                     temp=temp.getRightChild();
  379.             }
  380.         }
  381.         return temp;
  382.     }
  383.     private INode<T,V>BSTReplace(INode<T,V> node)
  384.     {
  385.         if(!node.getLeftChild().isNull()&&!node.getRightChild().isNull())
  386.         {
  387.             INode<T,V>temp=node.getRightChild();
  388.             while (!temp.getLeftChild().isNull())
  389.                 temp=temp.getLeftChild();
  390.             return temp;
  391.         }
  392.         if(node.getLeftChild().isNull()&&node.getRightChild().isNull())
  393.             return node.getRightChild();
  394.         if(!node.getLeftChild().isNull())
  395.             return node.getLeftChild();
  396.         return node.getRightChild();
  397.     }
  398.     private boolean RightChild(INode<T,V>node)
  399.     {
  400.         return node.getParent().getRightChild() == node;
  401.     }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement