示例#1
0
        private void RotateRight(RbNode <T> x)
        {
            var y = x.Left;

            x.Left = y.Right;
            if (y.Right != TNull)
            {
                y.Right.Parent = x;
            }

            y.Parent = x.Parent;

            if (x.Parent != null)
            {
                if (x == x.Parent.Right)
                {
                    x.Parent.Right = y;
                }
                else
                {
                    x.Parent.Left = y;
                }
            }
            else
            {
                Root = y;
            }

            y.Right  = x;
            x.Parent = y;
        }
示例#2
0
        private void InsertFix(RbNode <T> x)
        {
            while (x != Root && x.Parent.Red)
            {
                RbNode <T> y;
                if (x.Parent == x.Parent.Parent.Left)
                {
                    y = x.Parent.Parent.Right;
                    if (y.Red)
                    {
                        x.Parent.Red        = false;
                        y.Red               = false;
                        x.Parent.Parent.Red = true;
                        x = x.Parent.Parent;
                    }
                    else
                    {
                        if (x == x.Parent.Right)
                        {
                            x = x.Parent;
                            RotateLeft(x);
                        }

                        x.Parent.Red        = false;
                        x.Parent.Parent.Red = true;
                        RotateRight(x.Parent.Parent);
                    }
                }
                else
                {
                    y = x.Parent.Parent.Left;
                    if (y.Red)
                    {
                        x.Parent.Red        = false;
                        y.Red               = false;
                        x.Parent.Parent.Red = true;
                        x = x.Parent.Parent;
                    }
                    else
                    {
                        if (x == x.Parent.Left)
                        {
                            x = x.Parent;
                            RotateRight(x);
                        }

                        x.Parent.Red        = false;
                        x.Parent.Parent.Red = true;
                        RotateLeft(x.Parent.Parent);
                    }
                }
            }

            Root.Red = false;
        }
示例#3
0
        private int GetHeight(RbNode <T> x)
        {
            if (x == null || x == TNull)
            {
                return(-1);
            }
            var a = GetHeight(x.Left);
            var b = GetHeight(x.Right);

            return(a > b ? a + 1 : b + 1);
        }
示例#4
0
        public RbNode <T> Find(T key)
        {
            var current = Root;

            while (current != TNull)
            {
                if (RbNode <T> .IsEqualTo(key, current.Key))
                {
                    return(current);
                }

                current = RbNode <T> .IsGreaterThan(key, current.Key) ? current.Right : current.Left;
            }

            return(null);
        }
示例#5
0
        private void RemoveFix(RbNode <T> x)
        {
            while (x != Root && !x.Red)
            {
                if (x == x.Parent.Left)
                {
                    var w = x.Parent.Right;
                    if (w.Red)
                    {
                        w.Red        = false;
                        x.Parent.Red = true;
                        RotateLeft(x.Parent);
                        w = x.Parent.Right;
                    }

                    if (!w.Left.Red && !w.Right.Red)
                    {
                        w.Red = true;
                        x     = x.Parent;
                    }
                    else
                    {
                        if (!w.Right.Red)
                        {
                            w.Left.Red = false;
                            w.Red      = true;
                            RotateRight(w);
                            w = x.Parent.Right;
                        }

                        w.Red        = x.Parent.Red;
                        x.Parent.Red = false;
                        w.Right.Red  = false;
                        RotateLeft(x.Parent);
                        x = Root;
                    }
                }
                else
                {
                    var w = x.Parent.Left;
                    if (w.Red)
                    {
                        w.Red        = false;
                        x.Parent.Red = true;
                        RotateRight(x.Parent);
                        w = x.Parent.Left;
                    }

                    if (!w.Right.Red && !w.Left.Red)
                    {
                        w.Red = true;
                        x     = x.Parent;
                    }
                    else
                    {
                        if (!w.Left.Red)
                        {
                            w.Right.Red = false;
                            w.Red       = true;
                            RotateLeft(w);
                            w = x.Parent.Left;
                        }

                        w.Red        = x.Parent.Red;
                        x.Parent.Red = false;
                        w.Left.Red   = false;
                        RotateRight(x.Parent);
                        x = Root;
                    }
                }
            }

            x.Red = false;
        }