Ejemplo n.º 1
0
        /*     Parent     Parent
         *     |-         |+
         *     B          A
         *   -/ \        / \+
         *   A   BL  -> AL   B
         *  / \-          +/ \
         * AL   C          C   BR
         *
         *   - marks connections that are dettached
         *   + marks connections that are created
         */

        private AvlTreeNode <T> _rotateRight(AvlTreeNode <T> b)
        {
            AvlTreeNode <T> a      = b.Left;
            AvlTreeNode <T> parent = b.Parent; // may be null if B was root
            AvlTreeNode <T> c      = a.Right;  // may be null

            var direction = b.Direction;

            Debug.Assert(a != null);

            // a.Left remains so no need to change this
            // b.Right remains so no need to change this

            _dettachFromParent(b); // b from parent
            _dettachFromParent(a); // a from b
            _dettachFromParent(c); // c from a

            _attachToParentEdge(c, b.EdgeLeft());
            _attachToParentEdge(b, a.EdgeRight());
            _attachToParentEdge(a, parent.EdgeTo(direction));

            _recalc(b);
            _recalc(a);
            return(a);
        }
Ejemplo n.º 2
0
        private AvlTreeNode <T> rec_createRangeSubtree(T[] items, int start, int end)
        {
            if (start > end)
            {
                return(null);
            }

            var mid  = (start + end) / 2;
            var item = items[mid];

            var node  = new AvlTreeNode <T>(item);
            var left  = rec_createRangeSubtree(items, start, mid - 1);
            var right = rec_createRangeSubtree(items, mid + 1, end);

            if (left != null)
            {
                _attachToParentEdge(left, node.EdgeLeft());
            }
            if (right != null)
            {
                _attachToParentEdge(right, node.EdgeRight());
            }
            _recalc(node);
            return(node);
        }