Esempio 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;
        }
Esempio 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);
 }
Esempio n. 3
0
 public T Successor()
 {
     if (right is RBBranch <T> )
     {
         RBBranch <T> branch = (RBBranch <T>)right;
         return(branch.MinBranch().value);
     }
     return(NextRightParent().value);
 }
Esempio n. 4
0
 public T Predecessor()
 {
     if (left is RBBranch <T> )
     {
         RBBranch <T> branch = (RBBranch <T>)left;
         return(branch.MaxBranch().value);
     }
     return(NextLeftParent().value);
 }
Esempio n. 5
0
 void Insert(RBBranch <T> node)
 {
     root.Insert(node);
     node.InsertRepair();
     root = node;
     while (root.parent != null)
     {
         root = root.parent;
     }
 }
Esempio n. 6
0
        RBBranch <T> MinBranch()
        {
            if (!(left is RBBranch <T>))
            {
                return(this);
            }
            RBBranch <T> branch = (RBBranch <T>)left;

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

            return(branch.MaxBranch());
        }
Esempio 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();
        }
Esempio 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;
 }
Esempio 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);
        }
Esempio 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);
            }
        }
Esempio 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;
            }
        }
Esempio n. 13
0
 public RBLeaf(RBBranch <T> parent)
 {
     this.parent = parent;
     red         = false;
 }
Esempio n. 14
0
 public abstract void Insert(RBBranch <T> newNode);
Esempio 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();
            }
        }