Hallo, ich habe hier noch eine Schwachstelle, finde sie aber nicht. Kann mir jemand weiter helfen?
Es geht um eine Methode, die das letzte Blatt eines binary Trees entfernen soll. Sie checkt ob es rechts oder links ist und sucht somit das letzte. Nur leider ist etwas fehlerhaft und ich kommm nich drauf was.
Es geht um eine Methode, die das letzte Blatt eines binary Trees entfernen soll. Sie checkt ob es rechts oder links ist und sucht somit das letzte. Nur leider ist etwas fehlerhaft und ich kommm nich drauf was.
Java:
protected void removeLeaf(Node toRemove) { // Remove a leaf or a half-leave from the
if (toRemove.isLeftChild()) { // tree.
toRemove.parent.left = null;
if (toRemove.right != null) {
toRemove.parent.left = toRemove.right;
toRemove.right.parent = toRemove.parent;
} else if (toRemove.left != null) {
toRemove.parent.left = toRemove.left;
toRemove.left.parent = toRemove.parent;
}
} else if (toRemove.isRightChild()) {
toRemove.parent.right = null;
if (toRemove.left != null) {
toRemove.parent.right = toRemove.left;
toRemove.left.parent = toRemove.parent;
} else if (toRemove.right != null) {
toRemove.parent.right = toRemove.right;
toRemove.right.parent = toRemove.parent;
}
} else {
root = toRemove.left != null ? toRemove.left : toRemove.right;
}
}