// Recursive add algorithm private void AddTo(AVLTreeNode <T> node, T value) { // Case 1: Value is less than the current node value if (value.CompareTo(node.Value) < 0) { // if there is no left child make this the new left if (node.Left == null) { node.Left = new AVLTreeNode <T>(value, node, this); } else { // else add it to the left node AddTo(node.Left, value); } } // Case 2: Value is equal to or greater than the current value else { // If there is no right, add it to the right if (node.Right == null) { node.Right = new AVLTreeNode <T>(value, node, this); } else { // else add it to the right node AddTo(node.Right, value); } } node.Balance(); }
/// <summary> /// Adds a value, finding the correct place to insert it /// </summary> /// <param name="node"></param> /// <param name="value"></param> private void AddTo(AVLTreeNode <T> node, T value) { // Is value less than the current node value? if (value.CompareTo(node.Value) < 0) { // If the left node is null, make this value the left node if (node.Left == null) { node.Left = new AVLTreeNode <T>(value, node, this); } else { // Add the value to the left node recursively AddTo(node.Left, value); } } else { // value is equal to or greater than the current value // If the right node in null, make this value the right node if (node.Right == null) { node.Right = new AVLTreeNode <T>(value, node, this); } else { // Add the value to the right node recursively AddTo(node.Right, value); } } // Finally, balance the node node.Balance(); }
/// <summary> /// Removes the specified value from the tree if found /// </summary> /// <param name="value">The node value to find</param> /// <returns>True if found and removed, otherwise, false</returns> public bool Remove(T value) { AVLTreeNode <T> current = Find(value); if (current == null) { // Not found return(false); } // Grab the parent of the node to be removed AVLTreeNode <T> treeToBalance = current.Parent; // Decrement the count Count--; // If Current has no right child, current's left node replaces current if (current.Right == null) { if (current.Parent == null) { Head = current.Left; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { // If parent value is greater than current value, // make the current left child a left child of parent current.Parent.Left = current.Left; } else if (result < 0) { // If parent value is less than the current value, // make the current left child a right child of parent current.Parent.Right = current.Left; } } } else if (current.Right.Left == null) { // Current's right child has no left child, current's right child replaces current current.Right.Left = current.Left; if (current.Parent == null) { Head = current.Right; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { // If parent value is greater than the current value, // make the current right child a left child of parent current.Parent.Left = current.Right; } else if (result < 0) { // If parent value is less than the current value // make the current right child a right child of parent current.Parent.Right = current.Right; } } } else { // Current's right child has a left child // Replace current with current's right child's leftmost child // Get the right's leftmost child AVLTreeNode <T> leftmost = current.Right.Left; while (leftmost.Left != null) { leftmost = leftmost.Left; } // The parent's left subtree becomes the leftmost's right subtree leftmost.Parent.Left = leftmost.Right; // Assign leftmost's child nodes leftmost.Left = current.Left; leftmost.Right = current.Right; if (current.Parent == null) { Head = leftmost; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { // If parent value is greater than current value // make leftmost the parent's left child current.Parent.Left = leftmost; } else if (result < 0) { // If parent value is less than current value // make leftmost the the parent's right child current.Parent.Right = leftmost; } } } // Rebalance if (treeToBalance != null) { treeToBalance.Balance(); } else { if (Head != null) { Head.Balance(); } } return(true); }
/// <summary> /// Removes the first occurance of the specified value from the tree. /// </summary> /// <param name="value">The value to remove</param> /// <returns>True if the value was removed, false otherwise</returns> public bool Remove(T value) { AVLTreeNode <T> current; current = Find(value); if (current == null) { return(false); } AVLTreeNode <T> treeToBalance = current.Parent; Count--; // Case 1: If current has no right child, then current's left replaces current if (current.Right == null) { if (current.Parent == null) { Head = current.Left; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { // if parent value is greater than current value // make the current left child a left child of parent current.Parent.Left = current.Left; } else if (result < 0) { // if parent value is less than current value // make the current left child a right child of parent current.Parent.Right = current.Left; } } } // Case 2: If current's right child has no left child, then current's right child // replaces current else if (current.Right.Left == null) { current.Right.Left = current.Left; if (current.Parent == null) { Head = current.Right; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { // if parent value is greater than current value // make the current right child a left child of parent current.Parent.Left = current.Right; } else if (result < 0) { // if parent value is less than current value // make the current right child a right child of parent current.Parent.Right = current.Right; } } } // Case 3: If current's right child has a left child, replace current with current's // right child's left-most child else { // find the right's left-most child AVLTreeNode <T> leftmost = current.Right.Left; while (leftmost.Left != null) { leftmost = leftmost.Left; } // the parent's left subtree becomes the leftmost's right subtree leftmost.Parent.Left = leftmost.Right; // assign leftmost's left and right to current's left and right children leftmost.Left = current.Left; leftmost.Right = current.Right; if (current.Parent == null) { Head = leftmost; if (Head != null) { Head.Parent = null; } } else { int result = current.Parent.CompareTo(current.Value); if (result > 0) { // if parent value is greater than current value // make leftmost the parent's left child current.Parent.Left = leftmost; } else if (result < 0) { // if parent value is less than current value // make leftmost the parent's right child current.Parent.Right = leftmost; } } } if (treeToBalance != null) { treeToBalance.Balance(); } else { if (Head != null) { Head.Balance(); } } return(true); }