Пример #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);
        }
Пример #3
0
        private AVL_Node <T> Insert(AVL_Node <T> x, T item)
        {
            if (x == null)
            {
                x        = new AVL_Node <T> ();
                x.Key    = item;
                x.Height = 1;
                x.Size   = 1;
                x.Left   = null;
                x.Rigth  = null;
                Count++;
            }
            else
            {
                if (item.CompareTo(x.Key) == 0)
                {
                    return(x);
                }

                if (item.CompareTo(x.Key) < 0)
                {
                    x.Left = Insert(x.Left, item);
                }
                else
                {
                    x.Rigth = Insert(x.Rigth, item);
                }

                x = Balance(x);
            }

            return(Balance(x));
        }
Пример #4
0
        public AVL_Node <T> Contains(T item)
        {
            AVL_Node <T> x = C_Root;

            for (; ;)
            {
                if (x == null)
                {
                    return(null);
                }
                if (item.CompareTo(x.Key) == 0)
                {
                    return(x);
                }

                x = item.CompareTo(x.Key) < 0 ? x.Left : x.Rigth;
            }
        }