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 newRoot = x;
|
||||||
let [v, w] = [y.left, y.right];
|
let [v, w] = [y.left, y.right];
|
||||||
|
|
||||||
|
@ -32,23 +32,40 @@ class WAVLTree extends AVLTree {
|
||||||
|
|
||||||
if (wDiff == 1 && y.parent) {
|
if (wDiff == 1 && y.parent) {
|
||||||
newRoot = rotateLeft(y.parent);
|
newRoot = rotateLeft(y.parent);
|
||||||
|
if (rotatingAroundRoot) {
|
||||||
|
this.root = newRoot;
|
||||||
|
}
|
||||||
|
this.record();
|
||||||
|
|
||||||
y.promote();
|
y.promote();
|
||||||
|
this.record();
|
||||||
|
|
||||||
z.demote();
|
z.demote();
|
||||||
|
this.record();
|
||||||
|
|
||||||
if (z.type() == LEAF) {
|
if (z.type() == LEAF) {
|
||||||
z.demote();
|
z.demote();
|
||||||
|
this.record();
|
||||||
}
|
}
|
||||||
} else if (wDiff == 2 && v && v.parent) {
|
} else if (wDiff == 2 && v && v.parent) {
|
||||||
rotateRight(v.parent);
|
rotateRight(v.parent);
|
||||||
|
this.record();
|
||||||
|
|
||||||
newRoot = rotateLeft(v.parent);
|
newRoot = rotateLeft(v.parent);
|
||||||
|
if (rotatingAroundRoot) {
|
||||||
|
this.root = newRoot;
|
||||||
|
}
|
||||||
|
this.record();
|
||||||
|
|
||||||
v.promote().promote();
|
v.promote().promote();
|
||||||
y.demote();
|
this.record();
|
||||||
z.demote().demote();
|
|
||||||
}
|
|
||||||
|
|
||||||
return newRoot;
|
y.demote();
|
||||||
|
this.record();
|
||||||
|
|
||||||
|
z.demote().demote();
|
||||||
|
this.record();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bottomUpDelete(x, parent) {
|
bottomUpDelete(x, parent) {
|
||||||
|
@ -67,8 +84,11 @@ class WAVLTree extends AVLTree {
|
||||||
(yDiff == 2 || nodeDifferences(y).equals([2, 2]))
|
(yDiff == 2 || nodeDifferences(y).equals([2, 2]))
|
||||||
) {
|
) {
|
||||||
parent.demote();
|
parent.demote();
|
||||||
|
this.record();
|
||||||
|
|
||||||
if (yDiff != 2) {
|
if (yDiff != 2) {
|
||||||
y.demote();
|
y.demote();
|
||||||
|
this.record();
|
||||||
}
|
}
|
||||||
|
|
||||||
x = parent;
|
x = parent;
|
||||||
|
@ -88,34 +108,31 @@ class WAVLTree extends AVLTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
let rotatingAroundRoot = parent.parent == null;
|
let rotatingAroundRoot = parent.parent == null;
|
||||||
let newRoot = parent;
|
|
||||||
|
|
||||||
let parentNodeDiffs = nodeDifferences(parent);
|
let parentNodeDiffs = nodeDifferences(parent);
|
||||||
if (parentNodeDiffs.sort().equals([1, 3])) {
|
if (parentNodeDiffs.sort().equals([1, 3])) {
|
||||||
if (parent.left === x) {
|
if (parent.left === x) {
|
||||||
newRoot = this.fixDelete(
|
this.fixDelete(
|
||||||
x,
|
x,
|
||||||
parent.right,
|
parent.right,
|
||||||
parent,
|
parent,
|
||||||
false,
|
false,
|
||||||
|
rotatingAroundRoot,
|
||||||
rotateLeft,
|
rotateLeft,
|
||||||
rotateRight
|
rotateRight
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
newRoot = this.fixDelete(
|
this.fixDelete(
|
||||||
x,
|
x,
|
||||||
parent.left,
|
parent.left,
|
||||||
parent,
|
parent,
|
||||||
true,
|
true,
|
||||||
|
rotatingAroundRoot,
|
||||||
rotateRight,
|
rotateRight,
|
||||||
rotateLeft
|
rotateLeft
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rotatingAroundRoot) {
|
|
||||||
this.root = newRoot;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteFixup(y, parent) {
|
deleteFixup(y, parent) {
|
||||||
|
@ -123,6 +140,7 @@ class WAVLTree extends AVLTree {
|
||||||
|
|
||||||
if (nodeDifferences(z).equals([2, 2])) {
|
if (nodeDifferences(z).equals([2, 2])) {
|
||||||
z.demote();
|
z.demote();
|
||||||
|
this.record();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parent) {
|
if (!parent) {
|
||||||
|
|
Loading…
Reference in a new issue