public void Clear() { _root = null; }
public bool Remove(TK key) { BinaryTreeNode <TK, TV> parent = null; var current = _root; while (current != null) { var compareTo = key.CompareTo(current.Key); if (compareTo == 0) { if (current.Lower == null && current.Higher == null) { if (parent == null) { _root = null; return(true); } if (parent.Higher == current) { parent.Higher = null; } else { parent.Lower = null; } return(true); } if (current.Lower == null || current.Higher == null) { var child = current.Lower ?? current.Higher; if (parent == null) { _root = child; return(true); } if (parent.Higher == current) { parent.Higher = child; } else { parent.Lower = child; } return(true); } var rightSubtree = current.Higher; var currentLowestParent = current; var currentLowest = rightSubtree; while (currentLowest.Lower != null) { currentLowestParent = currentLowest; currentLowest = currentLowest.Lower; } current.Key = currentLowest.Key; current.Value = currentLowest.Value; currentLowestParent.Lower = null; return(true); } parent = current; current = compareTo < 0 ? current.Lower : current.Higher; } return(false); }