feat(wavl): refactor fixDelete
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
7217588536
commit
68e6da262c
1 changed files with 30 additions and 12 deletions
42
wavl.js
42
wavl.js
|
@ -20,7 +20,7 @@ class WAVLTree extends AVLTree {
|
|||
);
|
||||
}
|
||||
|
||||
fixDelete(x, y, z, reversed, rotateLeft, rotateRight) {
|
||||
fixDelete(x, y, z, reversed, rotatingAroundRoot, rotateLeft, rotateRight) {
|
||||
let newRoot = x;
|
||||
let [v, w] = [y.left, y.right];
|
||||
|
||||
|
@ -32,23 +32,40 @@ class WAVLTree extends AVLTree {
|
|||
|
||||
if (wDiff == 1 && y.parent) {
|
||||
newRoot = rotateLeft(y.parent);
|
||||
if (rotatingAroundRoot) {
|
||||
this.root = newRoot;
|
||||
}
|
||||
this.record();
|
||||
|
||||
y.promote();
|
||||
this.record();
|
||||
|
||||
z.demote();
|
||||
this.record();
|
||||
|
||||
if (z.type() == LEAF) {
|
||||
z.demote();
|
||||
this.record();
|
||||
}
|
||||
} else if (wDiff == 2 && v && v.parent) {
|
||||
rotateRight(v.parent);
|
||||
this.record();
|
||||
|
||||
newRoot = rotateLeft(v.parent);
|
||||
if (rotatingAroundRoot) {
|
||||
this.root = newRoot;
|
||||
}
|
||||
this.record();
|
||||
|
||||
v.promote().promote();
|
||||
y.demote();
|
||||
z.demote().demote();
|
||||
}
|
||||
this.record();
|
||||
|
||||
return newRoot;
|
||||
y.demote();
|
||||
this.record();
|
||||
|
||||
z.demote().demote();
|
||||
this.record();
|
||||
}
|
||||
}
|
||||
|
||||
bottomUpDelete(x, parent) {
|
||||
|
@ -67,8 +84,11 @@ class WAVLTree extends AVLTree {
|
|||
(yDiff == 2 || nodeDifferences(y).equals([2, 2]))
|
||||
) {
|
||||
parent.demote();
|
||||
this.record();
|
||||
|
||||
if (yDiff != 2) {
|
||||
y.demote();
|
||||
this.record();
|
||||
}
|
||||
|
||||
x = parent;
|
||||
|
@ -88,34 +108,31 @@ class WAVLTree extends AVLTree {
|
|||
}
|
||||
|
||||
let rotatingAroundRoot = parent.parent == null;
|
||||
let newRoot = parent;
|
||||
|
||||
let parentNodeDiffs = nodeDifferences(parent);
|
||||
if (parentNodeDiffs.sort().equals([1, 3])) {
|
||||
if (parent.left === x) {
|
||||
newRoot = this.fixDelete(
|
||||
this.fixDelete(
|
||||
x,
|
||||
parent.right,
|
||||
parent,
|
||||
false,
|
||||
rotatingAroundRoot,
|
||||
rotateLeft,
|
||||
rotateRight
|
||||
);
|
||||
} else {
|
||||
newRoot = this.fixDelete(
|
||||
this.fixDelete(
|
||||
x,
|
||||
parent.left,
|
||||
parent,
|
||||
true,
|
||||
rotatingAroundRoot,
|
||||
rotateRight,
|
||||
rotateLeft
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (rotatingAroundRoot) {
|
||||
this.root = newRoot;
|
||||
}
|
||||
}
|
||||
|
||||
deleteFixup(y, parent) {
|
||||
|
@ -123,6 +140,7 @@ class WAVLTree extends AVLTree {
|
|||
|
||||
if (nodeDifferences(z).equals([2, 2])) {
|
||||
z.demote();
|
||||
this.record();
|
||||
}
|
||||
|
||||
if (!parent) {
|
||||
|
|
Loading…
Reference in a new issue