void DeleteFromParent(BSTNode node) { BSTNode parent = null; FindParent(_root, node._key, ref parent); if (parent != null) { if (node.Equals(parent._left)) { parent._left = null; } else { parent._right = null; } } }
// Удаление узла void Delete(ref BSTNode deletingNode) { DeleteFromParent(deletingNode); if (deletingNode._left == null) { if (deletingNode._right == null) { deletingNode = null; } else { Transplant(ref deletingNode, ref deletingNode._right); } } else if (deletingNode._right == null) { Transplant(ref deletingNode, ref deletingNode._left); } else { var minNode = Minimum(deletingNode._right); BSTNode minNodeParent = null; FindParent(minNode, minNode._key, ref minNodeParent); if (!minNodeParent.Equals(deletingNode)) { Transplant(ref minNode, ref minNode._right); minNode._right = deletingNode._right; BSTNode minNodeRightParent = null; FindParent(minNode._right, minNode._key, ref minNodeRightParent); minNodeRightParent = minNode; } Transplant(ref minNode, ref minNodeParent); minNode._left = deletingNode._left; BSTNode minNodeLeftParent = null; FindParent(minNode._right, minNode._key, ref minNodeLeftParent); minNodeLeftParent = minNode; } }