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:
parent
b0b712b2f6
commit
966e1dd935
1 changed files with 4 additions and 10 deletions
|
@ -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();
|
||||||
|
|
Loading…
Reference in a new issue