fix(avl): do not propagate all the way if not necessary

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-16 19:27:43 +02:00
parent f17abfd50e
commit 41c9c05ab2
Signed by: mfocko
GPG key ID: 7C47D46246790496

21
avl.js
View file

@ -153,12 +153,10 @@ class AVLTree extends RankedTree {
this.record("Updating rank of nodes affected by rotations", n);
});
return factor == 0;
return factor != 0;
}
deleteFixup(y, parent) {
let x = y ? y : parent;
deleteFixup(x, parent) {
let factor = balanceFactor(x);
switch (factor) {
case 0:
@ -168,10 +166,10 @@ class AVLTree extends RankedTree {
x
);
return false;
return true;
case -1:
case 1:
return true;
return false;
}
let rotatingAroundRoot = x.parent == null;
@ -201,8 +199,15 @@ class AVLTree extends RankedTree {
}
deleteRebalance(node, parent) {
while (node || parent) {
this.deleteFixup(node, parent);
if (!node && !parent) {
return;
}
if (!node) {
[node, parent] = [parent, parent.parent];
}
while (node && this.deleteFixup(node, parent)) {
[node, parent] = [parent, parent ? parent.parent : null];
}
}