private bool GetLast(BinaryTreeNode <T> root, int height, out T value) { value = root.Value; if (height == 1) { return(true); } if (root.LeftChild == null && root.RightChild == null) { return(false); } bool done = false; if (!done && root.LeftChild != null) { done = GetLast(root.LeftChild, height - 1, out T newValue); if (done) { value = newValue; if (height == 2) { root.RemoveLeftChild(); } return(true); } } if (!done && root.RightChild != null) { done = GetLast(root.RightChild, height - 1, out T newValue); if (done) { value = newValue; if (height == 2) { root.RemoveRightChild(); } return(true); } } return(done); }
private bool DeleteNode(T item, BinaryTreeNode <T> root) { if (item == null || root == null) { return(false); } if (item.CompareTo(root.Value) == 0) { if (!root.isEmpty) { GetLast(Root, GetHeight(Root), out T last); if (last.CompareTo(root.Value) <= 0) { root.Value = last; UpHeapify(root); } else { root.Value = last; DownHeapify(root); } } return(true); } bool done = DeleteNode(item, root.LeftChild); if (done && root.LeftChild.isEmpty) { root.RemoveLeftChild(); } if (!done) { done = DeleteNode(item, root.RightChild); if (done && root.RightChild.isEmpty) { root.RemoveRightChild(); } } return(done); }