web/wavl.js

179 lines
4.8 KiB
JavaScript
Raw Normal View History

class WAVLTree extends AVLTree {
isCorrectNode(node, recursive) {
if (!node) {
return true;
}
nodeDifferences(node).forEach((childRank) => {
if ([1, 2].indexOf(childRank) == -1) {
return false;
}
});
if (node.type() == LEAF) {
return node.rank == 0;
}
recursive = recursive ?? true;
return (
!recursive ||
(this.isCorrectNode(node.left) && this.isCorrectNode(node.right))
);
}
fixDelete(x, y, z, reversed, rotatingAroundRoot, rotateLeft, rotateRight) {
let newRoot = x;
let [v, w] = [y.left, y.right];
if (reversed) {
[v, w] = [w, v];
}
let wDiff = nodeDifference(w, y);
if (wDiff == 1 && y.parent) {
newRoot = rotateLeft(y.parent);
if (rotatingAroundRoot) {
this.root = newRoot;
}
this.record(
"Final step of deletion rebalance: single rotation by parent of y"
);
y.promote();
this.record("Final step of deletion rebalance: promotion of y");
z.demote();
this.record("Final step of deletion rebalance: demotion of z");
if (z.type() == LEAF) {
z.demote();
this.record(
"Final step of deletion rebalance: demotion of leaf z"
);
}
} else if (wDiff == 2 && v && v.parent) {
rotateRight(v.parent);
this.record(
"Final step of deletion rebalance: first of double rotation by parent of v"
);
newRoot = rotateLeft(v.parent);
if (rotatingAroundRoot) {
this.root = newRoot;
}
this.record(
"Final step of deletion rebalance: second of double rotation by parent of v"
);
v.promote().promote();
this.record(
"Final step of deletion rebalance: double promotion of v"
);
y.demote();
this.record("Final step of deletion rebalance: demotion of y");
z.demote().demote();
this.record(
"Final step of deletion rebalance: double demotion of z"
);
}
}
bottomUpDelete(x, parent) {
let xDiff = nodeDifference(x, parent);
if (xDiff != 3 || !parent) {
return;
}
let y = parent.left === x ? parent.right : parent.left;
let yDiff = nodeDifference(y, parent);
while (
parent &&
xDiff == 3 &&
y &&
(yDiff == 2 || nodeDifferences(y).equals([2, 2]))
) {
parent.demote();
this.record("Propagating error by demoting parent");
if (yDiff != 2) {
y.demote();
this.record("Demoting y");
}
x = parent;
parent = x.parent;
if (!parent) {
return;
}
y = parent.left === x ? parent.right : parent.left;
xDiff = nodeDifference(x, parent);
yDiff = nodeDifference(y, parent);
}
if (!parent) {
return;
}
let rotatingAroundRoot = parent.parent == null;
let parentNodeDiffs = nodeDifferences(parent);
if (parentNodeDiffs.sort().equals([1, 3])) {
if (parent.left === x) {
this.fixDelete(
x,
parent.right,
parent,
false,
rotatingAroundRoot,
rotateLeft,
rotateRight
);
} else {
this.fixDelete(
x,
parent.left,
parent,
true,
rotatingAroundRoot,
rotateRight,
rotateLeft
);
}
}
}
deleteFixup(y, parent) {
if (nodeDifferences(y).equals([2, 2])) {
y.demote();
this.record("Starting deletion rebalance by demoting (2, 2) node");
parent = y.parent;
} else if (nodeDifferences(parent).equals([2, 2])) {
parent.demote();
this.record("Starting deletion rebalance by demoting (2, 2) node");
parent = parent.parent;
}
if (!parent) {
return;
}
[parent.left, parent.right].forEach((y) => {
if (nodeDifference(y, parent) == 3) {
this.bottomUpDelete(y, parent);
}
});
}
deleteRebalance(node, parent) {
this.deleteFixup(node, parent);
}
}