Example #1
0
        private void DeleteCorrectCase5(RBNode n)
        {
            RBNode s = n.Sibling();

            if (s.Color == RBColor.Black)
            {
                if ((n == n.parent.left) &&
                    (s.right.Color == RBColor.Black) &&
                    (s.left.Color == RBColor.Red))
                {
                    s.Color      = RBColor.Red;
                    s.left.Color = RBColor.Black;
                    RotateRight(s);
                }
                else if ((n == n.parent.right) &&
                         (s.left.Color == RBColor.Black) &&
                         (s.right.Color == RBColor.Red))
                {
                    s.Color       = RBColor.Red;
                    s.right.Color = RBColor.Black;
                    RotateLeft(s);
                }
            }
            DeleteCorrectCase6(n);
        }
Example #2
0
        private void DeleteCorrectCase2(RBNode n)
        {
            // у n черный цвет - 100500 и есть родитель
            // если есть родитель и у n черный цвет, то по св-ву 5 есть и брат

            RBNode s = n.Sibling();

            if (s.Color == RBColor.Red)  // родитель при этом не мб красным! ибо св-во 4
            {
                n.parent.Color = RBColor.Red;
                s.Color        = RBColor.Black;
                if (n == n.parent.left)
                {
                    RotateLeft(n.parent);
                }
                else
                {
                    RotateRight(n.parent);
                }
            }
            else
            {
                DeleteCorrectCase3(n);
            }
        }
Example #3
0
        private void DeleteCorrectCase4(RBNode n)
        {
            RBNode s = n.Sibling();

            if ((n.parent.Color == RBColor.Red) &&
                (s.Color == RBColor.Black) &&
                (s.left.Color == RBColor.Black) &&
                (s.right.Color == RBColor.Black))
            {
                s.Color        = RBColor.Red;
                n.parent.Color = RBColor.Black;
            }
            else
            {
                DeleteCorrectCase5(n);
            }
        }
Example #4
0
        private void DeleteCorrectCase6(RBNode n)
        {
            RBNode s = n.Sibling();

            s.Color        = n.parent.Color;
            n.parent.Color = RBColor.Black;

            if (n == n.parent.left)
            {
                s.right.Color = RBColor.Black;
                RotateLeft(n.parent);
            }
            else
            {
                s.left.Color = RBColor.Black;
                RotateRight(n.parent);
            }
        }
Example #5
0
        private RBNode[] RemoveRBNodeFindReplaced(string data)
        {
            RBNode[] nodes = new RBNode[3];

            RBNode n = SearchRBNode(data);

            if (n == null)
            {
                return(null);
            }

            nodes[2] = n.Sibling();

            RBNode replaced = null;
            RBNode p        = n.parent;

            if (n.left == null || n.right == null)
            {
                if (n.left == null)
                {
                    replaced = n.right;
                }
                else
                {
                    replaced = n.left;
                }
            }
            else
            {
                replaced = FindMax(n.left);

                if (replaced != n.left)
                {
                    replaced.parent.right = null;
                    replaced.left         = n.left;
                    n.left.parent         = replaced;
                }

                replaced.right = n.right;
                n.right.parent = replaced;
            }

            if (replaced != null)
            {
                replaced.parent = p;
            }
            //else
            //{
            //  DeleteCorrectCase1(n);
            //}

            if (p != null)
            {
                if (n == p.left)
                {
                    p.left = replaced;
                }
                else if (n == p.right)
                {
                    p.right = replaced;
                }
            }

            _count--;
            nodes[0] = n;
            nodes[1] = replaced;
            return(nodes);
        }