private Node <T> Remove(T value, Node <T> at) { if (at == null) { return(null); } int comparison = value.CompareTo(at.Value); if (comparison < 0) { at.Left = Remove(value, at.Left); } else if (comparison > 0) { at.Right = Remove(value, at.Right); } else //comparison == 0 { if (at.Left == null) { return(at.Right); } if (at.Right == null) { return(at.Left); } //both children exist RemoveMin(at.Right, ref at.Value); } return(Node <T> .Balance(at)); }
private static Node <T> RemoveMin(Node <T> at, ref T minValue) { if (at.Left == null) { minValue = at.Value; return(at.Right); } at.Left = RemoveMin(at.Left, ref minValue); return(Node <T> .Balance(at)); }
private static Node <T> Add(T item, Node <T> at) { if (at == null) { return new Node <T> { Value = item } } ; if (item.CompareTo(at.Value) <= 0) { at.Left = Add(item, at.Left); } else { at.Right = Add(item, at.Right); } return(Node <T> .Balance(at)); }