コード例 #1
0
        private INode <T> Remove(INode <T> node, T item, bool[] result)
        {
            if (node == null)
            {
                return(node);
            }
            else if (item.CompareTo(node.Item) < 0)
            {
                node.Left = Remove(node.Left, item, result);
            }
            else if (item.CompareTo(node.Item) > 0)
            {
                node.Right = Remove(node.Right, item, result);
            }
            else
            {
                result[0] = true;

                if (node.Left == null && node.Right == null)
                {
                    return(null);
                }
                else if (node.Left == null)
                {
                    node = node.Right;
                }
                else if (node.Right == null)
                {
                    node = node.Left;
                }
                else
                {
                    INode <T> succ = FindSuccessor(node);
                    node.Item  = succ.Item;
                    node.Right = Remove(node.Right, succ.Item, result);
                }
            }

            AvlNode <T> avlNode = (AvlNode <T>)node;

            AdjustHeight(avlNode);
            int balance = GetBalance(avlNode);

            return(CheckCasesRemove(balance, avlNode));
        }
コード例 #2
0
        /// <summary>
        /// Internal method to insert into a subtree.
        /// </summary>
        /// <param name="x">The item to insert.</param>
        /// <param name="subTree">The node that roots the subtree. </param>
        /// <returns></returns>
        private AvlNode Insert(T x, AvlNode subTree)
        {
            if (subTree == null)
            {
                return(new AvlNode(x, null, null));
            }

            int compareResult = Compare(x, subTree.Element);

            if (compareResult < 0)
            {
                subTree.Left = Insert(x, subTree.Left);
            }
            else if (compareResult > 0)
            {
                subTree.Right = Insert(x, subTree.Right);
            }
            else
            {
                /* Duplicate; Do nothing. */
            }
            return(Balance(subTree));
        }
コード例 #3
0
 private int GetBalance(AvlNode <T> node)
 {
     return(GetHeight(node.AvlLeft) - GetHeight(node.AvlRight));
 }
コード例 #4
0
 private void AdjustHeight(AvlNode <T> node)
 {
     node.Height = System.Math.Max(GetHeight(node.AvlLeft), GetHeight(node.AvlRight)) + 1;
 }
コード例 #5
0
 /// <summary>
 /// Return the height of the node, or -1 if null.
 /// </summary>
 /// <param name="node">The node that we are checking. </param>
 /// <returns>The height of the node.</returns>
 private int Height(AvlNode node)
 {
     return((node == null) ? -1 : node.Height);
 }
コード例 #6
0
 public AvlNode(T element, AvlNode left, AvlNode right)
 {
     _element = element;
     _left    = left;
     _right   = right;
 }
コード例 #7
0
 /// <summary>
 /// Double rotate tree node. First right child with its left child; then node k3 with new right child.
 /// For AVL trees, this is a double rotation for case 4. Update heights, the return new root.
 /// </summary>
 /// <param name="k2">The node that roots subtree that will be rotated.</param>
 /// <returns>The node that roots the new subtree.</returns>
 private AvlNode DoubleWithRightChild(AvlNode k3)
 {
     k3.Right = RotateWithLeftChild(k3.Right);
     return(RotateWithRightChild(k3));
 }
コード例 #8
0
 public AvlTree()
 {
     _root = null;
 }