Exemplo n.º 1
0
        public void RotateRight()
        {
            if (!(left is RBBranch <T>))
            {
                throw new InvalidOperationException("Can't rotate right a branch with a left leaf");
            }
            RBBranch <T> newLocalRoot = (RBBranch <T>)left;
            RBNode <T>   middle       = newLocalRoot.right;

            if (parent != null)
            {
                if (this == parent.left)
                {
                    parent.left = newLocalRoot;
                }
                else
                {
                    parent.right = newLocalRoot;
                }
            }
            newLocalRoot.parent = this.parent;
            this.parent         = newLocalRoot;
            newLocalRoot.right  = this;
            this.left           = middle;
            middle.parent       = this;
        }
Exemplo n.º 2
0
 public RBBranch(T value, RBBranch <T> parent)
 {
     this.parent = parent;
     this.value  = value;
     red         = true;
     left        = new RBLeaf <T> (this);
     right       = new RBLeaf <T> (this);
 }
Exemplo n.º 3
0
 public T Successor()
 {
     if (right is RBBranch <T> )
     {
         RBBranch <T> branch = (RBBranch <T>)right;
         return(branch.MinBranch().value);
     }
     return(NextRightParent().value);
 }
Exemplo n.º 4
0
 public T Predecessor()
 {
     if (left is RBBranch <T> )
     {
         RBBranch <T> branch = (RBBranch <T>)left;
         return(branch.MaxBranch().value);
     }
     return(NextLeftParent().value);
 }
Exemplo n.º 5
0
 void Insert(RBBranch <T> node)
 {
     root.Insert(node);
     node.InsertRepair();
     root = node;
     while (root.parent != null)
     {
         root = root.parent;
     }
 }
Exemplo n.º 6
0
        RBBranch <T> MinBranch()
        {
            if (!(left is RBBranch <T>))
            {
                return(this);
            }
            RBBranch <T> branch = (RBBranch <T>)left;

            return(branch.MinBranch());
        }
Exemplo n.º 7
0
        RBBranch <T> MaxBranch()
        {
            if (!(right is RBBranch <T>))
            {
                return(this);
            }
            RBBranch <T> branch = (RBBranch <T>)right;

            return(branch.MaxBranch());
        }
Exemplo n.º 8
0
        public void Delete()
        {
            RBBranch <T> minBranch = this;

            if (right is RBBranch <T> )
            {
                RBBranch <T> rightBranch = (RBBranch <T>)right;
                minBranch = rightBranch.MinBranch();
                value     = minBranch.value;
            }
            minBranch.DeleteBranchWithNoMoreThanOneChildBranch();
        }
Exemplo n.º 9
0
 public override void Insert(RBBranch <T> newNode)
 {
     if (parent != null)
     {
         if (parent.left == this)
         {
             parent.left = newNode;
         }
         else
         {
             parent.right = newNode;
         }
     }
     newNode.parent = parent;
 }
Exemplo n.º 10
0
        RBBranch <T> RepairRotateToOutside()
        {
            RBBranch <T> originalParent = parent;

            if (parent == Grandparent().left&& this == parent.right)
            {
                parent.RotateLeft();
                return(originalParent);
            }
            else if (parent == Grandparent().right&& this == parent.left)
            {
                parent.RotateRight();
                return(originalParent);
            }
            return(this);
        }
Exemplo n.º 11
0
        public override void Insert(RBBranch <T> newNode)
        {
            T   t             = newNode.value;
            int compareResult = t.CompareTo(value);

            if (compareResult == 0)
            {
                throw new ArgumentException(string.Format("Duplicate value {0} specified", t));
            }
            else if (compareResult > 0)
            {
                right.Insert(newNode);
            }
            else
            {
                left.Insert(newNode);
            }
        }
Exemplo n.º 12
0
        public void Remove(T value)
        {
            root.Remove(value);

            RBBranch <T> rootBranch = (RBBranch <T>)root;

            if (rootBranch.left.parent == null)
            {
                root = rootBranch.left;
            }
            else if (rootBranch.right.parent == null)
            {
                root = rootBranch.right;
            }
            while (root.parent != null)
            {
                root = root.parent;
            }
        }
Exemplo n.º 13
0
 public RBLeaf(RBBranch <T> parent)
 {
     this.parent = parent;
     red         = false;
 }
Exemplo n.º 14
0
 public abstract void Insert(RBBranch <T> newNode);
Exemplo n.º 15
0
        public void DeleteRepair()
        {
            if (parent == null)
            {
                return;
            }

            if (Sibling().red)
            {
                parent.red    = true;
                Sibling().red = false;
                if (this == parent.left)
                {
                    parent.RotateLeft();
                }
                else
                {
                    parent.RotateRight();
                }
            }

            // Sibling is a branch (not a leaf) because it has more black children than we do.
            RBBranch <T> sibling = (RBBranch <T>)Sibling();

            if ((!parent.red) &&
                (!sibling.red) &&
                (!sibling.left.red) &&
                (!sibling.right.red))
            {
                sibling.red = true;
                parent.DeleteRepair();
                return;
            }

            if ((parent.red) &&
                (!sibling.red) &&
                (!sibling.left.red) &&
                (!sibling.right.red))
            {
                sibling.red = true;
                parent.red  = false;
                return;
            }

            if ((this == parent.left) &&
                (!sibling.right.red))
            {
                sibling.left.red = false;
                sibling.red      = true;
                sibling.RotateRight();
            }
            else if ((this == parent.right) &&
                     (!sibling.left.red))
            {
                sibling.left.red = false;
                sibling.red      = true;
                sibling.RotateLeft();
            }

            // Sibling is still a branch either from the earlier reasoning, or because our
            // earlier sibling was rotated and a rotation must put a branch at the root.
            sibling = (RBBranch <T>)Sibling();

            sibling.red = parent.red;
            parent.red  = false;

            if (this == parent.left)
            {
                sibling.right.red = false;
                parent.RotateLeft();
            }
            else
            {
                sibling.left.red = false;
                parent.RotateRight();
            }
        }