feat: Test running overhaul, switch to Prettier & reformat everything (#1407)

* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
Roland Hummel
2023-10-03 23:08:19 +02:00
committed by GitHub
parent 0ca18c2b2c
commit 86d333ee94
392 changed files with 5849 additions and 16622 deletions

View File

@@ -13,9 +13,9 @@
* RETURN > 0 if a > b
* MUST RETURN 0 if a == b
*/
let utils;
(function (_utils) {
function comparator () {
let utils
;(function (_utils) {
function comparator() {
return function (v1, v2) {
if (v1 < v2) return -1
if (v2 < v1) return 1
@@ -32,7 +32,7 @@ let utils;
* If no argument is sent it uses utils.comparator
*/
const AVLTree = (function () {
function _avl (comp) {
function _avl(comp) {
/** @public comparator function */
this._comp = undefined
this._comp = comp !== undefined ? comp : utils.comparator()
@@ -53,7 +53,9 @@ const AVLTree = (function () {
// get height of a node
const getHeight = function (node) {
if (node == null) { return 0 }
if (node == null) {
return 0
}
return node._height
}
@@ -64,12 +66,15 @@ const AVLTree = (function () {
// update height of a node based on children's heights
const updateHeight = function (node) {
if (node == null) { return }
if (node == null) {
return
}
node._height = Math.max(getHeight(node._left), getHeight(node._right)) + 1
}
// Helper: To check if the balanceFactor is valid
const isValidBalanceFactor = (balanceFactor) => [0, 1, -1].includes(balanceFactor)
const isValidBalanceFactor = (balanceFactor) =>
[0, 1, -1].includes(balanceFactor)
// rotations of AVL Tree
const leftRotate = function (node) {
@@ -140,13 +145,18 @@ const AVLTree = (function () {
}
updateHeight(root)
const balanceFactor = getHeightDifference(root)
return isValidBalanceFactor(balanceFactor) ? root : insertBalance(root, val, balanceFactor, tree)
return isValidBalanceFactor(balanceFactor)
? root
: insertBalance(root, val, balanceFactor, tree)
}
// delete am element
const deleteElement = function (root, _val, tree) {
if (root == null) { return root }
if (tree._comp(root._val, _val) === 0) { // key found case
if (root == null) {
return root
}
if (tree._comp(root._val, _val) === 0) {
// key found case
if (root._left === null && root._right === null) {
root = null
tree.size--
@@ -177,7 +187,9 @@ const AVLTree = (function () {
}
// search tree for a element
const searchAVLTree = function (root, val, tree) {
if (root == null) { return null }
if (root == null) {
return null
}
if (tree._comp(root._val, val) === 0) {
return root
}
@@ -222,7 +234,7 @@ const AVLTree = (function () {
return prevSize !== this.size
}
return _avl
}())
})()
/**
* A Code for Testing the AVLTree

View File

@@ -1,19 +1,19 @@
/* Binary Search Tree!!
*
* Nodes that will go on the Binary Tree.
* They consist of the data in them, the node to the left, the node
* to the right, and the parent from which they came from.
*
* A binary tree is a data structure in which an element
* has two successors(children). The left child is usually
* smaller than the parent, and the right child is usually
* bigger.
*/
*
* Nodes that will go on the Binary Tree.
* They consist of the data in them, the node to the left, the node
* to the right, and the parent from which they came from.
*
* A binary tree is a data structure in which an element
* has two successors(children). The left child is usually
* smaller than the parent, and the right child is usually
* bigger.
*/
// class Node
const Node = (function Node () {
const Node = (function Node() {
// Node in the tree
function Node (val) {
function Node(val) {
this.value = val
this.left = null
this.right = null
@@ -32,7 +32,7 @@ const Node = (function Node () {
}
// Visit a node
Node.prototype.visit = function (output = value => console.log(value)) {
Node.prototype.visit = function (output = (value) => console.log(value)) {
// Recursively go left
if (this.left !== null) {
this.left.visit()
@@ -103,14 +103,14 @@ const Node = (function Node () {
}
// returns the constructor
return Node
}())
})()
// class Tree
const Tree = (function () {
function Tree () {
function Tree() {
// Just store the root
this.root = null
};
}
// Inorder traversal
Tree.prototype.traverse = function () {
@@ -149,6 +149,6 @@ const Tree = (function () {
// returns the constructor
return Tree
}())
})()
export { Tree }

View File

@@ -13,7 +13,7 @@
class SegmentTree {
size
tree
constructor (arr) {
constructor(arr) {
// we define tree like this
// tree[1] : root node of tree
// tree[i] : i'th node
@@ -28,7 +28,7 @@ class SegmentTree {
}
// function to build the tree
build (arr) {
build(arr) {
const { size, tree } = this
// insert leaf nodes in tree
// leaf nodes will start from index N
@@ -45,7 +45,7 @@ class SegmentTree {
}
}
update (index, value) {
update(index, value) {
const { size, tree } = this
// only update values in the parents of the given node being changed.
@@ -65,7 +65,7 @@ class SegmentTree {
}
// interval [L,R) with left index(L) included and right (R) excluded.
query (left, right) {
query(left, right) {
const { size, tree } = this
// cause R is excluded, increase right for convenient
right++

View File

@@ -1,4 +1,4 @@
const TrieNode = function TrieNode (key, parent) {
const TrieNode = function TrieNode(key, parent) {
this.key = key
this.count = 0
this.children = Object.create(null)
@@ -9,7 +9,7 @@ const TrieNode = function TrieNode (key, parent) {
}
}
function Trie () {
function Trie() {
// create only root with null key and parent
this.root = new TrieNode(null, null)
}
@@ -18,7 +18,9 @@ function Trie () {
Trie.findAllWords = function (root, word, output) {
if (root === null) return
if (root.count > 0) {
if (typeof output === 'object') { output.push({ word, count: root.count }) }
if (typeof output === 'object') {
output.push({ word, count: root.count })
}
}
let key
for (key in root.children) {
@@ -38,7 +40,9 @@ Trie.prototype.insert = function (word) {
const len = word.length
let i
for (i = 0; i < len; i++) {
if (node.children[word.charAt(i)] === undefined) { node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node) }
if (node.children[word.charAt(i)] === undefined) {
node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node)
}
node = node.children[word.charAt(i)]
}
node.count += 1
@@ -87,7 +91,11 @@ Trie.prototype.remove = function (word, count) {
// if the object forms some other objects prefix we don't delete it
// For checking an empty object
// https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
if (child.count <= 0 && (Object.keys(child.children).length && child.children.constructor === Object)) {
if (
child.count <= 0 &&
Object.keys(child.children).length &&
child.children.constructor === Object
) {
child.parent.children[child.key] = undefined
}
}

View File

@@ -12,14 +12,14 @@ describe('AVLTree Implementation: ', () => {
const emptyTree = new AVLTree(collator.compare)
beforeAll(() => {
demoData.forEach(item => {
demoData.forEach((item) => {
if (avlTree.add(item)) {
dataList.push(item)
}
})
avlStringTree._comp = collator.compare
stringData.forEach(item => avlStringTree.add(item))
stringData.forEach((item) => avlStringTree.add(item))
})
it('delete and search from empty tree', () => {
@@ -33,10 +33,10 @@ describe('AVLTree Implementation: ', () => {
})
it('search if inserted element is present', () => {
demoData.forEach(data => {
demoData.forEach((data) => {
expect(avlTree.find(data)).toBeTruthy()
})
stringData.forEach(data => {
stringData.forEach((data) => {
expect(avlStringTree.find(data)).toBeTruthy()
})
})