Exemple #1
0
        private AvlNode <TK, TV> LeftRotate(AvlNode <TK, TV> x)
        {
            var y  = x.Right;
            var t2 = y.Left;

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

            // Perform rotation
            y.Left   = x;
            y.Parent = x.Parent;

            x.Parent = y;
            x.Right  = t2;
            if (t2 != null)
            {
                t2.Parent = x;
            }

            // Update heights
            x.RefreshHeight();
            y.RefreshHeight();

            // Return new root
            return(y);
        }
Exemple #2
0
        private AvlNode <TK, TV> RightRotate(AvlNode <TK, TV> y)
        {
            var x  = y.Left;
            var t2 = x.Right;

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

            // Perform rotation
            x.Right  = y;
            x.Parent = y.Parent;

            y.Parent = x;
            y.Left   = t2;
            if (t2 != null)
            {
                t2.Parent = y;
            }

            // Update heights
            y.RefreshHeight();
            x.RefreshHeight();

            // Return new root
            return(x);
        }