chore: format sources

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-01 17:09:46 +02:00
parent d86301c8c1
commit 70c3b55f1a
Signed by: mfocko
GPG key ID: 7C47D46246790496
5 changed files with 56 additions and 27 deletions

12
avl.js
View file

@ -1,3 +1,4 @@
// TODO: Factor out to common module
Array.prototype.equals = function (other) {
if (this.length != other.length) {
return false;
@ -11,7 +12,7 @@ Array.prototype.equals = function(other) {
}
return true;
}
};
function balanceFactor(node) {
if (!node) {
@ -47,7 +48,10 @@ class AVLTree extends RankedTree {
return false;
}
return !recursive || (this.isCorrectNode(node.left) && this.isCorrectNode(node.right));
return (
!recursive ||
(this.isCorrectNode(node.left) && this.isCorrectNode(node.right))
);
}
fix0Child(x, y, z, rotateLeft, rotateRight) {
@ -132,7 +136,7 @@ class AVLTree extends RankedTree {
}
deleteFixup(y, parent) {
let x = (y) ? y : parent;
let x = y ? y : parent;
let factor = balanceFactor(x);
switch (factor) {
@ -156,7 +160,7 @@ class AVLTree extends RankedTree {
deleteRebalance(node, parent) {
while (node || parent) {
this.deleteFixup(node, parent);
[node, parent] = [parent, (parent) ? parent.parent : null];
[node, parent] = [parent, parent ? parent.parent : null];
}
}
}

View file

@ -91,7 +91,7 @@ function findParentNode(value, node, missing) {
missing = true;
}
newNode = node
newNode = node;
while (newNode && (missing || newNode.value != value)) {
node = newNode;
@ -110,7 +110,7 @@ function findParentNode(value, node, missing) {
function nodeSearch(value, node) {
while (node && node.value != value) {
node = (value < node.value) ? node.left : node.right;
node = value < node.value ? node.left : node.right;
}
return node;

View file

@ -120,10 +120,10 @@ class RankedTree {
this.transplant(node, node.left);
} else {
let successor = nodeMinimum(node.right);
parent = (successor.parent != node) ? successor.parent : successor;
parent = successor.parent != node ? successor.parent : successor;
if (successor.parent != node) {
parent = (successor.right) ? successor.right : successor.parent;
parent = successor.right ? successor.right : successor.parent;
this.transplant(successor, successor.right);
successor.right = node.right;
successor.right.parent = successor;
@ -140,7 +140,7 @@ class RankedTree {
delete(value) {
let node = this.root;
while (node && node.value != value) {
node = (value < node.value) ? node.left : node.right;
node = value < node.value ? node.left : node.right;
}
let toRebalance = this.deleteNode(node);

View file

@ -4,11 +4,14 @@ class RAVLTree extends WAVLTree {
return true;
}
if (!nodeDifferences(node).filter(d => d <= 0).length > 0) {
if (!nodeDifferences(node).filter((d) => d <= 0).length > 0) {
return false;
}
return !recursive || (this.isCorrectNode(node.left) && this.isCorrectNode(node.right));
return (
!recursive ||
(this.isCorrectNode(node.left) && this.isCorrectNode(node.right))
);
}
deleteRebalance(node, parent) {

42
wavl.js
View file

@ -4,7 +4,7 @@ class WAVLTree extends AVLTree {
return true;
}
nodeDifferences(node).forEach(childRank => {
nodeDifferences(node).forEach((childRank) => {
if ([1, 2].findIndex(childRank) == -1) {
return false;
}
@ -14,7 +14,10 @@ class WAVLTree extends AVLTree {
return node.rank == 0;
}
return !(recursive ?? true) || (this.isCorrectNode(node.left) && this.isCorrectNode(node.right));
return (
!(recursive ?? true) ||
(this.isCorrectNode(node.left) && this.isCorrectNode(node.right))
);
}
fixDelete(x, y, z, reversed, rotateLeft, rotateRight) {
@ -54,10 +57,15 @@ class WAVLTree extends AVLTree {
return;
}
let y = (parent.left === x) ? parent.right : parent.left;
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]))) {
while (
parent &&
xDiff == 3 &&
y &&
(yDiff == 2 || nodeDifferences(y).equals([2, 2]))
) {
parent.demote();
if (yDiff != 2) {
y.demote();
@ -69,7 +77,7 @@ class WAVLTree extends AVLTree {
return;
}
let y = (parent.left === x) ? parent.right : parent.left;
let y = parent.left === x ? parent.right : parent.left;
xDiff = nodeDifference(x, parent);
yDiff = nodeDifference(y, parent);
@ -85,9 +93,23 @@ class WAVLTree extends AVLTree {
let parentNodeDiffs = nodeDifferences(parent);
if (parentNodeDiffs.sort().equals([1, 3])) {
if (parent.left === x) {
newRoot = this.fixDelete(x, parent.right, parent, false, rotateLeft, rotateRight);
newRoot = this.fixDelete(
x,
parent.right,
parent,
false,
rotateLeft,
rotateRight
);
} else {
newRoot = this.fixDelete(x, parent.left, parent, true, rotateRight, rotateLeft);
newRoot = this.fixDelete(
x,
parent.left,
parent,
true,
rotateRight,
rotateLeft
);
}
}
@ -97,7 +119,7 @@ class WAVLTree extends AVLTree {
}
deleteFixup(y, parent) {
let z = (y) ? y : parent;
let z = y ? y : parent;
if (nodeDifferences(z).equals([2, 2])) {
z.demote();
@ -107,7 +129,7 @@ class WAVLTree extends AVLTree {
return;
}
[parent.left, parent.right].forEach(y => {
[parent.left, parent.right].forEach((y) => {
if (nodeDifference(y, parent) == 3) {
this.bottomUpDelete(y, parent);
}
@ -117,7 +139,7 @@ class WAVLTree extends AVLTree {
deleteRebalance(node, parent) {
while (node || parent) {
this.deleteFixup(node, parent);
[node, parent] = parent, (parent) ? parent.parent : null;
([node, parent] = parent), parent ? parent.parent : null;
}
}
}