/// <summary> Constructs the tree </summary> public BinarySearchTree() { this.root = null; }
public int CompareTo(BinaryTreeNode <T> other) => this.Value.CompareTo(other.Value);
private void Remove(BinaryTreeNode <T> node) { // Case 3: If the node has two children. // Note that if we get here at the end // the node will be with at most one child if (node.LeftChild != null && node.RightChild != null) { BinaryTreeNode <T> replacement = node.RightChild; while (replacement.LeftChild != null) { replacement = replacement.LeftChild; } node.Value = replacement.Value; node = replacement; } //Case 1 and 2: If the node has at most one child BinaryTreeNode <T> theChild = node.LeftChild != null ? node.LeftChild : node.RightChild; //If the element to be deleted has one child if (theChild != null) { theChild.Parent = node.Parent; //Handle the case when the element is the root if (node.Parent == null) { root = theChild; } else { //Replace the element with its child subtree if (node.Parent.LeftChild == node) { node.Parent.LeftChild = theChild; } else { node.Parent.RightChild = theChild; } } } else { //Handle the case when the element is the root if (node.Parent == null) { root = null; } else { //Remove the element - it is a leaf if (node.Parent.LeftChild == node) { node.Parent.LeftChild = null; } else { node.Parent.RightChild = null; } } } }
public override bool Equals(object obj) { BinaryTreeNode <T> other = (BinaryTreeNode <T>)obj; return(this.CompareTo(other) == 0); }
/// <summary> /// Compares current BinaryTreeNode to provided /// </summary> /// <param name="other">The node value to compare to</param> /// <returns> 1 if the current node value is greater than provided, /// -1 if less, 0 if they are equal </returns> public int CompareTo(BinaryTreeNode <T> other) { return(Value.CompareTo(other.Value)); }