public bool Remove(T value) { BinaryTreeNode <T> current, parent; current = FindWithParent(value, out parent); if (current == null) { return(false); } _count--; if (current.Right == null) { if (parent == null) { _head = current.Left; } int result1 = parent.Value.CompareTo(current.Value); if (result1 > 0) { parent.Left = current.Left; } if (result1 < 0) { parent.Right = current.Left; } return(true); } if (current.Right.Left == null) { current.Right.Left = current.Left; if (parent == null) { _head = current.Right; return(true); } int result2 = parent.Value.CompareTo(current.Value); if (result2 > 0) { parent.Left = current.Right; } if (result2 < 0) { parent.Right = current.Right; } return(true); } BinaryTreeNode <T> leftmost = current.Right.Left; BinaryTreeNode <T> leftmostParent = current.Right; while (leftmost.Left != null) { leftmostParent = leftmost; leftmost = leftmost.Left; } leftmostParent.Left = leftmost.Right; leftmost.Left = current.Left; leftmost.Right = current.Right; if (parent == null) { _head = leftmost; return(true); } int result = parent.Value.CompareTo(current.Value); if (result > 0) { parent.Left = leftmost; } if (result < 0) { parent.Right = leftmost; } Notify?.Invoke($"Devete node with value: {value}"); return(true); }
public void Clear() { _head = null; _count = 0; }