fix(avl): do not propagate all the way if not necessary
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
f17abfd50e
commit
41c9c05ab2
1 changed files with 13 additions and 8 deletions
21
avl.js
21
avl.js
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue