/// <summary> /// This Function is for Recursive Deletion. /// </summary> /// <param name="current">Current node</param> /// <param name="key">Key</param> /// <returns>Avl Node</returns> private AVLNode <TKey, TValue> RecursiveRemove(AVLNode <TKey, TValue> current, TKey key) { AVLNode <TKey, TValue> parent; if (current == null) { return(null); } else { // Left subtree. if (key.CompareTo(current.Key) < 0) { current.LeftNode = RecursiveRemove(current.LeftNode, key); if (BalanceFactor(current) == -2)//here { if (BalanceFactor(current.RightNode) <= 0) { current = RotateRight(current); } else { current = DoubleRight(current); } } } // Right subtree else if (key.CompareTo(current.Key) > 0) { current.RightNode = RecursiveRemove(current.RightNode, key); if (BalanceFactor(current) == 2) { if (BalanceFactor(current.LeftNode) >= 0) { current = RotateLeft(current); } else { current = DoubleLeft(current); } } } // When target is found. else { if (current.RightNode != null) { // Deleting its inorder successor. parent = current.RightNode; while (parent.LeftNode != null) { parent = parent.LeftNode; } current.Key = parent.Key; current.RightNode = RecursiveRemove(current.RightNode, parent.Key); // Rebalancing. if (BalanceFactor(current) == 2) { if (BalanceFactor(current.LeftNode) >= 0) { current = RotateLeft(current); } else { current = DoubleLeft(current); } } } // When current.left != null. else { return(current.LeftNode); } } } return(current); }