Beispiel #1
0
        public override AVLTreeNode <TKey, TValue> Delete(AVLTreeNode <TKey, TValue> root, TKey key)
        {
            if (root == null)
            {
                return(root);
            }

            if (root.Key.CompareTo(key) < 0)
            {
                root.RightChild = Delete(root.RightChild, key);
            }
            else if (root.Key.CompareTo(key) > 0)
            {
                root.LeftChild = Delete(root.LeftChild, key);
            }
            else if (root.Key.CompareTo(key) == 0) // The key is found
            {
                if (root.RightChild == null && root.LeftChild == null)
                {
                    root = null;
                }

                else if (root.RightChild == null)
                {
                    root = root.LeftChild;
                }

                else if (root.LeftChild == null)
                {
                    root = root.RightChild;
                }
                else
                {
                    /* Else replacing the node that has 2 non-null children with its in-order successor, or could alternatively replace it with its in-order predecessor. */
                    /* From definition of FindMin() it is obvious that the replacement node [rightChildMin] has less than 2 children. */
                    AVLTreeNode <TKey, TValue> rightChildMin = FindMin_BST(root.RightChild);
                    root.Key        = rightChildMin.Key;
                    root.Value      = rightChildMin.Value;
                    root.RightChild = Delete(root.RightChild, rightChildMin.Key); /* at this point both node, and rightChildMin have the same keys, but calling delete on the same key, will only result in the removal  of rightChildMin, because pf the root that is passed to Delete.*/
                }
            }

            if (root == null)
            {
                return(root);
            }

            var grandChild  = root?.GetGrandChildren()?.FirstOrDefault();
            var grandParent = root;
            var parent      = grandChild?.Parent;

            if (grandParent != null && parent != null)
            {
                int grandParentBalance = ComputeBalanceFactor(grandParent);
                if (grandParentBalance > 1)
                {
                    if (grandChild.FormsTriangle())
                    {
                        Contract.Assert(grandChild.IsLeftChild());
                        RotateRight(parent);
                        return(RotateLeft(grandParent));
                    }
                    else if (grandChild.FormsLine())
                    {
                        Contract.Assert(grandChild.IsRightChild());
                        return(RotateLeft(grandParent));
                    }
                }
                else if (grandParentBalance < -1)
                {
                    if (grandChild.FormsTriangle())
                    {
                        Contract.Assert(grandChild.IsRightChild());
                        RotateLeft(parent);
                        return(RotateRight(grandParent));
                    }
                    else if (grandChild.FormsLine())
                    {
                        Contract.Assert(grandChild.IsLeftChild());
                        return(RotateRight(grandParent));
                    }
                }
            }
            return(root);
        }
Beispiel #2
0
 /// <summary>
 /// Computes balance factor of a node. Which is the difference between the height of the left and right sub trees of the node.
 /// </summary>
 /// <param name="node">The node for which balance is computed.</param>
 /// <returns>the balance factor of the node. </returns>
 internal int ComputeBalanceFactor(AVLTreeNode <TKey, TValue> node)
 {
     return((node.RightChild == null ? 0 : GetHeight(node.RightChild)) - (node.LeftChild == null ? 0 : GetHeight(node.LeftChild)));
 }
Beispiel #3
0
 public override AVLTreeNode <TKey, TValue> FindMax(AVLTreeNode <TKey, TValue> root)
 {
     return(FindMax_BST(root));
 }
Beispiel #4
0
 public override bool Update(AVLTreeNode <TKey, TValue> root, TKey key, TValue value)
 {
     return(Update_BST(root, key, value));
 }
Beispiel #5
0
 public override AVLTreeNode <TKey, TValue> Search(AVLTreeNode <TKey, TValue> root, TKey key)
 {
     return(Search_BST(root, key));
 }
Beispiel #6
0
        public override AVLTreeNode <TKey, TValue> Insert(AVLTreeNode <TKey, TValue> root, AVLTreeNode <TKey, TValue> newNode)
        {
            root = Insert_BST(root, newNode); /* First insert the node using normal BinarySearchInsert to preserve the ordering property. */

            Balance(newNode);

            // Find the new root of the tree, as it might have changed during the operations.
            root = newNode;
            while (root.Parent != null)
            {
                root = root.Parent;
            }
            return(root);
        }