Пример #1
0
        private AVL_Node <T> Balance(AVL_Node <T> x)
        {
            x.Update();

            if (x.Balance_Factor > 1)
            {
                if (x.Rigth.Balance_Factor <= 0)
                {
                    x.Rigth = Rotate(x.Rigth, Direction.Rigth);
                }
                x = Rotate(x, Direction.Left);
            }
            else
            if (x.Balance_Factor < -1)
            {
                if (x.Left.Balance_Factor >= 0)
                {
                    x.Left = Rotate(x.Left, Direction.Left);
                }
                x = Rotate(x, Direction.Rigth);
            }

            x.Update();
            return(x);
        }
Пример #2
0
        private AVL_Node <T> Rotate(AVL_Node <T> x, Direction direction)
        {
            AVL_Node <T> y;

            if (direction == Direction.Left) // left
            {
                if (x == null || x.Rigth == null)
                {
                    return(x);
                }

                y       = x.Rigth;
                x.Rigth = y.Left;
                y.Left  = x;
            }
            else // right
            {
                if (x == null || x.Left == null)
                {
                    return(x);
                }

                y       = x.Left;
                x.Left  = y.Rigth;
                y.Rigth = x;
            }

            x.Update();
            y.Update();

            return(y);
        }