Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.filestructure.redblacktree;
- import javax.management.RuntimeErrorException;
- import java.util.ArrayList;
- import java.util.List;
- public class RedBlackTree<T extends Comparable<T>, V> implements IRedBlackTree<T, V> {
- private INode<T,V>root=new Node<>();
- private INode<T,V> current=root;
- @Override
- public INode<T,V> getRoot() {
- if(root.isNull())
- return null;
- return root;
- }
- @Override
- public boolean isEmpty() {
- return root.isNull();
- }
- @Override
- public void clear() {
- root=new Node<>();
- current =root;
- }
- @Override
- public V search(T key) {
- if(key==null)
- throw new RuntimeErrorException(new Error("null key inserted"));
- if(root.isNull()|| current.isNull())
- {
- current=root;
- return null;
- }
- if(current.getKey().compareTo(key)==0)
- {
- V answer= current.getValue();
- current =root;
- return answer;
- }
- if(current.getKey().compareTo(key)>0)
- current = current.getLeftChild();
- else
- current = current.getRightChild();
- return search(key);
- }
- @Override
- public boolean contains(T key) {
- V value= search(key);
- return value!=null;
- }
- @Override
- public void insert(T key, V value) {
- if(key==null)
- throw new RuntimeErrorException(new Error("null key inserted"));
- if(value==null)
- throw new RuntimeErrorException(new Error("null value inserted"));
- if(root.isNull())
- {
- root.setColor(false);
- root.setKey(key);
- root.setValue(value);
- root.setRightChild(new Node<>());
- root.setLeftChild(new Node<>());
- return;
- }
- if(contains(key))
- {
- Replace(key,value);
- return;
- }
- INode<T,V>parent=root;
- while(true)
- {
- if(current.isNull())
- {
- current =new Node<>();
- current.setColor(true);
- current.setKey(key);
- current.setValue(value);
- current.setParent(parent);
- current.setLeftChild(new Node<>());
- current.setRightChild(new Node<>());
- if(key.compareTo(parent.getKey())>0)
- parent.setRightChild(current);
- else
- parent.setLeftChild(current);
- //Balance(current);
- current=root;
- break;
- }
- if(current.getKey().compareTo(key)<0)
- {
- parent=current;
- current= current.getRightChild();
- }
- else
- {
- parent=current;
- current=current.getLeftChild();
- }
- }
- }
- private void Replace(T key,V value)
- {
- if(current.getKey().compareTo(key)==0)
- {
- current.setValue(value);
- current =root;
- return;
- }
- if(current.getKey().compareTo(key)>0)
- current = current.getLeftChild();
- else
- current = current.getRightChild();
- Replace(key,value);
- }
- private void LeftRotate(INode<T,V> x){
- INode<T,V> y=x.getRightChild();
- if(x==root)
- root=y;
- MoveDown(y,x);
- x.setRightChild(y.getLeftChild());
- if(!y.getLeftChild().isNull())
- y.getLeftChild().setParent(x);
- y.setLeftChild(x);
- }
- private void RightRotate(INode<T,V> x){
- INode<T,V> y=x.getLeftChild();
- if(x==root)
- root=y;
- MoveDown(y,x);
- x.setLeftChild(y.getRightChild());
- if(!y.getRightChild().isNull())
- y.getRightChild().setParent(x);
- y.setRightChild(x);
- }
- private void MoveDown(INode<T,V>y,INode<T,V>x)
- {
- if(x.getParent()!=null)
- {
- if (!RightChild(x))
- x.getParent().setLeftChild(y);
- else
- x.getParent().setRightChild(y);
- }
- y.setParent(x.getParent());
- x.setParent(y);
- }
- private void Balance(INode<T,V> node) {
- while(node.getParent().getColor()){
- if(node.getParent()==node.getParent().getParent().getLeftChild()){
- INode<T,V> y=node.getParent().getParent().getRightChild();
- if(y.getColor()){
- node.getParent().setColor(false);
- y.setColor(false);
- node.getParent().getParent().setColor(true);
- node=node.getParent().getParent();
- }else if(node==node.getParent().getRightChild()){
- node=node.getParent();
- LeftRotate(node);
- node.getParent().setColor(false);
- node.getParent().getParent().setColor(true);
- RightRotate(node.getParent().getParent());
- }
- }else if(node.getParent()==node.getParent().getParent().getRightChild()){
- node=node.getParent();
- RightRotate(node);
- node.getParent().setColor(false);
- node.getParent().getParent().setColor(true);
- LeftRotate(node);
- }
- }
- this.root.setColor(false);
- }
- @Override
- public boolean delete(T key) {
- if(key==null)
- throw new RuntimeErrorException(new Error("null key inserted"));
- if(root.isNull()||!contains(key))
- return false;
- List<INode<T,V>> nodes=BSTDeletion(key);
- INode<T,V>u=nodes.get(0),v=nodes.get(1);
- boolean BothBlack=((u.isNull()||!u.getColor())&&!v.getColor());
- INode<T,V>parent=v.getParent();
- if(u.isNull())
- {
- if(v==root)
- root=new Node<>();
- else
- {
- if(BothBlack)
- FixDoubleBlack(v);
- else
- {
- if(RightChild(v))
- {
- if(!v.getParent().getLeftChild().isNull())
- v.getParent().getLeftChild().setColor(true);
- }
- else
- {
- if(!v.getParent().getRightChild().isNull())
- v.getParent().getRightChild().setColor(true);
- }
- }
- if(!RightChild(v))
- parent.setLeftChild(new Node<>());
- else
- parent.setRightChild(new Node<>());
- }
- v=new Node<>();
- return true;
- }
- if(v.getRightChild().isNull()||v.getLeftChild().isNull())
- {
- if(v==root)
- {
- v.setValue(u.getValue());
- v.setLeftChild(new Node<>());
- v.setRightChild(new Node<>());
- u=new Node<>();
- }
- else
- {
- if(!RightChild(v))
- parent.setLeftChild(u);
- else
- parent.setRightChild(u);
- v=new Node<>();
- u.setParent(parent);
- if(BothBlack)
- FixDoubleBlack(u);
- else
- u.setColor(false);
- }
- return true;
- }
- T Key=u.getKey();V Value=u.getValue();
- u.setValue(v.getValue());u.setKey(v.getKey());
- v.setKey(Key);v.setValue(Value);
- return delete(u.getKey());
- }
- private void FixDoubleBlack(INode<T,V>node)
- {
- if(node==root)
- return;
- INode<T,V>sibling,parent=node.getParent();
- if(RightChild(node))
- sibling=node.getParent().getLeftChild();
- else
- sibling=node.getParent().getRightChild();
- if(sibling.isNull())
- FixDoubleBlack(parent);
- else
- {
- if (sibling.getColor()) {
- parent.setColor(true);
- sibling.setColor(false);
- if (!RightChild(sibling))
- RightRotate(parent);
- else
- LeftRotate(parent);
- FixDoubleBlack(node);
- }
- else
- {
- if(sibling.getLeftChild().getColor()||sibling.getRightChild().getColor())
- {
- if(!sibling.getLeftChild().isNull()&&sibling.getLeftChild().getColor())
- {
- if(!RightChild(sibling))
- {
- sibling.getLeftChild().setColor(sibling.getColor());
- sibling.setColor(parent.getColor());
- RightRotate(parent);
- }
- else
- {
- sibling.getLeftChild().setColor(parent.getColor());
- RightRotate(sibling);
- LeftRotate(parent);
- }
- }
- else
- {
- if(!RightChild(sibling))
- {
- sibling.getRightChild().setColor(parent.getColor());
- LeftRotate(sibling);
- RightRotate(parent);
- }
- else
- {
- sibling.getRightChild().setColor(sibling.getColor());
- sibling.setColor(parent.getColor());
- LeftRotate(parent);
- }
- }
- parent.setColor(false);
- }
- else
- {
- sibling.setColor(true);
- if(!parent.getColor())
- FixDoubleBlack(parent);
- else
- parent.setColor(false);
- }
- }
- }
- }
- //first element is successor,second element is to be deleted
- private List<INode<T,V>> BSTDeletion(T key)
- {
- List<INode<T,V>> container=new ArrayList<>();
- while(current.getKey().compareTo(key)!=0)
- {
- if(current.getKey().compareTo(key)>0)
- current=current.getLeftChild();
- else
- current=current.getRightChild();
- }//No children
- if(current.getLeftChild().isNull()&¤t.getRightChild().isNull())
- {
- container.add(current.getRightChild());
- container.add(current);
- }//One child Only
- else if(current.getLeftChild().isNull())
- {
- INode<T,V> tempNode=current.getRightChild();
- container.add(tempNode);
- container.add(current);
- }
- else if(current.getRightChild().isNull())
- {
- INode<T,V>tempNode=current.getLeftChild();
- container.add(tempNode);
- container.add(current);
- }//Both children
- else
- {
- INode<T,V>tempNode=current.getRightChild();
- while(!tempNode.getLeftChild().isNull())
- tempNode=tempNode.getLeftChild();
- container.add(tempNode);
- container.add(current);
- }
- return container;
- }
- private boolean RightChild(INode<T,V>node)
- {
- //right = true ...... left = false
- return node.getParent().getRightChild() == node;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement