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

14
avl.js
View file

@ -1,4 +1,5 @@
Array.prototype.equals = function(other) { // TODO: Factor out to common module
Array.prototype.equals = function (other) {
if (this.length != other.length) { if (this.length != other.length) {
return false; return false;
} }
@ -11,7 +12,7 @@ Array.prototype.equals = function(other) {
} }
return true; return true;
} };
function balanceFactor(node) { function balanceFactor(node) {
if (!node) { if (!node) {
@ -47,7 +48,10 @@ class AVLTree extends RankedTree {
return false; 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) { fix0Child(x, y, z, rotateLeft, rotateRight) {
@ -132,7 +136,7 @@ class AVLTree extends RankedTree {
} }
deleteFixup(y, parent) { deleteFixup(y, parent) {
let x = (y) ? y : parent; let x = y ? y : parent;
let factor = balanceFactor(x); let factor = balanceFactor(x);
switch (factor) { switch (factor) {
@ -156,7 +160,7 @@ class AVLTree extends RankedTree {
deleteRebalance(node, parent) { deleteRebalance(node, parent) {
while (node || parent) { while (node || parent) {
this.deleteFixup(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; missing = true;
} }
newNode = node newNode = node;
while (newNode && (missing || newNode.value != value)) { while (newNode && (missing || newNode.value != value)) {
node = newNode; node = newNode;
@ -110,7 +110,7 @@ function findParentNode(value, node, missing) {
function nodeSearch(value, node) { function nodeSearch(value, node) {
while (node && node.value != value) { while (node && node.value != value) {
node = (value < node.value) ? node.left : node.right; node = value < node.value ? node.left : node.right;
} }
return node; return node;

View file

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

View file

@ -4,11 +4,14 @@ class RAVLTree extends WAVLTree {
return true; return true;
} }
if (!nodeDifferences(node).filter(d => d <= 0).length > 0) { if (!nodeDifferences(node).filter((d) => d <= 0).length > 0) {
return false; 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) { deleteRebalance(node, parent) {

42
wavl.js
View file

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