fix: transplant with successor as a replace

When transplanting node with a successor, instead of redoing the links,
just swap the value and delete „abused“ successor, which allows more
straightforward rebalance.

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-06 09:40:51 +02:00
parent b0b712b2f6
commit 966e1dd935
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -143,18 +143,12 @@ class RankedTree {
this.transplant(node, node.left);
} else {
let successor = nodeMinimum(node.right);
parent = successor.parent != node ? successor.parent : successor;
if (successor.parent != node) {
parent = successor.right ? successor.right : successor.parent;
this.transplant(successor, successor.right);
successor.right = node.right;
successor.right.parent = successor;
}
node.value = successor.value;
successor.value = null;
this.record();
this.transplant(node, successor);
successor.left = node.left;
node.left.parent = successor;
return this.deleteNode(successor);
}
this.record();