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

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

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