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); this.transplant(node, node.left);
} else { } else {
let successor = nodeMinimum(node.right); let successor = nodeMinimum(node.right);
parent = successor.parent != node ? successor.parent : successor;
if (successor.parent != node) { node.value = successor.value;
parent = successor.right ? successor.right : successor.parent; successor.value = null;
this.transplant(successor, successor.right); this.record();
successor.right = node.right;
successor.right.parent = successor;
}
this.transplant(node, successor); return this.deleteNode(successor);
successor.left = node.left;
node.left.parent = successor;
} }
this.record(); this.record();