mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-08 21:23:32 +08:00
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:
@ -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
|
||||
|
Reference in New Issue
Block a user