diff --git a/.idea/data-structure-typed.iml b/.idea/data-structure-typed.iml index ac94332..fd37c0b 100644 --- a/.idea/data-structure-typed.iml +++ b/.idea/data-structure-typed.iml @@ -11,6 +11,7 @@ + diff --git a/README.md b/README.md index 4557519..725f386 100644 --- a/README.md +++ b/README.md @@ -371,55 +371,103 @@ import {UndirectedGraph} from 'data-structure-typed'; -## API docs - +[//]: # (## API docs) + +[//]: # () # Why +## Code design +By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design. + ## Complexities ### performance of Big O @@ -680,9 +728,6 @@ import {UndirectedGraph} from 'data-structure-typed'; -## Code design -By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design. - [//]: # (![overview diagram](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/assets/overview-diagram-of-data-structures.png?raw=true)) ![complexities](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/assets/complexities-diff.jpg?raw=true) diff --git a/package-lock.json b/package-lock.json index a895bf3..af3966f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,41 +1,42 @@ { "name": "data-structure-typed", - "version": "1.21.3", + "version": "1.3.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "data-structure-typed", - "version": "1.21.3", + "version": "1.3.3", "license": "MIT", "devDependencies": { "@types/jest": "^29.5.3", "@types/node": "^20.4.9", - "avl-tree-typed": "^1.21.3", - "binary-tree-typed": "^1.21.3", - "bst-typed": "^1.21.3", + "avl-tree-typed": "^1.3.3", + "binary-tree-typed": "^1.3.3", + "bst-typed": "^1.3.3", "dependency-cruiser": "^13.1.2", - "deque-typed": "^1.21.3", - "directed-graph-typed": "^1.21.3", - "doubly-linked-list-typed": "^1.21.3", - "graph-typed": "^1.21.3", - "heap-typed": "^1.21.3", + "deque-typed": "^1.3.3", + "directed-graph-typed": "^1.3.3", + "doubly-linked-list-typed": "^1.3.3", + "graph-typed": "^1.3.3", + "heap-typed": "^1.3.3", "jest": "^29.6.2", - "linked-list-typed": "^1.21.3", - "max-heap-typed": "^1.21.3", - "max-priority-queue-typed": "^1.21.3", - "min-heap-typed": "^1.21.3", - "min-priority-queue-typed": "^1.21.3", - "priority-queue-typed": "^1.21.3", - "singly-linked-list-typed": "^1.21.3", - "stack-typed": "^1.21.3", - "tree-multiset-typed": "^1.21.3", - "trie-typed": "^1.21.3", + "linked-list-typed": "^1.3.3", + "max-heap-typed": "^1.3.3", + "max-priority-queue-typed": "^1.3.3", + "min-heap-typed": "^1.3.3", + "min-priority-queue-typed": "^1.3.3", + "priority-queue-typed": "^1.3.3", + "queue-typed": "^1.3.3", + "singly-linked-list-typed": "^1.3.3", + "stack-typed": "^1.3.3", + "tree-multiset-typed": "^1.3.3", + "trie-typed": "^1.3.3", "ts-jest": "^29.1.1", "ts-loader": "^9.4.4", "typedoc": "^0.24.8", "typescript": "^4.9.5", - "undirected-graph-typed": "^1.21.3", + "undirected-graph-typed": "^1.3.3", "webpack": "^5.88.2", "webpack-cli": "^5.1.4" } @@ -123,30 +124,30 @@ } }, "node_modules/@babel/compat-data": { - "version": "7.22.9", - "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.22.9.tgz", - "integrity": "sha512-5UamI7xkUcJ3i9qVDS+KFDEK8/7oJ55/sJMB1Ge7IEapr7KfdfV/HErR+koZwOfd+SgtFKOKRhRakdg++DcJpQ==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.22.20.tgz", + "integrity": "sha512-BQYjKbpXjoXwFW5jGqiizJQQT/aC7pFm9Ok1OWssonuguICi264lbgMzRp2ZMmRSlfkX6DsWDDcsrctK8Rwfiw==", "dev": true, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/core": { - "version": "7.22.17", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.22.17.tgz", - "integrity": "sha512-2EENLmhpwplDux5PSsZnSbnSkB3tZ6QTksgO25xwEL7pIDcNOMhF5v/s6RzwjMZzZzw9Ofc30gHv5ChCC8pifQ==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.22.20.tgz", + "integrity": "sha512-Y6jd1ahLubuYweD/zJH+vvOY141v4f9igNQAQ+MBgq9JlHS2iTsZKn1aMsb3vGccZsXI16VzTBw52Xx0DWmtnA==", "dev": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.22.13", "@babel/generator": "^7.22.15", "@babel/helper-compilation-targets": "^7.22.15", - "@babel/helper-module-transforms": "^7.22.17", + "@babel/helper-module-transforms": "^7.22.20", "@babel/helpers": "^7.22.15", "@babel/parser": "^7.22.16", "@babel/template": "^7.22.15", - "@babel/traverse": "^7.22.17", - "@babel/types": "^7.22.17", + "@babel/traverse": "^7.22.20", + "@babel/types": "^7.22.19", "convert-source-map": "^1.7.0", "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", @@ -217,9 +218,9 @@ } }, "node_modules/@babel/helper-environment-visitor": { - "version": "7.22.5", - "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.5.tgz", - "integrity": "sha512-XGmhECfVA/5sAt+H+xpSg0mfrHq6FzNr9Oxh7PSEBBRUb/mL7Kz3NICXb194rCqAEdxkhPT1a88teizAFyvk8Q==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.20.tgz", + "integrity": "sha512-zfedSIzFhat/gFhWfHtgWvlec0nqB9YEIVrpuwjruLlXfUSnA8cJB0miHKwqDnQ7d32aKo2xt88/xZptwxbfhA==", "dev": true, "engines": { "node": ">=6.9.0" @@ -263,16 +264,16 @@ } }, "node_modules/@babel/helper-module-transforms": { - "version": "7.22.17", - "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.22.17.tgz", - "integrity": "sha512-XouDDhQESrLHTpnBtCKExJdyY4gJCdrvH2Pyv8r8kovX2U8G0dRUOT45T9XlbLtuu9CLXP15eusnkprhoPV5iQ==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.22.20.tgz", + "integrity": "sha512-dLT7JVWIUUxKOs1UnJUBR3S70YK+pKX6AbJgB2vMIvEkZkrfJDbYDJesnPshtKV4LhDOR3Oc5YULeDizRek+5A==", "dev": true, "dependencies": { - "@babel/helper-environment-visitor": "^7.22.5", + "@babel/helper-environment-visitor": "^7.22.20", "@babel/helper-module-imports": "^7.22.15", "@babel/helper-simple-access": "^7.22.5", "@babel/helper-split-export-declaration": "^7.22.6", - "@babel/helper-validator-identifier": "^7.22.15" + "@babel/helper-validator-identifier": "^7.22.20" }, "engines": { "node": ">=6.9.0" @@ -324,9 +325,9 @@ } }, "node_modules/@babel/helper-validator-identifier": { - "version": "7.22.15", - "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.15.tgz", - "integrity": "sha512-4E/F9IIEi8WR94324mbDUMo074YTheJmd7eZF5vITTeYchqAi6sYXRLHUVsmkdmY4QjfKTcB2jB7dVP3NaBElQ==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.20.tgz", + "integrity": "sha512-Y4OZ+ytlatR8AI+8KZfKuL5urKp7qey08ha31L8b3BwewJAoJamTzyvxPR/5D+KkdJCGPq/+8TukHBlY10FX9A==", "dev": true, "engines": { "node": ">=6.9.0" @@ -356,12 +357,12 @@ } }, "node_modules/@babel/highlight": { - "version": "7.22.13", - "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.22.13.tgz", - "integrity": "sha512-C/BaXcnnvBCmHTpz/VGZ8jgtE2aYlW4hxDhseJAWZb7gqGM/qtCK6iZUb0TyKFf7BOUsBH7Q7fkRsDRhg1XklQ==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.22.20.tgz", + "integrity": "sha512-dkdMCN3py0+ksCgYmGG8jKeGA/8Tk+gJwSYYlFGxG5lmhfKNoAy004YpLxpS1W2J8m/EK2Ew+yOs9pVRwO89mg==", "dev": true, "dependencies": { - "@babel/helper-validator-identifier": "^7.22.5", + "@babel/helper-validator-identifier": "^7.22.20", "chalk": "^2.4.2", "js-tokens": "^4.0.0" }, @@ -629,19 +630,19 @@ } }, "node_modules/@babel/traverse": { - "version": "7.22.17", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.22.17.tgz", - "integrity": "sha512-xK4Uwm0JnAMvxYZxOVecss85WxTEIbTa7bnGyf/+EgCL5Zt3U7htUpEOWv9detPlamGKuRzCqw74xVglDWpPdg==", + "version": "7.22.20", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.22.20.tgz", + "integrity": "sha512-eU260mPZbU7mZ0N+X10pxXhQFMGTeLb9eFS0mxehS8HZp9o1uSnFeWQuG1UPrlxgA7QoUzFhOnilHDp0AXCyHw==", "dev": true, "dependencies": { "@babel/code-frame": "^7.22.13", "@babel/generator": "^7.22.15", - "@babel/helper-environment-visitor": "^7.22.5", + "@babel/helper-environment-visitor": "^7.22.20", "@babel/helper-function-name": "^7.22.5", "@babel/helper-hoist-variables": "^7.22.5", "@babel/helper-split-export-declaration": "^7.22.6", "@babel/parser": "^7.22.16", - "@babel/types": "^7.22.17", + "@babel/types": "^7.22.19", "debug": "^4.1.0", "globals": "^11.1.0" }, @@ -650,13 +651,13 @@ } }, "node_modules/@babel/types": { - "version": "7.22.17", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.22.17.tgz", - "integrity": "sha512-YSQPHLFtQNE5xN9tHuZnzu8vPr61wVTBZdfv1meex1NBosa4iT05k/Jw06ddJugi4bk7The/oSwQGFcksmEJQg==", + "version": "7.22.19", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.22.19.tgz", + "integrity": "sha512-P7LAw/LbojPzkgp5oznjE6tQEIWbp4PkkfrZDINTro9zgBRtI324/EYsiSI7lhPbpIQ+DCeR2NNmMWANGGfZsg==", "dev": true, "dependencies": { "@babel/helper-string-parser": "^7.22.5", - "@babel/helper-validator-identifier": "^7.22.15", + "@babel/helper-validator-identifier": "^7.22.19", "to-fast-properties": "^2.0.0" }, "engines": { @@ -721,16 +722,16 @@ } }, "node_modules/@jest/console": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/console/-/console-29.6.4.tgz", - "integrity": "sha512-wNK6gC0Ha9QeEPSkeJedQuTQqxZYnDPuDcDhVuVatRvMkL4D0VTvFVZj+Yuh6caG2aOfzkUZ36KtCmLNtR02hw==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/console/-/console-29.7.0.tgz", + "integrity": "sha512-5Ni4CU7XHQi32IJ398EEP4RrB8eV09sXP2ROqD4bksHrnTree52PsxvX8tpL8LvTZ3pFzXyPbNQReSN41CAhOg==", "dev": true, "dependencies": { "@jest/types": "^29.6.3", "@types/node": "*", "chalk": "^4.0.0", - "jest-message-util": "^29.6.3", - "jest-util": "^29.6.3", + "jest-message-util": "^29.7.0", + "jest-util": "^29.7.0", "slash": "^3.0.0" }, "engines": { @@ -787,15 +788,15 @@ "dev": true }, "node_modules/@jest/core": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/core/-/core-29.6.4.tgz", - "integrity": "sha512-U/vq5ccNTSVgYH7mHnodHmCffGWHJnz/E1BEWlLuK5pM4FZmGfBn/nrJGLjUsSmyx3otCeqc1T31F4y08AMDLg==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/core/-/core-29.7.0.tgz", + "integrity": "sha512-n7aeXWKMnGtDA48y8TLWJPJmLmmZ642Ceo78cYWEpiD7FzDgmNDV/GCVRorPABdXLJZ/9wzzgZAlHjXjxDHGsg==", "dev": true, "dependencies": { - "@jest/console": "^29.6.4", - "@jest/reporters": "^29.6.4", - "@jest/test-result": "^29.6.4", - "@jest/transform": "^29.6.4", + "@jest/console": "^29.7.0", + "@jest/reporters": "^29.7.0", + "@jest/test-result": "^29.7.0", + "@jest/transform": "^29.7.0", "@jest/types": "^29.6.3", "@types/node": "*", "ansi-escapes": "^4.2.1", @@ -803,21 +804,21 @@ "ci-info": "^3.2.0", "exit": "^0.1.2", "graceful-fs": "^4.2.9", - "jest-changed-files": "^29.6.3", - "jest-config": "^29.6.4", - "jest-haste-map": "^29.6.4", - "jest-message-util": "^29.6.3", + "jest-changed-files": "^29.7.0", + "jest-config": "^29.7.0", + "jest-haste-map": "^29.7.0", + "jest-message-util": "^29.7.0", "jest-regex-util": "^29.6.3", - "jest-resolve": "^29.6.4", - "jest-resolve-dependencies": "^29.6.4", - "jest-runner": "^29.6.4", - "jest-runtime": "^29.6.4", - "jest-snapshot": "^29.6.4", - "jest-util": "^29.6.3", - "jest-validate": "^29.6.3", - "jest-watcher": "^29.6.4", + "jest-resolve": "^29.7.0", + "jest-resolve-dependencies": "^29.7.0", + "jest-runner": "^29.7.0", + "jest-runtime": "^29.7.0", + "jest-snapshot": "^29.7.0", + "jest-util": "^29.7.0", + "jest-validate": "^29.7.0", + "jest-watcher": "^29.7.0", "micromatch": "^4.0.4", - "pretty-format": "^29.6.3", + "pretty-format": "^29.7.0", "slash": "^3.0.0", "strip-ansi": "^6.0.0" }, @@ -904,37 +905,37 @@ } }, "node_modules/@jest/environment": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/environment/-/environment-29.6.4.tgz", - "integrity": "sha512-sQ0SULEjA1XUTHmkBRl7A1dyITM9yb1yb3ZNKPX3KlTd6IG7mWUe3e2yfExtC2Zz1Q+mMckOLHmL/qLiuQJrBQ==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/environment/-/environment-29.7.0.tgz", + "integrity": "sha512-aQIfHDq33ExsN4jP1NWGXhxgQ/wixs60gDiKO+XVMd8Mn0NWPWgc34ZQDTb2jKaUWQ7MuwoitXAsN2XVXNMpAw==", "dev": true, "dependencies": { - "@jest/fake-timers": "^29.6.4", + "@jest/fake-timers": "^29.7.0", "@jest/types": "^29.6.3", "@types/node": "*", - "jest-mock": "^29.6.3" + "jest-mock": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, "node_modules/@jest/expect": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/expect/-/expect-29.6.4.tgz", - "integrity": "sha512-Warhsa7d23+3X5bLbrbYvaehcgX5TLYhI03JKoedTiI8uJU4IhqYBWF7OSSgUyz4IgLpUYPkK0AehA5/fRclAA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/expect/-/expect-29.7.0.tgz", + "integrity": "sha512-8uMeAMycttpva3P1lBHB8VciS9V0XAr3GymPpipdyQXbBcuhkLQOSe8E/p92RyAdToS6ZD1tFkX+CkhoECE0dQ==", "dev": true, "dependencies": { - "expect": "^29.6.4", - "jest-snapshot": "^29.6.4" + "expect": "^29.7.0", + "jest-snapshot": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, "node_modules/@jest/expect-utils": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/expect-utils/-/expect-utils-29.6.4.tgz", - "integrity": "sha512-FEhkJhqtvBwgSpiTrocquJCdXPsyvNKcl/n7A3u7X4pVoF4bswm11c9d4AV+kfq2Gpv/mM8x7E7DsRvH+djkrg==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/expect-utils/-/expect-utils-29.7.0.tgz", + "integrity": "sha512-GlsNBWiFQFCVi9QVSx7f5AgMeLxe9YCCs5PuP2O2LdjDAA8Jh9eX7lA1Jq/xdXw3Wb3hyvlFNfZIfcRetSzYcA==", "dev": true, "dependencies": { "jest-get-type": "^29.6.3" @@ -944,47 +945,47 @@ } }, "node_modules/@jest/fake-timers": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/fake-timers/-/fake-timers-29.6.4.tgz", - "integrity": "sha512-6UkCwzoBK60edXIIWb0/KWkuj7R7Qq91vVInOe3De6DSpaEiqjKcJw4F7XUet24Wupahj9J6PlR09JqJ5ySDHw==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/fake-timers/-/fake-timers-29.7.0.tgz", + "integrity": "sha512-q4DH1Ha4TTFPdxLsqDXK1d3+ioSL7yL5oCMJZgDYm6i+6CygW5E5xVr/D1HdsGxjt1ZWSfUAs9OxSB/BNelWrQ==", "dev": true, "dependencies": { "@jest/types": "^29.6.3", "@sinonjs/fake-timers": "^10.0.2", "@types/node": "*", - "jest-message-util": "^29.6.3", - "jest-mock": "^29.6.3", - "jest-util": "^29.6.3" + "jest-message-util": "^29.7.0", + "jest-mock": "^29.7.0", + "jest-util": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, "node_modules/@jest/globals": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/globals/-/globals-29.6.4.tgz", - "integrity": "sha512-wVIn5bdtjlChhXAzVXavcY/3PEjf4VqM174BM3eGL5kMxLiZD5CLnbmkEyA1Dwh9q8XjP6E8RwjBsY/iCWrWsA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/globals/-/globals-29.7.0.tgz", + "integrity": "sha512-mpiz3dutLbkW2MNFubUGUEVLkTGiqW6yLVTA+JbP6fI6J5iL9Y0Nlg8k95pcF8ctKwCS7WVxteBs29hhfAotzQ==", "dev": true, "dependencies": { - "@jest/environment": "^29.6.4", - "@jest/expect": "^29.6.4", + "@jest/environment": "^29.7.0", + "@jest/expect": "^29.7.0", "@jest/types": "^29.6.3", - "jest-mock": "^29.6.3" + "jest-mock": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, "node_modules/@jest/reporters": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/reporters/-/reporters-29.6.4.tgz", - "integrity": "sha512-sxUjWxm7QdchdrD3NfWKrL8FBsortZeibSJv4XLjESOOjSUOkjQcb0ZHJwfhEGIvBvTluTzfG2yZWZhkrXJu8g==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/reporters/-/reporters-29.7.0.tgz", + "integrity": "sha512-DApq0KJbJOEzAFYjHADNNxAE3KbhxQB1y5Kplb5Waqw6zVbuWatSnMjE5gs8FUgEPmNsnZA3NCWl9NG0ia04Pg==", "dev": true, "dependencies": { "@bcoe/v8-coverage": "^0.2.3", - "@jest/console": "^29.6.4", - "@jest/test-result": "^29.6.4", - "@jest/transform": "^29.6.4", + "@jest/console": "^29.7.0", + "@jest/test-result": "^29.7.0", + "@jest/transform": "^29.7.0", "@jest/types": "^29.6.3", "@jridgewell/trace-mapping": "^0.3.18", "@types/node": "*", @@ -998,9 +999,9 @@ "istanbul-lib-report": "^3.0.0", "istanbul-lib-source-maps": "^4.0.0", "istanbul-reports": "^3.1.3", - "jest-message-util": "^29.6.3", - "jest-util": "^29.6.3", - "jest-worker": "^29.6.4", + "jest-message-util": "^29.7.0", + "jest-util": "^29.7.0", + "jest-worker": "^29.7.0", "slash": "^3.0.0", "string-length": "^4.0.1", "strip-ansi": "^6.0.0", @@ -1157,12 +1158,12 @@ } }, "node_modules/@jest/test-result": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/test-result/-/test-result-29.6.4.tgz", - "integrity": "sha512-uQ1C0AUEN90/dsyEirgMLlouROgSY+Wc/JanVVk0OiUKa5UFh7sJpMEM3aoUBAz2BRNvUJ8j3d294WFuRxSyOQ==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/test-result/-/test-result-29.7.0.tgz", + "integrity": "sha512-Fdx+tv6x1zlkJPcWXmMDAG2HBnaR9XPSd5aDWQVsfrZmLVT3lU1cwyxLgRmXR9yrq4NBoEm9BMsfgFzTQAbJYA==", "dev": true, "dependencies": { - "@jest/console": "^29.6.4", + "@jest/console": "^29.7.0", "@jest/types": "^29.6.3", "@types/istanbul-lib-coverage": "^2.0.0", "collect-v8-coverage": "^1.0.0" @@ -1172,14 +1173,14 @@ } }, "node_modules/@jest/test-sequencer": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/test-sequencer/-/test-sequencer-29.6.4.tgz", - "integrity": "sha512-E84M6LbpcRq3fT4ckfKs9ryVanwkaIB0Ws9bw3/yP4seRLg/VaCZ/LgW0MCq5wwk4/iP/qnilD41aj2fsw2RMg==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/test-sequencer/-/test-sequencer-29.7.0.tgz", + "integrity": "sha512-GQwJ5WZVrKnOJuiYiAF52UNUJXgTZx1NHjFSEB0qEMmSZKAkdMoIzw/Cj6x6NF4AvV23AUqDpFzQkN/eYCYTxw==", "dev": true, "dependencies": { - "@jest/test-result": "^29.6.4", + "@jest/test-result": "^29.7.0", "graceful-fs": "^4.2.9", - "jest-haste-map": "^29.6.4", + "jest-haste-map": "^29.7.0", "slash": "^3.0.0" }, "engines": { @@ -1187,9 +1188,9 @@ } }, "node_modules/@jest/transform": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/@jest/transform/-/transform-29.6.4.tgz", - "integrity": "sha512-8thgRSiXUqtr/pPGY/OsyHuMjGyhVnWrFAwoxmIemlBuiMyU1WFs0tXoNxzcr4A4uErs/ABre76SGmrr5ab/AA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/@jest/transform/-/transform-29.7.0.tgz", + "integrity": "sha512-ok/BTPFzFKVMwO5eOHRrvnBVHdRy9IrsrW1GpMaQ9MCnilNLXQKmAX8s1YXDFaai9xJpac2ySzV0YeRRECr2Vw==", "dev": true, "dependencies": { "@babel/core": "^7.11.6", @@ -1200,9 +1201,9 @@ "convert-source-map": "^2.0.0", "fast-json-stable-stringify": "^2.1.0", "graceful-fs": "^4.2.9", - "jest-haste-map": "^29.6.4", + "jest-haste-map": "^29.7.0", "jest-regex-util": "^29.6.3", - "jest-util": "^29.6.3", + "jest-util": "^29.7.0", "micromatch": "^4.0.4", "pirates": "^4.0.4", "slash": "^3.0.0", @@ -1420,9 +1421,9 @@ } }, "node_modules/@types/babel__core": { - "version": "7.20.1", - "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.1.tgz", - "integrity": "sha512-aACu/U/omhdk15O4Nfb+fHgH/z3QsfQzpnvRZhYhThms83ZnAOZz7zZAWO7mn2yyNQaA4xTO8GLK3uqFU4bYYw==", + "version": "7.20.2", + "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.2.tgz", + "integrity": "sha512-pNpr1T1xLUc2l3xJKuPtsEky3ybxN3m4fJkknfIpTCTfIZCDW57oAg+EfCgIIp2rvCe0Wn++/FfodDS4YXxBwA==", "dev": true, "dependencies": { "@babel/parser": "^7.20.7", @@ -1433,18 +1434,18 @@ } }, "node_modules/@types/babel__generator": { - "version": "7.6.4", - "resolved": "https://registry.npmjs.org/@types/babel__generator/-/babel__generator-7.6.4.tgz", - "integrity": "sha512-tFkciB9j2K755yrTALxD44McOrk+gfpIpvC3sxHjRawj6PfnQxrse4Clq5y/Rq+G3mrBurMax/lG8Qn2t9mSsg==", + "version": "7.6.5", + "resolved": "https://registry.npmjs.org/@types/babel__generator/-/babel__generator-7.6.5.tgz", + "integrity": "sha512-h9yIuWbJKdOPLJTbmSpPzkF67e659PbQDba7ifWm5BJ8xTv+sDmS7rFmywkWOvXedGTivCdeGSIIX8WLcRTz8w==", "dev": true, "dependencies": { "@babel/types": "^7.0.0" } }, "node_modules/@types/babel__template": { - "version": "7.4.1", - "resolved": "https://registry.npmjs.org/@types/babel__template/-/babel__template-7.4.1.tgz", - "integrity": "sha512-azBFKemX6kMg5Io+/rdGT0dkGreboUVR0Cdm3fz9QJWpaQGJRQXl7C+6hOTCZcMll7KFyEQpgbYI2lHdsS4U7g==", + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/@types/babel__template/-/babel__template-7.4.2.tgz", + "integrity": "sha512-/AVzPICMhMOMYoSx9MoKpGDKdBRsIXMNByh1PXSZoa+v6ZoLa8xxtsT/uLQ/NJm0XVAWl/BvId4MlDeXJaeIZQ==", "dev": true, "dependencies": { "@babel/parser": "^7.1.0", @@ -1452,9 +1453,9 @@ } }, "node_modules/@types/babel__traverse": { - "version": "7.20.1", - "resolved": "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.20.1.tgz", - "integrity": "sha512-MitHFXnhtgwsGZWtT68URpOvLN4EREih1u3QtQiN4VdAxWKRVvGCSvw/Qth0M0Qq3pJpnGOu5JaM/ydK7OGbqg==", + "version": "7.20.2", + "resolved": "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.20.2.tgz", + "integrity": "sha512-ojlGK1Hsfce93J0+kn3H5R73elidKUaZonirN33GSmgTUMpzI/MIFfSpF3haANe3G1bEBS9/9/QEqwTzwqFsKw==", "dev": true, "dependencies": { "@babel/types": "^7.20.7" @@ -1520,9 +1521,9 @@ } }, "node_modules/@types/jest": { - "version": "29.5.4", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.4.tgz", - "integrity": "sha512-PhglGmhWeD46FYOVLt3X7TiWjzwuVGW9wG/4qocPevXMjCmrIc5b6db9WjeGE4QYVpUAWMDv3v0IiBwObY289A==", + "version": "29.5.5", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.5.tgz", + "integrity": "sha512-ebylz2hnsWR9mYvmBFbXJXr+33UPc4+ZdxyDXh5w0FlPBTfCVN3wPL+kuOiQt3xvrK419v7XWeAs+AeOksafXg==", "dev": true, "dependencies": { "expect": "^29.0.0", @@ -1530,15 +1531,15 @@ } }, "node_modules/@types/json-schema": { - "version": "7.0.12", - "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz", - "integrity": "sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==", + "version": "7.0.13", + "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.13.tgz", + "integrity": "sha512-RbSSoHliUbnXj3ny0CNFOoxrIDV6SUGyStHsvDqosw6CkdPV8TtWGlfecuK4ToyMEAql6pzNxgCFKanovUzlgQ==", "dev": true }, "node_modules/@types/node": { - "version": "20.6.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.0.tgz", - "integrity": "sha512-najjVq5KN2vsH2U/xyh2opaSEz6cZMR2SetLIlxlj08nOcmPOemJmUK2o4kUzfLqfrWE0PIrNeE16XhYDd3nqg==", + "version": "20.6.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.2.tgz", + "integrity": "sha512-Y+/1vGBHV/cYk6OI1Na/LHzwnlNCAfU3ZNGrc1LdRe/LAIbdDPTTv/HU3M7yXN448aTVDq3eKRm2cg7iKLb8gw==", "dev": true }, "node_modules/@types/stack-utils": { @@ -1905,22 +1906,21 @@ } }, "node_modules/avl-tree-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.21.3.tgz", - "integrity": "sha512-r+CyVvaTy89z7THAePFsHFU1CbqU+mW66iAcBhl+ZBkn2aHLEksy+xEi7H7Axvq9Z7WEPiIDkwjDb+9gl6lrXg==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.3.3.tgz", + "integrity": "sha512-1lrKBu9QTsikYI4BsVmSguj4DzUwVRQDFpIEbzMVMcBmvXJJ2QXoXu+KHagBEMl+U9JRykKa/kT2jAjydH6VCQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/babel-jest": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.6.4.tgz", - "integrity": "sha512-meLj23UlSLddj6PC+YTOFRgDAtjnZom8w/ACsrx0gtPtv5cJZk0A5Unk5bV4wixD7XaPCN1fQvpww8czkZURmw==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz", + "integrity": "sha512-BrvGY3xZSwEcCzKvKsCi2GgHqDqsYkOP4/by5xCgIwGXQxIEh+8ew3gmrE1y7XRR6LHZIj6yLYnUi/mm2KXKBg==", "dev": true, "dependencies": { - "@jest/transform": "^29.6.4", + "@jest/transform": "^29.7.0", "@types/babel__core": "^7.1.14", "babel-plugin-istanbul": "^6.1.1", "babel-preset-jest": "^29.6.3", @@ -2086,13 +2086,12 @@ "dev": true }, "node_modules/binary-tree-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.21.3.tgz", - "integrity": "sha512-bkYpnvJ23WBOjBoZ8X7D/XKrYmIwMUa8Yqg/jPc8g+PT44aMBRk10nRrzjQjDDwHdGVyd3KOpwYOXFo/gjeH4A==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.3.3.tgz", + "integrity": "sha512-swfvq/xhIij2SUT6VnNVn0/+3ytf84MS1FhPlxYt0AOMG2cnc0YZQFtK+jKFh9VksNPQWBZgnGZY+m2ig0DqDA==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/brace-expansion": { @@ -2170,13 +2169,12 @@ } }, "node_modules/bst-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.21.3.tgz", - "integrity": "sha512-m1rIVcgzqwBAFfctlsouEElvmklHDK02i0g5QnzYMIPL94EHcFIRaqMFHwLDQAkynvo9zCLlxU0j9qzvHVRMGA==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.3.3.tgz", + "integrity": "sha512-PjoU2wIGwwLmuO6YYnT7KqU+F+rlSZqxke93XScTsNUsHs9M8KJTx066pGf1Z9rSmBvohjdjtKwkGgqDEbbM7Q==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/buffer-from": { @@ -2204,9 +2202,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001532", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001532.tgz", - "integrity": "sha512-FbDFnNat3nMnrROzqrsg314zhqN5LGQ1kyyMk2opcrwGbVGpHRhgCWtAgD5YJUqNAiQ+dklreil/c3Qf1dfCTw==", + "version": "1.0.30001538", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001538.tgz", + "integrity": "sha512-HWJnhnID+0YMtGlzcp3T9drmBJUVDchPJ08tpUGFLs9CYlwWPH2uLgpHn8fND5pCgXVtnGS3H4QR9XLMHVNkHw==", "dev": true, "funding": [ { @@ -2451,6 +2449,76 @@ "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", "dev": true }, + "node_modules/create-jest": { + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/create-jest/-/create-jest-29.7.0.tgz", + "integrity": "sha512-Adz2bdH0Vq3F53KEMJOoftQFutWCukm6J24wbPWRO4k1kMY7gS7ds/uoJkNuV8wDCtWWnuwGcJwpWcih+zEW1Q==", + "dev": true, + "dependencies": { + "@jest/types": "^29.6.3", + "chalk": "^4.0.0", + "exit": "^0.1.2", + "graceful-fs": "^4.2.9", + "jest-config": "^29.7.0", + "jest-util": "^29.7.0", + "prompts": "^2.0.1" + }, + "bin": { + "create-jest": "bin/create-jest.js" + }, + "engines": { + "node": "^14.15.0 || ^16.10.0 || >=18.0.0" + } + }, + "node_modules/create-jest/node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/create-jest/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/create-jest/node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/create-jest/node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -2466,9 +2534,9 @@ } }, "node_modules/data-structure-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.21.3.tgz", - "integrity": "sha512-1HRQH46pcgMfyIMMrucw1L1JR4MwIvSq0qKqLdeJEV3qvwbi3SN0dXjaIQzpO9lYaHDgrB42rIxNgQ+ak74scA==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.3.3.tgz", + "integrity": "sha512-cQNIBSPeFVpjgBPWPDdEvOeWbsH6DurH9+EurN0EE8bJtK0zUhcTkgB7/vGmJldXykRS6KITxAqbwdcewcP12w==", "dev": true }, "node_modules/debug": { @@ -2557,13 +2625,12 @@ } }, "node_modules/deque-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/deque-typed/-/deque-typed-1.21.3.tgz", - "integrity": "sha512-GE4+pM/t0VjcNa1Ei8wQE5rq1giCYWWLmsg2QYTJ6zbqRhU0axQZddAruhY9RLvlW0XXMtRhmC77LvLoYXjNxw==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/deque-typed/-/deque-typed-1.3.3.tgz", + "integrity": "sha512-7g1Hh9v7rCZnnTJEem7916SYl8ofusczzo4UIWxUKjCRki7e1SG2fcWx1o3S7YCs6+6lNAPjHK796DHmpvYdDQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/detect-newline": { @@ -2585,23 +2652,21 @@ } }, "node_modules/directed-graph-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/directed-graph-typed/-/directed-graph-typed-1.21.3.tgz", - "integrity": "sha512-MFcY1Py97Aj3r+KburlKWjHsecyZDNZxag+Buuyz2cdHFbInBGdcJ88qRIFEmJA6MGM8wBy9mSPKkEsstN9JtQ==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/directed-graph-typed/-/directed-graph-typed-1.3.3.tgz", + "integrity": "sha512-3dwjBo1tyO3mMR7e6hK9wcobi/uuOLbxF2Dq9KohbFPLNwtE5s9VqZaAy4HosV0PfdJv9r+tSXW9DqvwR+KqIw==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/doubly-linked-list-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/doubly-linked-list-typed/-/doubly-linked-list-typed-1.21.3.tgz", - "integrity": "sha512-jbeAv/VoM8/fmUx9dKhPgK9thCsuuqqvFMe3TFhTMFU/MHboRLTJ0el+1oeM+g5GoTaaaqRZUe9Lr9vNTJYJqQ==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/doubly-linked-list-typed/-/doubly-linked-list-typed-1.3.3.tgz", + "integrity": "sha512-sxLEEJj4C5jJxe8e58RtZHpcCcPij6anubm6F/k7v7NFa2rnfVJuDGRSsNRbgKDzymHGsZ38jEp8bcTNI9e+Mg==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/eastasianwidth": { @@ -2611,9 +2676,9 @@ "dev": true }, "node_modules/electron-to-chromium": { - "version": "1.4.513", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.513.tgz", - "integrity": "sha512-cOB0xcInjm+E5qIssHeXJ29BaUyWpMyFKT5RB3bsLENDheCja0wMkHJyiPl0NBE/VzDI7JDuNEQWhe6RitEUcw==", + "version": "1.4.523", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.523.tgz", + "integrity": "sha512-9AreocSUWnzNtvLcbpng6N+GkXnCcBR80IQkxRC9Dfdyg4gaWNUPBujAHUpKkiUkoSoR9UlhA4zD/IgBklmhzg==", "dev": true }, "node_modules/emittery": { @@ -2669,9 +2734,9 @@ } }, "node_modules/es-module-lexer": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.3.0.tgz", - "integrity": "sha512-vZK7T0N2CBmBOixhmjdqx2gWVbFZ4DXZ/NyRMZVlJXPa7CyFS+/a4QQsDGDQy9ZfEzxFuNEsMLeQJnKP2p5/JA==", + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.3.1.tgz", + "integrity": "sha512-JUFAyicQV9mXc3YRxPnDlrfBKpqt6hUYzz9/boprUJHs4e4KVr3XwOF70doO6gwXUor6EWZJAyWAfKki84t20Q==", "dev": true }, "node_modules/escalade": { @@ -2799,16 +2864,16 @@ } }, "node_modules/expect": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/expect/-/expect-29.6.4.tgz", - "integrity": "sha512-F2W2UyQ8XYyftHT57dtfg8Ue3X5qLgm2sSug0ivvLRH/VKNRL/pDxg/TH7zVzbQB0tu80clNFy6LU7OS/VSEKA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/expect/-/expect-29.7.0.tgz", + "integrity": "sha512-2Zks0hf1VLFYI1kbh0I5jP3KHHyCHpkfyHBzsSXRFgl/Bg9mWYfMW8oD+PdMPlEwy5HNsR9JutYy6pMeOh61nw==", "dev": true, "dependencies": { - "@jest/expect-utils": "^29.6.4", + "@jest/expect-utils": "^29.7.0", "jest-get-type": "^29.6.3", - "jest-matcher-utils": "^29.6.4", - "jest-message-util": "^29.6.3", - "jest-util": "^29.6.3" + "jest-matcher-utils": "^29.7.0", + "jest-message-util": "^29.7.0", + "jest-util": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -3025,13 +3090,12 @@ "dev": true }, "node_modules/graph-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/graph-typed/-/graph-typed-1.21.3.tgz", - "integrity": "sha512-RLSgAY4IyEgkdXKXfwh886KomgwLeLO4vgp6JHjv5kwZLNIZ3nFOsOJCEjx+tWC6sKGnxIdlXhzUrSALMjclLg==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/graph-typed/-/graph-typed-1.3.3.tgz", + "integrity": "sha512-wJvbk4kFCEkCYs6lJ1QjEUT5GsZXBbn7qaA4Qwt4c/HTEJbX6X63fYkomAf8kqoSQuTlXr+yk8WfZJYWDiJULg==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/has": { @@ -3056,13 +3120,12 @@ } }, "node_modules/heap-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.21.3.tgz", - "integrity": "sha512-7lqPbHfEuKxLSnPHHb9PMmVLGEf1d/ly0vZ/KLoAQef3z2cLS3A5xg2HU2J1R9wySNjT39x1qN7tOUrokYqwmg==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.3.3.tgz", + "integrity": "sha512-j0tnpR+v2ul3bHM3VoRSY6b8M73FaSzmbZNwSNxhJ7YRt/isLF1V7LOMZeFeQUj+fO902f0IZqqGanEFRux/uA==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/html-escaper": { @@ -3369,15 +3432,15 @@ } }, "node_modules/jest": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest/-/jest-29.6.4.tgz", - "integrity": "sha512-tEFhVQFF/bzoYV1YuGyzLPZ6vlPrdfvDmmAxudA1dLEuiztqg2Rkx20vkKY32xiDROcD2KXlgZ7Cu8RPeEHRKw==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest/-/jest-29.7.0.tgz", + "integrity": "sha512-NIy3oAFp9shda19hy4HK0HRTWKtPJmGdnvywu01nOqNC2vZg+Z+fvJDxpMQA88eb2I9EcafcdjYgsDthnYTvGw==", "dev": true, "dependencies": { - "@jest/core": "^29.6.4", + "@jest/core": "^29.7.0", "@jest/types": "^29.6.3", "import-local": "^3.0.2", - "jest-cli": "^29.6.4" + "jest-cli": "^29.7.0" }, "bin": { "jest": "bin/jest.js" @@ -3395,13 +3458,13 @@ } }, "node_modules/jest-changed-files": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/jest-changed-files/-/jest-changed-files-29.6.3.tgz", - "integrity": "sha512-G5wDnElqLa4/c66ma5PG9eRjE342lIbF6SUnTJi26C3J28Fv2TVY2rOyKB9YGbSA5ogwevgmxc4j4aVjrEK6Yg==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-changed-files/-/jest-changed-files-29.7.0.tgz", + "integrity": "sha512-fEArFiwf1BpQ+4bXSprcDc3/x4HSzL4al2tozwVpDFpsxALjLYdyiIK4e5Vz66GQJIbXJ82+35PtysofptNX2w==", "dev": true, "dependencies": { "execa": "^5.0.0", - "jest-util": "^29.6.3", + "jest-util": "^29.7.0", "p-limit": "^3.1.0" }, "engines": { @@ -3409,28 +3472,28 @@ } }, "node_modules/jest-circus": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-circus/-/jest-circus-29.6.4.tgz", - "integrity": "sha512-YXNrRyntVUgDfZbjXWBMPslX1mQ8MrSG0oM/Y06j9EYubODIyHWP8hMUbjbZ19M3M+zamqEur7O80HODwACoJw==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-circus/-/jest-circus-29.7.0.tgz", + "integrity": "sha512-3E1nCMgipcTkCocFwM90XXQab9bS+GMsjdpmPrlelaxwD93Ad8iVEjX/vvHPdLPnFf+L40u+5+iutRdA1N9myw==", "dev": true, "dependencies": { - "@jest/environment": "^29.6.4", - "@jest/expect": "^29.6.4", - "@jest/test-result": "^29.6.4", + "@jest/environment": "^29.7.0", + "@jest/expect": "^29.7.0", + "@jest/test-result": "^29.7.0", "@jest/types": "^29.6.3", "@types/node": "*", "chalk": "^4.0.0", "co": "^4.6.0", "dedent": "^1.0.0", "is-generator-fn": "^2.0.0", - "jest-each": "^29.6.3", - "jest-matcher-utils": "^29.6.4", - "jest-message-util": "^29.6.3", - "jest-runtime": "^29.6.4", - "jest-snapshot": "^29.6.4", - "jest-util": "^29.6.3", + "jest-each": "^29.7.0", + "jest-matcher-utils": "^29.7.0", + "jest-message-util": "^29.7.0", + "jest-runtime": "^29.7.0", + "jest-snapshot": "^29.7.0", + "jest-util": "^29.7.0", "p-limit": "^3.1.0", - "pretty-format": "^29.6.3", + "pretty-format": "^29.7.0", "pure-rand": "^6.0.0", "slash": "^3.0.0", "stack-utils": "^2.0.3" @@ -3489,22 +3552,21 @@ "dev": true }, "node_modules/jest-cli": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-cli/-/jest-cli-29.6.4.tgz", - "integrity": "sha512-+uMCQ7oizMmh8ZwRfZzKIEszFY9ksjjEQnTEMTaL7fYiL3Kw4XhqT9bYh+A4DQKUb67hZn2KbtEnDuHvcgK4pQ==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-cli/-/jest-cli-29.7.0.tgz", + "integrity": "sha512-OVVobw2IubN/GSYsxETi+gOe7Ka59EFMR/twOU3Jb2GnKKeMGJB5SGUUrEz3SFVmJASUdZUzy83sLNNQ2gZslg==", "dev": true, "dependencies": { - "@jest/core": "^29.6.4", - "@jest/test-result": "^29.6.4", + "@jest/core": "^29.7.0", + "@jest/test-result": "^29.7.0", "@jest/types": "^29.6.3", "chalk": "^4.0.0", + "create-jest": "^29.7.0", "exit": "^0.1.2", - "graceful-fs": "^4.2.9", "import-local": "^3.0.2", - "jest-config": "^29.6.4", - "jest-util": "^29.6.3", - "jest-validate": "^29.6.3", - "prompts": "^2.0.1", + "jest-config": "^29.7.0", + "jest-util": "^29.7.0", + "jest-validate": "^29.7.0", "yargs": "^17.3.1" }, "bin": { @@ -3572,31 +3634,31 @@ "dev": true }, "node_modules/jest-config": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-config/-/jest-config-29.6.4.tgz", - "integrity": "sha512-JWohr3i9m2cVpBumQFv2akMEnFEPVOh+9L2xIBJhJ0zOaci2ZXuKJj0tgMKQCBZAKA09H049IR4HVS/43Qb19A==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-config/-/jest-config-29.7.0.tgz", + "integrity": "sha512-uXbpfeQ7R6TZBqI3/TxCU4q4ttk3u0PJeC+E0zbfSoSjq6bJ7buBPxzQPL0ifrkY4DNu4JUdk0ImlBUYi840eQ==", "dev": true, "dependencies": { "@babel/core": "^7.11.6", - "@jest/test-sequencer": "^29.6.4", + "@jest/test-sequencer": "^29.7.0", "@jest/types": "^29.6.3", - "babel-jest": "^29.6.4", + "babel-jest": "^29.7.0", "chalk": "^4.0.0", "ci-info": "^3.2.0", "deepmerge": "^4.2.2", "glob": "^7.1.3", "graceful-fs": "^4.2.9", - "jest-circus": "^29.6.4", - "jest-environment-node": "^29.6.4", + "jest-circus": "^29.7.0", + "jest-environment-node": "^29.7.0", "jest-get-type": "^29.6.3", "jest-regex-util": "^29.6.3", - "jest-resolve": "^29.6.4", - "jest-runner": "^29.6.4", - "jest-util": "^29.6.3", - "jest-validate": "^29.6.3", + "jest-resolve": "^29.7.0", + "jest-runner": "^29.7.0", + "jest-util": "^29.7.0", + "jest-validate": "^29.7.0", "micromatch": "^4.0.4", "parse-json": "^5.2.0", - "pretty-format": "^29.6.3", + "pretty-format": "^29.7.0", "slash": "^3.0.0", "strip-json-comments": "^3.1.1" }, @@ -3708,15 +3770,15 @@ } }, "node_modules/jest-diff": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-diff/-/jest-diff-29.6.4.tgz", - "integrity": "sha512-9F48UxR9e4XOEZvoUXEHSWY4qC4zERJaOfrbBg9JpbJOO43R1vN76REt/aMGZoY6GD5g84nnJiBIVlscegefpw==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-diff/-/jest-diff-29.7.0.tgz", + "integrity": "sha512-LMIgiIrhigmPrs03JHpxUh2yISK3vLFPkAodPeo0+BuF7wA2FoQbkEg1u8gBYBThncu7e1oEDUfIXVuTqLRUjw==", "dev": true, "dependencies": { "chalk": "^4.0.0", "diff-sequences": "^29.6.3", "jest-get-type": "^29.6.3", - "pretty-format": "^29.6.3" + "pretty-format": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -3772,9 +3834,9 @@ "dev": true }, "node_modules/jest-docblock": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/jest-docblock/-/jest-docblock-29.6.3.tgz", - "integrity": "sha512-2+H+GOTQBEm2+qFSQ7Ma+BvyV+waiIFxmZF5LdpBsAEjWX8QYjSCa4FrkIYtbfXUJJJnFCYrOtt6TZ+IAiTjBQ==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-docblock/-/jest-docblock-29.7.0.tgz", + "integrity": "sha512-q617Auw3A612guyaFgsbFeYpNP5t2aoUNLwBUbc/0kD1R4t9ixDbyFTHd1nok4epoVFpr7PmeWHrhvuV3XaJ4g==", "dev": true, "dependencies": { "detect-newline": "^3.0.0" @@ -3784,16 +3846,16 @@ } }, "node_modules/jest-each": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/jest-each/-/jest-each-29.6.3.tgz", - "integrity": "sha512-KoXfJ42k8cqbkfshW7sSHcdfnv5agDdHCPA87ZBdmHP+zJstTJc0ttQaJ/x7zK6noAL76hOuTIJ6ZkQRS5dcyg==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-each/-/jest-each-29.7.0.tgz", + "integrity": "sha512-gns+Er14+ZrEoC5fhOfYCY1LOHHr0TI+rQUHZS8Ttw2l7gl+80eHc/gFf2Ktkw0+SIACDTeWvpFcv3B04VembQ==", "dev": true, "dependencies": { "@jest/types": "^29.6.3", "chalk": "^4.0.0", "jest-get-type": "^29.6.3", - "jest-util": "^29.6.3", - "pretty-format": "^29.6.3" + "jest-util": "^29.7.0", + "pretty-format": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -3849,17 +3911,17 @@ "dev": true }, "node_modules/jest-environment-node": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-environment-node/-/jest-environment-node-29.6.4.tgz", - "integrity": "sha512-i7SbpH2dEIFGNmxGCpSc2w9cA4qVD+wfvg2ZnfQ7XVrKL0NA5uDVBIiGH8SR4F0dKEv/0qI5r+aDomDf04DpEQ==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-environment-node/-/jest-environment-node-29.7.0.tgz", + "integrity": "sha512-DOSwCRqXirTOyheM+4d5YZOrWcdu0LNZ87ewUoywbcb2XR4wKgqiG8vNeYwhjFMbEkfju7wx2GYH0P2gevGvFw==", "dev": true, "dependencies": { - "@jest/environment": "^29.6.4", - "@jest/fake-timers": "^29.6.4", + "@jest/environment": "^29.7.0", + "@jest/fake-timers": "^29.7.0", "@jest/types": "^29.6.3", "@types/node": "*", - "jest-mock": "^29.6.3", - "jest-util": "^29.6.3" + "jest-mock": "^29.7.0", + "jest-util": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -3875,9 +3937,9 @@ } }, "node_modules/jest-haste-map": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-haste-map/-/jest-haste-map-29.6.4.tgz", - "integrity": "sha512-12Ad+VNTDHxKf7k+M65sviyynRoZYuL1/GTuhEVb8RYsNSNln71nANRb/faSyWvx0j+gHcivChXHIoMJrGYjog==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-haste-map/-/jest-haste-map-29.7.0.tgz", + "integrity": "sha512-fP8u2pyfqx0K1rGn1R9pyE0/KTn+G7PxktWidOBTqFPLYX0b9ksaMFkhK5vrS3DVun09pckLdlx90QthlW7AmA==", "dev": true, "dependencies": { "@jest/types": "^29.6.3", @@ -3887,8 +3949,8 @@ "fb-watchman": "^2.0.0", "graceful-fs": "^4.2.9", "jest-regex-util": "^29.6.3", - "jest-util": "^29.6.3", - "jest-worker": "^29.6.4", + "jest-util": "^29.7.0", + "jest-worker": "^29.7.0", "micromatch": "^4.0.4", "walker": "^1.0.8" }, @@ -3900,28 +3962,28 @@ } }, "node_modules/jest-leak-detector": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/jest-leak-detector/-/jest-leak-detector-29.6.3.tgz", - "integrity": "sha512-0kfbESIHXYdhAdpLsW7xdwmYhLf1BRu4AA118/OxFm0Ho1b2RcTmO4oF6aAMaxpxdxnJ3zve2rgwzNBD4Zbm7Q==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-leak-detector/-/jest-leak-detector-29.7.0.tgz", + "integrity": "sha512-kYA8IJcSYtST2BY9I+SMC32nDpBT3J2NvWJx8+JCuCdl/CR1I4EKUJROiP8XtCcxqgTTBGJNdbB1A8XRKbTetw==", "dev": true, "dependencies": { "jest-get-type": "^29.6.3", - "pretty-format": "^29.6.3" + "pretty-format": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, "node_modules/jest-matcher-utils": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-matcher-utils/-/jest-matcher-utils-29.6.4.tgz", - "integrity": "sha512-KSzwyzGvK4HcfnserYqJHYi7sZVqdREJ9DMPAKVbS98JsIAvumihaNUbjrWw0St7p9IY7A9UskCW5MYlGmBQFQ==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-matcher-utils/-/jest-matcher-utils-29.7.0.tgz", + "integrity": "sha512-sBkD+Xi9DtcChsI3L3u0+N0opgPYnCRPtGcQYrgXmR+hmt/fYfWAL0xRXYU8eWOdfuLgBe0YCW3AFtnRLagq/g==", "dev": true, "dependencies": { "chalk": "^4.0.0", - "jest-diff": "^29.6.4", + "jest-diff": "^29.7.0", "jest-get-type": "^29.6.3", - "pretty-format": "^29.6.3" + "pretty-format": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -3977,9 +4039,9 @@ "dev": true }, "node_modules/jest-message-util": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/jest-message-util/-/jest-message-util-29.6.3.tgz", - "integrity": "sha512-FtzaEEHzjDpQp51HX4UMkPZjy46ati4T5pEMyM6Ik48ztu4T9LQplZ6OsimHx7EuM9dfEh5HJa6D3trEftu3dA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-message-util/-/jest-message-util-29.7.0.tgz", + "integrity": "sha512-GBEV4GRADeP+qtB2+6u61stea8mGcOT4mCtrYISZwfu9/ISHFJ/5zOMXYbpBE9RsS5+Gb63DW4FgmnKJ79Kf6w==", "dev": true, "dependencies": { "@babel/code-frame": "^7.12.13", @@ -3988,7 +4050,7 @@ "chalk": "^4.0.0", "graceful-fs": "^4.2.9", "micromatch": "^4.0.4", - "pretty-format": "^29.6.3", + "pretty-format": "^29.7.0", "slash": "^3.0.0", "stack-utils": "^2.0.3" }, @@ -4046,14 +4108,14 @@ "dev": true }, "node_modules/jest-mock": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-29.6.3.tgz", - "integrity": "sha512-Z7Gs/mOyTSR4yPsaZ72a/MtuK6RnC3JYqWONe48oLaoEcYwEDxqvbXz85G4SJrm2Z5Ar9zp6MiHF4AlFlRM4Pg==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-29.7.0.tgz", + "integrity": "sha512-ITOMZn+UkYS4ZFh83xYAOzWStloNzJFO2s8DWrE4lhtGD+AorgnbkiKERe4wQVBydIGPx059g6riW5Btp6Llnw==", "dev": true, "dependencies": { "@jest/types": "^29.6.3", "@types/node": "*", - "jest-util": "^29.6.3" + "jest-util": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -4086,17 +4148,17 @@ } }, "node_modules/jest-resolve": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-resolve/-/jest-resolve-29.6.4.tgz", - "integrity": "sha512-fPRq+0vcxsuGlG0O3gyoqGTAxasagOxEuyoxHeyxaZbc9QNek0AmJWSkhjlMG+mTsj+8knc/mWb3fXlRNVih7Q==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-resolve/-/jest-resolve-29.7.0.tgz", + "integrity": "sha512-IOVhZSrg+UvVAshDSDtHyFCCBUl/Q3AAJv8iZ6ZjnZ74xzvwuzLXid9IIIPgTnY62SJjfuupMKZsZQRsCvxEgA==", "dev": true, "dependencies": { "chalk": "^4.0.0", "graceful-fs": "^4.2.9", - "jest-haste-map": "^29.6.4", + "jest-haste-map": "^29.7.0", "jest-pnp-resolver": "^1.2.2", - "jest-util": "^29.6.3", - "jest-validate": "^29.6.3", + "jest-util": "^29.7.0", + "jest-validate": "^29.7.0", "resolve": "^1.20.0", "resolve.exports": "^2.0.0", "slash": "^3.0.0" @@ -4106,13 +4168,13 @@ } }, "node_modules/jest-resolve-dependencies": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-resolve-dependencies/-/jest-resolve-dependencies-29.6.4.tgz", - "integrity": "sha512-7+6eAmr1ZBF3vOAJVsfLj1QdqeXG+WYhidfLHBRZqGN24MFRIiKG20ItpLw2qRAsW/D2ZUUmCNf6irUr/v6KHA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-resolve-dependencies/-/jest-resolve-dependencies-29.7.0.tgz", + "integrity": "sha512-un0zD/6qxJ+S0et7WxeI3H5XSe9lTBBR7bOHCHXkKR6luG5mwDDlIzVQ0V5cZCuoTgEdcdwzTghYkTWfubi+nA==", "dev": true, "dependencies": { "jest-regex-util": "^29.6.3", - "jest-snapshot": "^29.6.4" + "jest-snapshot": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -4168,30 +4230,30 @@ "dev": true }, "node_modules/jest-runner": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-runner/-/jest-runner-29.6.4.tgz", - "integrity": "sha512-SDaLrMmtVlQYDuG0iSPYLycG8P9jLI+fRm8AF/xPKhYDB2g6xDWjXBrR5M8gEWsK6KVFlebpZ4QsrxdyIX1Jaw==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-runner/-/jest-runner-29.7.0.tgz", + "integrity": "sha512-fsc4N6cPCAahybGBfTRcq5wFR6fpLznMg47sY5aDpsoejOcVYFb07AHuSnR0liMcPTgBsA3ZJL6kFOjPdoNipQ==", "dev": true, "dependencies": { - "@jest/console": "^29.6.4", - "@jest/environment": "^29.6.4", - "@jest/test-result": "^29.6.4", - "@jest/transform": "^29.6.4", + "@jest/console": "^29.7.0", + "@jest/environment": "^29.7.0", + "@jest/test-result": "^29.7.0", + "@jest/transform": "^29.7.0", "@jest/types": "^29.6.3", "@types/node": "*", "chalk": "^4.0.0", "emittery": "^0.13.1", "graceful-fs": "^4.2.9", - "jest-docblock": "^29.6.3", - "jest-environment-node": "^29.6.4", - "jest-haste-map": "^29.6.4", - "jest-leak-detector": "^29.6.3", - "jest-message-util": "^29.6.3", - "jest-resolve": "^29.6.4", - "jest-runtime": "^29.6.4", - "jest-util": "^29.6.3", - "jest-watcher": "^29.6.4", - "jest-worker": "^29.6.4", + "jest-docblock": "^29.7.0", + "jest-environment-node": "^29.7.0", + "jest-haste-map": "^29.7.0", + "jest-leak-detector": "^29.7.0", + "jest-message-util": "^29.7.0", + "jest-resolve": "^29.7.0", + "jest-runtime": "^29.7.0", + "jest-util": "^29.7.0", + "jest-watcher": "^29.7.0", + "jest-worker": "^29.7.0", "p-limit": "^3.1.0", "source-map-support": "0.5.13" }, @@ -4249,17 +4311,17 @@ "dev": true }, "node_modules/jest-runtime": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-runtime/-/jest-runtime-29.6.4.tgz", - "integrity": "sha512-s/QxMBLvmwLdchKEjcLfwzP7h+jsHvNEtxGP5P+Fl1FMaJX2jMiIqe4rJw4tFprzCwuSvVUo9bn0uj4gNRXsbA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-runtime/-/jest-runtime-29.7.0.tgz", + "integrity": "sha512-gUnLjgwdGqW7B4LvOIkbKs9WGbn+QLqRQQ9juC6HndeDiezIwhDP+mhMwHWCEcfQ5RUXa6OPnFF8BJh5xegwwQ==", "dev": true, "dependencies": { - "@jest/environment": "^29.6.4", - "@jest/fake-timers": "^29.6.4", - "@jest/globals": "^29.6.4", + "@jest/environment": "^29.7.0", + "@jest/fake-timers": "^29.7.0", + "@jest/globals": "^29.7.0", "@jest/source-map": "^29.6.3", - "@jest/test-result": "^29.6.4", - "@jest/transform": "^29.6.4", + "@jest/test-result": "^29.7.0", + "@jest/transform": "^29.7.0", "@jest/types": "^29.6.3", "@types/node": "*", "chalk": "^4.0.0", @@ -4267,13 +4329,13 @@ "collect-v8-coverage": "^1.0.0", "glob": "^7.1.3", "graceful-fs": "^4.2.9", - "jest-haste-map": "^29.6.4", - "jest-message-util": "^29.6.3", - "jest-mock": "^29.6.3", + "jest-haste-map": "^29.7.0", + "jest-message-util": "^29.7.0", + "jest-mock": "^29.7.0", "jest-regex-util": "^29.6.3", - "jest-resolve": "^29.6.4", - "jest-snapshot": "^29.6.4", - "jest-util": "^29.6.3", + "jest-resolve": "^29.7.0", + "jest-snapshot": "^29.7.0", + "jest-util": "^29.7.0", "slash": "^3.0.0", "strip-bom": "^4.0.0" }, @@ -4373,9 +4435,9 @@ } }, "node_modules/jest-snapshot": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-snapshot/-/jest-snapshot-29.6.4.tgz", - "integrity": "sha512-VC1N8ED7+4uboUKGIDsbvNAZb6LakgIPgAF4RSpF13dN6YaMokfRqO+BaqK4zIh6X3JffgwbzuGqDEjHm/MrvA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-snapshot/-/jest-snapshot-29.7.0.tgz", + "integrity": "sha512-Rm0BMWtxBcioHr1/OX5YCP8Uov4riHvKPknOGs804Zg9JGZgmIBkbtlxJC/7Z4msKYVbIJtfU+tKb8xlYNfdkw==", "dev": true, "dependencies": { "@babel/core": "^7.11.6", @@ -4383,20 +4445,20 @@ "@babel/plugin-syntax-jsx": "^7.7.2", "@babel/plugin-syntax-typescript": "^7.7.2", "@babel/types": "^7.3.3", - "@jest/expect-utils": "^29.6.4", - "@jest/transform": "^29.6.4", + "@jest/expect-utils": "^29.7.0", + "@jest/transform": "^29.7.0", "@jest/types": "^29.6.3", "babel-preset-current-node-syntax": "^1.0.0", "chalk": "^4.0.0", - "expect": "^29.6.4", + "expect": "^29.7.0", "graceful-fs": "^4.2.9", - "jest-diff": "^29.6.4", + "jest-diff": "^29.7.0", "jest-get-type": "^29.6.3", - "jest-matcher-utils": "^29.6.4", - "jest-message-util": "^29.6.3", - "jest-util": "^29.6.3", + "jest-matcher-utils": "^29.7.0", + "jest-message-util": "^29.7.0", + "jest-util": "^29.7.0", "natural-compare": "^1.4.0", - "pretty-format": "^29.6.3", + "pretty-format": "^29.7.0", "semver": "^7.5.3" }, "engines": { @@ -4453,9 +4515,9 @@ "dev": true }, "node_modules/jest-util": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-29.6.3.tgz", - "integrity": "sha512-QUjna/xSy4B32fzcKTSz1w7YYzgiHrjjJjevdRf61HYk998R5vVMMNmrHESYZVDS5DSWs+1srPLPKxXPkeSDOA==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-29.7.0.tgz", + "integrity": "sha512-z6EbKajIpqGKU56y5KBUgy1dt1ihhQJgWzUlZHArA/+X2ad7Cb5iF+AK1EWVL/Bo7Rz9uurpqw6SiBCefUbCGA==", "dev": true, "dependencies": { "@jest/types": "^29.6.3", @@ -4519,9 +4581,9 @@ "dev": true }, "node_modules/jest-validate": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/jest-validate/-/jest-validate-29.6.3.tgz", - "integrity": "sha512-e7KWZcAIX+2W1o3cHfnqpGajdCs1jSM3DkXjGeLSNmCazv1EeI1ggTeK5wdZhF+7N+g44JI2Od3veojoaumlfg==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-validate/-/jest-validate-29.7.0.tgz", + "integrity": "sha512-ZB7wHqaRGVw/9hST/OuFUReG7M8vKeq0/J2egIGLdvjHCmYqGARhzXmtgi+gVeZ5uXFF219aOc3Ls2yLg27tkw==", "dev": true, "dependencies": { "@jest/types": "^29.6.3", @@ -4529,7 +4591,7 @@ "chalk": "^4.0.0", "jest-get-type": "^29.6.3", "leven": "^3.1.0", - "pretty-format": "^29.6.3" + "pretty-format": "^29.7.0" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -4597,18 +4659,18 @@ "dev": true }, "node_modules/jest-watcher": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-watcher/-/jest-watcher-29.6.4.tgz", - "integrity": "sha512-oqUWvx6+On04ShsT00Ir9T4/FvBeEh2M9PTubgITPxDa739p4hoQweWPRGyYeaojgT0xTpZKF0Y/rSY1UgMxvQ==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-watcher/-/jest-watcher-29.7.0.tgz", + "integrity": "sha512-49Fg7WXkU3Vl2h6LbLtMQ/HyB6rXSIX7SqvBLQmssRBGN9I0PNvPmAmCWSOY6SOvrjhI/F7/bGAv9RtnsPA03g==", "dev": true, "dependencies": { - "@jest/test-result": "^29.6.4", + "@jest/test-result": "^29.7.0", "@jest/types": "^29.6.3", "@types/node": "*", "ansi-escapes": "^4.2.1", "chalk": "^4.0.0", "emittery": "^0.13.1", - "jest-util": "^29.6.3", + "jest-util": "^29.7.0", "string-length": "^4.0.1" }, "engines": { @@ -4665,13 +4727,13 @@ "dev": true }, "node_modules/jest-worker": { - "version": "29.6.4", - "resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-29.6.4.tgz", - "integrity": "sha512-6dpvFV4WjcWbDVGgHTWo/aupl8/LbBx2NSKfiwqf79xC/yeJjKHT1+StcKy/2KTmW16hE68ccKVOtXf+WZGz7Q==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-29.7.0.tgz", + "integrity": "sha512-eIz2msL/EzL9UFTFFx7jBTkeZfku0yUAyZZZmJ93H2TYEiroIx2PQjEXcwYtYl8zXCxb+PAmA2hLIt/6ZEkPHw==", "dev": true, "dependencies": { "@types/node": "*", - "jest-util": "^29.6.3", + "jest-util": "^29.7.0", "merge-stream": "^2.0.0", "supports-color": "^8.0.0" }, @@ -4789,13 +4851,12 @@ "dev": true }, "node_modules/linked-list-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/linked-list-typed/-/linked-list-typed-1.21.3.tgz", - "integrity": "sha512-cyHvhIs+rSgqIgcq3weE4ng4WRtDLE3wPm0wa21p1G6gLrtcnRfwWbnOwe68sD7z3st6CRRyiyS2128g9YDeDA==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/linked-list-typed/-/linked-list-typed-1.3.3.tgz", + "integrity": "sha512-mccqLmNbBG3xwT/nnFlO7vhgynQHzl+JOE7SOYlxeKaCJ5Nrh35w2gqCf9OV9f3TeouA2/CBwsViY01OFl52jA==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/loader-runner": { @@ -4889,23 +4950,21 @@ } }, "node_modules/max-heap-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/max-heap-typed/-/max-heap-typed-1.21.3.tgz", - "integrity": "sha512-F8DbeN0gaVqbIPfBBcVsA+yqvZAyR5WWb9Wjw+N1zaTyikOUsfHbZO+xUQXyAP3fG7bSlS9vr/C1Gf2BE+FcxQ==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/max-heap-typed/-/max-heap-typed-1.3.3.tgz", + "integrity": "sha512-yiMWyCcUa0aDWNEwE/7dlklphwTUvBrMITZQ3pH4sbrv8snpe+kO8M5EWWwPvLPaOiG1PKYSjH8pspE2xCCv6Q==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/max-priority-queue-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/max-priority-queue-typed/-/max-priority-queue-typed-1.21.3.tgz", - "integrity": "sha512-IOclpEFpQyUDzH2Hp3JhX2TiHaGSeIDclBcUNpZoI/XNpOenkfuqzzV1QGG+BAz3dPD2vq/9TjVRRrHypPma6A==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/max-priority-queue-typed/-/max-priority-queue-typed-1.3.3.tgz", + "integrity": "sha512-xriejx/vg+6ffJAZm+7PkR1KoMa5IHwuh7RxkDIUBeqhINaUhhz8c2BGAWwZx9Ug0XaGIgy8VzQik1G2B2rQnQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/merge-stream": { @@ -4958,23 +5017,21 @@ } }, "node_modules/min-heap-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/min-heap-typed/-/min-heap-typed-1.21.3.tgz", - "integrity": "sha512-S43A9yI3tg22Ojnu1x1Amt0dU5LrqS19SOKKDyAtXnUzS5RMkZM/b5iw2nJ5mrEP2FJwjOyj+XLapSyXw9powA==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/min-heap-typed/-/min-heap-typed-1.3.3.tgz", + "integrity": "sha512-/l+HTy3Gqf1NhhsmYVRm8wVx6QA/jgOFVBhb0YUY9g1szAnoopZJ2OOWUlbTrqaU+wyfOi7xqL+dFfootZrYcA==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/min-priority-queue-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/min-priority-queue-typed/-/min-priority-queue-typed-1.21.3.tgz", - "integrity": "sha512-woyiLWvMaHJfBFziPSffy74Hk+80K23yEEG/QUDqS6hMbM+BUla7NhWFfqJOal0igMqgI9u73XMEnQAwrfR/2g==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/min-priority-queue-typed/-/min-priority-queue-typed-1.3.3.tgz", + "integrity": "sha512-c7LDSu9wc1uEunDZjPhksBkD+iKQ9YUSanSZUqQ6qVitwFeHvzqhct4v2NteYbqhBAEFC6Jzlpty2g5rXe3CtQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/minimatch": { @@ -5252,9 +5309,9 @@ } }, "node_modules/pretty-format": { - "version": "29.6.3", - "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.6.3.tgz", - "integrity": "sha512-ZsBgjVhFAj5KeK+nHfF1305/By3lechHQSMWCTl8iHSbfOm2TN5nHEtFc/+W7fAyUeCs2n5iow72gld4gW0xDw==", + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", + "integrity": "sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==", "dev": true, "dependencies": { "@jest/schemas": "^29.6.3", @@ -5266,13 +5323,12 @@ } }, "node_modules/priority-queue-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/priority-queue-typed/-/priority-queue-typed-1.21.3.tgz", - "integrity": "sha512-YJSppKblM25t6L3kn/9MsgHsQi6Z3vvuoX6xWsA4Np69SMsAqMfsKrCO7oSYfEDZzW9LxeL/rZzjn0GeygraaQ==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/priority-queue-typed/-/priority-queue-typed-1.3.3.tgz", + "integrity": "sha512-yv92rNbm3tD3ZNvZQyjEsotQMgGO4dIhhR0nGtP3gUv8D50Ci7Q6+WPfknb2i1G9EY9okr3o6gYtLLxc21LHpQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/prompts": { @@ -5313,6 +5369,15 @@ } ] }, + "node_modules/queue-typed": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/queue-typed/-/queue-typed-1.3.3.tgz", + "integrity": "sha512-i+RPjK5pc9mPgzJZ5hWmgUfU1ahQ1AcXR1teqX7cGKpOrqgQCosVXUbrTm04ddwTK83IWip4HS5pvoArRtoC7w==", + "dev": true, + "dependencies": { + "data-structure-typed": "^1.3.3" + } + }, "node_modules/randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -5368,9 +5433,9 @@ } }, "node_modules/resolve": { - "version": "1.22.4", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.4.tgz", - "integrity": "sha512-PXNdCiPqDqeUou+w1C2eTQbNfxKSuMxqTCuvlmmMsk1NWHL5fRrhY6Pl0qEYYc6+QqGClco1Qj8XnjPego4wfg==", + "version": "1.22.6", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.6.tgz", + "integrity": "sha512-njhxM7mV12JfufShqGy3Rz8j11RPdLy4xi15UurGJeoHLfJpVXKdh3ueuOqbYUcDZnffr6X739JBo5LzyahEsw==", "dev": true, "dependencies": { "is-core-module": "^2.13.0", @@ -5604,13 +5669,12 @@ } }, "node_modules/singly-linked-list-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/singly-linked-list-typed/-/singly-linked-list-typed-1.21.3.tgz", - "integrity": "sha512-gcCqO7Lc164qkiQXSs04fcDsSAc3aPsA+Hcz9zEFVbGHHaYEZW04wPwU67XnSHpVtSU8FnMlWVkPxJq7Xf2h6g==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/singly-linked-list-typed/-/singly-linked-list-typed-1.3.3.tgz", + "integrity": "sha512-wlbm0pDRiFX0QuEmDTtskJ8x0Fay6+h/qKXjVBi3ux1ClU8dC+7URblZGW3tWnUyInHoNmj9RO4m+X54+xvklw==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/sisteransi": { @@ -5654,13 +5718,12 @@ "dev": true }, "node_modules/stack-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/stack-typed/-/stack-typed-1.21.3.tgz", - "integrity": "sha512-wLqkd6jg7RuOsrs8LFvoFh1zAHLB2H2NLfxpB1tfpx46gm2eJHPFFY9gpk3DATDaOHDV87dpEXrM40tVT6ALrg==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/stack-typed/-/stack-typed-1.3.3.tgz", + "integrity": "sha512-iLfmbhCYUSzK6WVsKdV7Tv++dRKmEs48YpJ50/gDPupg7dYYkb5noMT1OaarXaPTRS1WKZIWgczcAxiiL0pjPg==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/stack-utils": { @@ -6064,23 +6127,21 @@ } }, "node_modules/tree-multiset-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/tree-multiset-typed/-/tree-multiset-typed-1.21.3.tgz", - "integrity": "sha512-iawtJsC3lj3w6CprTHA/V2XCDC6xUd39wJ1+A+tZb9zsiIGBSwvHVaFGGjU8+IXGsH0LOt3tUTrOgifWX5dEzA==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/tree-multiset-typed/-/tree-multiset-typed-1.3.3.tgz", + "integrity": "sha512-IsvMymeguS/+UqX7j57Ah5gIWkEJIba4K+8MFacIHfnqriLqKUFPCy4msXqU9obGFhD6AOgiwcpxbKtx/3v9ww==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/trie-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/trie-typed/-/trie-typed-1.21.3.tgz", - "integrity": "sha512-4ia0nlFhYmOromnrbGn4isLoyAYHftA7d0UAq77bC7ujro4Z5TWLEr7dEZ07m/vwvz55QeC+bzVKJyl04/QNrw==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/trie-typed/-/trie-typed-1.3.3.tgz", + "integrity": "sha512-OdEfMXjDshD404TSPqXuroEk6iJYJJaKHuVgQdBLQHefOhtK09gm1/iUd33tplt1iEHKZ1LvqGVas8dhwGt/Gw==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/ts-jest": { @@ -6336,13 +6397,12 @@ } }, "node_modules/undirected-graph-typed": { - "version": "1.21.3", - "resolved": "https://registry.npmjs.org/undirected-graph-typed/-/undirected-graph-typed-1.21.3.tgz", - "integrity": "sha512-tU02rCmLp68fTHLy3H7KSb0xzxxDkDDSwMiNavOxCiovv5mcgc026U/pRtHvAFaNtRFKcCA6ABtbUZqtrgErEA==", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/undirected-graph-typed/-/undirected-graph-typed-1.3.3.tgz", + "integrity": "sha512-Lgb3Qw3il08rad35V14HiuO47PbujZzrGvBbRzHuyzt0NuOQjGAajQkq+J8GE3lptgedQaKhzUWYFzmPV5aNYQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.21.3", - "zod": "^3.22.2" + "data-structure-typed": "^1.3.3" } }, "node_modules/update-browserslist-db": { @@ -6834,15 +6894,6 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } - }, - "node_modules/zod": { - "version": "3.22.2", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.2.tgz", - "integrity": "sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==", - "dev": true, - "funding": { - "url": "https://github.com/sponsors/colinhacks" - } } } } diff --git a/package.json b/package.json index 82323c2..2b6aeb3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "data-structure-typed", - "version": "1.3.1", + "version": "1.3.3", "description": "Data Structures of Javascript & TypeScript. AVLTree, Binary Search Tree, Binary Tree, Tree Multiset, Graph, Heap, Priority Queue, Linked List.", "main": "dist/index.js", "module": "dist/index.js", @@ -10,7 +10,7 @@ "build:browser": "webpack", "build:docs": "typedoc --out docs ./src", "test": "jest", - "update:test-deps": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed --save-dev", + "update:test-deps": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev", "deps:check": "dependency-cruiser src", "build:publish": "npm run test && npm run build && npm run build:docs && npm publish" }, @@ -134,31 +134,32 @@ "devDependencies": { "@types/jest": "^29.5.3", "@types/node": "^20.4.9", - "avl-tree-typed": "^1.21.3", - "binary-tree-typed": "^1.21.3", - "bst-typed": "^1.21.3", + "avl-tree-typed": "^1.3.3", + "binary-tree-typed": "^1.3.3", + "bst-typed": "^1.3.3", "dependency-cruiser": "^13.1.2", - "deque-typed": "^1.21.3", - "directed-graph-typed": "^1.21.3", - "doubly-linked-list-typed": "^1.21.3", - "graph-typed": "^1.21.3", - "heap-typed": "^1.21.3", + "deque-typed": "^1.3.3", + "directed-graph-typed": "^1.3.3", + "doubly-linked-list-typed": "^1.3.3", + "graph-typed": "^1.3.3", + "heap-typed": "^1.3.3", "jest": "^29.6.2", - "linked-list-typed": "^1.21.3", - "max-heap-typed": "^1.21.3", - "max-priority-queue-typed": "^1.21.3", - "min-heap-typed": "^1.21.3", - "min-priority-queue-typed": "^1.21.3", - "priority-queue-typed": "^1.21.3", - "singly-linked-list-typed": "^1.21.3", - "stack-typed": "^1.21.3", - "tree-multiset-typed": "^1.21.3", - "trie-typed": "^1.21.3", + "linked-list-typed": "^1.3.3", + "max-heap-typed": "^1.3.3", + "max-priority-queue-typed": "^1.3.3", + "min-heap-typed": "^1.3.3", + "min-priority-queue-typed": "^1.3.3", + "priority-queue-typed": "^1.3.3", + "queue-typed": "^1.3.3", + "singly-linked-list-typed": "^1.3.3", + "stack-typed": "^1.3.3", + "tree-multiset-typed": "^1.3.3", + "trie-typed": "^1.3.3", "ts-jest": "^29.1.1", "ts-loader": "^9.4.4", "typedoc": "^0.24.8", "typescript": "^4.9.5", - "undirected-graph-typed": "^1.21.3", + "undirected-graph-typed": "^1.3.3", "webpack": "^5.88.2", "webpack-cli": "^5.1.4" } diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index 661a4e4..8462f12 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -49,7 +49,7 @@ export class DoublyLinkedListNode { } } -export class DoublyLinkedList { +export class DoublyLinkedList { /** * The constructor initializes the linked list with an empty head, tail, and length. @@ -122,8 +122,8 @@ export class DoublyLinkedList { * @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the * list is empty, it returns null. */ - pop(): T | null { - if (!this.tail) return null; + pop(): T | undefined { + if (!this.tail) return undefined; const removedNode = this.tail; if (this.head === this.tail) { this.head = null; @@ -141,8 +141,8 @@ export class DoublyLinkedList { * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked * list. */ - shift(): T | null { - if (!this.head) return null; + shift(): T | undefined { + if (!this.head) return undefined; const removedNode = this.head; if (this.head === this.tail) { this.head = null; @@ -180,8 +180,8 @@ export class DoublyLinkedList { * @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds * or the linked list is empty, it will return null. */ - getAt(index: number): T | null { - if (index < 0 || index >= this.length) return null; + getAt(index: number): T | undefined { + if (index < 0 || index >= this.length) return undefined; let current = this.head; for (let i = 0; i < index; i++) { current = current!.next; @@ -264,8 +264,8 @@ export class DoublyLinkedList { * @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of * bounds. */ - deleteAt(index: number): T | null { - if (index < 0 || index >= this.length) return null; + deleteAt(index: number): T | undefined { + if (index < 0 || index >= this.length) return undefined; if (index === 0) return this.shift(); if (index === this.length - 1) return this.pop(); @@ -280,6 +280,7 @@ export class DoublyLinkedList { delete(valOrNode: T): boolean; delete(valOrNode: DoublyLinkedListNode): boolean; + /** * The `delete` function removes a node from a doubly linked list based on either the node itself or its value. * @param {T | DoublyLinkedListNode} valOrNode - The `valOrNode` parameter can accept either a value of type `T` or @@ -495,6 +496,7 @@ export class DoublyLinkedList { insertAfter(existingValueOrNode: T, newValue: T): boolean; insertAfter(existingValueOrNode: DoublyLinkedListNode, newValue: T): boolean; + /** * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. * @param {T | DoublyLinkedListNode} existingValueOrNode - The existing value or node in the doubly linked list @@ -533,6 +535,7 @@ export class DoublyLinkedList { insertBefore(existingValueOrNode: T, newValue: T): boolean; insertBefore(existingValueOrNode: DoublyLinkedListNode, newValue: T): boolean; + /** * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list. * @param {T | DoublyLinkedListNode} existingValueOrNode - The existing value or node in the doubly linked list diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index fa4f651..ecc37cd 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -173,8 +173,8 @@ export class SinglyLinkedList { * @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or * `null` if the index is out of bounds. */ - getAt(index: number): T | null { - if (index < 0 || index >= this.length) return null; + getAt(index: number): T | undefined { + if (index < 0 || index >= this.length) return undefined; let current = this.head; for (let i = 0; i < index; i++) { current = current!.next; @@ -218,6 +218,7 @@ export class SinglyLinkedList { delete(valueOrNode: T): boolean; delete(valueOrNode: SinglyLinkedListNode): boolean; + /** * The delete function removes a node with a specific value from a singly linked list. * @param {T | SinglyLinkedListNode} valueOrNode - The `valueOrNode` parameter can accept either a value of type `T` @@ -399,6 +400,7 @@ export class SinglyLinkedList { insertBefore(existingValue: T, newValue: T): boolean insertBefore(existingValue: SinglyLinkedListNode, newValue: T): boolean + /** * The `insertBefore` function inserts a new value before an existing value in a singly linked list. * @param {T | SinglyLinkedListNode} existingValueOrNode - The existing value or node that you want to insert the @@ -438,6 +440,7 @@ export class SinglyLinkedList { insertAfter(existingValueOrNode: T, newValue: T): boolean insertAfter(existingValueOrNode: SinglyLinkedListNode, newValue: T): boolean + /** * The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list. * @param {T | SinglyLinkedListNode} existingValueOrNode - The existing value or node in the linked list after which @@ -487,4 +490,14 @@ export class SinglyLinkedList { return count; } + + + * [Symbol.iterator]() { + let current = this.head; + + while (current) { + yield current.val; + current = current.next; + } + } } diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index d77e24d..2c93e35 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -5,7 +5,7 @@ */ import {SinglyLinkedList} from '../linked-list'; -export class Queue extends SinglyLinkedList { +export class LinkedListQueue extends SinglyLinkedList { /** * The enqueue function adds a value to the end of an array. @@ -30,20 +30,9 @@ export class Queue extends SinglyLinkedList { peek(): T | undefined { return this.head?.val; } - - * [Symbol.iterator]() { - let current = this.head; - - while (current) { - yield current.val; - current = current.next; - } - } } -export class ArrayQueue { - protected _nodes: T[]; - protected _offset: number; +export class Queue { /** * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0. @@ -56,12 +45,32 @@ export class ArrayQueue { this._offset = 0; } + private _nodes: T[]; + + get nodes(): T[] { + return this._nodes; + } + + set nodes(value: T[]) { + this._nodes = value; + } + + private _offset: number; + + get offset(): number { + return this._offset; + } + + set offset(value: number) { + this._offset = value; + } + /** * The size function returns the number of elements in an array. * @returns {number} The size of the array, which is the difference between the length of the array and the offset. */ get size(): number { - return this._nodes.length - this._offset; + return this.nodes.length - this.offset; } /** @@ -72,8 +81,8 @@ export class ArrayQueue { * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input * array. */ - static fromArray(elements: T[]): ArrayQueue { - return new ArrayQueue(elements); + static fromArray(elements: T[]): Queue { + return new Queue(elements); } /** @@ -81,8 +90,8 @@ export class ArrayQueue { * @param {T} element - The `element` parameter represents the element that you want to add to the queue. * @returns The `add` method is returning a `Queue` object. */ - push(element: T): ArrayQueue { - this._nodes.push(element); + push(element: T): Queue { + this.nodes.push(element); return this; } @@ -91,18 +100,18 @@ export class ArrayQueue { * necessary to optimize performance. * @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty. */ - shift(): T | null { - if (this.size === 0) return null; + shift(): T | undefined { + if (this.size === 0) return undefined; const first = this.peek(); - this._offset += 1; + this.offset += 1; - if (this._offset * 2 < this._nodes.length) return first; + if (this.offset * 2 < this.nodes.length) return first; // only remove dequeued elements when reaching half size // to decrease latency of shifting elements. - this._nodes = this._nodes.slice(this._offset); - this._offset = 0; + this.nodes = this.nodes.slice(this.offset); + this.offset = 0; return first; } @@ -111,8 +120,8 @@ export class ArrayQueue { * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at * the `_offset` index. If the data structure is empty (size is 0), it returns `null`. */ - peek(): T | null { - return this.size > 0 ? this._nodes[this._offset] : null; + peek(): T | undefined { + return this.size > 0 ? this.nodes[this.offset] : undefined; } /** @@ -120,8 +129,8 @@ export class ArrayQueue { * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the * array is empty, it returns `null`. */ - peekLast(): T | null { - return this.size > 0 ? this._nodes[this._nodes.length - 1] : null; + peekLast(): T | undefined { + return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined; } /** @@ -136,10 +145,14 @@ export class ArrayQueue { * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty. * @returns The method is returning a value of type T or null. */ - dequeue(): T | null { + dequeue(): T | undefined { return this.shift(); } + getAt(index: number): T | undefined { + return this.nodes[index]; + } + /** * The function checks if a data structure is empty by comparing its size to zero. * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not. @@ -153,22 +166,28 @@ export class ArrayQueue { * @returns An array of type T is being returned. */ toArray(): T[] { - return this._nodes.slice(this._offset); + return this.nodes.slice(this.offset); } /** * The clear function resets the nodes array and offset to their initial values. */ clear(): void { - this._nodes = []; - this._offset = 0; + this.nodes = []; + this.offset = 0; } /** * The `clone()` function returns a new Queue object with the same elements as the original Queue. * @returns The `clone()` method is returning a new instance of the `Queue` class. */ - clone(): ArrayQueue { - return new ArrayQueue(this._nodes.slice(this._offset)); + clone(): Queue { + return new Queue(this.nodes.slice(this.offset)); + } + + * [Symbol.iterator]() { + for (const item of this.nodes) { + yield item; + } } } diff --git a/tests/integration/avl-tree.test.ts b/tests/integration/avl-tree.test.ts index 9a37fe4..b95e812 100644 --- a/tests/integration/avl-tree.test.ts +++ b/tests/integration/avl-tree.test.ts @@ -1,114 +1,114 @@ import {AVLTree} from 'avl-tree-typed'; describe('AVL Tree Test', () => { - it('should perform various operations on a AVL Tree', () => { + it('should perform various operations on a AVL Tree', () => { - const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; - const tree = new AVLTree(); + const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; + const tree = new AVLTree(); - for (const i of arr) tree.add(i, i); + for (const i of arr) tree.add(i, i); - const node6 = tree.get(6); + const node6 = tree.get(6); - expect(node6 && tree.getHeight(node6)).toBe(3); - expect(node6 && tree.getDepth(node6)).toBe(1); + expect(node6 && tree.getHeight(node6)).toBe(3); + expect(node6 && tree.getDepth(node6)).toBe(1); - const getNodeById = tree.get(10, 'id'); - expect(getNodeById?.id).toBe(10); + const getNodeById = tree.get(10, 'id'); + expect(getNodeById?.id).toBe(10); - const getMinNodeByRoot = tree.getLeftMost(); - expect(getMinNodeByRoot?.id).toBe(1); + const getMinNodeByRoot = tree.getLeftMost(); + expect(getMinNodeByRoot?.id).toBe(1); - const node15 = tree.get(15); - const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15); - expect(getMinNodeBySpecificNode?.id).toBe(12); + const node15 = tree.get(15); + const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15); + expect(getMinNodeBySpecificNode?.id).toBe(12); - const subTreeSum = node15 && tree.subTreeSum(node15); - expect(subTreeSum).toBe(70); + const subTreeSum = node15 && tree.subTreeSum(node15); + expect(subTreeSum).toBe(70); - const lesserSum = tree.lesserSum(10); - expect(lesserSum).toBe(45); + const lesserSum = tree.lesserSum(10); + expect(lesserSum).toBe(45); - // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class. - expect(node15?.val).toBe(15); + // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class. + expect(node15?.val).toBe(15); - const node11 = tree.get(11); + const node11 = tree.get(11); - const dfs = tree.DFS('in', 'node'); - expect(dfs[0].id).toBe(1); - expect(dfs[dfs.length - 1].id).toBe(16); + const dfs = tree.DFS('in', 'node'); + expect(dfs[0].id).toBe(1); + expect(dfs[dfs.length - 1].id).toBe(16); - tree.perfectlyBalance(); - const bfs = tree.BFS('node'); - expect(tree.isPerfectlyBalanced()).toBe(true); - expect(bfs[0].id).toBe(8); - expect(bfs[bfs.length - 1].id).toBe(16); + tree.perfectlyBalance(); + const bfs = tree.BFS('node'); + expect(tree.isPerfectlyBalanced()).toBe(true); + expect(bfs[0].id).toBe(8); + expect(bfs[bfs.length - 1].id).toBe(16); - expect(tree.remove(11, true)[0].deleted?.id).toBe(11); - expect(tree.isAVLBalanced()).toBe(true); - expect(node15 && tree.getHeight(node15)).toBe(2); + expect(tree.remove(11, true)[0].deleted?.id).toBe(11); + expect(tree.isAVLBalanced()).toBe(true); + expect(node15 && tree.getHeight(node15)).toBe(2); - expect(tree.remove(1, true)[0].deleted?.id).toBe(1); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(4); + expect(tree.remove(1, true)[0].deleted?.id).toBe(1); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(4); - expect(tree.remove(4, true)[0].deleted?.id).toBe(4); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(4); + expect(tree.remove(4, true)[0].deleted?.id).toBe(4); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(4); - expect(tree.remove(10, true)[0].deleted?.id).toBe(10); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(10, true)[0].deleted?.id).toBe(10); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(15, true)[0].deleted?.id).toBe(15); - expect(tree.isAVLBalanced()).toBe(true); + expect(tree.remove(15, true)[0].deleted?.id).toBe(15); + expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(5, true)[0].deleted?.id).toBe(5); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(5, true)[0].deleted?.id).toBe(5); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(13, true)[0].deleted?.id).toBe(13); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(13, true)[0].deleted?.id).toBe(13); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(3, true)[0].deleted?.id).toBe(3); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(3, true)[0].deleted?.id).toBe(3); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(8, true)[0].deleted?.id).toBe(8); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(8, true)[0].deleted?.id).toBe(8); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(6, true)[0].deleted?.id).toBe(6); - expect(tree.remove(6, true).length).toBe(0); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(2); + expect(tree.remove(6, true)[0].deleted?.id).toBe(6); + expect(tree.remove(6, true).length).toBe(0); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(2); - expect(tree.remove(7, true)[0].deleted?.id).toBe(7); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(2); + expect(tree.remove(7, true)[0].deleted?.id).toBe(7); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(2); - expect(tree.remove(9, true)[0].deleted?.id).toBe(9); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(2); - expect(tree.remove(14, true)[0].deleted?.id).toBe(14); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(1); + expect(tree.remove(9, true)[0].deleted?.id).toBe(9); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(2); + expect(tree.remove(14, true)[0].deleted?.id).toBe(14); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(1); - expect(tree.isAVLBalanced()).toBe(true); - const lastBFSIds = tree.BFS(); - expect(lastBFSIds[0]).toBe(12); - expect(lastBFSIds[1]).toBe(2); - expect(lastBFSIds[2]).toBe(16); + expect(tree.isAVLBalanced()).toBe(true); + const lastBFSIds = tree.BFS(); + expect(lastBFSIds[0]).toBe(12); + expect(lastBFSIds[1]).toBe(2); + expect(lastBFSIds[2]).toBe(16); - const lastBFSNodes = tree.BFS('node'); - expect(lastBFSNodes[0].id).toBe(12); - expect(lastBFSNodes[1].id).toBe(2); - expect(lastBFSNodes[2].id).toBe(16); - }); + const lastBFSNodes = tree.BFS('node'); + expect(lastBFSNodes[0].id).toBe(12); + expect(lastBFSNodes[1].id).toBe(2); + expect(lastBFSNodes[2].id).toBe(16); + }); }); diff --git a/tests/integration/bst.test.ts b/tests/integration/bst.test.ts index 51ec598..37c6f47 100644 --- a/tests/integration/bst.test.ts +++ b/tests/integration/bst.test.ts @@ -1,371 +1,371 @@ import {BST, BSTNode} from 'bst-typed'; describe('Individual package BST operations test', () => { - it('should perform various operations on a Binary Search Tree with numeric values', () => { - const bst = new BST(); - expect(bst).toBeInstanceOf(BST); - bst.add(11, 11); - bst.add(3, 3); - const idsOrVals = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; - bst.addMany(idsOrVals, idsOrVals); - expect(bst.root).toBeInstanceOf(BSTNode); + it('should perform various operations on a Binary Search Tree with numeric values', () => { + const bst = new BST(); + expect(bst).toBeInstanceOf(BST); + bst.add(11, 11); + bst.add(3, 3); + const idsOrVals = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; + bst.addMany(idsOrVals, idsOrVals); + expect(bst.root).toBeInstanceOf(BSTNode); - if (bst.root) expect(bst.root.id).toBe(11); + if (bst.root) expect(bst.root.id).toBe(11); - expect(bst.size).toBe(16); + expect(bst.size).toBe(16); - expect(bst.has(6)).toBe(true); + expect(bst.has(6)).toBe(true); - const node6 = bst.get(6); - expect(node6 && bst.getHeight(6)).toBe(2); - expect(node6 && bst.getDepth(6)).toBe(3); + const node6 = bst.get(6); + expect(node6 && bst.getHeight(6)).toBe(2); + expect(node6 && bst.getDepth(6)).toBe(3); - const nodeId10 = bst.get(10); - expect(nodeId10?.id).toBe(10); + const nodeId10 = bst.get(10); + expect(nodeId10?.id).toBe(10); - const nodeVal9 = bst.get(9, 'val'); - expect(nodeVal9?.id).toBe(9); + const nodeVal9 = bst.get(9, 'val'); + expect(nodeVal9?.id).toBe(9); - const leftMost = bst.getLeftMost(); - expect(leftMost?.id).toBe(1); + const leftMost = bst.getLeftMost(); + expect(leftMost?.id).toBe(1); - const node15 = bst.get(15); - const minNodeBySpecificNode = node15 && bst.getLeftMost(node15); - expect(minNodeBySpecificNode?.id).toBe(12); - - const subTreeSum = node15 && bst.subTreeSum(15); - expect(subTreeSum).toBe(70); - - const lesserSum = bst.lesserSum(10); - expect(lesserSum).toBe(45); - - expect(node15).toBeInstanceOf(BSTNode); - - const node11 = bst.get(11); - expect(node11).toBeInstanceOf(BSTNode); - - const dfsInorderNodes = bst.DFS('in', 'node'); - expect(dfsInorderNodes[0].id).toBe(1); - expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); - - bst.perfectlyBalance(); - expect(bst.isPerfectlyBalanced()).toBe(true); - - const bfsNodesAfterBalanced = bst.BFS('node'); - expect(bfsNodesAfterBalanced[0].id).toBe(8); - expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); - - const removed11 = bst.remove(11, true); - expect(removed11).toBeInstanceOf(Array); - expect(removed11[0]).toBeDefined(); - expect(removed11[0].deleted).toBeDefined(); - - if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); - - expect(bst.isAVLBalanced()).toBe(true); - - expect(bst.getHeight(15)).toBe(1); - - const removed1 = bst.remove(1, true); - expect(removed1).toBeInstanceOf(Array); - expect(removed1[0]).toBeDefined(); - expect(removed1[0].deleted).toBeDefined(); - if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); - - expect(bst.isAVLBalanced()).toBe(true); - - expect(bst.getHeight()).toBe(4); - - const removed4 = bst.remove(4, true); - expect(removed4).toBeInstanceOf(Array); - expect(removed4[0]).toBeDefined(); - expect(removed4[0].deleted).toBeDefined(); - if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(4); - - const removed10 = bst.remove(10, true); - expect(removed10).toBeInstanceOf(Array); - expect(removed10[0]).toBeDefined(); - expect(removed10[0].deleted).toBeDefined(); - if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(4); - - const removed15 = bst.remove(15, true); - expect(removed15).toBeInstanceOf(Array); - expect(removed15[0]).toBeDefined(); - expect(removed15[0].deleted).toBeDefined(); - if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); - - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(3); - - const removed5 = bst.remove(5, true); - expect(removed5).toBeInstanceOf(Array); - expect(removed5[0]).toBeDefined(); - expect(removed5[0].deleted).toBeDefined(); - if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); - - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(3); - - const removed13 = bst.remove(13, true); - expect(removed13).toBeInstanceOf(Array); - expect(removed13[0]).toBeDefined(); - expect(removed13[0].deleted).toBeDefined(); - if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(3); - - const removed3 = bst.remove(3, true); - expect(removed3).toBeInstanceOf(Array); - expect(removed3[0]).toBeDefined(); - expect(removed3[0].deleted).toBeDefined(); - if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(3); - - const removed8 = bst.remove(8, true); - expect(removed8).toBeInstanceOf(Array); - expect(removed8[0]).toBeDefined(); - expect(removed8[0].deleted).toBeDefined(); - if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(3); - - const removed6 = bst.remove(6, true); - expect(removed6).toBeInstanceOf(Array); - expect(removed6[0]).toBeDefined(); - expect(removed6[0].deleted).toBeDefined(); - if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); - expect(bst.remove(6, true).length).toBe(0); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(3); - - const removed7 = bst.remove(7, true); - expect(removed7).toBeInstanceOf(Array); - expect(removed7[0]).toBeDefined(); - expect(removed7[0].deleted).toBeDefined(); - if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(3); - - const removed9 = bst.remove(9, true); - expect(removed9).toBeInstanceOf(Array); - expect(removed9[0]).toBeDefined(); - expect(removed9[0].deleted).toBeDefined(); - if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(3); - - const removed14 = bst.remove(14, true); - expect(removed14).toBeInstanceOf(Array); - expect(removed14[0]).toBeDefined(); - expect(removed14[0].deleted).toBeDefined(); - if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(2); - - - expect(bst.isAVLBalanced()).toBe(false); - - const bfsIDs = bst.BFS(); - expect(bfsIDs[0]).toBe(2); - expect(bfsIDs[1]).toBe(12); - expect(bfsIDs[2]).toBe(16); - - const bfsNodes = bst.BFS('node'); - expect(bfsNodes[0].id).toBe(2); - expect(bfsNodes[1].id).toBe(12); - expect(bfsNodes[2].id).toBe(16); - - }); - - it('should perform various operations on a Binary Search Tree with object values', () => { - const objBST = new BST>(); - expect(objBST).toBeInstanceOf(BST); - objBST.add(11, {id: 11, keyA: 11}); - objBST.add(3, {id: 3, keyA: 3}); - const values = [{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8}, - {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2}, - {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12}, - {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7}, - {id: 10, keyA: 10}, {id: 5, keyA: 5}]; - - objBST.addMany(values.map(item => item.id), values); - - expect(objBST.root).toBeInstanceOf(BSTNode); - - if (objBST.root) expect(objBST.root.id).toBe(11); - - expect(objBST.has(6)).toBe(true); - - const node6 = objBST.get(6); - expect(node6 && objBST.getHeight(node6)).toBe(2); - expect(node6 && objBST.getDepth(node6)).toBe(3); - - const nodeId10 = objBST.get(10, 'id'); - expect(nodeId10?.id).toBe(10); - - const nodeVal9 = objBST.get(9, 'id'); - expect(nodeVal9?.id).toBe(9); - - const leftMost = objBST.getLeftMost(); - expect(leftMost?.id).toBe(1); - - const node15 = objBST.get(15); - expect(node15?.val).toEqual({id: 15, keyA: 15}); - const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15); - expect(minNodeBySpecificNode?.id).toBe(12); - - const subTreeSum = node15 && objBST.subTreeSum(node15); - expect(subTreeSum).toBe(70); - - const lesserSum = objBST.lesserSum(10); - expect(lesserSum).toBe(45); - - expect(node15).toBeInstanceOf(BSTNode); - - const node11 = objBST.get(11); - expect(node11).toBeInstanceOf(BSTNode); - - const dfsInorderNodes = objBST.DFS('in', 'node'); - expect(dfsInorderNodes[0].id).toBe(1); - expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); - - objBST.perfectlyBalance(); - expect(objBST.isPerfectlyBalanced()).toBe(true); - - const bfsNodesAfterBalanced = objBST.BFS('node'); - expect(bfsNodesAfterBalanced[0].id).toBe(8); - expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); - - const removed11 = objBST.remove(11, true); - expect(removed11).toBeInstanceOf(Array); - expect(removed11[0]).toBeDefined(); - expect(removed11[0].deleted).toBeDefined(); - - if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); - - expect(objBST.isAVLBalanced()).toBe(true); - - expect(node15 && objBST.getHeight(node15)).toBe(2); - - const removed1 = objBST.remove(1, true); - expect(removed1).toBeInstanceOf(Array); - expect(removed1[0]).toBeDefined(); - expect(removed1[0].deleted).toBeDefined(); - if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); - - expect(objBST.isAVLBalanced()).toBe(true); - - expect(objBST.getHeight()).toBe(4); - - const removed4 = objBST.remove(4, true); - expect(removed4).toBeInstanceOf(Array); - expect(removed4[0]).toBeDefined(); - expect(removed4[0].deleted).toBeDefined(); - if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(4); - - const removed10 = objBST.remove(10, true); - expect(removed10).toBeInstanceOf(Array); - expect(removed10[0]).toBeDefined(); - expect(removed10[0].deleted).toBeDefined(); - if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(4); - - const removed15 = objBST.remove(15, true); - expect(removed15).toBeInstanceOf(Array); - expect(removed15[0]).toBeDefined(); - expect(removed15[0].deleted).toBeDefined(); - if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); - - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(3); - - const removed5 = objBST.remove(5, true); - expect(removed5).toBeInstanceOf(Array); - expect(removed5[0]).toBeDefined(); - expect(removed5[0].deleted).toBeDefined(); - if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); - - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(3); - - const removed13 = objBST.remove(13, true); - expect(removed13).toBeInstanceOf(Array); - expect(removed13[0]).toBeDefined(); - expect(removed13[0].deleted).toBeDefined(); - if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(3); - - const removed3 = objBST.remove(3, true); - expect(removed3).toBeInstanceOf(Array); - expect(removed3[0]).toBeDefined(); - expect(removed3[0].deleted).toBeDefined(); - if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(3); - - const removed8 = objBST.remove(8, true); - expect(removed8).toBeInstanceOf(Array); - expect(removed8[0]).toBeDefined(); - expect(removed8[0].deleted).toBeDefined(); - if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(3); - - const removed6 = objBST.remove(6, true); - expect(removed6).toBeInstanceOf(Array); - expect(removed6[0]).toBeDefined(); - expect(removed6[0].deleted).toBeDefined(); - if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); - expect(objBST.remove(6, true).length).toBe(0); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(3); - - const removed7 = objBST.remove(7, true); - expect(removed7).toBeInstanceOf(Array); - expect(removed7[0]).toBeDefined(); - expect(removed7[0].deleted).toBeDefined(); - if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(3); - - const removed9 = objBST.remove(9, true); - expect(removed9).toBeInstanceOf(Array); - expect(removed9[0]).toBeDefined(); - expect(removed9[0].deleted).toBeDefined(); - if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(3); - - const removed14 = objBST.remove(14, true); - expect(removed14).toBeInstanceOf(Array); - expect(removed14[0]).toBeDefined(); - expect(removed14[0].deleted).toBeDefined(); - if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(2); - - - expect(objBST.isAVLBalanced()).toBe(false); - - const bfsIDs = objBST.BFS(); - expect(bfsIDs[0]).toBe(2); - expect(bfsIDs[1]).toBe(12); - expect(bfsIDs[2]).toBe(16); - - const bfsNodes = objBST.BFS('node'); - expect(bfsNodes[0].id).toBe(2); - expect(bfsNodes[1].id).toBe(12); - expect(bfsNodes[2].id).toBe(16); - - }); + const node15 = bst.get(15); + const minNodeBySpecificNode = node15 && bst.getLeftMost(node15); + expect(minNodeBySpecificNode?.id).toBe(12); + + const subTreeSum = node15 && bst.subTreeSum(15); + expect(subTreeSum).toBe(70); + + const lesserSum = bst.lesserSum(10); + expect(lesserSum).toBe(45); + + expect(node15).toBeInstanceOf(BSTNode); + + const node11 = bst.get(11); + expect(node11).toBeInstanceOf(BSTNode); + + const dfsInorderNodes = bst.DFS('in', 'node'); + expect(dfsInorderNodes[0].id).toBe(1); + expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); + + bst.perfectlyBalance(); + expect(bst.isPerfectlyBalanced()).toBe(true); + + const bfsNodesAfterBalanced = bst.BFS('node'); + expect(bfsNodesAfterBalanced[0].id).toBe(8); + expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); + + const removed11 = bst.remove(11, true); + expect(removed11).toBeInstanceOf(Array); + expect(removed11[0]).toBeDefined(); + expect(removed11[0].deleted).toBeDefined(); + + if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); + + expect(bst.isAVLBalanced()).toBe(true); + + expect(bst.getHeight(15)).toBe(1); + + const removed1 = bst.remove(1, true); + expect(removed1).toBeInstanceOf(Array); + expect(removed1[0]).toBeDefined(); + expect(removed1[0].deleted).toBeDefined(); + if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); + + expect(bst.isAVLBalanced()).toBe(true); + + expect(bst.getHeight()).toBe(4); + + const removed4 = bst.remove(4, true); + expect(removed4).toBeInstanceOf(Array); + expect(removed4[0]).toBeDefined(); + expect(removed4[0].deleted).toBeDefined(); + if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(4); + + const removed10 = bst.remove(10, true); + expect(removed10).toBeInstanceOf(Array); + expect(removed10[0]).toBeDefined(); + expect(removed10[0].deleted).toBeDefined(); + if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(4); + + const removed15 = bst.remove(15, true); + expect(removed15).toBeInstanceOf(Array); + expect(removed15[0]).toBeDefined(); + expect(removed15[0].deleted).toBeDefined(); + if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); + + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(3); + + const removed5 = bst.remove(5, true); + expect(removed5).toBeInstanceOf(Array); + expect(removed5[0]).toBeDefined(); + expect(removed5[0].deleted).toBeDefined(); + if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); + + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(3); + + const removed13 = bst.remove(13, true); + expect(removed13).toBeInstanceOf(Array); + expect(removed13[0]).toBeDefined(); + expect(removed13[0].deleted).toBeDefined(); + if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(3); + + const removed3 = bst.remove(3, true); + expect(removed3).toBeInstanceOf(Array); + expect(removed3[0]).toBeDefined(); + expect(removed3[0].deleted).toBeDefined(); + if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(3); + + const removed8 = bst.remove(8, true); + expect(removed8).toBeInstanceOf(Array); + expect(removed8[0]).toBeDefined(); + expect(removed8[0].deleted).toBeDefined(); + if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(3); + + const removed6 = bst.remove(6, true); + expect(removed6).toBeInstanceOf(Array); + expect(removed6[0]).toBeDefined(); + expect(removed6[0].deleted).toBeDefined(); + if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); + expect(bst.remove(6, true).length).toBe(0); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(3); + + const removed7 = bst.remove(7, true); + expect(removed7).toBeInstanceOf(Array); + expect(removed7[0]).toBeDefined(); + expect(removed7[0].deleted).toBeDefined(); + if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(3); + + const removed9 = bst.remove(9, true); + expect(removed9).toBeInstanceOf(Array); + expect(removed9[0]).toBeDefined(); + expect(removed9[0].deleted).toBeDefined(); + if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(3); + + const removed14 = bst.remove(14, true); + expect(removed14).toBeInstanceOf(Array); + expect(removed14[0]).toBeDefined(); + expect(removed14[0].deleted).toBeDefined(); + if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(2); + + + expect(bst.isAVLBalanced()).toBe(false); + + const bfsIDs = bst.BFS(); + expect(bfsIDs[0]).toBe(2); + expect(bfsIDs[1]).toBe(12); + expect(bfsIDs[2]).toBe(16); + + const bfsNodes = bst.BFS('node'); + expect(bfsNodes[0].id).toBe(2); + expect(bfsNodes[1].id).toBe(12); + expect(bfsNodes[2].id).toBe(16); + + }); + + it('should perform various operations on a Binary Search Tree with object values', () => { + const objBST = new BST>(); + expect(objBST).toBeInstanceOf(BST); + objBST.add(11, {id: 11, keyA: 11}); + objBST.add(3, {id: 3, keyA: 3}); + const values = [{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8}, + {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2}, + {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12}, + {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7}, + {id: 10, keyA: 10}, {id: 5, keyA: 5}]; + + objBST.addMany(values.map(item => item.id), values); + + expect(objBST.root).toBeInstanceOf(BSTNode); + + if (objBST.root) expect(objBST.root.id).toBe(11); + + expect(objBST.has(6)).toBe(true); + + const node6 = objBST.get(6); + expect(node6 && objBST.getHeight(node6)).toBe(2); + expect(node6 && objBST.getDepth(node6)).toBe(3); + + const nodeId10 = objBST.get(10, 'id'); + expect(nodeId10?.id).toBe(10); + + const nodeVal9 = objBST.get(9, 'id'); + expect(nodeVal9?.id).toBe(9); + + const leftMost = objBST.getLeftMost(); + expect(leftMost?.id).toBe(1); + + const node15 = objBST.get(15); + expect(node15?.val).toEqual({id: 15, keyA: 15}); + const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15); + expect(minNodeBySpecificNode?.id).toBe(12); + + const subTreeSum = node15 && objBST.subTreeSum(node15); + expect(subTreeSum).toBe(70); + + const lesserSum = objBST.lesserSum(10); + expect(lesserSum).toBe(45); + + expect(node15).toBeInstanceOf(BSTNode); + + const node11 = objBST.get(11); + expect(node11).toBeInstanceOf(BSTNode); + + const dfsInorderNodes = objBST.DFS('in', 'node'); + expect(dfsInorderNodes[0].id).toBe(1); + expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); + + objBST.perfectlyBalance(); + expect(objBST.isPerfectlyBalanced()).toBe(true); + + const bfsNodesAfterBalanced = objBST.BFS('node'); + expect(bfsNodesAfterBalanced[0].id).toBe(8); + expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); + + const removed11 = objBST.remove(11, true); + expect(removed11).toBeInstanceOf(Array); + expect(removed11[0]).toBeDefined(); + expect(removed11[0].deleted).toBeDefined(); + + if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); + + expect(objBST.isAVLBalanced()).toBe(true); + + expect(node15 && objBST.getHeight(node15)).toBe(2); + + const removed1 = objBST.remove(1, true); + expect(removed1).toBeInstanceOf(Array); + expect(removed1[0]).toBeDefined(); + expect(removed1[0].deleted).toBeDefined(); + if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); + + expect(objBST.isAVLBalanced()).toBe(true); + + expect(objBST.getHeight()).toBe(4); + + const removed4 = objBST.remove(4, true); + expect(removed4).toBeInstanceOf(Array); + expect(removed4[0]).toBeDefined(); + expect(removed4[0].deleted).toBeDefined(); + if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(4); + + const removed10 = objBST.remove(10, true); + expect(removed10).toBeInstanceOf(Array); + expect(removed10[0]).toBeDefined(); + expect(removed10[0].deleted).toBeDefined(); + if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(4); + + const removed15 = objBST.remove(15, true); + expect(removed15).toBeInstanceOf(Array); + expect(removed15[0]).toBeDefined(); + expect(removed15[0].deleted).toBeDefined(); + if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); + + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(3); + + const removed5 = objBST.remove(5, true); + expect(removed5).toBeInstanceOf(Array); + expect(removed5[0]).toBeDefined(); + expect(removed5[0].deleted).toBeDefined(); + if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); + + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(3); + + const removed13 = objBST.remove(13, true); + expect(removed13).toBeInstanceOf(Array); + expect(removed13[0]).toBeDefined(); + expect(removed13[0].deleted).toBeDefined(); + if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(3); + + const removed3 = objBST.remove(3, true); + expect(removed3).toBeInstanceOf(Array); + expect(removed3[0]).toBeDefined(); + expect(removed3[0].deleted).toBeDefined(); + if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(3); + + const removed8 = objBST.remove(8, true); + expect(removed8).toBeInstanceOf(Array); + expect(removed8[0]).toBeDefined(); + expect(removed8[0].deleted).toBeDefined(); + if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(3); + + const removed6 = objBST.remove(6, true); + expect(removed6).toBeInstanceOf(Array); + expect(removed6[0]).toBeDefined(); + expect(removed6[0].deleted).toBeDefined(); + if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); + expect(objBST.remove(6, true).length).toBe(0); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(3); + + const removed7 = objBST.remove(7, true); + expect(removed7).toBeInstanceOf(Array); + expect(removed7[0]).toBeDefined(); + expect(removed7[0].deleted).toBeDefined(); + if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(3); + + const removed9 = objBST.remove(9, true); + expect(removed9).toBeInstanceOf(Array); + expect(removed9[0]).toBeDefined(); + expect(removed9[0].deleted).toBeDefined(); + if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(3); + + const removed14 = objBST.remove(14, true); + expect(removed14).toBeInstanceOf(Array); + expect(removed14[0]).toBeDefined(); + expect(removed14[0].deleted).toBeDefined(); + if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(2); + + + expect(objBST.isAVLBalanced()).toBe(false); + + const bfsIDs = objBST.BFS(); + expect(bfsIDs[0]).toBe(2); + expect(bfsIDs[1]).toBe(12); + expect(bfsIDs[2]).toBe(16); + + const bfsNodes = objBST.BFS('node'); + expect(bfsNodes[0].id).toBe(2); + expect(bfsNodes[1].id).toBe(12); + expect(bfsNodes[2].id).toBe(16); + + }); }); diff --git a/tests/integration/heap.test.js b/tests/integration/heap.test.js index bfb0aa9..f8aa94f 100644 --- a/tests/integration/heap.test.js +++ b/tests/integration/heap.test.js @@ -1,19 +1,19 @@ const {MinHeap} = require('heap-typed'); describe('JS Heap Operation Test', () => { - it('should numeric heap work well', function () { - const minNumHeap = new MinHeap(); - minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9); - expect(minNumHeap.poll()).toBe(0); - expect(minNumHeap.poll()).toBe(1); - expect(minNumHeap.peek()).toBe(2); - expect(minNumHeap.toArray().length).toBe(4); - expect(minNumHeap.toArray()[0]).toBe(2); - expect(minNumHeap.toArray()[1]).toBe(5); - expect(minNumHeap.toArray()[2]).toBe(9); - expect(minNumHeap.toArray()[3]).toBe(6); + it('should numeric heap work well', function () { + const minNumHeap = new MinHeap(); + minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9); + expect(minNumHeap.poll()).toBe(0); + expect(minNumHeap.poll()).toBe(1); + expect(minNumHeap.peek()).toBe(2); + expect(minNumHeap.toArray().length).toBe(4); + expect(minNumHeap.toArray()[0]).toBe(2); + expect(minNumHeap.toArray()[1]).toBe(5); + expect(minNumHeap.toArray()[2]).toBe(9); + expect(minNumHeap.toArray()[3]).toBe(6); - }); + }); }); diff --git a/tests/integration/index.html b/tests/integration/index.html index 73b956a..471464d 100644 --- a/tests/integration/index.html +++ b/tests/integration/index.html @@ -1,31 +1,45 @@ - - CDN Test + + CDN Test - + -
-
    +
    +
      -
    -
    - + avlTree.add(1, 1); + console.log(avlTree.BFS()); + $modules.append($avlTree); + + + const queue = new ArrayQueue(); + for (let i = 0; i < 1000000; i++) { + queue.enqueue(i); + } + let last = 0; + const startTime = performance.now(); + + for (let i = 0; i < 1000000; i++) { + last = queue.dequeue(); + } + + console.log(performance.now() - startTime) + - \ No newline at end of file + diff --git a/tests/unit/data-structures/binary-tree/avl-tree.test.ts b/tests/unit/data-structures/binary-tree/avl-tree.test.ts index b568f0e..007ea37 100644 --- a/tests/unit/data-structures/binary-tree/avl-tree.test.ts +++ b/tests/unit/data-structures/binary-tree/avl-tree.test.ts @@ -1,111 +1,111 @@ import {AVLTree} from '../../../../src'; describe('AVL Tree Test', () => { - it('should perform various operations on a AVL Tree', () => { + it('should perform various operations on a AVL Tree', () => { - const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; - const tree = new AVLTree(); + const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; + const tree = new AVLTree(); - for (const i of arr) tree.add(i, i); + for (const i of arr) tree.add(i, i); - const node6 = tree.get(6); + const node6 = tree.get(6); - expect(node6 && tree.getHeight(node6)).toBe(3); - expect(node6 && tree.getDepth(node6)).toBe(1); + expect(node6 && tree.getHeight(node6)).toBe(3); + expect(node6 && tree.getDepth(node6)).toBe(1); - const getNodeById = tree.get(10, 'id'); - expect(getNodeById?.id).toBe(10); + const getNodeById = tree.get(10, 'id'); + expect(getNodeById?.id).toBe(10); - const getMinNodeByRoot = tree.getLeftMost(); - expect(getMinNodeByRoot?.id).toBe(1); + const getMinNodeByRoot = tree.getLeftMost(); + expect(getMinNodeByRoot?.id).toBe(1); - const node15 = tree.get(15); - const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15); - expect(getMinNodeBySpecificNode?.id).toBe(12); + const node15 = tree.get(15); + const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15); + expect(getMinNodeBySpecificNode?.id).toBe(12); - const subTreeSum = node15 && tree.subTreeSum(node15); - expect(subTreeSum).toBe(70); + const subTreeSum = node15 && tree.subTreeSum(node15); + expect(subTreeSum).toBe(70); - const lesserSum = tree.lesserSum(10); - expect(lesserSum).toBe(45); + const lesserSum = tree.lesserSum(10); + expect(lesserSum).toBe(45); - // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class. - expect(node15?.val).toBe(15); + // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class. + expect(node15?.val).toBe(15); - const dfs = tree.DFS('in', 'node'); - expect(dfs[0].id).toBe(1); - expect(dfs[dfs.length - 1].id).toBe(16); + const dfs = tree.DFS('in', 'node'); + expect(dfs[0].id).toBe(1); + expect(dfs[dfs.length - 1].id).toBe(16); - tree.perfectlyBalance(); - const bfs = tree.BFS('node'); - expect(tree.isPerfectlyBalanced()).toBe(true); - expect(bfs[0].id).toBe(8); - expect(bfs[bfs.length - 1].id).toBe(16); + tree.perfectlyBalance(); + const bfs = tree.BFS('node'); + expect(tree.isPerfectlyBalanced()).toBe(true); + expect(bfs[0].id).toBe(8); + expect(bfs[bfs.length - 1].id).toBe(16); - expect(tree.remove(11, true)[0].deleted?.id).toBe(11); - expect(tree.isAVLBalanced()).toBe(true); - expect(node15 && tree.getHeight(node15)).toBe(2); + expect(tree.remove(11, true)[0].deleted?.id).toBe(11); + expect(tree.isAVLBalanced()).toBe(true); + expect(node15 && tree.getHeight(node15)).toBe(2); - expect(tree.remove(1, true)[0].deleted?.id).toBe(1); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(4); + expect(tree.remove(1, true)[0].deleted?.id).toBe(1); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(4); - expect(tree.remove(4, true)[0].deleted?.id).toBe(4); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(4); + expect(tree.remove(4, true)[0].deleted?.id).toBe(4); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(4); - expect(tree.remove(10, true)[0].deleted?.id).toBe(10); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(10, true)[0].deleted?.id).toBe(10); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(15, true)[0].deleted?.id).toBe(15); - expect(tree.isAVLBalanced()).toBe(true); + expect(tree.remove(15, true)[0].deleted?.id).toBe(15); + expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(5, true)[0].deleted?.id).toBe(5); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(5, true)[0].deleted?.id).toBe(5); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(13, true)[0].deleted?.id).toBe(13); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(13, true)[0].deleted?.id).toBe(13); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(3, true)[0].deleted?.id).toBe(3); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(3, true)[0].deleted?.id).toBe(3); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(8, true)[0].deleted?.id).toBe(8); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(3); + expect(tree.remove(8, true)[0].deleted?.id).toBe(8); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(3); - expect(tree.remove(6, true)[0].deleted?.id).toBe(6); - expect(tree.remove(6, true).length).toBe(0); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(2); + expect(tree.remove(6, true)[0].deleted?.id).toBe(6); + expect(tree.remove(6, true).length).toBe(0); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(2); - expect(tree.remove(7, true)[0].deleted?.id).toBe(7); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(2); + expect(tree.remove(7, true)[0].deleted?.id).toBe(7); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(2); - expect(tree.remove(9, true)[0].deleted?.id).toBe(9); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(2); - expect(tree.remove(14, true)[0].deleted?.id).toBe(14); - expect(tree.isAVLBalanced()).toBe(true); - expect(tree.getHeight()).toBe(1); + expect(tree.remove(9, true)[0].deleted?.id).toBe(9); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(2); + expect(tree.remove(14, true)[0].deleted?.id).toBe(14); + expect(tree.isAVLBalanced()).toBe(true); + expect(tree.getHeight()).toBe(1); - expect(tree.isAVLBalanced()).toBe(true); - const lastBFSIds = tree.BFS(); - expect(lastBFSIds[0]).toBe(12); - expect(lastBFSIds[1]).toBe(2); - expect(lastBFSIds[2]).toBe(16); + expect(tree.isAVLBalanced()).toBe(true); + const lastBFSIds = tree.BFS(); + expect(lastBFSIds[0]).toBe(12); + expect(lastBFSIds[1]).toBe(2); + expect(lastBFSIds[2]).toBe(16); - const lastBFSNodes = tree.BFS('node'); - expect(lastBFSNodes[0].id).toBe(12); - expect(lastBFSNodes[1].id).toBe(2); - expect(lastBFSNodes[2].id).toBe(16); - }); + const lastBFSNodes = tree.BFS('node'); + expect(lastBFSNodes[0].id).toBe(12); + expect(lastBFSNodes[1].id).toBe(2); + expect(lastBFSNodes[2].id).toBe(16); + }); }); diff --git a/tests/unit/data-structures/binary-tree/bst.test.ts b/tests/unit/data-structures/binary-tree/bst.test.ts index 074addd..d74da9e 100644 --- a/tests/unit/data-structures/binary-tree/bst.test.ts +++ b/tests/unit/data-structures/binary-tree/bst.test.ts @@ -1,371 +1,371 @@ import {BST, BSTNode} from '../../../../src'; describe('BST operations test', () => { - it('should perform various operations on a Binary Search Tree with numeric values', () => { - const bst = new BST(); - expect(bst).toBeInstanceOf(BST); - bst.add(11, 11); - bst.add(3, 3); - const idsAndValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; - bst.addMany(idsAndValues, idsAndValues); - expect(bst.root).toBeInstanceOf(BSTNode); + it('should perform various operations on a Binary Search Tree with numeric values', () => { + const bst = new BST(); + expect(bst).toBeInstanceOf(BST); + bst.add(11, 11); + bst.add(3, 3); + const idsAndValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; + bst.addMany(idsAndValues, idsAndValues); + expect(bst.root).toBeInstanceOf(BSTNode); - if (bst.root) expect(bst.root.id).toBe(11); + if (bst.root) expect(bst.root.id).toBe(11); - expect(bst.size).toBe(16); + expect(bst.size).toBe(16); - expect(bst.has(6)).toBe(true); + expect(bst.has(6)).toBe(true); - const node6 = bst.get(6); - expect(node6 && bst.getHeight(6)).toBe(2); - expect(node6 && bst.getDepth(6)).toBe(3); + const node6 = bst.get(6); + expect(node6 && bst.getHeight(6)).toBe(2); + expect(node6 && bst.getDepth(6)).toBe(3); - const nodeId10 = bst.get(10); - expect(nodeId10?.id).toBe(10); + const nodeId10 = bst.get(10); + expect(nodeId10?.id).toBe(10); - const nodeVal9 = bst.get(9, 'val'); - expect(nodeVal9?.id).toBe(9); + const nodeVal9 = bst.get(9, 'val'); + expect(nodeVal9?.id).toBe(9); - const leftMost = bst.getLeftMost(); - expect(leftMost?.id).toBe(1); + const leftMost = bst.getLeftMost(); + expect(leftMost?.id).toBe(1); - const node15 = bst.get(15); - const minNodeBySpecificNode = node15 && bst.getLeftMost(node15); - expect(minNodeBySpecificNode?.id).toBe(12); - - const subTreeSum = node15 && bst.subTreeSum(15); - expect(subTreeSum).toBe(70); - - const lesserSum = bst.lesserSum(10); - expect(lesserSum).toBe(45); - - expect(node15).toBeInstanceOf(BSTNode); - - const node11 = bst.get(11); - expect(node11).toBeInstanceOf(BSTNode); - - const dfsInorderNodes = bst.DFS('in', 'node'); - expect(dfsInorderNodes[0].id).toBe(1); - expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); - - bst.perfectlyBalance(); - expect(bst.isPerfectlyBalanced()).toBe(true); - - const bfsNodesAfterBalanced = bst.BFS('node'); - expect(bfsNodesAfterBalanced[0].id).toBe(8); - expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); - - const removed11 = bst.remove(11, true); - expect(removed11).toBeInstanceOf(Array); - expect(removed11[0]).toBeDefined(); - expect(removed11[0].deleted).toBeDefined(); - - if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); - - expect(bst.isAVLBalanced()).toBe(true); - - expect(bst.getHeight(15)).toBe(1); - - const removed1 = bst.remove(1, true); - expect(removed1).toBeInstanceOf(Array); - expect(removed1[0]).toBeDefined(); - expect(removed1[0].deleted).toBeDefined(); - if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); - - expect(bst.isAVLBalanced()).toBe(true); - - expect(bst.getHeight()).toBe(4); - - const removed4 = bst.remove(4, true); - expect(removed4).toBeInstanceOf(Array); - expect(removed4[0]).toBeDefined(); - expect(removed4[0].deleted).toBeDefined(); - if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(4); - - const removed10 = bst.remove(10, true); - expect(removed10).toBeInstanceOf(Array); - expect(removed10[0]).toBeDefined(); - expect(removed10[0].deleted).toBeDefined(); - if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(4); - - const removed15 = bst.remove(15, true); - expect(removed15).toBeInstanceOf(Array); - expect(removed15[0]).toBeDefined(); - expect(removed15[0].deleted).toBeDefined(); - if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); - - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(3); - - const removed5 = bst.remove(5, true); - expect(removed5).toBeInstanceOf(Array); - expect(removed5[0]).toBeDefined(); - expect(removed5[0].deleted).toBeDefined(); - if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); - - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(3); - - const removed13 = bst.remove(13, true); - expect(removed13).toBeInstanceOf(Array); - expect(removed13[0]).toBeDefined(); - expect(removed13[0].deleted).toBeDefined(); - if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(3); - - const removed3 = bst.remove(3, true); - expect(removed3).toBeInstanceOf(Array); - expect(removed3[0]).toBeDefined(); - expect(removed3[0].deleted).toBeDefined(); - if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(3); - - const removed8 = bst.remove(8, true); - expect(removed8).toBeInstanceOf(Array); - expect(removed8[0]).toBeDefined(); - expect(removed8[0].deleted).toBeDefined(); - if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); - expect(bst.isAVLBalanced()).toBe(true); - expect(bst.getHeight()).toBe(3); - - const removed6 = bst.remove(6, true); - expect(removed6).toBeInstanceOf(Array); - expect(removed6[0]).toBeDefined(); - expect(removed6[0].deleted).toBeDefined(); - if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); - expect(bst.remove(6, true).length).toBe(0); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(3); - - const removed7 = bst.remove(7, true); - expect(removed7).toBeInstanceOf(Array); - expect(removed7[0]).toBeDefined(); - expect(removed7[0].deleted).toBeDefined(); - if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(3); - - const removed9 = bst.remove(9, true); - expect(removed9).toBeInstanceOf(Array); - expect(removed9[0]).toBeDefined(); - expect(removed9[0].deleted).toBeDefined(); - if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(3); - - const removed14 = bst.remove(14, true); - expect(removed14).toBeInstanceOf(Array); - expect(removed14[0]).toBeDefined(); - expect(removed14[0].deleted).toBeDefined(); - if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); - expect(bst.isAVLBalanced()).toBe(false); - expect(bst.getHeight()).toBe(2); - - - expect(bst.isAVLBalanced()).toBe(false); - - const bfsIDs = bst.BFS(); - expect(bfsIDs[0]).toBe(2); - expect(bfsIDs[1]).toBe(12); - expect(bfsIDs[2]).toBe(16); - - const bfsNodes = bst.BFS('node'); - expect(bfsNodes[0].id).toBe(2); - expect(bfsNodes[1].id).toBe(12); - expect(bfsNodes[2].id).toBe(16); - - }); - - it('should perform various operations on a Binary Search Tree with object values', () => { - const objBST = new BST>(); - expect(objBST).toBeInstanceOf(BST); - objBST.add(11, {id: 11, keyA: 11}); - objBST.add(3, {id: 3, keyA: 3}); - const values = [{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8}, - {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2}, - {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12}, - {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7}, - {id: 10, keyA: 10}, {id: 5, keyA: 5}]; - - objBST.addMany(values.map(item => item.id), values); - - expect(objBST.root).toBeInstanceOf(BSTNode); - - if (objBST.root) expect(objBST.root.id).toBe(11); - - expect(objBST.has(6)).toBe(true); - - const node6 = objBST.get(6); - expect(node6 && objBST.getHeight(node6)).toBe(2); - expect(node6 && objBST.getDepth(node6)).toBe(3); - - const nodeId10 = objBST.get(10, 'id'); - expect(nodeId10?.id).toBe(10); - - const nodeVal9 = objBST.get(9, 'id'); - expect(nodeVal9?.id).toBe(9); - - const leftMost = objBST.getLeftMost(); - expect(leftMost?.id).toBe(1); - - const node15 = objBST.get(15); - expect(node15?.val).toEqual({id: 15, keyA: 15}); - const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15); - expect(minNodeBySpecificNode?.id).toBe(12); - - const subTreeSum = node15 && objBST.subTreeSum(node15); - expect(subTreeSum).toBe(70); - - const lesserSum = objBST.lesserSum(10); - expect(lesserSum).toBe(45); - - expect(node15).toBeInstanceOf(BSTNode); - - const node11 = objBST.get(11); - expect(node11).toBeInstanceOf(BSTNode); - - const dfsInorderNodes = objBST.DFS('in', 'node'); - expect(dfsInorderNodes[0].id).toBe(1); - expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); - - objBST.perfectlyBalance(); - expect(objBST.isPerfectlyBalanced()).toBe(true); - - const bfsNodesAfterBalanced = objBST.BFS('node'); - expect(bfsNodesAfterBalanced[0].id).toBe(8); - expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); - - const removed11 = objBST.remove(11, true); - expect(removed11).toBeInstanceOf(Array); - expect(removed11[0]).toBeDefined(); - expect(removed11[0].deleted).toBeDefined(); - - if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); - - expect(objBST.isAVLBalanced()).toBe(true); - - expect(node15 && objBST.getHeight(node15)).toBe(2); - - const removed1 = objBST.remove(1, true); - expect(removed1).toBeInstanceOf(Array); - expect(removed1[0]).toBeDefined(); - expect(removed1[0].deleted).toBeDefined(); - if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); - - expect(objBST.isAVLBalanced()).toBe(true); - - expect(objBST.getHeight()).toBe(4); - - const removed4 = objBST.remove(4, true); - expect(removed4).toBeInstanceOf(Array); - expect(removed4[0]).toBeDefined(); - expect(removed4[0].deleted).toBeDefined(); - if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(4); - - const removed10 = objBST.remove(10, true); - expect(removed10).toBeInstanceOf(Array); - expect(removed10[0]).toBeDefined(); - expect(removed10[0].deleted).toBeDefined(); - if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(4); - - const removed15 = objBST.remove(15, true); - expect(removed15).toBeInstanceOf(Array); - expect(removed15[0]).toBeDefined(); - expect(removed15[0].deleted).toBeDefined(); - if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); - - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(3); - - const removed5 = objBST.remove(5, true); - expect(removed5).toBeInstanceOf(Array); - expect(removed5[0]).toBeDefined(); - expect(removed5[0].deleted).toBeDefined(); - if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); - - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(3); - - const removed13 = objBST.remove(13, true); - expect(removed13).toBeInstanceOf(Array); - expect(removed13[0]).toBeDefined(); - expect(removed13[0].deleted).toBeDefined(); - if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(3); - - const removed3 = objBST.remove(3, true); - expect(removed3).toBeInstanceOf(Array); - expect(removed3[0]).toBeDefined(); - expect(removed3[0].deleted).toBeDefined(); - if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(3); - - const removed8 = objBST.remove(8, true); - expect(removed8).toBeInstanceOf(Array); - expect(removed8[0]).toBeDefined(); - expect(removed8[0].deleted).toBeDefined(); - if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); - expect(objBST.isAVLBalanced()).toBe(true); - expect(objBST.getHeight()).toBe(3); - - const removed6 = objBST.remove(6, true); - expect(removed6).toBeInstanceOf(Array); - expect(removed6[0]).toBeDefined(); - expect(removed6[0].deleted).toBeDefined(); - if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); - expect(objBST.remove(6, true).length).toBe(0); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(3); - - const removed7 = objBST.remove(7, true); - expect(removed7).toBeInstanceOf(Array); - expect(removed7[0]).toBeDefined(); - expect(removed7[0].deleted).toBeDefined(); - if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(3); - - const removed9 = objBST.remove(9, true); - expect(removed9).toBeInstanceOf(Array); - expect(removed9[0]).toBeDefined(); - expect(removed9[0].deleted).toBeDefined(); - if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(3); - - const removed14 = objBST.remove(14, true); - expect(removed14).toBeInstanceOf(Array); - expect(removed14[0]).toBeDefined(); - expect(removed14[0].deleted).toBeDefined(); - if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); - expect(objBST.isAVLBalanced()).toBe(false); - expect(objBST.getHeight()).toBe(2); - - - expect(objBST.isAVLBalanced()).toBe(false); - - const bfsIDs = objBST.BFS(); - expect(bfsIDs[0]).toBe(2); - expect(bfsIDs[1]).toBe(12); - expect(bfsIDs[2]).toBe(16); - - const bfsNodes = objBST.BFS('node'); - expect(bfsNodes[0].id).toBe(2); - expect(bfsNodes[1].id).toBe(12); - expect(bfsNodes[2].id).toBe(16); - - }); + const node15 = bst.get(15); + const minNodeBySpecificNode = node15 && bst.getLeftMost(node15); + expect(minNodeBySpecificNode?.id).toBe(12); + + const subTreeSum = node15 && bst.subTreeSum(15); + expect(subTreeSum).toBe(70); + + const lesserSum = bst.lesserSum(10); + expect(lesserSum).toBe(45); + + expect(node15).toBeInstanceOf(BSTNode); + + const node11 = bst.get(11); + expect(node11).toBeInstanceOf(BSTNode); + + const dfsInorderNodes = bst.DFS('in', 'node'); + expect(dfsInorderNodes[0].id).toBe(1); + expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); + + bst.perfectlyBalance(); + expect(bst.isPerfectlyBalanced()).toBe(true); + + const bfsNodesAfterBalanced = bst.BFS('node'); + expect(bfsNodesAfterBalanced[0].id).toBe(8); + expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); + + const removed11 = bst.remove(11, true); + expect(removed11).toBeInstanceOf(Array); + expect(removed11[0]).toBeDefined(); + expect(removed11[0].deleted).toBeDefined(); + + if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); + + expect(bst.isAVLBalanced()).toBe(true); + + expect(bst.getHeight(15)).toBe(1); + + const removed1 = bst.remove(1, true); + expect(removed1).toBeInstanceOf(Array); + expect(removed1[0]).toBeDefined(); + expect(removed1[0].deleted).toBeDefined(); + if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); + + expect(bst.isAVLBalanced()).toBe(true); + + expect(bst.getHeight()).toBe(4); + + const removed4 = bst.remove(4, true); + expect(removed4).toBeInstanceOf(Array); + expect(removed4[0]).toBeDefined(); + expect(removed4[0].deleted).toBeDefined(); + if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(4); + + const removed10 = bst.remove(10, true); + expect(removed10).toBeInstanceOf(Array); + expect(removed10[0]).toBeDefined(); + expect(removed10[0].deleted).toBeDefined(); + if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(4); + + const removed15 = bst.remove(15, true); + expect(removed15).toBeInstanceOf(Array); + expect(removed15[0]).toBeDefined(); + expect(removed15[0].deleted).toBeDefined(); + if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); + + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(3); + + const removed5 = bst.remove(5, true); + expect(removed5).toBeInstanceOf(Array); + expect(removed5[0]).toBeDefined(); + expect(removed5[0].deleted).toBeDefined(); + if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); + + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(3); + + const removed13 = bst.remove(13, true); + expect(removed13).toBeInstanceOf(Array); + expect(removed13[0]).toBeDefined(); + expect(removed13[0].deleted).toBeDefined(); + if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(3); + + const removed3 = bst.remove(3, true); + expect(removed3).toBeInstanceOf(Array); + expect(removed3[0]).toBeDefined(); + expect(removed3[0].deleted).toBeDefined(); + if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(3); + + const removed8 = bst.remove(8, true); + expect(removed8).toBeInstanceOf(Array); + expect(removed8[0]).toBeDefined(); + expect(removed8[0].deleted).toBeDefined(); + if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); + expect(bst.isAVLBalanced()).toBe(true); + expect(bst.getHeight()).toBe(3); + + const removed6 = bst.remove(6, true); + expect(removed6).toBeInstanceOf(Array); + expect(removed6[0]).toBeDefined(); + expect(removed6[0].deleted).toBeDefined(); + if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); + expect(bst.remove(6, true).length).toBe(0); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(3); + + const removed7 = bst.remove(7, true); + expect(removed7).toBeInstanceOf(Array); + expect(removed7[0]).toBeDefined(); + expect(removed7[0].deleted).toBeDefined(); + if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(3); + + const removed9 = bst.remove(9, true); + expect(removed9).toBeInstanceOf(Array); + expect(removed9[0]).toBeDefined(); + expect(removed9[0].deleted).toBeDefined(); + if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(3); + + const removed14 = bst.remove(14, true); + expect(removed14).toBeInstanceOf(Array); + expect(removed14[0]).toBeDefined(); + expect(removed14[0].deleted).toBeDefined(); + if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); + expect(bst.isAVLBalanced()).toBe(false); + expect(bst.getHeight()).toBe(2); + + + expect(bst.isAVLBalanced()).toBe(false); + + const bfsIDs = bst.BFS(); + expect(bfsIDs[0]).toBe(2); + expect(bfsIDs[1]).toBe(12); + expect(bfsIDs[2]).toBe(16); + + const bfsNodes = bst.BFS('node'); + expect(bfsNodes[0].id).toBe(2); + expect(bfsNodes[1].id).toBe(12); + expect(bfsNodes[2].id).toBe(16); + + }); + + it('should perform various operations on a Binary Search Tree with object values', () => { + const objBST = new BST>(); + expect(objBST).toBeInstanceOf(BST); + objBST.add(11, {id: 11, keyA: 11}); + objBST.add(3, {id: 3, keyA: 3}); + const values = [{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8}, + {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2}, + {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12}, + {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7}, + {id: 10, keyA: 10}, {id: 5, keyA: 5}]; + + objBST.addMany(values.map(item => item.id), values); + + expect(objBST.root).toBeInstanceOf(BSTNode); + + if (objBST.root) expect(objBST.root.id).toBe(11); + + expect(objBST.has(6)).toBe(true); + + const node6 = objBST.get(6); + expect(node6 && objBST.getHeight(node6)).toBe(2); + expect(node6 && objBST.getDepth(node6)).toBe(3); + + const nodeId10 = objBST.get(10, 'id'); + expect(nodeId10?.id).toBe(10); + + const nodeVal9 = objBST.get(9, 'id'); + expect(nodeVal9?.id).toBe(9); + + const leftMost = objBST.getLeftMost(); + expect(leftMost?.id).toBe(1); + + const node15 = objBST.get(15); + expect(node15?.val).toEqual({id: 15, keyA: 15}); + const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15); + expect(minNodeBySpecificNode?.id).toBe(12); + + const subTreeSum = node15 && objBST.subTreeSum(node15); + expect(subTreeSum).toBe(70); + + const lesserSum = objBST.lesserSum(10); + expect(lesserSum).toBe(45); + + expect(node15).toBeInstanceOf(BSTNode); + + const node11 = objBST.get(11); + expect(node11).toBeInstanceOf(BSTNode); + + const dfsInorderNodes = objBST.DFS('in', 'node'); + expect(dfsInorderNodes[0].id).toBe(1); + expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); + + objBST.perfectlyBalance(); + expect(objBST.isPerfectlyBalanced()).toBe(true); + + const bfsNodesAfterBalanced = objBST.BFS('node'); + expect(bfsNodesAfterBalanced[0].id).toBe(8); + expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); + + const removed11 = objBST.remove(11, true); + expect(removed11).toBeInstanceOf(Array); + expect(removed11[0]).toBeDefined(); + expect(removed11[0].deleted).toBeDefined(); + + if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); + + expect(objBST.isAVLBalanced()).toBe(true); + + expect(node15 && objBST.getHeight(node15)).toBe(2); + + const removed1 = objBST.remove(1, true); + expect(removed1).toBeInstanceOf(Array); + expect(removed1[0]).toBeDefined(); + expect(removed1[0].deleted).toBeDefined(); + if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); + + expect(objBST.isAVLBalanced()).toBe(true); + + expect(objBST.getHeight()).toBe(4); + + const removed4 = objBST.remove(4, true); + expect(removed4).toBeInstanceOf(Array); + expect(removed4[0]).toBeDefined(); + expect(removed4[0].deleted).toBeDefined(); + if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(4); + + const removed10 = objBST.remove(10, true); + expect(removed10).toBeInstanceOf(Array); + expect(removed10[0]).toBeDefined(); + expect(removed10[0].deleted).toBeDefined(); + if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(4); + + const removed15 = objBST.remove(15, true); + expect(removed15).toBeInstanceOf(Array); + expect(removed15[0]).toBeDefined(); + expect(removed15[0].deleted).toBeDefined(); + if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); + + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(3); + + const removed5 = objBST.remove(5, true); + expect(removed5).toBeInstanceOf(Array); + expect(removed5[0]).toBeDefined(); + expect(removed5[0].deleted).toBeDefined(); + if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); + + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(3); + + const removed13 = objBST.remove(13, true); + expect(removed13).toBeInstanceOf(Array); + expect(removed13[0]).toBeDefined(); + expect(removed13[0].deleted).toBeDefined(); + if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(3); + + const removed3 = objBST.remove(3, true); + expect(removed3).toBeInstanceOf(Array); + expect(removed3[0]).toBeDefined(); + expect(removed3[0].deleted).toBeDefined(); + if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(3); + + const removed8 = objBST.remove(8, true); + expect(removed8).toBeInstanceOf(Array); + expect(removed8[0]).toBeDefined(); + expect(removed8[0].deleted).toBeDefined(); + if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); + expect(objBST.isAVLBalanced()).toBe(true); + expect(objBST.getHeight()).toBe(3); + + const removed6 = objBST.remove(6, true); + expect(removed6).toBeInstanceOf(Array); + expect(removed6[0]).toBeDefined(); + expect(removed6[0].deleted).toBeDefined(); + if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); + expect(objBST.remove(6, true).length).toBe(0); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(3); + + const removed7 = objBST.remove(7, true); + expect(removed7).toBeInstanceOf(Array); + expect(removed7[0]).toBeDefined(); + expect(removed7[0].deleted).toBeDefined(); + if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(3); + + const removed9 = objBST.remove(9, true); + expect(removed9).toBeInstanceOf(Array); + expect(removed9[0]).toBeDefined(); + expect(removed9[0].deleted).toBeDefined(); + if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(3); + + const removed14 = objBST.remove(14, true); + expect(removed14).toBeInstanceOf(Array); + expect(removed14[0]).toBeDefined(); + expect(removed14[0].deleted).toBeDefined(); + if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); + expect(objBST.isAVLBalanced()).toBe(false); + expect(objBST.getHeight()).toBe(2); + + + expect(objBST.isAVLBalanced()).toBe(false); + + const bfsIDs = objBST.BFS(); + expect(bfsIDs[0]).toBe(2); + expect(bfsIDs[1]).toBe(12); + expect(bfsIDs[2]).toBe(16); + + const bfsNodes = objBST.BFS('node'); + expect(bfsNodes[0].id).toBe(2); + expect(bfsNodes[1].id).toBe(12); + expect(bfsNodes[2].id).toBe(16); + + }); }); diff --git a/tests/unit/data-structures/binary-tree/overall.test.ts b/tests/unit/data-structures/binary-tree/overall.test.ts index 3e5f1f9..aa8f49d 100644 --- a/tests/unit/data-structures/binary-tree/overall.test.ts +++ b/tests/unit/data-structures/binary-tree/overall.test.ts @@ -1,57 +1,57 @@ import {AVLTree, BST, BSTNode} from '../../../../src'; describe('Overall BinaryTree Test', () => { - it('should perform various operations on BinaryTree', () => { - const bst = new BST(); - bst.add(11); - bst.add(3); - bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]); - bst.size === 16; // true - expect(bst.size).toBe(16); // true - bst.has(6); // true - expect(bst.has(6)).toBe(true); // true - const node6 = bst.get(6); - bst.getHeight(6) === 2; // true - bst.getHeight() === 5; // true - bst.getDepth(6) === 3; // true - expect(bst.getHeight(6)).toBe(2); // true - expect(bst.getHeight()).toBe(5); // true - expect(bst.getDepth(6)).toBe(3); // true - const leftMost = bst.getLeftMost(); - leftMost?.id === 1; // true - expect(leftMost?.id).toBe(1); - bst.remove(6); - bst.get(6); // null - expect(bst.get(6)).toBeNull(); - bst.isAVLBalanced(); // true or false - expect(bst.isAVLBalanced()).toBe(true); - const bfsIDs = bst.BFS(); - bfsIDs[0] === 11; // true - expect(bfsIDs[0]).toBe(11); + it('should perform various operations on BinaryTree', () => { + const bst = new BST(); + bst.add(11); + bst.add(3); + bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]); + bst.size === 16; // true + expect(bst.size).toBe(16); // true + bst.has(6); // true + expect(bst.has(6)).toBe(true); // true + const node6 = bst.get(6); + bst.getHeight(6) === 2; // true + bst.getHeight() === 5; // true + bst.getDepth(6) === 3; // true + expect(bst.getHeight(6)).toBe(2); // true + expect(bst.getHeight()).toBe(5); // true + expect(bst.getDepth(6)).toBe(3); // true + const leftMost = bst.getLeftMost(); + leftMost?.id === 1; // true + expect(leftMost?.id).toBe(1); + bst.remove(6); + bst.get(6); // null + expect(bst.get(6)).toBeNull(); + bst.isAVLBalanced(); // true or false + expect(bst.isAVLBalanced()).toBe(true); + const bfsIDs = bst.BFS(); + bfsIDs[0] === 11; // true + expect(bfsIDs[0]).toBe(11); - const objBST = new BST>(); - objBST.add(11, {id: 11, keyA: 11}); - objBST.add(3, {id: 3, keyA: 3}); + const objBST = new BST>(); + objBST.add(11, {id: 11, keyA: 11}); + objBST.add(3, {id: 3, keyA: 3}); - objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], - [ - {id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8}, - {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2}, - {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12}, - {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7}, - {id: 10, keyA: 10}, {id: 5, keyA: 5} - ]); + objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], + [ + {id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8}, + {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2}, + {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12}, + {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7}, + {id: 10, keyA: 10}, {id: 5, keyA: 5} + ]); - objBST.remove(11); + objBST.remove(11); - const avlTree = new AVLTree(); - avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]) - avlTree.isAVLBalanced(); // true - expect(avlTree.isAVLBalanced()).toBe(true); // true - avlTree.remove(10); - avlTree.isAVLBalanced(); // true - expect(avlTree.isAVLBalanced()).toBe(true); // true + const avlTree = new AVLTree(); + avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]) + avlTree.isAVLBalanced(); // true + expect(avlTree.isAVLBalanced()).toBe(true); // true + avlTree.remove(10); + avlTree.isAVLBalanced(); // true + expect(avlTree.isAVLBalanced()).toBe(true); // true - }); + }); }); diff --git a/tests/unit/data-structures/binary-tree/tree-multiset.test.ts b/tests/unit/data-structures/binary-tree/tree-multiset.test.ts index 7d91969..a2b20b3 100644 --- a/tests/unit/data-structures/binary-tree/tree-multiset.test.ts +++ b/tests/unit/data-structures/binary-tree/tree-multiset.test.ts @@ -1,405 +1,405 @@ import {TreeMultiset, TreeMultisetNode} from '../../../../src'; describe('TreeMultiset operations test', () => { - it('should perform various operations on a Binary Search Tree with numeric values', () => { - const treeMultiset = new TreeMultiset(); + it('should perform various operations on a Binary Search Tree with numeric values', () => { + const treeMultiset = new TreeMultiset(); - expect(treeMultiset instanceof TreeMultiset); - treeMultiset.add(11, 11); - treeMultiset.add(3, 3); - const idAndValues = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; - treeMultiset.addMany(idAndValues, idAndValues); - expect(treeMultiset.root instanceof TreeMultisetNode); + expect(treeMultiset instanceof TreeMultiset); + treeMultiset.add(11, 11); + treeMultiset.add(3, 3); + const idAndValues = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; + treeMultiset.addMany(idAndValues, idAndValues); + expect(treeMultiset.root instanceof TreeMultisetNode); - if (treeMultiset.root) expect(treeMultiset.root.id == 11); + if (treeMultiset.root) expect(treeMultiset.root.id == 11); - expect(treeMultiset.size).toBe(16); - expect(treeMultiset.count).toBe(18); - expect(treeMultiset.BFS('id')) + expect(treeMultiset.size).toBe(16); + expect(treeMultiset.count).toBe(18); + expect(treeMultiset.BFS('id')) - expect(treeMultiset.has(6)); + expect(treeMultiset.has(6)); - expect(treeMultiset.getHeight(6)).toBe(3); - expect(treeMultiset.getDepth(6)).toBe(1); - const nodeId10 = treeMultiset.get(10); - expect(nodeId10?.id).toBe(10); + expect(treeMultiset.getHeight(6)).toBe(3); + expect(treeMultiset.getDepth(6)).toBe(1); + const nodeId10 = treeMultiset.get(10); + expect(nodeId10?.id).toBe(10); - const nodeVal9 = treeMultiset.get(9, 'val'); - expect(nodeVal9?.id).toBe(9); + const nodeVal9 = treeMultiset.get(9, 'val'); + expect(nodeVal9?.id).toBe(9); - const nodesByCount1 = treeMultiset.getNodesByCount(1); - expect(nodesByCount1.length).toBe(14); + const nodesByCount1 = treeMultiset.getNodesByCount(1); + expect(nodesByCount1.length).toBe(14); - const nodesByCount2 = treeMultiset.getNodesByCount(2); - expect(nodesByCount2.length).toBe(2); - const leftMost = treeMultiset.getLeftMost(); - expect(leftMost?.id).toBe(1); + const nodesByCount2 = treeMultiset.getNodesByCount(2); + expect(nodesByCount2.length).toBe(2); + const leftMost = treeMultiset.getLeftMost(); + expect(leftMost?.id).toBe(1); - const node15 = treeMultiset.get(15); - const minNodeBySpecificNode = node15 && treeMultiset.getLeftMost(node15); - expect(minNodeBySpecificNode?.id).toBe(12); + const node15 = treeMultiset.get(15); + const minNodeBySpecificNode = node15 && treeMultiset.getLeftMost(node15); + expect(minNodeBySpecificNode?.id).toBe(12); - const subTreeSum = node15 && treeMultiset.subTreeSum(15); - expect(subTreeSum).toBe(70); - const lesserSum = treeMultiset.lesserSum(10); - expect(lesserSum).toBe(45); + const subTreeSum = node15 && treeMultiset.subTreeSum(15); + expect(subTreeSum).toBe(70); + const lesserSum = treeMultiset.lesserSum(10); + expect(lesserSum).toBe(45); - expect(node15 instanceof TreeMultisetNode); - if (node15 instanceof TreeMultisetNode) { - const subTreeAdd = treeMultiset.subTreeAddCount(15, 1); - expect(subTreeAdd); - } - const node11 = treeMultiset.get(11); - expect(node11 instanceof TreeMultisetNode); - if (node11 instanceof TreeMultisetNode) { - const allGreaterNodesAdded = treeMultiset.allGreaterNodesAddCount(11, 2); - expect(allGreaterNodesAdded); - } + expect(node15 instanceof TreeMultisetNode); + if (node15 instanceof TreeMultisetNode) { + const subTreeAdd = treeMultiset.subTreeAddCount(15, 1); + expect(subTreeAdd); + } + const node11 = treeMultiset.get(11); + expect(node11 instanceof TreeMultisetNode); + if (node11 instanceof TreeMultisetNode) { + const allGreaterNodesAdded = treeMultiset.allGreaterNodesAddCount(11, 2); + expect(allGreaterNodesAdded); + } - const dfsInorderNodes = treeMultiset.DFS('in', 'node'); - expect(dfsInorderNodes[0].id).toBe(1); - expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); - expect(treeMultiset.isPerfectlyBalanced()).toBe(false); + const dfsInorderNodes = treeMultiset.DFS('in', 'node'); + expect(dfsInorderNodes[0].id).toBe(1); + expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); + expect(treeMultiset.isPerfectlyBalanced()).toBe(false); - treeMultiset.perfectlyBalance(); + treeMultiset.perfectlyBalance(); - expect(treeMultiset.isPerfectlyBalanced()).toBe(true); - expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.isPerfectlyBalanced()).toBe(true); + expect(treeMultiset.isAVLBalanced()).toBe(true); - const bfsNodesAfterBalanced = treeMultiset.BFS('node'); - expect(bfsNodesAfterBalanced[0].id).toBe(8); - expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); + const bfsNodesAfterBalanced = treeMultiset.BFS('node'); + expect(bfsNodesAfterBalanced[0].id).toBe(8); + expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); - const removed11 = treeMultiset.remove(11, true); - expect(removed11 instanceof Array); - expect(removed11[0]); - expect(removed11[0].deleted); + const removed11 = treeMultiset.remove(11, true); + expect(removed11 instanceof Array); + expect(removed11[0]); + expect(removed11[0].deleted); - if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); + if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); - expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight(15)).toBe(1); + expect(treeMultiset.getHeight(15)).toBe(1); - const removed1 = treeMultiset.remove(1, true); - expect(removed1 instanceof Array); - expect(removed1[0]); - expect(removed1[0].deleted); - if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); + const removed1 = treeMultiset.remove(1, true); + expect(removed1 instanceof Array); + expect(removed1[0]); + expect(removed1[0].deleted); + if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); - expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(4); + expect(treeMultiset.getHeight()).toBe(4); - const removed4 = treeMultiset.remove(4, true); - expect(removed4 instanceof Array); - expect(removed4[0]); - expect(removed4[0].deleted); - if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); + const removed4 = treeMultiset.remove(4, true); + expect(removed4 instanceof Array); + expect(removed4[0]); + expect(removed4[0].deleted); + if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(4); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(4); - const removed10 = treeMultiset.remove(10, true); - expect(removed10 instanceof Array); - expect(removed10[0]); - expect(removed10[0].deleted); - if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); - expect(treeMultiset.isAVLBalanced()).toBe(true); + const removed10 = treeMultiset.remove(10, true); + expect(removed10 instanceof Array); + expect(removed10[0]); + expect(removed10[0].deleted); + if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); + expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(3); + expect(treeMultiset.getHeight()).toBe(3); - const removed15 = treeMultiset.remove(15, true); - expect(removed15 instanceof Array); - expect(removed15[0]); - expect(removed15[0].deleted); - if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); + const removed15 = treeMultiset.remove(15, true); + expect(removed15 instanceof Array); + expect(removed15[0]); + expect(removed15[0].deleted); + if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(3); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(3); - const removed5 = treeMultiset.remove(5, true); - expect(removed5 instanceof Array); - expect(removed5[0]); - expect(removed5[0].deleted); - if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); + const removed5 = treeMultiset.remove(5, true); + expect(removed5 instanceof Array); + expect(removed5[0]); + expect(removed5[0].deleted); + if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(3); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(3); - const removed13 = treeMultiset.remove(13, true); - expect(removed13 instanceof Array); - expect(removed13[0]); - expect(removed13[0].deleted); - if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(3); + const removed13 = treeMultiset.remove(13, true); + expect(removed13 instanceof Array); + expect(removed13[0]); + expect(removed13[0].deleted); + if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(3); - const removed3 = treeMultiset.remove(3, true); - expect(removed3 instanceof Array); - expect(removed3[0]); - expect(removed3[0].deleted); - if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(3); + const removed3 = treeMultiset.remove(3, true); + expect(removed3 instanceof Array); + expect(removed3[0]); + expect(removed3[0].deleted); + if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(3); - const removed8 = treeMultiset.remove(8, true); - expect(removed8 instanceof Array); - expect(removed8[0]); - expect(removed8[0].deleted); - if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(3); + const removed8 = treeMultiset.remove(8, true); + expect(removed8 instanceof Array); + expect(removed8[0]); + expect(removed8[0].deleted); + if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(3); - const removed6 = treeMultiset.remove(6, true); - expect(removed6 instanceof Array); - expect(removed6[0]); - expect(removed6[0].deleted); - if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); - expect(treeMultiset.remove(6, true).length).toBe(0); - expect(treeMultiset.isAVLBalanced()).toBe(true); + const removed6 = treeMultiset.remove(6, true); + expect(removed6 instanceof Array); + expect(removed6[0]); + expect(removed6[0].deleted); + if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); + expect(treeMultiset.remove(6, true).length).toBe(0); + expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(2); + expect(treeMultiset.getHeight()).toBe(2); - const removed7 = treeMultiset.remove(7, true); - expect(removed7 instanceof Array); - expect(removed7[0]); - expect(removed7[0].deleted); - if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(2); + const removed7 = treeMultiset.remove(7, true); + expect(removed7 instanceof Array); + expect(removed7[0]); + expect(removed7[0].deleted); + if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(2); - const removed9 = treeMultiset.remove(9, true); - expect(removed9 instanceof Array); - expect(removed9[0]); - expect(removed9[0].deleted); - if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(2); + const removed9 = treeMultiset.remove(9, true); + expect(removed9 instanceof Array); + expect(removed9[0]); + expect(removed9[0].deleted); + if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(2); - const removed14 = treeMultiset.remove(14, true); - expect(removed14 instanceof Array); - expect(removed14[0]); - expect(removed14[0].deleted); - if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); - expect(treeMultiset.isAVLBalanced()).toBe(true); - expect(treeMultiset.getHeight()).toBe(1); + const removed14 = treeMultiset.remove(14, true); + expect(removed14 instanceof Array); + expect(removed14[0]); + expect(removed14[0].deleted); + if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); + expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.getHeight()).toBe(1); - expect(treeMultiset.isAVLBalanced()).toBe(true); + expect(treeMultiset.isAVLBalanced()).toBe(true); - const bfsIDs = treeMultiset.BFS(); + const bfsIDs = treeMultiset.BFS(); - expect(bfsIDs[0]).toBe(12); - expect(bfsIDs[1]).toBe(2); - expect(bfsIDs[2]).toBe(16); + expect(bfsIDs[0]).toBe(12); + expect(bfsIDs[1]).toBe(2); + expect(bfsIDs[2]).toBe(16); - const bfsNodes = treeMultiset.BFS('node'); + const bfsNodes = treeMultiset.BFS('node'); - expect(bfsNodes[0].id).toBe(12); - expect(bfsNodes[1].id).toBe(2); - expect(bfsNodes[2].id).toBe(16); + expect(bfsNodes[0].id).toBe(12); + expect(bfsNodes[1].id).toBe(2); + expect(bfsNodes[2].id).toBe(16); - expect(treeMultiset.count).toBe(9); - }); + expect(treeMultiset.count).toBe(9); + }); - it('should perform various operations on a Binary Search Tree with object values', () => { - const objTreeMultiset = new TreeMultiset>(); - expect(objTreeMultiset).toBeInstanceOf(TreeMultiset); - objTreeMultiset.add(11, {id: 11, keyA: 11}); - objTreeMultiset.add(3, {id: 3, keyA: 3}); - const values = [{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8}, - {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2}, - {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12}, - {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7}, - {id: 10, keyA: 10}, {id: 5, keyA: 5}]; + it('should perform various operations on a Binary Search Tree with object values', () => { + const objTreeMultiset = new TreeMultiset>(); + expect(objTreeMultiset).toBeInstanceOf(TreeMultiset); + objTreeMultiset.add(11, {id: 11, keyA: 11}); + objTreeMultiset.add(3, {id: 3, keyA: 3}); + const values = [{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8}, + {id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2}, + {id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12}, + {id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7}, + {id: 10, keyA: 10}, {id: 5, keyA: 5}]; - objTreeMultiset.addMany(values.map(item => item.id), values); + objTreeMultiset.addMany(values.map(item => item.id), values); - expect(objTreeMultiset.root).toBeInstanceOf(TreeMultisetNode); + expect(objTreeMultiset.root).toBeInstanceOf(TreeMultisetNode); - if (objTreeMultiset.root) expect(objTreeMultiset.root.id).toBe(11); + if (objTreeMultiset.root) expect(objTreeMultiset.root.id).toBe(11); - expect(objTreeMultiset.count).toBe(16); + expect(objTreeMultiset.count).toBe(16); - expect(objTreeMultiset.has(6)).toBe(true); + expect(objTreeMultiset.has(6)).toBe(true); - const node6 = objTreeMultiset.get(6); - // expect(node6 && objTreeMultiset.getHeight(node6)).toBe(2); - // expect(node6 && objTreeMultiset.getDepth(node6)).toBe(3); - // - // const nodeId10 = objTreeMultiset.get(10, 'id'); - // expect(nodeId10?.id).toBe(10); - // - // const nodeVal9 = objTreeMultiset.get(9, 'id'); - // expect(nodeVal9?.id).toBe(9); - // - // const nodesByCount1 = objTreeMultiset.getNodesByCount(1); - // expect(nodesByCount1.length).toBe(16); - // - // const leftMost = objTreeMultiset.getLeftMost(); - // expect(leftMost?.id).toBe(1); - // - // const node15 = objTreeMultiset.get(15); - // expect(node15?.val).toEqual({id: 15, keyA: 15}); - // const minNodeBySpecificNode = node15 && objTreeMultiset.getLeftMost(node15); - // expect(minNodeBySpecificNode?.id).toBe(12); - // - // const subTreeSum = node15 && objTreeMultiset.subTreeSum(node15); - // expect(subTreeSum).toBe(70); - // - // const lesserSum = objTreeMultiset.lesserSum(10); - // expect(lesserSum).toBe(45); - // - // expect(node15).toBeInstanceOf(TreeMultisetNode); - // if (node15 instanceof TreeMultisetNode) { - // const subTreeAdd = objTreeMultiset.subTreeAddCount(node15, 1); - // expect(subTreeAdd).toBeDefined(); - // } - // - // const node11 = objTreeMultiset.get(11); - // expect(node11).toBeInstanceOf(TreeMultisetNode); - // if (node11 instanceof TreeMultisetNode) { - // const allGreaterNodesAdded = objTreeMultiset.allGreaterNodesAddCount(node11, 2); - // expect(allGreaterNodesAdded).toBeDefined(); - // } - // - // const dfsInorderNodes = objTreeMultiset.DFS('in', 'node'); - // expect(dfsInorderNodes[0].id).toBe(1); - // expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); - // - // objTreeMultiset.perfectlyBalance(); - // expect(objTreeMultiset.isPerfectlyBalanced()).toBe(true); - // - // const bfsNodesAfterBalanced = objTreeMultiset.BFS('node'); - // expect(bfsNodesAfterBalanced[0].id).toBe(8); - // expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); - // - // const removed11 = objTreeMultiset.remove(11, true); - // expect(removed11).toBeInstanceOf(Array); - // expect(removed11[0]).toBeDefined(); - // expect(removed11[0].deleted).toBeDefined(); - // - // if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); - // - // expect(objTreeMultiset.isAVLBalanced()).toBe(true); - // - // expect(node15 && objTreeMultiset.getHeight(node15)).toBe(2); - // - // const removed1 = objTreeMultiset.remove(1, true); - // expect(removed1).toBeInstanceOf(Array); - // expect(removed1[0]).toBeDefined(); - // expect(removed1[0].deleted).toBeDefined(); - // if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); - // - // expect(objTreeMultiset.isAVLBalanced()).toBe(true); - // - // expect(objTreeMultiset.getHeight()).toBe(4); - // - // const removed4 = objTreeMultiset.remove(4, true); - // expect(removed4).toBeInstanceOf(Array); - // expect(removed4[0]).toBeDefined(); - // expect(removed4[0].deleted).toBeDefined(); - // if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); - // expect(objTreeMultiset.isAVLBalanced()).toBe(true); - // expect(objTreeMultiset.getHeight()).toBe(4); - // - // const removed10 = objTreeMultiset.remove(10, true); - // expect(removed10).toBeInstanceOf(Array); - // expect(removed10[0]).toBeDefined(); - // expect(removed10[0].deleted).toBeDefined(); - // if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); - // expect(objTreeMultiset.isAVLBalanced()).toBe(false); - // expect(objTreeMultiset.getHeight()).toBe(4); - // - // const removed15 = objTreeMultiset.remove(15, true); - // expect(removed15).toBeInstanceOf(Array); - // expect(removed15[0]).toBeDefined(); - // expect(removed15[0].deleted).toBeDefined(); - // if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); - // - // expect(objTreeMultiset.isAVLBalanced()).toBe(true); - // expect(objTreeMultiset.getHeight()).toBe(3); - // - // const removed5 = objTreeMultiset.remove(5, true); - // expect(removed5).toBeInstanceOf(Array); - // expect(removed5[0]).toBeDefined(); - // expect(removed5[0].deleted).toBeDefined(); - // if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); - // - // expect(objTreeMultiset.isAVLBalanced()).toBe(true); - // expect(objTreeMultiset.getHeight()).toBe(3); - // - // const removed13 = objTreeMultiset.remove(13, true); - // expect(removed13).toBeInstanceOf(Array); - // expect(removed13[0]).toBeDefined(); - // expect(removed13[0].deleted).toBeDefined(); - // if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); - // expect(objTreeMultiset.isAVLBalanced()).toBe(true); - // expect(objTreeMultiset.getHeight()).toBe(3); - // - // const removed3 = objTreeMultiset.remove(3, true); - // expect(removed3).toBeInstanceOf(Array); - // expect(removed3[0]).toBeDefined(); - // expect(removed3[0].deleted).toBeDefined(); - // if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); - // expect(objTreeMultiset.isAVLBalanced()).toBe(false); - // expect(objTreeMultiset.getHeight()).toBe(3); - // - // const removed8 = objTreeMultiset.remove(8, true); - // expect(removed8).toBeInstanceOf(Array); - // expect(removed8[0]).toBeDefined(); - // expect(removed8[0].deleted).toBeDefined(); - // if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); - // expect(objTreeMultiset.isAVLBalanced()).toBe(true); - // expect(objTreeMultiset.getHeight()).toBe(3); - // - // const removed6 = objTreeMultiset.remove(6, true); - // expect(removed6).toBeInstanceOf(Array); - // expect(removed6[0]).toBeDefined(); - // expect(removed6[0].deleted).toBeDefined(); - // if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); - // expect(objTreeMultiset.remove(6, true).length).toBe(0); - // expect(objTreeMultiset.isAVLBalanced()).toBe(false); - // expect(objTreeMultiset.getHeight()).toBe(3); - // - // const removed7 = objTreeMultiset.remove(7, true); - // expect(removed7).toBeInstanceOf(Array); - // expect(removed7[0]).toBeDefined(); - // expect(removed7[0].deleted).toBeDefined(); - // if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); - // expect(objTreeMultiset.isAVLBalanced()).toBe(false); - // expect(objTreeMultiset.getHeight()).toBe(3); - // - // const removed9 = objTreeMultiset.remove(9, true); - // expect(removed9).toBeInstanceOf(Array); - // expect(removed9[0]).toBeDefined(); - // expect(removed9[0].deleted).toBeDefined(); - // if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); - // expect(objTreeMultiset.isAVLBalanced()).toBe(false); - // expect(objTreeMultiset.getHeight()).toBe(3); - // - // const removed14 = objTreeMultiset.remove(14, true); - // expect(removed14).toBeInstanceOf(Array); - // expect(removed14[0]).toBeDefined(); - // expect(removed14[0].deleted).toBeDefined(); - // if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); - // expect(objTreeMultiset.isAVLBalanced()).toBe(false); - // expect(objTreeMultiset.getHeight()).toBe(2); - // - // - // expect(objTreeMultiset.isAVLBalanced()).toBe(false); - // - // const bfsIDs = objTreeMultiset.BFS(); - // expect(bfsIDs[0]).toBe(2); - // expect(bfsIDs[1]).toBe(12); - // expect(bfsIDs[2]).toBe(16); - // - // const bfsNodes = objTreeMultiset.BFS('node'); - // expect(bfsNodes[0].id).toBe(2); - // expect(bfsNodes[1].id).toBe(12); - // expect(bfsNodes[2].id).toBe(16); - // - // expect(objTreeMultiset.count).toBe(5); - }); + const node6 = objTreeMultiset.get(6); + // expect(node6 && objTreeMultiset.getHeight(node6)).toBe(2); + // expect(node6 && objTreeMultiset.getDepth(node6)).toBe(3); + // + // const nodeId10 = objTreeMultiset.get(10, 'id'); + // expect(nodeId10?.id).toBe(10); + // + // const nodeVal9 = objTreeMultiset.get(9, 'id'); + // expect(nodeVal9?.id).toBe(9); + // + // const nodesByCount1 = objTreeMultiset.getNodesByCount(1); + // expect(nodesByCount1.length).toBe(16); + // + // const leftMost = objTreeMultiset.getLeftMost(); + // expect(leftMost?.id).toBe(1); + // + // const node15 = objTreeMultiset.get(15); + // expect(node15?.val).toEqual({id: 15, keyA: 15}); + // const minNodeBySpecificNode = node15 && objTreeMultiset.getLeftMost(node15); + // expect(minNodeBySpecificNode?.id).toBe(12); + // + // const subTreeSum = node15 && objTreeMultiset.subTreeSum(node15); + // expect(subTreeSum).toBe(70); + // + // const lesserSum = objTreeMultiset.lesserSum(10); + // expect(lesserSum).toBe(45); + // + // expect(node15).toBeInstanceOf(TreeMultisetNode); + // if (node15 instanceof TreeMultisetNode) { + // const subTreeAdd = objTreeMultiset.subTreeAddCount(node15, 1); + // expect(subTreeAdd).toBeDefined(); + // } + // + // const node11 = objTreeMultiset.get(11); + // expect(node11).toBeInstanceOf(TreeMultisetNode); + // if (node11 instanceof TreeMultisetNode) { + // const allGreaterNodesAdded = objTreeMultiset.allGreaterNodesAddCount(node11, 2); + // expect(allGreaterNodesAdded).toBeDefined(); + // } + // + // const dfsInorderNodes = objTreeMultiset.DFS('in', 'node'); + // expect(dfsInorderNodes[0].id).toBe(1); + // expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16); + // + // objTreeMultiset.perfectlyBalance(); + // expect(objTreeMultiset.isPerfectlyBalanced()).toBe(true); + // + // const bfsNodesAfterBalanced = objTreeMultiset.BFS('node'); + // expect(bfsNodesAfterBalanced[0].id).toBe(8); + // expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16); + // + // const removed11 = objTreeMultiset.remove(11, true); + // expect(removed11).toBeInstanceOf(Array); + // expect(removed11[0]).toBeDefined(); + // expect(removed11[0].deleted).toBeDefined(); + // + // if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11); + // + // expect(objTreeMultiset.isAVLBalanced()).toBe(true); + // + // expect(node15 && objTreeMultiset.getHeight(node15)).toBe(2); + // + // const removed1 = objTreeMultiset.remove(1, true); + // expect(removed1).toBeInstanceOf(Array); + // expect(removed1[0]).toBeDefined(); + // expect(removed1[0].deleted).toBeDefined(); + // if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1); + // + // expect(objTreeMultiset.isAVLBalanced()).toBe(true); + // + // expect(objTreeMultiset.getHeight()).toBe(4); + // + // const removed4 = objTreeMultiset.remove(4, true); + // expect(removed4).toBeInstanceOf(Array); + // expect(removed4[0]).toBeDefined(); + // expect(removed4[0].deleted).toBeDefined(); + // if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4); + // expect(objTreeMultiset.isAVLBalanced()).toBe(true); + // expect(objTreeMultiset.getHeight()).toBe(4); + // + // const removed10 = objTreeMultiset.remove(10, true); + // expect(removed10).toBeInstanceOf(Array); + // expect(removed10[0]).toBeDefined(); + // expect(removed10[0].deleted).toBeDefined(); + // if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10); + // expect(objTreeMultiset.isAVLBalanced()).toBe(false); + // expect(objTreeMultiset.getHeight()).toBe(4); + // + // const removed15 = objTreeMultiset.remove(15, true); + // expect(removed15).toBeInstanceOf(Array); + // expect(removed15[0]).toBeDefined(); + // expect(removed15[0].deleted).toBeDefined(); + // if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15); + // + // expect(objTreeMultiset.isAVLBalanced()).toBe(true); + // expect(objTreeMultiset.getHeight()).toBe(3); + // + // const removed5 = objTreeMultiset.remove(5, true); + // expect(removed5).toBeInstanceOf(Array); + // expect(removed5[0]).toBeDefined(); + // expect(removed5[0].deleted).toBeDefined(); + // if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5); + // + // expect(objTreeMultiset.isAVLBalanced()).toBe(true); + // expect(objTreeMultiset.getHeight()).toBe(3); + // + // const removed13 = objTreeMultiset.remove(13, true); + // expect(removed13).toBeInstanceOf(Array); + // expect(removed13[0]).toBeDefined(); + // expect(removed13[0].deleted).toBeDefined(); + // if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13); + // expect(objTreeMultiset.isAVLBalanced()).toBe(true); + // expect(objTreeMultiset.getHeight()).toBe(3); + // + // const removed3 = objTreeMultiset.remove(3, true); + // expect(removed3).toBeInstanceOf(Array); + // expect(removed3[0]).toBeDefined(); + // expect(removed3[0].deleted).toBeDefined(); + // if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3); + // expect(objTreeMultiset.isAVLBalanced()).toBe(false); + // expect(objTreeMultiset.getHeight()).toBe(3); + // + // const removed8 = objTreeMultiset.remove(8, true); + // expect(removed8).toBeInstanceOf(Array); + // expect(removed8[0]).toBeDefined(); + // expect(removed8[0].deleted).toBeDefined(); + // if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8); + // expect(objTreeMultiset.isAVLBalanced()).toBe(true); + // expect(objTreeMultiset.getHeight()).toBe(3); + // + // const removed6 = objTreeMultiset.remove(6, true); + // expect(removed6).toBeInstanceOf(Array); + // expect(removed6[0]).toBeDefined(); + // expect(removed6[0].deleted).toBeDefined(); + // if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6); + // expect(objTreeMultiset.remove(6, true).length).toBe(0); + // expect(objTreeMultiset.isAVLBalanced()).toBe(false); + // expect(objTreeMultiset.getHeight()).toBe(3); + // + // const removed7 = objTreeMultiset.remove(7, true); + // expect(removed7).toBeInstanceOf(Array); + // expect(removed7[0]).toBeDefined(); + // expect(removed7[0].deleted).toBeDefined(); + // if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7); + // expect(objTreeMultiset.isAVLBalanced()).toBe(false); + // expect(objTreeMultiset.getHeight()).toBe(3); + // + // const removed9 = objTreeMultiset.remove(9, true); + // expect(removed9).toBeInstanceOf(Array); + // expect(removed9[0]).toBeDefined(); + // expect(removed9[0].deleted).toBeDefined(); + // if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9); + // expect(objTreeMultiset.isAVLBalanced()).toBe(false); + // expect(objTreeMultiset.getHeight()).toBe(3); + // + // const removed14 = objTreeMultiset.remove(14, true); + // expect(removed14).toBeInstanceOf(Array); + // expect(removed14[0]).toBeDefined(); + // expect(removed14[0].deleted).toBeDefined(); + // if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14); + // expect(objTreeMultiset.isAVLBalanced()).toBe(false); + // expect(objTreeMultiset.getHeight()).toBe(2); + // + // + // expect(objTreeMultiset.isAVLBalanced()).toBe(false); + // + // const bfsIDs = objTreeMultiset.BFS(); + // expect(bfsIDs[0]).toBe(2); + // expect(bfsIDs[1]).toBe(12); + // expect(bfsIDs[2]).toBe(16); + // + // const bfsNodes = objTreeMultiset.BFS('node'); + // expect(bfsNodes[0].id).toBe(2); + // expect(bfsNodes[1].id).toBe(12); + // expect(bfsNodes[2].id).toBe(16); + // + // expect(objTreeMultiset.count).toBe(5); + }); }); diff --git a/tests/unit/data-structures/constants/index.ts b/tests/unit/data-structures/constants/index.ts deleted file mode 100644 index 27b2090..0000000 --- a/tests/unit/data-structures/constants/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './magnitude'; \ No newline at end of file diff --git a/tests/unit/data-structures/constants/magnitude.ts b/tests/unit/data-structures/constants/magnitude.ts deleted file mode 100644 index eaed3fe..0000000 --- a/tests/unit/data-structures/constants/magnitude.ts +++ /dev/null @@ -1,11 +0,0 @@ -const orderReducedBy = 2; // reduction of magnitude's order compared to the baseline magnitude - -export const magnitude = { - CONSTANT: Math.floor(Number.MAX_SAFE_INTEGER / Math.pow(10, orderReducedBy)), - LOG_N: Math.pow(10, 9 - orderReducedBy), - LINEAR: Math.pow(10, 6 - orderReducedBy), - N_LOG_N: Math.pow(10, 5 - orderReducedBy), - SQUARED: Math.pow(10, 4 - orderReducedBy), - CUBED: Math.pow(10, 3 - orderReducedBy), - FACTORIAL: 20 - orderReducedBy -} \ No newline at end of file diff --git a/tests/unit/data-structures/graph/abstract-graph.test.ts b/tests/unit/data-structures/graph/abstract-graph.test.ts index dc5d2c9..8b2fb42 100644 --- a/tests/unit/data-structures/graph/abstract-graph.test.ts +++ b/tests/unit/data-structures/graph/abstract-graph.test.ts @@ -1,5 +1,5 @@ describe('AbstractGraph Operation Test', () => { - it('should xxx', function () { - expect(true).toBe(true); - }); -}); \ No newline at end of file + it('should xxx', function () { + expect(true).toBe(true); + }); +}); diff --git a/tests/unit/data-structures/graph/directed-graph.test.ts b/tests/unit/data-structures/graph/directed-graph.test.ts index fa1b669..d0fafdf 100644 --- a/tests/unit/data-structures/graph/directed-graph.test.ts +++ b/tests/unit/data-structures/graph/directed-graph.test.ts @@ -1,517 +1,517 @@ import {DirectedEdge, DirectedGraph, DirectedVertex, VertexId} from '../../../../src'; describe('DirectedGraph Operation Test', () => { - let graph: DirectedGraph; + let graph: DirectedGraph; - beforeEach(() => { - graph = new DirectedGraph(); - }); + beforeEach(() => { + graph = new DirectedGraph(); + }); - it('should add vertices', () => { - const vertex1 = new DirectedVertex('A'); - const vertex2 = new DirectedVertex('B'); + it('should add vertices', () => { + const vertex1 = new DirectedVertex('A'); + const vertex2 = new DirectedVertex('B'); - graph.addVertex(vertex1); - graph.addVertex(vertex2); + graph.addVertex(vertex1); + graph.addVertex(vertex2); - expect(graph.hasVertex(vertex1)).toBe(true); - expect(graph.hasVertex(vertex2)).toBe(true); - }); + expect(graph.hasVertex(vertex1)).toBe(true); + expect(graph.hasVertex(vertex2)).toBe(true); + }); - it('should add edges', () => { - const vertex1 = new DirectedVertex('A'); - const vertex2 = new DirectedVertex('B'); - const edge = new DirectedEdge('A', 'B'); + it('should add edges', () => { + const vertex1 = new DirectedVertex('A'); + const vertex2 = new DirectedVertex('B'); + const edge = new DirectedEdge('A', 'B'); - graph.addVertex(vertex1); - graph.addVertex(vertex2); - graph.addEdge(edge); + graph.addVertex(vertex1); + graph.addVertex(vertex2); + graph.addEdge(edge); - expect(graph.hasEdge('A', 'B')).toBe(true); - expect(graph.hasEdge('B', 'A')).toBe(false); - }); + expect(graph.hasEdge('A', 'B')).toBe(true); + expect(graph.hasEdge('B', 'A')).toBe(false); + }); - it('should remove edges', () => { - const vertex1 = new DirectedVertex('A'); - const vertex2 = new DirectedVertex('B'); - const edge = new DirectedEdge('A', 'B'); + it('should remove edges', () => { + const vertex1 = new DirectedVertex('A'); + const vertex2 = new DirectedVertex('B'); + const edge = new DirectedEdge('A', 'B'); - graph.addVertex(vertex1); - graph.addVertex(vertex2); - graph.addEdge(edge); + graph.addVertex(vertex1); + graph.addVertex(vertex2); + graph.addEdge(edge); - expect(graph.removeEdge(edge)).toBe(edge); - expect(graph.hasEdge('A', 'B')).toBe(false); - }); + expect(graph.removeEdge(edge)).toBe(edge); + expect(graph.hasEdge('A', 'B')).toBe(false); + }); - it('should perform topological sort', () => { - const vertexA = new DirectedVertex('A'); - const vertexB = new DirectedVertex('B'); - const vertexC = new DirectedVertex('C'); - const edgeAB = new DirectedEdge('A', 'B'); - const edgeBC = new DirectedEdge('B', 'C'); + it('should perform topological sort', () => { + const vertexA = new DirectedVertex('A'); + const vertexB = new DirectedVertex('B'); + const vertexC = new DirectedVertex('C'); + const edgeAB = new DirectedEdge('A', 'B'); + const edgeBC = new DirectedEdge('B', 'C'); - graph.addVertex(vertexA); - graph.addVertex(vertexB); - graph.addVertex(vertexC); - graph.addEdge(edgeAB); - graph.addEdge(edgeBC); + graph.addVertex(vertexA); + graph.addVertex(vertexB); + graph.addVertex(vertexC); + graph.addEdge(edgeAB); + graph.addEdge(edgeBC); - const topologicalOrder = graph.topologicalSort(); - if (topologicalOrder) expect(topologicalOrder).toEqual(['A', 'B', 'C']); - }); + const topologicalOrder = graph.topologicalSort(); + if (topologicalOrder) expect(topologicalOrder).toEqual(['A', 'B', 'C']); + }); }); class MyVertex extends DirectedVertex { - constructor(id: VertexId, val?: V) { - super(id, val); - this._data = val; - } + constructor(id: VertexId, val?: V) { + super(id, val); + this._data = val; + } - private _data: string | undefined; + private _data: string | undefined; - get data(): string | undefined { - return this._data; - } + get data(): string | undefined { + return this._data; + } - set data(value: string | undefined) { - this._data = value; - } + set data(value: string | undefined) { + this._data = value; + } } class MyEdge extends DirectedEdge { - constructor(v1: VertexId, v2: VertexId, weight?: number, val?: E) { - super(v1, v2, weight, val); - this._data = val; - } + constructor(v1: VertexId, v2: VertexId, weight?: number, val?: E) { + super(v1, v2, weight, val); + this._data = val; + } - private _data: string | undefined; + private _data: string | undefined; - get data(): string | undefined { - return this._data; - } + get data(): string | undefined { + return this._data; + } - set data(value: string | undefined) { - this._data = value; - } + set data(value: string | undefined) { + this._data = value; + } } class MyDirectedGraph, E extends MyEdge> extends DirectedGraph { - createVertex(id: VertexId, val: V['val']): V { - return new MyVertex(id, val) as V; - } + createVertex(id: VertexId, val: V['val']): V { + return new MyVertex(id, val) as V; + } - createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E { - return new MyEdge(src, dest, weight ?? 1, val) as E; - } + createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E { + return new MyEdge(src, dest, weight ?? 1, val) as E; + } } describe('Inherit from DirectedGraph and perform operations', () => { - let myGraph = new MyDirectedGraph, MyEdge>(); - beforeEach(() => { - myGraph = new MyDirectedGraph(); - }); + let myGraph = new MyDirectedGraph, MyEdge>(); + beforeEach(() => { + myGraph = new MyDirectedGraph(); + }); - it('Add vertices', () => { - myGraph.addVertex(1, 'data1'); - myGraph.addVertex(2, 'data2'); - myGraph.addVertex(3, 'data3'); - myGraph.addVertex(4, 'data4'); - myGraph.addVertex(5, 'data5'); - myGraph.addVertex(new MyVertex(6, 'data6')); - myGraph.addVertex(new MyVertex(7, 'data7')); - myGraph.addVertex(new MyVertex(8, 'data8')); - myGraph.addVertex(new MyVertex(9, 'data9')); + it('Add vertices', () => { + myGraph.addVertex(1, 'data1'); + myGraph.addVertex(2, 'data2'); + myGraph.addVertex(3, 'data3'); + myGraph.addVertex(4, 'data4'); + myGraph.addVertex(5, 'data5'); + myGraph.addVertex(new MyVertex(6, 'data6')); + myGraph.addVertex(new MyVertex(7, 'data7')); + myGraph.addVertex(new MyVertex(8, 'data8')); + myGraph.addVertex(new MyVertex(9, 'data9')); - }); + }); - it('Add edges', () => { - myGraph.addVertex(1, 'data1'); - myGraph.addVertex(2, 'data2'); - myGraph.addEdge(1, 2, 10, 'edge-data1-2'); - myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1')); + it('Add edges', () => { + myGraph.addVertex(1, 'data1'); + myGraph.addVertex(2, 'data2'); + myGraph.addEdge(1, 2, 10, 'edge-data1-2'); + myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1')); - expect(myGraph.edgeSet().length).toBe(2); - // TODO - expect(myGraph.getEdge(1, 2)).toBeInstanceOf(MyEdge); - expect(myGraph.getEdge(2, 1)).toBeInstanceOf(MyEdge); - }); + expect(myGraph.edgeSet().length).toBe(2); + // TODO + expect(myGraph.getEdge(1, 2)).toBeInstanceOf(MyEdge); + expect(myGraph.getEdge(2, 1)).toBeInstanceOf(MyEdge); + }); - it('Get edge', () => { - myGraph.addVertex(1, 'val1'); - myGraph.addVertex(2, 'val1'); - myGraph.addEdge(1, 2, 1, 'val1'); - const edge1 = myGraph.getEdge(1, 2); - const edge2 = myGraph.getEdge(myGraph.getVertex(1), myGraph.getVertex(2)); - const edge3 = myGraph.getEdge(1, '100'); - // edge1.data has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class. - expect(edge1).toBeInstanceOf(MyEdge); - if (edge1) { - expect(edge1.data).toBe('val1'); - expect(edge1?.val).toBe('val1'); - expect(edge1).toBeInstanceOf(MyEdge); - expect(edge1.src).toBe(1); - expect(edge1).toEqual(edge2); - expect(edge3).toBeNull(); - } + it('Get edge', () => { + myGraph.addVertex(1, 'val1'); + myGraph.addVertex(2, 'val1'); + myGraph.addEdge(1, 2, 1, 'val1'); + const edge1 = myGraph.getEdge(1, 2); + const edge2 = myGraph.getEdge(myGraph.getVertex(1), myGraph.getVertex(2)); + const edge3 = myGraph.getEdge(1, '100'); + // edge1.data has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class. + expect(edge1).toBeInstanceOf(MyEdge); + if (edge1) { + expect(edge1.data).toBe('val1'); + expect(edge1?.val).toBe('val1'); + expect(edge1).toBeInstanceOf(MyEdge); + expect(edge1.src).toBe(1); + expect(edge1).toEqual(edge2); + expect(edge3).toBeNull(); + } - }); + }); - it('Edge set and vertex set', () => { - const edges = myGraph.edgeSet(); - const vertices = myGraph.vertices; + it('Edge set and vertex set', () => { + const edges = myGraph.edgeSet(); + const vertices = myGraph.vertices; - }); + }); - it('Remove edge between vertices', () => { - myGraph.addVertex(1, 'data1'); - myGraph.addVertex(2, 'data2'); - myGraph.addEdge(1, 2, 10, 'edge-data1-2'); + it('Remove edge between vertices', () => { + myGraph.addVertex(1, 'data1'); + myGraph.addVertex(2, 'data2'); + myGraph.addEdge(1, 2, 10, 'edge-data1-2'); - const removedEdge = myGraph.removeEdgeSrcToDest(1, 2); - const edgeAfterRemoval = myGraph.getEdge(1, 2); + const removedEdge = myGraph.removeEdgeSrcToDest(1, 2); + const edgeAfterRemoval = myGraph.getEdge(1, 2); - expect(removedEdge).toBeInstanceOf(MyEdge); - if (removedEdge) { - removedEdge && expect(removedEdge.val).toBe('edge-data1-2'); - removedEdge && expect(removedEdge.src).toBe(1) - } - expect(edgeAfterRemoval).toBeNull(); - }); + expect(removedEdge).toBeInstanceOf(MyEdge); + if (removedEdge) { + removedEdge && expect(removedEdge.val).toBe('edge-data1-2'); + removedEdge && expect(removedEdge.src).toBe(1) + } + expect(edgeAfterRemoval).toBeNull(); + }); - it('Topological sort', () => { + it('Topological sort', () => { - const sorted = myGraph.topologicalSort(); + const sorted = myGraph.topologicalSort(); - expect(sorted).toBeInstanceOf(Array); - if (sorted && sorted.length > 0) { - expect(sorted.length).toBe(9); - if (sorted[0] instanceof MyVertex) expect(sorted[0].data).toBe('data9'); - sorted[3] instanceof MyVertex && expect(sorted[3].data).toBe('data6'); - sorted[8] instanceof MyVertex && expect(sorted[8].id).toBe(1); - } + expect(sorted).toBeInstanceOf(Array); + if (sorted && sorted.length > 0) { + expect(sorted.length).toBe(9); + if (sorted[0] instanceof MyVertex) expect(sorted[0].data).toBe('data9'); + sorted[3] instanceof MyVertex && expect(sorted[3].data).toBe('data6'); + sorted[8] instanceof MyVertex && expect(sorted[8].id).toBe(1); + } - }); + }); - it('Minimum path between vertices', () => { - myGraph.addVertex(new MyVertex(1, 'data1')); - myGraph.addVertex(new MyVertex(2, 'data2')); - myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2')); + it('Minimum path between vertices', () => { + myGraph.addVertex(new MyVertex(1, 'data1')); + myGraph.addVertex(new MyVertex(2, 'data2')); + myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2')); - const minPath = myGraph.getMinPathBetween(1, 2); - }); + const minPath = myGraph.getMinPathBetween(1, 2); + }); - it('All paths between vertices', () => { - // Add vertices and edges as needed for this test - myGraph.addVertex(new MyVertex(1, 'data1')); - myGraph.addVertex(new MyVertex(2, 'data2')); - myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2')); + it('All paths between vertices', () => { + // Add vertices and edges as needed for this test + myGraph.addVertex(new MyVertex(1, 'data1')); + myGraph.addVertex(new MyVertex(2, 'data2')); + myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2')); - const allPaths = myGraph.getAllPathsBetween(1, 2); + const allPaths = myGraph.getAllPathsBetween(1, 2); - // Add expect statements here to verify the allPaths - }); + // Add expect statements here to verify the allPaths + }); }); describe('Inherit from DirectedGraph and perform operations test2.', () => { - const myGraph = new MyDirectedGraph, MyEdge>(); + const myGraph = new MyDirectedGraph, MyEdge>(); - it('should test graph operations', () => { - const vertex1 = new MyVertex(1, 'data1'); - const vertex2 = new MyVertex(2, 'data2'); - const vertex3 = new MyVertex(3, 'data3'); - const vertex4 = new MyVertex(4, 'data4'); - const vertex5 = new MyVertex(5, 'data5'); - const vertex6 = new MyVertex(6, 'data6'); - const vertex7 = new MyVertex(7, 'data7'); - const vertex8 = new MyVertex(8, 'data8'); - const vertex9 = new MyVertex(9, 'data9'); - myGraph.addVertex(vertex1); - myGraph.addVertex(vertex2); - myGraph.addVertex(vertex3); - myGraph.addVertex(vertex4); - myGraph.addVertex(vertex5); - myGraph.addVertex(vertex6); - myGraph.addVertex(vertex7); - myGraph.addVertex(vertex8); - myGraph.addVertex(vertex9); + it('should test graph operations', () => { + const vertex1 = new MyVertex(1, 'data1'); + const vertex2 = new MyVertex(2, 'data2'); + const vertex3 = new MyVertex(3, 'data3'); + const vertex4 = new MyVertex(4, 'data4'); + const vertex5 = new MyVertex(5, 'data5'); + const vertex6 = new MyVertex(6, 'data6'); + const vertex7 = new MyVertex(7, 'data7'); + const vertex8 = new MyVertex(8, 'data8'); + const vertex9 = new MyVertex(9, 'data9'); + myGraph.addVertex(vertex1); + myGraph.addVertex(vertex2); + myGraph.addVertex(vertex3); + myGraph.addVertex(vertex4); + myGraph.addVertex(vertex5); + myGraph.addVertex(vertex6); + myGraph.addVertex(vertex7); + myGraph.addVertex(vertex8); + myGraph.addVertex(vertex9); - myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2')); - myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1')); + myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2')); + myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1')); - expect(myGraph.getEdge(1, 2)).toBeTruthy(); - expect(myGraph.getEdge(2, 1)).toBeTruthy(); - expect(myGraph.getEdge(1, '100')).toBeFalsy(); + expect(myGraph.getEdge(1, 2)).toBeTruthy(); + expect(myGraph.getEdge(2, 1)).toBeTruthy(); + expect(myGraph.getEdge(1, '100')).toBeFalsy(); - myGraph.removeEdgeSrcToDest(1, 2); - expect(myGraph.getEdge(1, 2)).toBeFalsy(); + myGraph.removeEdgeSrcToDest(1, 2); + expect(myGraph.getEdge(1, 2)).toBeFalsy(); - myGraph.addEdge(3, 1, 3, 'edge-data-3-1'); + myGraph.addEdge(3, 1, 3, 'edge-data-3-1'); - myGraph.addEdge(1, 9, 19, 'edge-data1-9'); + myGraph.addEdge(1, 9, 19, 'edge-data1-9'); - myGraph.addEdge(9, 7, 97, 'edge-data9-7'); + myGraph.addEdge(9, 7, 97, 'edge-data9-7'); - myGraph.addEdge(7, 9, 79, 'edge-data7-9'); + myGraph.addEdge(7, 9, 79, 'edge-data7-9'); - myGraph.addEdge(1, 4, 14, 'edge-data1-4'); + myGraph.addEdge(1, 4, 14, 'edge-data1-4'); - myGraph.addEdge(4, 7, 47, 'edge-data4-7'); + myGraph.addEdge(4, 7, 47, 'edge-data4-7'); - myGraph.addEdge(1, 2, 12, 'edge-data1-2'); + myGraph.addEdge(1, 2, 12, 'edge-data1-2'); - myGraph.addEdge(2, 3, 23, 'edge-data2-3'); + myGraph.addEdge(2, 3, 23, 'edge-data2-3'); - myGraph.addEdge(3, 5, 35, 'edge-data3-5'); + myGraph.addEdge(3, 5, 35, 'edge-data3-5'); - myGraph.addEdge(5, 7, 57, 'edge-data5-7'); + myGraph.addEdge(5, 7, 57, 'edge-data5-7'); - myGraph.addEdge(new MyEdge(7, 3, 73, 'edge-data7-3')); - const topologicalSorted = myGraph.topologicalSort(); - expect(topologicalSorted).toBeNull(); + myGraph.addEdge(new MyEdge(7, 3, 73, 'edge-data7-3')); + const topologicalSorted = myGraph.topologicalSort(); + expect(topologicalSorted).toBeNull(); - const minPath1to7 = myGraph.getMinPathBetween(1, 7); + const minPath1to7 = myGraph.getMinPathBetween(1, 7); - expect(minPath1to7).toBeInstanceOf(Array); - if (minPath1to7 && minPath1to7.length > 0) { - expect(minPath1to7).toHaveLength(3); - expect(minPath1to7[0]).toBeInstanceOf(MyVertex); - expect(minPath1to7[0].id).toBe(1); - expect(minPath1to7[1].id).toBe(9); - expect(minPath1to7[2].id).toBe(7); - } + expect(minPath1to7).toBeInstanceOf(Array); + if (minPath1to7 && minPath1to7.length > 0) { + expect(minPath1to7).toHaveLength(3); + expect(minPath1to7[0]).toBeInstanceOf(MyVertex); + expect(minPath1to7[0].id).toBe(1); + expect(minPath1to7[1].id).toBe(9); + expect(minPath1to7[2].id).toBe(7); + } - const fordResult1 = myGraph.bellmanFord(1); - expect(fordResult1).toBeTruthy(); - expect(fordResult1.hasNegativeCycle).toBeUndefined(); - const {distMap, preMap, paths, min, minPath} = fordResult1; - expect(distMap).toBeInstanceOf(Map); - expect(distMap.size).toBe(9); - expect(distMap.get(vertex1)).toBe(0); - expect(distMap.get(vertex2)).toBe(12); - expect(distMap.get(vertex3)).toBe(35); - expect(distMap.get(vertex4)).toBe(14); - expect(distMap.get(vertex5)).toBe(70); - expect(distMap.get(vertex6)).toBe(Infinity); - expect(distMap.get(vertex7)).toBe(61); - expect(distMap.get(vertex8)).toBe(Infinity); - expect(distMap.get(vertex9)).toBe(19); + const fordResult1 = myGraph.bellmanFord(1); + expect(fordResult1).toBeTruthy(); + expect(fordResult1.hasNegativeCycle).toBeUndefined(); + const {distMap, preMap, paths, min, minPath} = fordResult1; + expect(distMap).toBeInstanceOf(Map); + expect(distMap.size).toBe(9); + expect(distMap.get(vertex1)).toBe(0); + expect(distMap.get(vertex2)).toBe(12); + expect(distMap.get(vertex3)).toBe(35); + expect(distMap.get(vertex4)).toBe(14); + expect(distMap.get(vertex5)).toBe(70); + expect(distMap.get(vertex6)).toBe(Infinity); + expect(distMap.get(vertex7)).toBe(61); + expect(distMap.get(vertex8)).toBe(Infinity); + expect(distMap.get(vertex9)).toBe(19); - expect(preMap).toBeInstanceOf(Map); - expect(preMap.size).toBe(0); + expect(preMap).toBeInstanceOf(Map); + expect(preMap.size).toBe(0); - expect(paths).toBeInstanceOf(Array); - expect(paths.length).toBe(0); - expect(min).toBe(Infinity); - expect(minPath).toBeInstanceOf(Array); + expect(paths).toBeInstanceOf(Array); + expect(paths.length).toBe(0); + expect(min).toBe(Infinity); + expect(minPath).toBeInstanceOf(Array); - const floydResult = myGraph.floyd(); - expect(floydResult).toBeTruthy(); - if (floydResult) { - const {costs, predecessor} = floydResult; - expect(costs).toBeInstanceOf(Array); - expect(costs.length).toBe(9); - expect(costs[0]).toEqual([32, 12, 35, 14, 70, Infinity, 61, Infinity, 19]); - expect(costs[1]).toEqual([20, 32, 23, 34, 58, Infinity, 81, Infinity, 39]); - expect(costs[2]).toEqual([3, 15, 38, 17, 35, Infinity, 64, Infinity, 22]); - expect(costs[3]).toEqual([123, 135, 120, 137, 155, Infinity, 47, Infinity, 126]); - expect(costs[4]).toEqual([133, 145, 130, 147, 165, Infinity, 57, Infinity, 136]); - expect(costs[5]).toEqual([Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]); - expect(costs[6]).toEqual([76, 88, 73, 90, 108, Infinity, 137, Infinity, 79]); - expect(costs[7]).toEqual([Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]); - expect(costs[8]).toEqual([173, 185, 170, 187, 205, Infinity, 97, Infinity, 176]); + const floydResult = myGraph.floyd(); + expect(floydResult).toBeTruthy(); + if (floydResult) { + const {costs, predecessor} = floydResult; + expect(costs).toBeInstanceOf(Array); + expect(costs.length).toBe(9); + expect(costs[0]).toEqual([32, 12, 35, 14, 70, Infinity, 61, Infinity, 19]); + expect(costs[1]).toEqual([20, 32, 23, 34, 58, Infinity, 81, Infinity, 39]); + expect(costs[2]).toEqual([3, 15, 38, 17, 35, Infinity, 64, Infinity, 22]); + expect(costs[3]).toEqual([123, 135, 120, 137, 155, Infinity, 47, Infinity, 126]); + expect(costs[4]).toEqual([133, 145, 130, 147, 165, Infinity, 57, Infinity, 136]); + expect(costs[5]).toEqual([Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]); + expect(costs[6]).toEqual([76, 88, 73, 90, 108, Infinity, 137, Infinity, 79]); + expect(costs[7]).toEqual([Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]); + expect(costs[8]).toEqual([173, 185, 170, 187, 205, Infinity, 97, Infinity, 176]); - expect(predecessor).toBeInstanceOf(Array); - expect(predecessor.length).toBe(9); - expect(predecessor[0]).toEqual([vertex2, null, vertex2, null, vertex3, null, vertex4, null, null]); - expect(predecessor[1]).toEqual([null, vertex1, null, vertex1, vertex3, null, vertex4, null, vertex1]); - expect(predecessor[5]).toEqual([null, null, null, null, null, null, null, null, null]); - expect(predecessor[7]).toEqual([null, null, null, null, null, null, null, null, null]); - expect(predecessor[8]).toEqual([vertex7, vertex7, vertex7, vertex7, vertex7, null, null, null, vertex7]); - } + expect(predecessor).toBeInstanceOf(Array); + expect(predecessor.length).toBe(9); + expect(predecessor[0]).toEqual([vertex2, null, vertex2, null, vertex3, null, vertex4, null, null]); + expect(predecessor[1]).toEqual([null, vertex1, null, vertex1, vertex3, null, vertex4, null, vertex1]); + expect(predecessor[5]).toEqual([null, null, null, null, null, null, null, null, null]); + expect(predecessor[7]).toEqual([null, null, null, null, null, null, null, null, null]); + expect(predecessor[8]).toEqual([vertex7, vertex7, vertex7, vertex7, vertex7, null, null, null, vertex7]); + } - const dijkstraRes12tt = myGraph.dijkstra(1, 2, true, true); + const dijkstraRes12tt = myGraph.dijkstra(1, 2, true, true); - expect(dijkstraRes12tt).toBeTruthy(); - if (dijkstraRes12tt) { - const {distMap, minDist, minPath, paths, preMap, seen} = dijkstraRes12tt; - expect(distMap).toBeInstanceOf(Map); - expect(distMap.size).toBe(9); - expect(distMap.get(vertex1)).toBe(0); - expect(distMap.get(vertex2)).toBe(12); - expect(distMap.get(vertex3)).toBe(Infinity); - expect(distMap.get(vertex4)).toBe(14); - expect(distMap.get(vertex5)).toBe(Infinity); - expect(distMap.get(vertex6)).toBe(Infinity); - expect(distMap.get(vertex7)).toBe(Infinity); - expect(distMap.get(vertex8)).toBe(Infinity); - expect(distMap.get(vertex9)).toBe(19); + expect(dijkstraRes12tt).toBeTruthy(); + if (dijkstraRes12tt) { + const {distMap, minDist, minPath, paths, preMap, seen} = dijkstraRes12tt; + expect(distMap).toBeInstanceOf(Map); + expect(distMap.size).toBe(9); + expect(distMap.get(vertex1)).toBe(0); + expect(distMap.get(vertex2)).toBe(12); + expect(distMap.get(vertex3)).toBe(Infinity); + expect(distMap.get(vertex4)).toBe(14); + expect(distMap.get(vertex5)).toBe(Infinity); + expect(distMap.get(vertex6)).toBe(Infinity); + expect(distMap.get(vertex7)).toBe(Infinity); + expect(distMap.get(vertex8)).toBe(Infinity); + expect(distMap.get(vertex9)).toBe(19); - expect(minDist).toBe(12); - expect(minPath).toBeInstanceOf(Array); - expect(minPath.length).toBe(2); - expect(minPath[0]).toBe(vertex1); - expect(minPath[1]).toBe(vertex2); + expect(minDist).toBe(12); + expect(minPath).toBeInstanceOf(Array); + expect(minPath.length).toBe(2); + expect(minPath[0]).toBe(vertex1); + expect(minPath[1]).toBe(vertex2); - expect(paths).toBeInstanceOf(Array); - expect(paths.length).toBe(9); - expect(paths[0]).toBeInstanceOf(Array); - expect(paths[0][0]).toBe(vertex1); + expect(paths).toBeInstanceOf(Array); + expect(paths.length).toBe(9); + expect(paths[0]).toBeInstanceOf(Array); + expect(paths[0][0]).toBe(vertex1); - expect(paths[1]).toBeInstanceOf(Array); - expect(paths[1][0]).toBe(vertex1); - expect(paths[1][1]).toBe(vertex2); + expect(paths[1]).toBeInstanceOf(Array); + expect(paths[1][0]).toBe(vertex1); + expect(paths[1][1]).toBe(vertex2); - expect(paths[2]).toBeInstanceOf(Array); - expect(paths[2][0]).toBe(vertex3); - expect(paths[3]).toBeInstanceOf(Array); - expect(paths[3][0]).toBe(vertex1); - expect(paths[3][1]).toBe(vertex4); - expect(paths[4]).toBeInstanceOf(Array); - expect(paths[4][0]).toBe(vertex5); + expect(paths[2]).toBeInstanceOf(Array); + expect(paths[2][0]).toBe(vertex3); + expect(paths[3]).toBeInstanceOf(Array); + expect(paths[3][0]).toBe(vertex1); + expect(paths[3][1]).toBe(vertex4); + expect(paths[4]).toBeInstanceOf(Array); + expect(paths[4][0]).toBe(vertex5); - expect(paths[5]).toBeInstanceOf(Array); - expect(paths[5][0]).toBe(vertex6); - expect(paths[6]).toBeInstanceOf(Array); - expect(paths[6][0]).toBe(vertex7); - expect(paths[7]).toBeInstanceOf(Array); - expect(paths[7][0]).toBe(vertex8); - expect(paths[8]).toBeInstanceOf(Array); - expect(paths[8][0]).toBe(vertex1); - expect(paths[8][1]).toBe(vertex9); + expect(paths[5]).toBeInstanceOf(Array); + expect(paths[5][0]).toBe(vertex6); + expect(paths[6]).toBeInstanceOf(Array); + expect(paths[6][0]).toBe(vertex7); + expect(paths[7]).toBeInstanceOf(Array); + expect(paths[7][0]).toBe(vertex8); + expect(paths[8]).toBeInstanceOf(Array); + expect(paths[8][0]).toBe(vertex1); + expect(paths[8][1]).toBe(vertex9); - } + } - const dijkstraRes1ntt = myGraph.dijkstra(1, null, true, true); + const dijkstraRes1ntt = myGraph.dijkstra(1, null, true, true); - expect(dijkstraRes1ntt).toBeTruthy(); - if (dijkstraRes1ntt) { - const {distMap, minDist, minPath, paths, preMap, seen} = dijkstraRes1ntt; - expect(distMap).toBeInstanceOf(Map); - expect(distMap.size).toBe(9); - expect(distMap.get(vertex1)).toBe(0); - expect(distMap.get(vertex2)).toBe(12); - expect(distMap.get(vertex3)).toBe(35); - expect(distMap.get(vertex4)).toBe(14); - expect(distMap.get(vertex5)).toBe(70); - expect(distMap.get(vertex6)).toBe(Infinity); - expect(distMap.get(vertex7)).toBe(61); - expect(distMap.get(vertex8)).toBe(Infinity); - expect(distMap.get(vertex9)).toBe(19); + expect(dijkstraRes1ntt).toBeTruthy(); + if (dijkstraRes1ntt) { + const {distMap, minDist, minPath, paths, preMap, seen} = dijkstraRes1ntt; + expect(distMap).toBeInstanceOf(Map); + expect(distMap.size).toBe(9); + expect(distMap.get(vertex1)).toBe(0); + expect(distMap.get(vertex2)).toBe(12); + expect(distMap.get(vertex3)).toBe(35); + expect(distMap.get(vertex4)).toBe(14); + expect(distMap.get(vertex5)).toBe(70); + expect(distMap.get(vertex6)).toBe(Infinity); + expect(distMap.get(vertex7)).toBe(61); + expect(distMap.get(vertex8)).toBe(Infinity); + expect(distMap.get(vertex9)).toBe(19); - expect(minDist).toBe(12); - expect(minPath).toBeInstanceOf(Array); - expect(minPath.length).toBe(2); - expect(minPath[0]).toBe(vertex1); - expect(minPath[1]).toBe(vertex2); + expect(minDist).toBe(12); + expect(minPath).toBeInstanceOf(Array); + expect(minPath.length).toBe(2); + expect(minPath[0]).toBe(vertex1); + expect(minPath[1]).toBe(vertex2); - expect(paths).toBeInstanceOf(Array); - expect(paths.length).toBe(9); - expect(paths[0]).toBeInstanceOf(Array); - expect(paths[0][0]).toBe(vertex1); + expect(paths).toBeInstanceOf(Array); + expect(paths.length).toBe(9); + expect(paths[0]).toBeInstanceOf(Array); + expect(paths[0][0]).toBe(vertex1); - expect(paths[1]).toBeInstanceOf(Array); - expect(paths[1][0]).toBe(vertex1); - expect(paths[1][1]).toBe(vertex2); + expect(paths[1]).toBeInstanceOf(Array); + expect(paths[1][0]).toBe(vertex1); + expect(paths[1][1]).toBe(vertex2); - expect(paths[2]).toBeInstanceOf(Array); - expect(paths[2][0]).toBe(vertex1); - expect(paths[2][1]).toBe(vertex2); - expect(paths[2][2]).toBe(vertex3); + expect(paths[2]).toBeInstanceOf(Array); + expect(paths[2][0]).toBe(vertex1); + expect(paths[2][1]).toBe(vertex2); + expect(paths[2][2]).toBe(vertex3); - expect(paths[3]).toBeInstanceOf(Array); - expect(paths[3][0]).toBe(vertex1); - expect(paths[3][1]).toBe(vertex4); + expect(paths[3]).toBeInstanceOf(Array); + expect(paths[3][0]).toBe(vertex1); + expect(paths[3][1]).toBe(vertex4); - expect(paths[4]).toBeInstanceOf(Array); - expect(paths[4][0]).toBe(vertex1); - expect(paths[4][1]).toBe(vertex2); - expect(paths[4][2]).toBe(vertex3); - expect(paths[4][3]).toBe(vertex5); + expect(paths[4]).toBeInstanceOf(Array); + expect(paths[4][0]).toBe(vertex1); + expect(paths[4][1]).toBe(vertex2); + expect(paths[4][2]).toBe(vertex3); + expect(paths[4][3]).toBe(vertex5); - expect(paths[5]).toBeInstanceOf(Array); - expect(paths[5][0]).toBe(vertex6); + expect(paths[5]).toBeInstanceOf(Array); + expect(paths[5][0]).toBe(vertex6); - expect(paths[6]).toBeInstanceOf(Array); - expect(paths[6][0]).toBe(vertex1); - expect(paths[6][1]).toBe(vertex4); - expect(paths[6][2]).toBe(vertex7); + expect(paths[6]).toBeInstanceOf(Array); + expect(paths[6][0]).toBe(vertex1); + expect(paths[6][1]).toBe(vertex4); + expect(paths[6][2]).toBe(vertex7); - expect(paths[7]).toBeInstanceOf(Array); - expect(paths[7][0]).toBe(vertex8); + expect(paths[7]).toBeInstanceOf(Array); + expect(paths[7][0]).toBe(vertex8); - expect(paths[8]).toBeInstanceOf(Array); - expect(paths[8][0]).toBe(vertex1); - expect(paths[8][1]).toBe(vertex9); + expect(paths[8]).toBeInstanceOf(Array); + expect(paths[8][0]).toBe(vertex1); + expect(paths[8][1]).toBe(vertex9); - } + } - const dijkstraWithoutHeapRes1ntt = myGraph.dijkstraWithoutHeap(1, null, true, true); - expect(dijkstraWithoutHeapRes1ntt).toBeTruthy(); - if (dijkstraWithoutHeapRes1ntt) { - const {distMap, minDist, minPath, paths, preMap, seen} = dijkstraWithoutHeapRes1ntt; - expect(distMap).toBeInstanceOf(Map); - expect(distMap.size).toBe(9); - expect(distMap.get(vertex1)).toBe(0); - expect(distMap.get(vertex2)).toBe(12); - expect(distMap.get(vertex3)).toBe(35); - expect(distMap.get(vertex4)).toBe(14); - expect(distMap.get(vertex5)).toBe(70); - expect(distMap.get(vertex6)).toBe(Infinity); - expect(distMap.get(vertex7)).toBe(61); - expect(distMap.get(vertex8)).toBe(Infinity); - expect(distMap.get(vertex9)).toBe(19); + const dijkstraWithoutHeapRes1ntt = myGraph.dijkstraWithoutHeap(1, null, true, true); + expect(dijkstraWithoutHeapRes1ntt).toBeTruthy(); + if (dijkstraWithoutHeapRes1ntt) { + const {distMap, minDist, minPath, paths, preMap, seen} = dijkstraWithoutHeapRes1ntt; + expect(distMap).toBeInstanceOf(Map); + expect(distMap.size).toBe(9); + expect(distMap.get(vertex1)).toBe(0); + expect(distMap.get(vertex2)).toBe(12); + expect(distMap.get(vertex3)).toBe(35); + expect(distMap.get(vertex4)).toBe(14); + expect(distMap.get(vertex5)).toBe(70); + expect(distMap.get(vertex6)).toBe(Infinity); + expect(distMap.get(vertex7)).toBe(61); + expect(distMap.get(vertex8)).toBe(Infinity); + expect(distMap.get(vertex9)).toBe(19); - expect(minDist).toBe(12); - expect(minPath).toBeInstanceOf(Array); - expect(minPath.length).toBe(2); - expect(minPath[0]).toBe(vertex1); - expect(minPath[1]).toBe(vertex2); + expect(minDist).toBe(12); + expect(minPath).toBeInstanceOf(Array); + expect(minPath.length).toBe(2); + expect(minPath[0]).toBe(vertex1); + expect(minPath[1]).toBe(vertex2); - expect(paths).toBeInstanceOf(Array); - expect(paths.length).toBe(9); - expect(paths[0]).toBeInstanceOf(Array); - expect(paths[0][0]).toBe(vertex1); + expect(paths).toBeInstanceOf(Array); + expect(paths.length).toBe(9); + expect(paths[0]).toBeInstanceOf(Array); + expect(paths[0][0]).toBe(vertex1); - expect(paths[1]).toBeInstanceOf(Array); - expect(paths[1][0]).toBe(vertex1); - expect(paths[1][1]).toBe(vertex2); + expect(paths[1]).toBeInstanceOf(Array); + expect(paths[1][0]).toBe(vertex1); + expect(paths[1][1]).toBe(vertex2); - expect(paths[2]).toBeInstanceOf(Array); - expect(paths[2][0]).toBe(vertex1); - expect(paths[2][1]).toBe(vertex2); - expect(paths[2][2]).toBe(vertex3); + expect(paths[2]).toBeInstanceOf(Array); + expect(paths[2][0]).toBe(vertex1); + expect(paths[2][1]).toBe(vertex2); + expect(paths[2][2]).toBe(vertex3); - expect(paths[3]).toBeInstanceOf(Array); - expect(paths[3][0]).toBe(vertex1); - expect(paths[3][1]).toBe(vertex4); + expect(paths[3]).toBeInstanceOf(Array); + expect(paths[3][0]).toBe(vertex1); + expect(paths[3][1]).toBe(vertex4); - expect(paths[4]).toBeInstanceOf(Array); - expect(paths[4][0]).toBe(vertex1); - expect(paths[4][1]).toBe(vertex2); - expect(paths[4][2]).toBe(vertex3); - expect(paths[4][3]).toBe(vertex5); + expect(paths[4]).toBeInstanceOf(Array); + expect(paths[4][0]).toBe(vertex1); + expect(paths[4][1]).toBe(vertex2); + expect(paths[4][2]).toBe(vertex3); + expect(paths[4][3]).toBe(vertex5); - expect(paths[5]).toBeInstanceOf(Array); - expect(paths[5][0]).toBe(vertex6); + expect(paths[5]).toBeInstanceOf(Array); + expect(paths[5][0]).toBe(vertex6); - expect(paths[6]).toBeInstanceOf(Array); - expect(paths[6][0]).toBe(vertex1); - expect(paths[6][1]).toBe(vertex4); - expect(paths[6][2]).toBe(vertex7); + expect(paths[6]).toBeInstanceOf(Array); + expect(paths[6][0]).toBe(vertex1); + expect(paths[6][1]).toBe(vertex4); + expect(paths[6][2]).toBe(vertex7); - expect(paths[7]).toBeInstanceOf(Array); - expect(paths[7][0]).toBe(vertex8); + expect(paths[7]).toBeInstanceOf(Array); + expect(paths[7][0]).toBe(vertex8); - expect(paths[8]).toBeInstanceOf(Array); - expect(paths[8][0]).toBe(vertex1); - expect(paths[8][1]).toBe(vertex9); + expect(paths[8]).toBeInstanceOf(Array); + expect(paths[8][0]).toBe(vertex1); + expect(paths[8][1]).toBe(vertex9); - } - }); + } + }); }); diff --git a/tests/unit/data-structures/graph/map-graph.test.ts b/tests/unit/data-structures/graph/map-graph.test.ts index 30a7285..78ea03c 100644 --- a/tests/unit/data-structures/graph/map-graph.test.ts +++ b/tests/unit/data-structures/graph/map-graph.test.ts @@ -1,46 +1,46 @@ import {MapGraph, MapVertex} from '../../../../src'; describe('MapGraph Operation Test', () => { - it('dijkstra shortest path', () => { - const mapGraph = new MapGraph([5.500338, 100.173665]); + it('dijkstra shortest path', () => { + const mapGraph = new MapGraph([5.500338, 100.173665]); - mapGraph.addVertex(new MapVertex('Surin', 5.466724, 100.274805)); - mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', 5.475141, 100.276670)); - mapGraph.addVertex(new MapVertex('Lotus', 5.459044, 100.308767)); - mapGraph.addVertex(new MapVertex('The Breeza', 5.454197, 100.307859)); - mapGraph.addVertex(new MapVertex('Hard Rock Hotel', 5.467850, 100.241876)); - mapGraph.addVertex(new MapVertex('Mira', 5.456749, 100.286650)); - mapGraph.addVertex(new MapVertex('Penang Bible Church', 5.428683, 100.314825)); - mapGraph.addVertex(new MapVertex('Queensbay', 5.332760, 100.306651)); - mapGraph.addVertex(new MapVertex('Saanen Goat Farm', 5.405738, 100.207699)); - mapGraph.addVertex(new MapVertex('Trinity Auto', 5.401126, 100.303739)); - mapGraph.addVertex(new MapVertex('Penang Airport', 5.293185, 100.265772)); - mapGraph.addEdge('Surin', 'Lotus', 4.7); - mapGraph.addEdge('Lotus', 'The Breeza', 1); - mapGraph.addEdge('Batu Feringgi Beach', 'Hard Rock Hotel', 5.2); - mapGraph.addEdge('Surin', 'Mira', 2.8); - mapGraph.addEdge('Mira', 'Penang Bible Church', 7.0); - mapGraph.addEdge('Lotus', 'Penang Bible Church', 5.7); - mapGraph.addEdge('Penang Bible Church', 'Queensbay', 13.9); - mapGraph.addEdge('Hard Rock Hotel', 'Saanen Goat Farm', 18.5); - mapGraph.addEdge('The Breeza', 'Trinity Auto', 9.1); - mapGraph.addEdge('Trinity Auto', 'Saanen Goat Farm', 26.3); - mapGraph.addEdge('The Breeza', 'Penang Airport', 24.8); - mapGraph.addEdge('Penang Airport', 'Saanen Goat Farm', 21.2); - const expected1 = ['Surin', 'Lotus', 'The Breeza', 'Trinity Auto', 'Saanen Goat Farm']; + mapGraph.addVertex(new MapVertex('Surin', 5.466724, 100.274805)); + mapGraph.addVertex(new MapVertex('Batu Feringgi Beach', 5.475141, 100.276670)); + mapGraph.addVertex(new MapVertex('Lotus', 5.459044, 100.308767)); + mapGraph.addVertex(new MapVertex('The Breeza', 5.454197, 100.307859)); + mapGraph.addVertex(new MapVertex('Hard Rock Hotel', 5.467850, 100.241876)); + mapGraph.addVertex(new MapVertex('Mira', 5.456749, 100.286650)); + mapGraph.addVertex(new MapVertex('Penang Bible Church', 5.428683, 100.314825)); + mapGraph.addVertex(new MapVertex('Queensbay', 5.332760, 100.306651)); + mapGraph.addVertex(new MapVertex('Saanen Goat Farm', 5.405738, 100.207699)); + mapGraph.addVertex(new MapVertex('Trinity Auto', 5.401126, 100.303739)); + mapGraph.addVertex(new MapVertex('Penang Airport', 5.293185, 100.265772)); + mapGraph.addEdge('Surin', 'Lotus', 4.7); + mapGraph.addEdge('Lotus', 'The Breeza', 1); + mapGraph.addEdge('Batu Feringgi Beach', 'Hard Rock Hotel', 5.2); + mapGraph.addEdge('Surin', 'Mira', 2.8); + mapGraph.addEdge('Mira', 'Penang Bible Church', 7.0); + mapGraph.addEdge('Lotus', 'Penang Bible Church', 5.7); + mapGraph.addEdge('Penang Bible Church', 'Queensbay', 13.9); + mapGraph.addEdge('Hard Rock Hotel', 'Saanen Goat Farm', 18.5); + mapGraph.addEdge('The Breeza', 'Trinity Auto', 9.1); + mapGraph.addEdge('Trinity Auto', 'Saanen Goat Farm', 26.3); + mapGraph.addEdge('The Breeza', 'Penang Airport', 24.8); + mapGraph.addEdge('Penang Airport', 'Saanen Goat Farm', 21.2); + const expected1 = ['Surin', 'Lotus', 'The Breeza', 'Trinity Auto', 'Saanen Goat Farm']; - const minPathBetween = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm'); - expect(minPathBetween?.map(v => v.id)).toEqual(expected1); - const surinToSaanenGoatFarmDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true); - expect(surinToSaanenGoatFarmDij?.minPath.map(v => v.id)).toEqual(expected1); - expect(surinToSaanenGoatFarmDij?.minDist).toBe(41.1); - mapGraph.addEdge('Surin', 'Batu Feringgi Beach', 1.5); - const expected2 = ['Surin', 'Batu Feringgi Beach', 'Hard Rock Hotel', 'Saanen Goat Farm']; - const minPathBetweenViaBFB = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm', true); - expect(minPathBetweenViaBFB?.map(v => v.id)).toEqual(expected2); - const surinToSaanenGoatFarmViaDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true); - expect(surinToSaanenGoatFarmViaDij?.minPath.map(v => v.id)).toEqual(expected2); - expect(surinToSaanenGoatFarmViaDij?.minDist).toBe(25.2); - }); + const minPathBetween = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm'); + expect(minPathBetween?.map(v => v.id)).toEqual(expected1); + const surinToSaanenGoatFarmDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true); + expect(surinToSaanenGoatFarmDij?.minPath.map(v => v.id)).toEqual(expected1); + expect(surinToSaanenGoatFarmDij?.minDist).toBe(41.1); + mapGraph.addEdge('Surin', 'Batu Feringgi Beach', 1.5); + const expected2 = ['Surin', 'Batu Feringgi Beach', 'Hard Rock Hotel', 'Saanen Goat Farm']; + const minPathBetweenViaBFB = mapGraph.getMinPathBetween('Surin', 'Saanen Goat Farm', true); + expect(minPathBetweenViaBFB?.map(v => v.id)).toEqual(expected2); + const surinToSaanenGoatFarmViaDij = mapGraph.dijkstra('Surin', 'Saanen Goat Farm', true, true); + expect(surinToSaanenGoatFarmViaDij?.minPath.map(v => v.id)).toEqual(expected2); + expect(surinToSaanenGoatFarmViaDij?.minDist).toBe(25.2); + }); }); diff --git a/tests/unit/data-structures/graph/overall.test.ts b/tests/unit/data-structures/graph/overall.test.ts index 130876a..52b0e35 100644 --- a/tests/unit/data-structures/graph/overall.test.ts +++ b/tests/unit/data-structures/graph/overall.test.ts @@ -2,49 +2,49 @@ import {DirectedGraph, UndirectedGraph} from '../../../../src'; describe('Overall Graph Operation Test', () => { - it('Overall DirectedGraph Operation Test', () => { - const graph = new DirectedGraph(); + it('Overall DirectedGraph Operation Test', () => { + const graph = new DirectedGraph(); - graph.addVertex('A'); - graph.addVertex('B'); + graph.addVertex('A'); + graph.addVertex('B'); - graph.hasVertex('A'); // true - graph.hasVertex('B'); // true - graph.hasVertex('C'); // false - expect(graph.hasVertex('A')).toBe(true); // true - expect(graph.hasVertex('B')).toBe(true); // true - expect(graph.hasVertex('C')).toBe(false); // false + graph.hasVertex('A'); // true + graph.hasVertex('B'); // true + graph.hasVertex('C'); // false + expect(graph.hasVertex('A')).toBe(true); // true + expect(graph.hasVertex('B')).toBe(true); // true + expect(graph.hasVertex('C')).toBe(false); // false - graph.addEdge('A', 'B'); - graph.hasEdge('A', 'B'); // true - graph.hasEdge('B', 'A'); // false - expect(graph.hasEdge('A', 'B')).toBe(true); // true - expect(graph.hasEdge('B', 'A')).toBe(false); // false + graph.addEdge('A', 'B'); + graph.hasEdge('A', 'B'); // true + graph.hasEdge('B', 'A'); // false + expect(graph.hasEdge('A', 'B')).toBe(true); // true + expect(graph.hasEdge('B', 'A')).toBe(false); // false - graph.removeEdgeSrcToDest('A', 'B'); - graph.hasEdge('A', 'B'); // false - expect(graph.hasEdge('A', 'B')).toBe(false); // false + graph.removeEdgeSrcToDest('A', 'B'); + graph.hasEdge('A', 'B'); // false + expect(graph.hasEdge('A', 'B')).toBe(false); // false - graph.addVertex('C'); + graph.addVertex('C'); - graph.addEdge('A', 'B'); - graph.addEdge('B', 'C'); + graph.addEdge('A', 'B'); + graph.addEdge('B', 'C'); - const topologicalOrderIds = graph.topologicalSort(); - expect(topologicalOrderIds).toEqual(['A', 'B', 'C']) - }); - it('Overall UndirectedGraph Operation Test', () => { - const graph = new UndirectedGraph(); - graph.addVertex('A'); - graph.addVertex('B'); - graph.addVertex('C'); - graph.addVertex('D'); - graph.removeVertex('C'); - graph.addEdge('A', 'B'); - graph.addEdge('B', 'D'); + const topologicalOrderIds = graph.topologicalSort(); + expect(topologicalOrderIds).toEqual(['A', 'B', 'C']) + }); + it('Overall UndirectedGraph Operation Test', () => { + const graph = new UndirectedGraph(); + graph.addVertex('A'); + graph.addVertex('B'); + graph.addVertex('C'); + graph.addVertex('D'); + graph.removeVertex('C'); + graph.addEdge('A', 'B'); + graph.addEdge('B', 'D'); - const dijkstraResult = graph.dijkstra('A'); - Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D'] - expect(Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id)).toEqual(['A', 'B', 'D']); - }); + const dijkstraResult = graph.dijkstra('A'); + Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D'] + expect(Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id)).toEqual(['A', 'B', 'D']); + }); }); diff --git a/tests/unit/data-structures/graph/undirected-graph.test.ts b/tests/unit/data-structures/graph/undirected-graph.test.ts index 2efeaba..ffd1158 100644 --- a/tests/unit/data-structures/graph/undirected-graph.test.ts +++ b/tests/unit/data-structures/graph/undirected-graph.test.ts @@ -1,60 +1,60 @@ import {UndirectedEdge, UndirectedGraph, UndirectedVertex} from '../../../../src'; describe('UndirectedGraph Operation Test', () => { - let graph: UndirectedGraph; + let graph: UndirectedGraph; - beforeEach(() => { - graph = new UndirectedGraph(); - }); + beforeEach(() => { + graph = new UndirectedGraph(); + }); - it('should add vertices', () => { - const vertex1 = new UndirectedVertex('A'); - const vertex2 = new UndirectedVertex('B'); + it('should add vertices', () => { + const vertex1 = new UndirectedVertex('A'); + const vertex2 = new UndirectedVertex('B'); - graph.addVertex(vertex1); - graph.addVertex(vertex2); + graph.addVertex(vertex1); + graph.addVertex(vertex2); - expect(graph.hasVertex(vertex1)).toBe(true); - expect(graph.hasVertex(vertex2)).toBe(true); - }); + expect(graph.hasVertex(vertex1)).toBe(true); + expect(graph.hasVertex(vertex2)).toBe(true); + }); - it('should add edges', () => { - const vertex1 = new UndirectedVertex('A'); - const vertex2 = new UndirectedVertex('B'); - const edge = new UndirectedEdge('A', 'B'); + it('should add edges', () => { + const vertex1 = new UndirectedVertex('A'); + const vertex2 = new UndirectedVertex('B'); + const edge = new UndirectedEdge('A', 'B'); - graph.addVertex(vertex1); - graph.addVertex(vertex2); - graph.addEdge(edge); + graph.addVertex(vertex1); + graph.addVertex(vertex2); + graph.addEdge(edge); - expect(graph.hasEdge('A', 'B')).toBe(true); - expect(graph.hasEdge('B', 'A')).toBe(true); - }); + expect(graph.hasEdge('A', 'B')).toBe(true); + expect(graph.hasEdge('B', 'A')).toBe(true); + }); - it('should remove edges', () => { - const vertex1 = new UndirectedVertex('A'); - const vertex2 = new UndirectedVertex('B'); - const edge = new UndirectedEdge('A', 'B'); + it('should remove edges', () => { + const vertex1 = new UndirectedVertex('A'); + const vertex2 = new UndirectedVertex('B'); + const edge = new UndirectedEdge('A', 'B'); - graph.addVertex(vertex1); - graph.addVertex(vertex2); - graph.addEdge(edge); + graph.addVertex(vertex1); + graph.addVertex(vertex2); + graph.addEdge(edge); - expect(graph.removeEdge(edge)).toBe(edge); - expect(graph.hasEdge('A', 'B')).toBe(false); - }); + expect(graph.removeEdge(edge)).toBe(edge); + expect(graph.hasEdge('A', 'B')).toBe(false); + }); - it('should perform topological sort', () => { - graph.addVertex('A'); - graph.addVertex('B'); - graph.addVertex('C'); - graph.addVertex('D'); - graph.removeVertex('C'); - graph.addEdge('A', 'B'); - graph.addEdge('B', 'D'); + it('should perform topological sort', () => { + graph.addVertex('A'); + graph.addVertex('B'); + graph.addVertex('C'); + graph.addVertex('D'); + graph.removeVertex('C'); + graph.addEdge('A', 'B'); + graph.addEdge('B', 'D'); - const dijkstraResult = graph.dijkstra('A'); - expect(Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id)).toEqual(['A', 'B', 'D']); - }); + const dijkstraResult = graph.dijkstra('A'); + expect(Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id)).toEqual(['A', 'B', 'D']); + }); }); diff --git a/tests/unit/data-structures/heap/heap.test.ts b/tests/unit/data-structures/heap/heap.test.ts index 4ffb930..ca8d446 100644 --- a/tests/unit/data-structures/heap/heap.test.ts +++ b/tests/unit/data-structures/heap/heap.test.ts @@ -1,56 +1,56 @@ import {MaxHeap, MinHeap} from '../../../../src'; describe('Heap Operation Test', () => { - it('should numeric heap work well', function () { - const minNumHeap = new MinHeap(); - minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9); - expect(minNumHeap.has(1)).toBe(true); - expect(minNumHeap.has(2)).toBe(true); - expect(minNumHeap.poll()).toBe(0); - expect(minNumHeap.poll()).toBe(1); - expect(minNumHeap.peek()).toBe(2); - expect(!minNumHeap.has(1)); - expect(minNumHeap.has(2)); - const arrFromHeap = minNumHeap.toArray(); - expect(arrFromHeap.length).toBe(4); - expect(arrFromHeap[0]).toBe(2); - expect(arrFromHeap[1]).toBe(5); - expect(arrFromHeap[2]).toBe(9); - expect(arrFromHeap[3]).toBe(6); - expect(minNumHeap.sort()).toEqual([2, 5, 6, 9]); - }); + it('should numeric heap work well', function () { + const minNumHeap = new MinHeap(); + minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9); + expect(minNumHeap.has(1)).toBe(true); + expect(minNumHeap.has(2)).toBe(true); + expect(minNumHeap.poll()).toBe(0); + expect(minNumHeap.poll()).toBe(1); + expect(minNumHeap.peek()).toBe(2); + expect(!minNumHeap.has(1)); + expect(minNumHeap.has(2)); + const arrFromHeap = minNumHeap.toArray(); + expect(arrFromHeap.length).toBe(4); + expect(arrFromHeap[0]).toBe(2); + expect(arrFromHeap[1]).toBe(5); + expect(arrFromHeap[2]).toBe(9); + expect(arrFromHeap[3]).toBe(6); + expect(minNumHeap.sort()).toEqual([2, 5, 6, 9]); + }); - it('should object heap work well', function () { - const minHeap = new MinHeap<{ a: string }>(); - minHeap.add(1, {a: 'a1'}); - minHeap.add(6, {a: 'a6'}); - minHeap.add(2, {a: 'a2'}); - minHeap.add(0, {a: 'a0'}); + it('should object heap work well', function () { + const minHeap = new MinHeap<{ a: string }>(); + minHeap.add(1, {a: 'a1'}); + minHeap.add(6, {a: 'a6'}); + minHeap.add(2, {a: 'a2'}); + minHeap.add(0, {a: 'a0'}); - expect(minHeap.peek()).toEqual({a: 'a0'}); - expect(minHeap.toArray()).toEqual(([{'a': 'a0'}, {'a': 'a1'}, {'a': 'a2'}, {'a': 'a6'}])); - let i = 0; - const expectPolled = [{'a': 'a0'}, {'a': 'a1'}, {'a': 'a2'}, {'a': 'a6'}]; - while (minHeap.size > 0) { - expect(minHeap.poll()).toEqual(expectPolled[i]); - i++; - } + expect(minHeap.peek()).toEqual({a: 'a0'}); + expect(minHeap.toArray()).toEqual(([{'a': 'a0'}, {'a': 'a1'}, {'a': 'a2'}, {'a': 'a6'}])); + let i = 0; + const expectPolled = [{'a': 'a0'}, {'a': 'a1'}, {'a': 'a2'}, {'a': 'a6'}]; + while (minHeap.size > 0) { + expect(minHeap.poll()).toEqual(expectPolled[i]); + i++; + } - const maxHeap = new MaxHeap<{ a: string }>(); - maxHeap.add(1, {a: 'a1'}); - maxHeap.add(6, {a: 'a6'}); - maxHeap.add(5, {a: 'a5'}); - maxHeap.add(2, {a: 'a2'}); - maxHeap.add(0, {a: 'a0'}); - maxHeap.add(9, {a: 'a9'}); - expect(maxHeap.peek()).toEqual({'a': 'a9'}); - expect(maxHeap.toArray()).toEqual([{'a': 'a9'}, {'a': 'a2'}, {'a': 'a6'}, {'a': 'a1'}, {'a': 'a0'}, {'a': 'a5'}]); - const maxExpectPolled = [{'a': 'a9'}, {'a': 'a6'}, {'a': 'a5'}, {'a': 'a2'}, {'a': 'a1'}, {'a': 'a0'}]; - let maxI = 0; - while (maxHeap.size > 0) { - expect(maxHeap.poll()).toEqual(maxExpectPolled[maxI]); - maxI++; - } - }); + const maxHeap = new MaxHeap<{ a: string }>(); + maxHeap.add(1, {a: 'a1'}); + maxHeap.add(6, {a: 'a6'}); + maxHeap.add(5, {a: 'a5'}); + maxHeap.add(2, {a: 'a2'}); + maxHeap.add(0, {a: 'a0'}); + maxHeap.add(9, {a: 'a9'}); + expect(maxHeap.peek()).toEqual({'a': 'a9'}); + expect(maxHeap.toArray()).toEqual([{'a': 'a9'}, {'a': 'a2'}, {'a': 'a6'}, {'a': 'a1'}, {'a': 'a0'}, {'a': 'a5'}]); + const maxExpectPolled = [{'a': 'a9'}, {'a': 'a6'}, {'a': 'a5'}, {'a': 'a2'}, {'a': 'a1'}, {'a': 'a0'}]; + let maxI = 0; + while (maxHeap.size > 0) { + expect(maxHeap.poll()).toEqual(maxExpectPolled[maxI]); + maxI++; + } + }); }); diff --git a/tests/unit/data-structures/heap/max-heap.test.ts b/tests/unit/data-structures/heap/max-heap.test.ts index 5fdce92..3adcad6 100644 --- a/tests/unit/data-structures/heap/max-heap.test.ts +++ b/tests/unit/data-structures/heap/max-heap.test.ts @@ -2,41 +2,41 @@ import {HeapItem, MaxHeap} from '../../../../src'; describe('MaxHeap Operation Test', () => { - it('should object Max Heap operations be proper', function () { - const maxHeap = new MaxHeap<{ keyA: string }>(); - const myObj1 = {keyA: 'a1'}, myObj6 = {keyA: 'a6'}, myObj5 = {keyA: 'a5'}, myObj2 = {keyA: 'a2'}, - myObj0 = {keyA: 'a0'}, myObj9 = {keyA: 'a9'}; - maxHeap.add(1, myObj1); - expect(maxHeap.has(myObj1)).toBe(true); - expect(maxHeap.has(myObj9)).toBe(false); - maxHeap.add(6, myObj6); - expect(maxHeap.has(myObj6)).toBe(true); - maxHeap.add(5, myObj5); - expect(maxHeap.has(myObj5)).toBe(true); - maxHeap.add(2, myObj2); - expect(maxHeap.has(myObj2)).toBe(true); - expect(maxHeap.has(myObj6)).toBe(true); - maxHeap.add(0, myObj0); - expect(maxHeap.has(myObj0)).toBe(true); - expect(maxHeap.has(myObj9)).toBe(false); - maxHeap.add(9, myObj9); - expect(maxHeap.has(myObj9)).toBe(true); + it('should object Max Heap operations be proper', function () { + const maxHeap = new MaxHeap<{ keyA: string }>(); + const myObj1 = {keyA: 'a1'}, myObj6 = {keyA: 'a6'}, myObj5 = {keyA: 'a5'}, myObj2 = {keyA: 'a2'}, + myObj0 = {keyA: 'a0'}, myObj9 = {keyA: 'a9'}; + maxHeap.add(1, myObj1); + expect(maxHeap.has(myObj1)).toBe(true); + expect(maxHeap.has(myObj9)).toBe(false); + maxHeap.add(6, myObj6); + expect(maxHeap.has(myObj6)).toBe(true); + maxHeap.add(5, myObj5); + expect(maxHeap.has(myObj5)).toBe(true); + maxHeap.add(2, myObj2); + expect(maxHeap.has(myObj2)).toBe(true); + expect(maxHeap.has(myObj6)).toBe(true); + maxHeap.add(0, myObj0); + expect(maxHeap.has(myObj0)).toBe(true); + expect(maxHeap.has(myObj9)).toBe(false); + maxHeap.add(9, myObj9); + expect(maxHeap.has(myObj9)).toBe(true); - const peek9 = maxHeap.peek(true); - peek9 && peek9.val && expect(peek9.val.keyA).toBe('a9'); + const peek9 = maxHeap.peek(true); + peek9 && peek9.val && expect(peek9.val.keyA).toBe('a9'); - const heapToArr = maxHeap.toArray(true); - expect(heapToArr.map(item => item?.val?.keyA)).toEqual(['a9', 'a2', 'a6', 'a1', 'a0', 'a5']); + const heapToArr = maxHeap.toArray(true); + expect(heapToArr.map(item => item?.val?.keyA)).toEqual(['a9', 'a2', 'a6', 'a1', 'a0', 'a5']); - const values = ['a9', 'a6', 'a5', 'a2', 'a1', 'a0']; - let i = 0; - while (maxHeap.size > 0) { - const polled = maxHeap.poll(true); - expect(polled).toBeInstanceOf(HeapItem); - polled && expect(polled.val).toHaveProperty('keyA'); - polled && polled.val && expect(polled.val.keyA).toBe(values[i]); - i++; - } - }); + const values = ['a9', 'a6', 'a5', 'a2', 'a1', 'a0']; + let i = 0; + while (maxHeap.size > 0) { + const polled = maxHeap.poll(true); + expect(polled).toBeInstanceOf(HeapItem); + polled && expect(polled.val).toHaveProperty('keyA'); + polled && polled.val && expect(polled.val.keyA).toBe(values[i]); + i++; + } + }); -}); \ No newline at end of file +}); diff --git a/tests/unit/data-structures/heap/min-heap.test.ts b/tests/unit/data-structures/heap/min-heap.test.ts index 3376228..de232fa 100644 --- a/tests/unit/data-structures/heap/min-heap.test.ts +++ b/tests/unit/data-structures/heap/min-heap.test.ts @@ -2,80 +2,80 @@ import {HeapItem, MinHeap} from '../../../../src'; describe('MinHeap Operation Test', () => { - it('should numeric Min Heap operations be proper', function () { - const minNumHeap = new MinHeap(); - expect(minNumHeap).toBeInstanceOf(MinHeap); + it('should numeric Min Heap operations be proper', function () { + const minNumHeap = new MinHeap(); + expect(minNumHeap).toBeInstanceOf(MinHeap); - minNumHeap.add(1); - expect(minNumHeap.has(1)).toBe(true); - minNumHeap.add(6); - expect(minNumHeap.has(2)).toBe(false); - expect(minNumHeap.has(6)).toBe(true); - minNumHeap.add(2); - expect(minNumHeap.has(2)).toBe(true); - minNumHeap.add(0); - expect(minNumHeap.has(0)).toBe(true); - minNumHeap.add(5); - expect(minNumHeap.has(5)).toBe(true); - minNumHeap.add(9); - expect(minNumHeap.has(9)).toBe(true); - expect(minNumHeap.size).toBe(6); + minNumHeap.add(1); + expect(minNumHeap.has(1)).toBe(true); + minNumHeap.add(6); + expect(minNumHeap.has(2)).toBe(false); + expect(minNumHeap.has(6)).toBe(true); + minNumHeap.add(2); + expect(minNumHeap.has(2)).toBe(true); + minNumHeap.add(0); + expect(minNumHeap.has(0)).toBe(true); + minNumHeap.add(5); + expect(minNumHeap.has(5)).toBe(true); + minNumHeap.add(9); + expect(minNumHeap.has(9)).toBe(true); + expect(minNumHeap.size).toBe(6); - const poll1 = minNumHeap.poll(true); - expect(poll1).toBeInstanceOf(HeapItem) - poll1 instanceof HeapItem && expect(poll1.val).toBe(0); + const poll1 = minNumHeap.poll(true); + expect(poll1).toBeInstanceOf(HeapItem) + poll1 instanceof HeapItem && expect(poll1.val).toBe(0); - const poll2 = minNumHeap.poll(true); - expect(poll2).toBeInstanceOf(HeapItem) - poll2 instanceof HeapItem && expect(poll2.val).toBe(1); + const poll2 = minNumHeap.poll(true); + expect(poll2).toBeInstanceOf(HeapItem) + poll2 instanceof HeapItem && expect(poll2.val).toBe(1); - const peek1 = minNumHeap.peek(true); - expect(peek1).toBeInstanceOf(HeapItem) - peek1 instanceof HeapItem && expect(peek1.val).toBe(2); + const peek1 = minNumHeap.peek(true); + expect(peek1).toBeInstanceOf(HeapItem) + peek1 instanceof HeapItem && expect(peek1.val).toBe(2); - const heapArray = minNumHeap.toArray(true); - expect(heapArray).toBeInstanceOf(Array); - expect(heapArray.map(item => item?.priority)).toEqual([2, 5, 9, 6]); - expect(minNumHeap.size).toBe(4); - }); + const heapArray = minNumHeap.toArray(true); + expect(heapArray).toBeInstanceOf(Array); + expect(heapArray.map(item => item?.priority)).toEqual([2, 5, 9, 6]); + expect(minNumHeap.size).toBe(4); + }); - it('should object Min Heap operations be proper', function () { - class MyObject { - keyA: string; + it('should object Min Heap operations be proper', function () { + class MyObject { + keyA: string; - constructor(keyA: string) { - this.keyA = keyA; - } - } + constructor(keyA: string) { + this.keyA = keyA; + } + } - const minObjHeap = new MinHeap(); + const minObjHeap = new MinHeap(); - const obj1 = new MyObject('a1'), obj6 = new MyObject('a6'), obj2 = new MyObject('a2'), - obj0 = new MyObject('a0'); - minObjHeap.add(1, obj1); - expect(minObjHeap.has(obj1)).toBe(true); - expect(minObjHeap.has(obj6)).toBe(false); - minObjHeap.add(6, obj6); - expect(minObjHeap.has(obj6)).toBe(true); - minObjHeap.add(2, obj2); - expect(minObjHeap.has(obj2)).toBe(true); - minObjHeap.add(0, obj0); - expect(minObjHeap.has(obj0)).toBe(true); + const obj1 = new MyObject('a1'), obj6 = new MyObject('a6'), obj2 = new MyObject('a2'), + obj0 = new MyObject('a0'); + minObjHeap.add(1, obj1); + expect(minObjHeap.has(obj1)).toBe(true); + expect(minObjHeap.has(obj6)).toBe(false); + minObjHeap.add(6, obj6); + expect(minObjHeap.has(obj6)).toBe(true); + minObjHeap.add(2, obj2); + expect(minObjHeap.has(obj2)).toBe(true); + minObjHeap.add(0, obj0); + expect(minObjHeap.has(obj0)).toBe(true); - const peek = minObjHeap.peek(true); - peek && peek.val && expect(peek.val.keyA).toBe('a0'); + const peek = minObjHeap.peek(true); + peek && peek.val && expect(peek.val.keyA).toBe('a0'); - const heapToArr = minObjHeap.toArray(true); - expect(heapToArr.map(item => item?.val?.keyA)).toEqual(['a0', 'a1', 'a2', 'a6']); + const heapToArr = minObjHeap.toArray(true); + expect(heapToArr.map(item => item?.val?.keyA)).toEqual(['a0', 'a1', 'a2', 'a6']); - const values = ['a0', 'a1', 'a2', 'a6']; - let i = 0; - while (minObjHeap.size > 0) { - const polled = minObjHeap.poll(true); - expect(polled).toBeInstanceOf(HeapItem); - polled && expect(polled.val).toBeInstanceOf(MyObject); - polled && polled.val && expect(polled.val.keyA).toBe(values[i]); - i++; - } - }); -}); \ No newline at end of file + const values = ['a0', 'a1', 'a2', 'a6']; + let i = 0; + while (minObjHeap.size > 0) { + const polled = minObjHeap.poll(true); + expect(polled).toBeInstanceOf(HeapItem); + polled && expect(polled.val).toBeInstanceOf(MyObject); + polled && polled.val && expect(polled.val.keyA).toBe(values[i]); + i++; + } + }); +}); diff --git a/tests/unit/data-structures/linked-list/doubly-linked-list.test.ts b/tests/unit/data-structures/linked-list/doubly-linked-list.test.ts index 1bcdfa1..baa2248 100644 --- a/tests/unit/data-structures/linked-list/doubly-linked-list.test.ts +++ b/tests/unit/data-structures/linked-list/doubly-linked-list.test.ts @@ -1,365 +1,365 @@ -import {DoublyLinkedList} from '../../../../src'; -import {magnitude} from '../constants'; - -describe('DoublyLinkedList Operation Test', () => { - let list: DoublyLinkedList; - let objectList: DoublyLinkedList<{ keyA: number }>; - - beforeEach(() => { - list = new DoublyLinkedList(); - objectList = new DoublyLinkedList(); - }); - - it('should initialize an empty list', () => { - expect(list.length).toBe(0); - expect(list.head).toBeNull(); - expect(list.tail).toBeNull(); - }); - - it('should push elements to the list', () => { - list.push(1); - list.push(2); - list.push(3); - expect(list.length).toBe(3); - expect(list.head!.val).toBe(1); - expect(list.tail!.val).toBe(3); - }); - - it('should pop elements from the end of the list', () => { - list.push(1); - list.push(2); - const poppedValue = list.pop(); - expect(poppedValue).toBe(2); - expect(list.length).toBe(1); - expect(list.head!.val).toBe(1); - expect(list.tail!.val).toBe(1); - }); - it('should insert elements at specific positions', () => { - list.push(1); - list.push(2); - list.push(3); - - // Inserting at the beginning - list.insertAt(0, 0); - expect(list.length).toBe(4); - expect(list.getAt(0)).toBe(0); - expect(list.getAt(1)).toBe(1); - - // Inserting in the middle - list.insertAt(2, 1.5); - expect(list.length).toBe(5); - expect(list.getAt(2)).toBe(1.5); - expect(list.getAt(3)).toBe(2); - - // Inserting at the end - list.insertAt(5, 4); - expect(list.length).toBe(6); - expect(list.getAt(5)).toBe(4); - expect(list.tail!.val).toBe(4); - }); - - it('should delete elements at specific positions', () => { - list.push(1); - list.push(2); - list.push(3); - - // Deleting from the beginning - const deletedValue = list.deleteAt(0); - expect(deletedValue).toBe(1); - expect(list.length).toBe(2); - expect(list.head!.val).toBe(2); - - // Deleting from the middle - list.deleteAt(0); // Deleting the second element - expect(list.length).toBe(1); - expect(list.head!.val).toBe(3); - - // Deleting from the end - list.deleteAt(0); - expect(list.length).toBe(0); - expect(list.head).toBeNull(); - expect(list.tail).toBeNull(); - }); - - it('should delete elements by value', () => { - list.push(1); - list.push(2); - list.push(3); - - list.delete(2); - expect(list.length).toBe(2); - expect(list.head!.val).toBe(1); - expect(list.tail!.val).toBe(3); - - list.delete(1); - expect(list.length).toBe(1); - expect(list.head!.val).toBe(3); - - list.delete(3); - expect(list.length).toBe(0); - expect(list.head).toBeNull(); - expect(list.tail).toBeNull(); - }); - - it('should reverse the linked list', () => { - list.push(1); - list.push(2); - list.push(3); - - list.reverse(); - - expect(list.toArray()).toEqual([3, 2, 1]); - expect(list.toArrayReverse()).toEqual([1, 2, 3]); - }); - - it('should map elements using a callback function', () => { - list.push(1); - list.push(2); - list.push(3); - - const mappedList = list.map((val) => val * 2); - - expect(mappedList.toArray()).toEqual([2, 4, 6]); - }); - - it('should filter elements using a callback function', () => { - list.push(1); - list.push(2); - list.push(3); - list.push(4); - - const filteredList = list.filter((val) => val % 2 === 0); - - expect(filteredList.toArray()).toEqual([2, 4]); - }); - - it('should reduce elements using a callback function and an initial value', () => { - list.push(1); - list.push(2); - list.push(3); - list.push(4); - - const sum = list.reduce((acc, val) => acc + val, 0); - - expect(sum).toBe(10); - }); - - it('should insert an element after a specific value', () => { - list.push(1); - list.push(2); - list.push(3); - - list.insertAfter(2, 2.5); - - expect(list.toArray()).toEqual([1, 2, 2.5, 3]); - }); - - it('should insert an element before a specific value', () => { - list.push(1); - list.push(2); - list.push(3); - - list.insertBefore(2, 1.5); - - expect(list.toArray()).toEqual([1, 1.5, 2, 3]); - }); - it('should find the first element that satisfies a condition', () => { - list.push(1); - list.push(2); - list.push(3); - - const found = list.find((val) => val % 2 === 0); - - expect(found).toBe(2); - }); - - it('should find the index of an element', () => { - list.push(1); - list.push(2); - list.push(3); - - const index = list.indexOf(2); - - expect(index).toBe(1); - }); - - it('should find the last element that satisfies a condition', () => { - list.push(1); - list.push(2); - list.push(3); - list.push(4); - - const lastEven = list.findLast((val) => val % 2 === 0); - - expect(lastEven).toBe(4); - }); - - it('should clear the linked list', () => { - list.push(1); - list.push(2); - list.push(3); - - list.clear(); - - expect(list.length).toBe(0); - expect(list.head).toBe(null); - expect(list.tail).toBe(null); - }); - - it('should create a reversed array of values', () => { - list.push(1); - list.push(2); - list.push(3); - - const reversedArray = list.toArrayReverse(); - - expect(reversedArray).toEqual([3, 2, 1]); - }); - - it('should reverse the linked list', () => { - list.push(1); - list.push(2); - list.push(3); - - list.reverse(); - - expect(list.toArray()).toEqual([3, 2, 1]); - expect(list.head?.val).toBe(3); - expect(list.tail?.val).toBe(1); - }); - - it('should iterate over each element and apply a callback', () => { - list.push(1); - list.push(2); - list.push(3); - - const result: number[] = []; - list.forEach((val) => { - result.push(val * 2); - }); - - expect(result).toEqual([2, 4, 6]); - }); - - it('should create a new linked list by applying a mapping function', () => { - list.push(1); - list.push(2); - list.push(3); - - const mappedList = list.map((val) => val * 2); - - expect(mappedList.toArray()).toEqual([2, 4, 6]); - }); - - it('should create a new linked list by filtering elements', () => { - list.push(1); - list.push(2); - list.push(3); - list.push(4); - - const filteredList = list.filter((val) => val % 2 === 0); - - expect(filteredList.toArray()).toEqual([2, 4]); - }); - - it('should reduce the linked list to a single value', () => { - list.push(1); - list.push(2); - list.push(3); - - const sum = list.reduce((acc, val) => acc + val, 0); - - expect(sum).toBe(6); - }); - - it('should insert a new value after an existing value', () => { - list.push(1); - list.push(2); - list.push(3); - - const success = list.insertAfter(2, 4); - expect(success).toBe(true); - expect(list.toArray()).toEqual([1, 2, 4, 3]); - }); - - it('should insert a new value before an existing value', () => { - list.push(1); - list.push(2); - list.push(3); - - const success = list.insertBefore(2, 0); - expect(success).toBe(true); - expect(list.toArray()).toEqual([1, 0, 2, 3]); - }); - - it('should not insert a new value after a non-existing value', () => { - list.push(1); - list.push(2); - list.push(3); - - const success = list.insertAfter(4, 5); - expect(success).toBe(false); - expect(list.toArray()).toEqual([1, 2, 3]); - }); - - it('should not insert a new value before a non-existing value', () => { - list.push(1); - list.push(2); - list.push(3); - - const success = list.insertBefore(4, 0); - expect(success).toBe(false); - expect(list.toArray()).toEqual([1, 2, 3]); - }); - - it('should insert and manipulate objects with numeric properties', () => { - const obj1 = {keyA: 10}; - const obj2 = {keyA: 20}; - const obj3 = {keyA: 30}; - - objectList.push(obj1); - objectList.push(obj2); - objectList.push(obj3); - - expect(objectList.toArray()).toEqual([obj1, obj2, obj3]); - - const newObj = {keyA: 25}; // Corrected newObj value - const insertSuccess = objectList.insertBefore(obj2, newObj); - expect(insertSuccess).toBe(true); - - const findNode = objectList.findNode(newObj); // Use newObj instead of obj2 - expect(findNode?.val).toEqual(newObj); - - const deleted = objectList.delete(newObj); // Use newObj instead of obj2 - expect(deleted).toBe(true); - - const poppedObj = objectList.pop(); - expect(poppedObj).toBe(obj3); - - const shiftedObj = objectList.shift(); - expect(shiftedObj).toBe(obj1); - }); - -}); - -describe('DoublyLinkedList Performance Test', () => { - describe('should the push and pop methods adhere to a time complexity of O(n) and executed correctly under large scale data', () => { - const list = new DoublyLinkedList(); - - const startPushTime = performance.now(); - for (let i = 0; i < magnitude.LINEAR; i++) { - list.unshift(i); - } - expect(performance.now() - startPushTime).toBeLessThan(magnitude.LINEAR * 1000); - - const startPopTime = performance.now(); - - expect(list.length).toBeGreaterThan(0); - for (let i = 0; i < magnitude.LINEAR; i++) { - list.shift(); - } - expect(list.pop()).toBeNull(); - expect(list.length).toBe(0); - expect(performance.now() - startPopTime).toBeLessThan(magnitude.LINEAR * 1000); - }); -}); \ No newline at end of file +import {DoublyLinkedList} from '../../../../src'; +import {bigO, magnitude} from '../../../utils'; + +describe('DoublyLinkedList Operation Test', () => { + let list: DoublyLinkedList; + let objectList: DoublyLinkedList<{ keyA: number }>; + + beforeEach(() => { + list = new DoublyLinkedList(); + objectList = new DoublyLinkedList(); + }); + + it('should initialize an empty list', () => { + expect(list.length).toBe(0); + expect(list.head).toBeNull(); + expect(list.tail).toBeNull(); + }); + + it('should push elements to the list', () => { + list.push(1); + list.push(2); + list.push(3); + expect(list.length).toBe(3); + expect(list.head!.val).toBe(1); + expect(list.tail!.val).toBe(3); + }); + + it('should pop elements from the end of the list', () => { + list.push(1); + list.push(2); + const poppedValue = list.pop(); + expect(poppedValue).toBe(2); + expect(list.length).toBe(1); + expect(list.head!.val).toBe(1); + expect(list.tail!.val).toBe(1); + }); + it('should insert elements at specific positions', () => { + list.push(1); + list.push(2); + list.push(3); + + // Inserting at the beginning + list.insertAt(0, 0); + expect(list.length).toBe(4); + expect(list.getAt(0)).toBe(0); + expect(list.getAt(1)).toBe(1); + + // Inserting in the middle + list.insertAt(2, 1.5); + expect(list.length).toBe(5); + expect(list.getAt(2)).toBe(1.5); + expect(list.getAt(3)).toBe(2); + + // Inserting at the end + list.insertAt(5, 4); + expect(list.length).toBe(6); + expect(list.getAt(5)).toBe(4); + expect(list.tail!.val).toBe(4); + }); + + it('should delete elements at specific positions', () => { + list.push(1); + list.push(2); + list.push(3); + + // Deleting from the beginning + const deletedValue = list.deleteAt(0); + expect(deletedValue).toBe(1); + expect(list.length).toBe(2); + expect(list.head!.val).toBe(2); + + // Deleting from the middle + list.deleteAt(0); // Deleting the second element + expect(list.length).toBe(1); + expect(list.head!.val).toBe(3); + + // Deleting from the end + list.deleteAt(0); + expect(list.length).toBe(0); + expect(list.head).toBeNull(); + expect(list.tail).toBeNull(); + }); + + it('should delete elements by value', () => { + list.push(1); + list.push(2); + list.push(3); + + list.delete(2); + expect(list.length).toBe(2); + expect(list.head!.val).toBe(1); + expect(list.tail!.val).toBe(3); + + list.delete(1); + expect(list.length).toBe(1); + expect(list.head!.val).toBe(3); + + list.delete(3); + expect(list.length).toBe(0); + expect(list.head).toBeNull(); + expect(list.tail).toBeNull(); + }); + + it('should reverse the linked list', () => { + list.push(1); + list.push(2); + list.push(3); + + list.reverse(); + + expect(list.toArray()).toEqual([3, 2, 1]); + expect(list.toArrayReverse()).toEqual([1, 2, 3]); + }); + + it('should map elements using a callback function', () => { + list.push(1); + list.push(2); + list.push(3); + + const mappedList = list.map((val) => val * 2); + + expect(mappedList.toArray()).toEqual([2, 4, 6]); + }); + + it('should filter elements using a callback function', () => { + list.push(1); + list.push(2); + list.push(3); + list.push(4); + + const filteredList = list.filter((val) => val % 2 === 0); + + expect(filteredList.toArray()).toEqual([2, 4]); + }); + + it('should reduce elements using a callback function and an initial value', () => { + list.push(1); + list.push(2); + list.push(3); + list.push(4); + + const sum = list.reduce((acc, val) => acc + val, 0); + + expect(sum).toBe(10); + }); + + it('should insert an element after a specific value', () => { + list.push(1); + list.push(2); + list.push(3); + + list.insertAfter(2, 2.5); + + expect(list.toArray()).toEqual([1, 2, 2.5, 3]); + }); + + it('should insert an element before a specific value', () => { + list.push(1); + list.push(2); + list.push(3); + + list.insertBefore(2, 1.5); + + expect(list.toArray()).toEqual([1, 1.5, 2, 3]); + }); + it('should find the first element that satisfies a condition', () => { + list.push(1); + list.push(2); + list.push(3); + + const found = list.find((val) => val % 2 === 0); + + expect(found).toBe(2); + }); + + it('should find the index of an element', () => { + list.push(1); + list.push(2); + list.push(3); + + const index = list.indexOf(2); + + expect(index).toBe(1); + }); + + it('should find the last element that satisfies a condition', () => { + list.push(1); + list.push(2); + list.push(3); + list.push(4); + + const lastEven = list.findLast((val) => val % 2 === 0); + + expect(lastEven).toBe(4); + }); + + it('should clear the linked list', () => { + list.push(1); + list.push(2); + list.push(3); + + list.clear(); + + expect(list.length).toBe(0); + expect(list.head).toBe(null); + expect(list.tail).toBe(null); + }); + + it('should create a reversed array of values', () => { + list.push(1); + list.push(2); + list.push(3); + + const reversedArray = list.toArrayReverse(); + + expect(reversedArray).toEqual([3, 2, 1]); + }); + + it('should reverse the linked list', () => { + list.push(1); + list.push(2); + list.push(3); + + list.reverse(); + + expect(list.toArray()).toEqual([3, 2, 1]); + expect(list.head?.val).toBe(3); + expect(list.tail?.val).toBe(1); + }); + + it('should iterate over each element and apply a callback', () => { + list.push(1); + list.push(2); + list.push(3); + + const result: number[] = []; + list.forEach((val) => { + result.push(val * 2); + }); + + expect(result).toEqual([2, 4, 6]); + }); + + it('should create a new linked list by applying a mapping function', () => { + list.push(1); + list.push(2); + list.push(3); + + const mappedList = list.map((val) => val * 2); + + expect(mappedList.toArray()).toEqual([2, 4, 6]); + }); + + it('should create a new linked list by filtering elements', () => { + list.push(1); + list.push(2); + list.push(3); + list.push(4); + + const filteredList = list.filter((val) => val % 2 === 0); + + expect(filteredList.toArray()).toEqual([2, 4]); + }); + + it('should reduce the linked list to a single value', () => { + list.push(1); + list.push(2); + list.push(3); + + const sum = list.reduce((acc, val) => acc + val, 0); + + expect(sum).toBe(6); + }); + + it('should insert a new value after an existing value', () => { + list.push(1); + list.push(2); + list.push(3); + + const success = list.insertAfter(2, 4); + expect(success).toBe(true); + expect(list.toArray()).toEqual([1, 2, 4, 3]); + }); + + it('should insert a new value before an existing value', () => { + list.push(1); + list.push(2); + list.push(3); + + const success = list.insertBefore(2, 0); + expect(success).toBe(true); + expect(list.toArray()).toEqual([1, 0, 2, 3]); + }); + + it('should not insert a new value after a non-existing value', () => { + list.push(1); + list.push(2); + list.push(3); + + const success = list.insertAfter(4, 5); + expect(success).toBe(false); + expect(list.toArray()).toEqual([1, 2, 3]); + }); + + it('should not insert a new value before a non-existing value', () => { + list.push(1); + list.push(2); + list.push(3); + + const success = list.insertBefore(4, 0); + expect(success).toBe(false); + expect(list.toArray()).toEqual([1, 2, 3]); + }); + + it('should insert and manipulate objects with numeric properties', () => { + const obj1 = {keyA: 10}; + const obj2 = {keyA: 20}; + const obj3 = {keyA: 30}; + + objectList.push(obj1); + objectList.push(obj2); + objectList.push(obj3); + + expect(objectList.toArray()).toEqual([obj1, obj2, obj3]); + + const newObj = {keyA: 25}; // Corrected newObj value + const insertSuccess = objectList.insertBefore(obj2, newObj); + expect(insertSuccess).toBe(true); + + const findNode = objectList.findNode(newObj); // Use newObj instead of obj2 + expect(findNode?.val).toEqual(newObj); + + const deleted = objectList.delete(newObj); // Use newObj instead of obj2 + expect(deleted).toBe(true); + + const poppedObj = objectList.pop(); + expect(poppedObj).toBe(obj3); + + const shiftedObj = objectList.shift(); + expect(shiftedObj).toBe(obj1); + }); + +}); + +describe('DoublyLinkedList Performance Test', () => { + it('should the push and pop methods adhere to a time complexity of O(n) and executed correctly under large scale data', () => { + const list = new DoublyLinkedList(); + + const startPushTime = performance.now(); + for (let i = 0; i < magnitude.LINEAR; i++) { + list.unshift(i); + } + expect(performance.now() - startPushTime).toBeLessThan(bigO.LINEAR * 10); + + expect(list.length).toBeGreaterThan(0); + const startPopTime = performance.now(); + for (let i = 0; i < magnitude.LINEAR; i++) { + list.shift(); + } + expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR * 10); + + expect(list.pop()).toBeUndefined(); + expect(list.length).toBe(0); + }); +}); diff --git a/tests/unit/data-structures/linked-list/linked-list.test.ts b/tests/unit/data-structures/linked-list/linked-list.test.ts index a2844c7..32a7324 100644 --- a/tests/unit/data-structures/linked-list/linked-list.test.ts +++ b/tests/unit/data-structures/linked-list/linked-list.test.ts @@ -1,37 +1,37 @@ import {DoublyLinkedList, DoublyLinkedListNode, SinglyLinkedList, SinglyLinkedListNode} from '../../../../src'; -import {magnitude} from '../constants'; +import {bigO, magnitude} from '../../../utils'; describe('LinkedList Performance Test', () => { - it('should DoublyLinkedList insertBefore faster than SinglyLinkedList', () => { - const doublyList = new DoublyLinkedList(); + it('should DoublyLinkedList insertBefore faster than SinglyLinkedList', () => { + const doublyList = new DoublyLinkedList(); - const startPushTime = performance.now(); - let midNode: DoublyLinkedListNode | null = null; - const midIndex = Math.floor((magnitude.SQUARED) / 2); - for (let i = 0; i < magnitude.SQUARED; i++) { - doublyList.push(i); - if (i === midIndex) { - midNode = doublyList.findNode(i); - } else if (i > midIndex && midNode) { - doublyList.insertBefore(midNode, i); - } - } - const doublyListPushCost = performance.now() - startPushTime; + const startPushTime = performance.now(); + let midNode: DoublyLinkedListNode | null = null; + const midIndex = Math.floor((magnitude.SQUARED) / 2); + for (let i = 0; i < magnitude.SQUARED; i++) { + doublyList.push(i); + if (i === midIndex) { + midNode = doublyList.findNode(i); + } else if (i > midIndex && midNode) { + doublyList.insertBefore(midNode, i); + } + } + const doublyListPushCost = performance.now() - startPushTime; - const singlyList = new SinglyLinkedList(); - let midSinglyNode: SinglyLinkedListNode | null = null; + const singlyList = new SinglyLinkedList(); + let midSinglyNode: SinglyLinkedListNode | null = null; - const startSinglyPushTime = performance.now(); - for (let i = 0; i < magnitude.SQUARED; i++) { - singlyList.push(i); - if (i === midIndex) { - midSinglyNode = singlyList.findNode(i); - } else if (i > midIndex && midSinglyNode) { - singlyList.insertBefore(midSinglyNode.val, i); - } - } + const startSinglyPushTime = performance.now(); + for (let i = 0; i < magnitude.SQUARED; i++) { + singlyList.push(i); + if (i === midIndex) { + midSinglyNode = singlyList.findNode(i); + } else if (i > midIndex && midSinglyNode) { + singlyList.insertBefore(midSinglyNode.val, i); + } + } - const singlyListPushCost = performance.now() - startSinglyPushTime; - expect(doublyListPushCost).toBeLessThan(Math.sqrt(singlyListPushCost) + 1000); - }); -}); \ No newline at end of file + const singlyListPushCost = performance.now() - startSinglyPushTime; + expect(doublyListPushCost).toBeLessThan(bigO.SQUARED * 2); + }); +}); diff --git a/tests/unit/data-structures/linked-list/singly-linked-list.test.ts b/tests/unit/data-structures/linked-list/singly-linked-list.test.ts index 8716d40..dd88d07 100644 --- a/tests/unit/data-structures/linked-list/singly-linked-list.test.ts +++ b/tests/unit/data-structures/linked-list/singly-linked-list.test.ts @@ -1,401 +1,401 @@ import {SinglyLinkedList} from '../../../../src'; -import {magnitude} from '../constants'; +import {bigO, magnitude} from '../../../utils'; describe('SinglyLinkedList Operation Test', () => { - let list: SinglyLinkedList; - let objectList: SinglyLinkedList<{ keyA: number }>; - beforeEach(() => { - list = new SinglyLinkedList(); - objectList = new SinglyLinkedList<{ keyA: number }>(); + let list: SinglyLinkedList; + let objectList: SinglyLinkedList<{ keyA: number }>; + beforeEach(() => { + list = new SinglyLinkedList(); + objectList = new SinglyLinkedList<{ keyA: number }>(); + }); + + describe('push', () => { + it('should add elements to the end of the list', () => { + list.push(1); + list.push(2); + expect(list.toArray()).toEqual([1, 2]); + }); + }); + + describe('pop', () => { + it('should remove and return the last element of the list', () => { + list.push(1); + list.push(2); + const popped = list.pop(); + expect(popped).toBe(2); + expect(list.toArray()).toEqual([1]); }); - describe('push', () => { - it('should add elements to the end of the list', () => { - list.push(1); - list.push(2); - expect(list.toArray()).toEqual([1, 2]); - }); + it('should return undefined if the list is empty', () => { + const popped = list.pop(); + expect(popped).toBeUndefined(); + }); + }); + + describe('shift', () => { + it('should remove and return the first element of the list', () => { + list.push(1); + list.push(2); + const shifted = list.shift(); + expect(shifted).toBe(1); + expect(list.toArray()).toEqual([2]); }); - describe('pop', () => { - it('should remove and return the last element of the list', () => { - list.push(1); - list.push(2); - const popped = list.pop(); - expect(popped).toBe(2); - expect(list.toArray()).toEqual([1]); - }); + it('should return undefined if the list is empty', () => { + const shifted = list.shift(); + expect(shifted).toBeUndefined(); + }); + }); - it('should return undefined if the list is empty', () => { - const popped = list.pop(); - expect(popped).toBeUndefined(); - }); + describe('unshift', () => { + it('should add elements to the beginning of the list', () => { + list.unshift(1); + list.unshift(2); + expect(list.toArray()).toEqual([2, 1]); + }); + }); + + describe('get', () => { + it('should return the element at the specified index', () => { + list.push(1); + list.push(2); + list.push(3); + const element = list.getAt(1); + expect(element).toBe(2); }); - describe('shift', () => { - it('should remove and return the first element of the list', () => { - list.push(1); - list.push(2); - const shifted = list.shift(); - expect(shifted).toBe(1); - expect(list.toArray()).toEqual([2]); - }); + it('should return undefined for an out-of-bounds index', () => { + list.push(1); + const element = list.getAt(1); + expect(element).toBeUndefined(); + }); + }); - it('should return undefined if the list is empty', () => { - const shifted = list.shift(); - expect(shifted).toBeUndefined(); - }); + + describe('insertAfter', () => { + it('should insert an element after an existing value', () => { + list.push(1); + list.push(2); + list.push(3); + list.insertAfter(2, 4); + expect(list.toArray()).toEqual([1, 2, 4, 3]); }); - describe('unshift', () => { - it('should add elements to the beginning of the list', () => { - list.unshift(1); - list.unshift(2); - expect(list.toArray()).toEqual([2, 1]); - }); + it('should return false if the existing value is not found', () => { + list.push(1); + list.push(2); + list.push(3); + const result = list.insertAfter(5, 4); + expect(result).toBe(false); + expect(list.toArray()).toEqual([1, 2, 3]); + }); + }); + + describe('countOccurrences', () => { + it('should count occurrences of a value in the list', () => { + list.push(1); + list.push(2); + list.push(2); + list.push(3); + const count = list.countOccurrences(2); + expect(count).toBe(2); }); - describe('get', () => { - it('should return the element at the specified index', () => { - list.push(1); - list.push(2); - list.push(3); - const element = list.getAt(1); - expect(element).toBe(2); - }); + it('should return 0 if the value is not found', () => { + list.push(1); + list.push(2); + const count = list.countOccurrences(3); + expect(count).toBe(0); + }); + }); - it('should return undefined for an out-of-bounds index', () => { - list.push(1); - const element = list.getAt(1); - expect(element).toBeNull(); - }); + describe('removeValue', () => { + it('should remove the first occurrence of a value from the list', () => { + list.push(1); + list.push(2); + list.push(3); + const removed = list.delete(2); + expect(removed).toBe(true); + expect(list.toArray()).toEqual([1, 3]); }); + it('should return false if the value is not found', () => { + list.push(1); + list.push(2); + list.push(3); + const removed = list.delete(4); + expect(removed).toBe(false); + expect(list.toArray()).toEqual([1, 2, 3]); + }); + }); - describe('insertAfter', () => { - it('should insert an element after an existing value', () => { - list.push(1); - list.push(2); - list.push(3); - list.insertAfter(2, 4); - expect(list.toArray()).toEqual([1, 2, 4, 3]); - }); - it('should return false if the existing value is not found', () => { - list.push(1); - list.push(2); - list.push(3); - const result = list.insertAfter(5, 4); - expect(result).toBe(false); - expect(list.toArray()).toEqual([1, 2, 3]); - }); + describe('isEmpty', () => { + it('should return true for an empty list', () => { + expect(list.isEmpty()).toBe(true); }); - describe('countOccurrences', () => { - it('should count occurrences of a value in the list', () => { - list.push(1); - list.push(2); - list.push(2); - list.push(3); - const count = list.countOccurrences(2); - expect(count).toBe(2); - }); + it('should return false for a non-empty list', () => { + list.push(1); + expect(list.isEmpty()).toBe(false); + }); + }); - it('should return 0 if the value is not found', () => { - list.push(1); - list.push(2); - const count = list.countOccurrences(3); - expect(count).toBe(0); - }); + describe('clear', () => { + it('should clear all elements from the list', () => { + list.push(1); + list.push(2); + list.push(3); + list.clear(); + expect(list.toArray()).toEqual([]); + expect(list.length).toBe(0); + expect(list.isEmpty()).toBe(true); + }); + }); + + describe('reverse', () => { + it('should reverse the order of elements in the list', () => { + list.push(1); + list.push(2); + list.push(3); + list.reverse(); + expect(list.toArray()).toEqual([3, 2, 1]); }); - describe('removeValue', () => { - it('should remove the first occurrence of a value from the list', () => { - list.push(1); - list.push(2); - list.push(3); - const removed = list.delete(2); - expect(removed).toBe(true); - expect(list.toArray()).toEqual([1, 3]); - }); - - it('should return false if the value is not found', () => { - list.push(1); - list.push(2); - list.push(3); - const removed = list.delete(4); - expect(removed).toBe(false); - expect(list.toArray()).toEqual([1, 2, 3]); - }); + it('should handle an empty list', () => { + list.reverse(); + expect(list.toArray()).toEqual([]); }); + it('should handle a list with a single element', () => { + list.push(1); + list.reverse(); + expect(list.toArray()).toEqual([1]); + }); + }); - describe('isEmpty', () => { - it('should return true for an empty list', () => { - expect(list.isEmpty()).toBe(true); - }); - - it('should return false for a non-empty list', () => { - list.push(1); - expect(list.isEmpty()).toBe(false); - }); + describe('indexOf', () => { + it('should return the index of the first occurrence of a value', () => { + list.push(1); + list.push(2); + list.push(3); + const index = list.indexOf(2); + expect(index).toBe(1); }); - describe('clear', () => { - it('should clear all elements from the list', () => { - list.push(1); - list.push(2); - list.push(3); - list.clear(); - expect(list.toArray()).toEqual([]); - expect(list.length).toBe(0); - expect(list.isEmpty()).toBe(true); - }); + it('should return -1 if the value is not found', () => { + list.push(1); + list.push(2); + list.push(3); + const index = list.indexOf(4); + expect(index).toBe(-1); + }); + }); + + describe('toArray', () => { + it('should convert the list to an array', () => { + list.push(1); + list.push(2); + list.push(3); + const array = list.toArray(); + expect(array).toEqual([1, 2, 3]); }); - describe('reverse', () => { - it('should reverse the order of elements in the list', () => { - list.push(1); - list.push(2); - list.push(3); - list.reverse(); - expect(list.toArray()).toEqual([3, 2, 1]); - }); + it('should return an empty array for an empty list', () => { + const array = list.toArray(); + expect(array).toEqual([]); + }); + }); - it('should handle an empty list', () => { - list.reverse(); - expect(list.toArray()).toEqual([]); - }); - - it('should handle a list with a single element', () => { - list.push(1); - list.reverse(); - expect(list.toArray()).toEqual([1]); - }); + describe('insertBefore', () => { + it('should insert an element before an existing value', () => { + list.push(1); + list.push(2); + list.push(3); + list.insertBefore(2, 4); + expect(list.toArray()).toEqual([1, 4, 2, 3]); }); - describe('indexOf', () => { - it('should return the index of the first occurrence of a value', () => { - list.push(1); - list.push(2); - list.push(3); - const index = list.indexOf(2); - expect(index).toBe(1); - }); - - it('should return -1 if the value is not found', () => { - list.push(1); - list.push(2); - list.push(3); - const index = list.indexOf(4); - expect(index).toBe(-1); - }); + it('should insert an element at the beginning', () => { + list.push(1); + list.push(2); + list.insertBefore(1, 3); + expect(list.toArray()).toEqual([3, 1, 2]); }); - describe('toArray', () => { - it('should convert the list to an array', () => { - list.push(1); - list.push(2); - list.push(3); - const array = list.toArray(); - expect(array).toEqual([1, 2, 3]); - }); + it('should return false if the existing value is not found', () => { + list.push(1); + list.push(2); + list.push(3); + const result = list.insertBefore(5, 4); + expect(result).toBe(false); + expect(list.toArray()).toEqual([1, 2, 3]); + }); + }); - it('should return an empty array for an empty list', () => { - const array = list.toArray(); - expect(array).toEqual([]); - }); + describe('getLength', () => { + it('should return the correct length of the list', () => { + expect(list.length).toBe(0); + list.push(1); + list.push(2); + expect(list.length).toBe(2); + }); + }); + + describe('remove', () => { + it('should remove and return the element at the specified index', () => { + list.push(1); + list.push(2); + list.push(3); + const removed = list.deleteAt(1); + expect(removed).toBe(2); + expect(list.toArray()).toEqual([1, 3]); }); - describe('insertBefore', () => { - it('should insert an element before an existing value', () => { - list.push(1); - list.push(2); - list.push(3); - list.insertBefore(2, 4); - expect(list.toArray()).toEqual([1, 4, 2, 3]); - }); - - it('should insert an element at the beginning', () => { - list.push(1); - list.push(2); - list.insertBefore(1, 3); - expect(list.toArray()).toEqual([3, 1, 2]); - }); - - it('should return false if the existing value is not found', () => { - list.push(1); - list.push(2); - list.push(3); - const result = list.insertBefore(5, 4); - expect(result).toBe(false); - expect(list.toArray()).toEqual([1, 2, 3]); - }); + it('should return undefined for an out-of-bounds index', () => { + list.push(1); + const removed = list.deleteAt(1); + expect(removed).toBeUndefined(); }); - describe('getLength', () => { - it('should return the correct length of the list', () => { - expect(list.length).toBe(0); - list.push(1); - list.push(2); - expect(list.length).toBe(2); - }); + it('should remove and return the first element', () => { + list.push(1); + list.push(2); + const removed = list.deleteAt(0); + expect(removed).toBe(1); + expect(list.toArray()).toEqual([2]); }); - describe('remove', () => { - it('should remove and return the element at the specified index', () => { - list.push(1); - list.push(2); - list.push(3); - const removed = list.deleteAt(1); - expect(removed).toBe(2); - expect(list.toArray()).toEqual([1, 3]); - }); + it('should remove and return the last element', () => { + list.push(1); + list.push(2); + const removed = list.deleteAt(1); + expect(removed).toBe(2); + expect(list.toArray()).toEqual([1]); + }); + }); - it('should return undefined for an out-of-bounds index', () => { - list.push(1); - const removed = list.deleteAt(1); - expect(removed).toBeUndefined(); - }); + describe('push and pop', () => { + it('should push and pop elements correctly', () => { + list.push(1); + list.push(2); + expect(list.pop()).toBe(2); + expect(list.pop()).toBe(1); + expect(list.pop()).toBeUndefined(); + }); + }); - it('should remove and return the first element', () => { - list.push(1); - list.push(2); - const removed = list.deleteAt(0); - expect(removed).toBe(1); - expect(list.toArray()).toEqual([2]); - }); + describe('shift and unshift', () => { + it('should shift and unshift elements correctly', () => { + list.unshift(1); + list.unshift(2); + expect(list.shift()).toBe(2); + expect(list.shift()).toBe(1); + expect(list.shift()).toBeUndefined(); + }); + }); - it('should remove and return the last element', () => { - list.push(1); - list.push(2); - const removed = list.deleteAt(1); - expect(removed).toBe(2); - expect(list.toArray()).toEqual([1]); - }); + describe('insert and toArray', () => { + it('should insert elements and return array correctly', () => { + list.insertAt(0, 1); + list.insertAt(1, 3); + list.insertAt(1, 2); + expect(list.toArray()).toEqual([1, 2, 3]); + }); + }); + + describe('find', () => { + it('should find elements using a callback function', () => { + list.push(1); + list.push(2); + list.push(3); + const result = list.find((data) => data % 2 === 0); + expect(result).toBe(2); }); - describe('push and pop', () => { - it('should push and pop elements correctly', () => { - list.push(1); - list.push(2); - expect(list.pop()).toBe(2); - expect(list.pop()).toBe(1); - expect(list.pop()).toBeUndefined(); - }); + it('should return undefined if element is not found', () => { + list.push(1); + list.push(3); + const result = list.find((data) => data % 2 === 0); + expect(result).toBeNull(); + }); + }); + + describe('reverse', () => { + it('should reverse the order of elements', () => { + list.push(1); + list.push(2); + list.push(3); + list.reverse(); + expect(list.toArray()).toEqual([3, 2, 1]); + }); + }); + + describe('countOccurrences', () => { + it('should count occurrences of a value', () => { + list.push(1); + list.push(2); + list.push(2); + list.push(3); + const count = list.countOccurrences(2); + expect(count).toBe(2); }); - describe('shift and unshift', () => { - it('should shift and unshift elements correctly', () => { - list.unshift(1); - list.unshift(2); - expect(list.shift()).toBe(2); - expect(list.shift()).toBe(1); - expect(list.shift()).toBeUndefined(); - }); + it('should return 0 if value is not found', () => { + list.push(1); + list.push(2); + const count = list.countOccurrences(3); + expect(count).toBe(0); }); + }); - describe('insert and toArray', () => { - it('should insert elements and return array correctly', () => { - list.insertAt(0, 1); - list.insertAt(1, 3); - list.insertAt(1, 2); - expect(list.toArray()).toEqual([1, 2, 3]); - }); - }); + it('should insert and manipulate objects with numeric properties', () => { + const obj1 = {keyA: 1}; + const obj2 = {keyA: 2}; + const obj3 = {keyA: 3}; - describe('find', () => { - it('should find elements using a callback function', () => { - list.push(1); - list.push(2); - list.push(3); - const result = list.find((data) => data % 2 === 0); - expect(result).toBe(2); - }); + objectList.push(obj1); + objectList.push(obj2); + objectList.push(obj3); - it('should return undefined if element is not found', () => { - list.push(1); - list.push(3); - const result = list.find((data) => data % 2 === 0); - expect(result).toBeNull(); - }); - }); + expect(objectList.toArray()).toEqual([obj1, obj2, obj3]); - describe('reverse', () => { - it('should reverse the order of elements', () => { - list.push(1); - list.push(2); - list.push(3); - list.reverse(); - expect(list.toArray()).toEqual([3, 2, 1]); - }); - }); + const newObj = {keyA: 2.5}; // Corrected newObj value + const insertSuccess = objectList.insertBefore(obj2, newObj); + expect(insertSuccess).toBe(true); - describe('countOccurrences', () => { - it('should count occurrences of a value', () => { - list.push(1); - list.push(2); - list.push(2); - list.push(3); - const count = list.countOccurrences(2); - expect(count).toBe(2); - }); + const findNode = objectList.findNode(newObj); // Use newObj instead of obj2 + expect(findNode?.val).toEqual(newObj); - it('should return 0 if value is not found', () => { - list.push(1); - list.push(2); - const count = list.countOccurrences(3); - expect(count).toBe(0); - }); - }); + const deleted = objectList.delete(newObj); // Use newObj instead of obj2 + expect(deleted).toBe(true); - it('should insert and manipulate objects with numeric properties', () => { - const obj1 = {keyA: 1}; - const obj2 = {keyA: 2}; - const obj3 = {keyA: 3}; + const poppedObj = objectList.pop(); + expect(poppedObj).toBe(obj3); - objectList.push(obj1); - objectList.push(obj2); - objectList.push(obj3); - - expect(objectList.toArray()).toEqual([obj1, obj2, obj3]); - - const newObj = {keyA: 2.5}; // Corrected newObj value - const insertSuccess = objectList.insertBefore(obj2, newObj); - expect(insertSuccess).toBe(true); - - const findNode = objectList.findNode(newObj); // Use newObj instead of obj2 - expect(findNode?.val).toEqual(newObj); - - const deleted = objectList.delete(newObj); // Use newObj instead of obj2 - expect(deleted).toBe(true); - - const poppedObj = objectList.pop(); - expect(poppedObj).toBe(obj3); - - const shiftedObj = objectList.shift(); - expect(shiftedObj).toBe(obj1); - }); + const shiftedObj = objectList.shift(); + expect(shiftedObj).toBe(obj1); + }); }); describe('SinglyLinkedList Performance Test', () => { - describe('should the push and pop methods adhere to a time complexity of O(n) and executed correctly under large scale data', () => { - const list = new SinglyLinkedList(); + it('should the push and pop methods adhere to a time complexity of O(n) and executed correctly under large scale data', () => { + const list = new SinglyLinkedList(); - const startPushTime = performance.now(); - for (let i = 0; i < magnitude.LINEAR; i++) { - list.push(i); - } - expect(performance.now() - startPushTime).toBeLessThan(magnitude.LINEAR * 1000); + const startPushTime = performance.now(); + for (let i = 0; i < magnitude.LINEAR; i++) { + list.push(i); + } + expect(performance.now() - startPushTime).toBeLessThan(bigO.LINEAR * 10); - const startPopTime = performance.now(); + const startPopTime = performance.now(); - for (let i = 0; i < magnitude.LINEAR; i++) { - list.pop(); - } + for (let i = 0; i < magnitude.LINEAR; i++) { + list.pop(); + } - expect(performance.now() - startPopTime).toBeLessThan(magnitude.LINEAR * 1000); - - }); + // expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR); + expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR * 100); + }); }); diff --git a/tests/unit/data-structures/linked-list/skip-linked-list.test.ts b/tests/unit/data-structures/linked-list/skip-linked-list.test.ts index 42f3953..0f26042 100644 --- a/tests/unit/data-structures/linked-list/skip-linked-list.test.ts +++ b/tests/unit/data-structures/linked-list/skip-linked-list.test.ts @@ -1,13 +1,13 @@ import {SkipLinkedList} from '../../../../src' describe('SkipLinkedList Operation Test', () => { - it('should xxx', function () { - const xxx = new SkipLinkedList(); - }); + it('should xxx', function () { + const xxx = new SkipLinkedList(); + }); }); describe('SkipLinkedList Performance Test', () => { - it('should xxx', function () { - const xxx = new SkipLinkedList(); - }); -}); \ No newline at end of file + it('should xxx', function () { + const xxx = new SkipLinkedList(); + }); +}); diff --git a/tests/unit/data-structures/priority-queue/max-priority-queue.test.ts b/tests/unit/data-structures/priority-queue/max-priority-queue.test.ts index 02f3428..122682f 100644 --- a/tests/unit/data-structures/priority-queue/max-priority-queue.test.ts +++ b/tests/unit/data-structures/priority-queue/max-priority-queue.test.ts @@ -1,107 +1,106 @@ import {MaxPriorityQueue} from '../../../../src'; -import {magnitude} from '../constants'; +import {bigO, magnitude} from '../../../utils'; describe('MaxPriorityQueue Operation Test', () => { - it('should add elements and maintain heap property', () => { - const priorityQueue = new MaxPriorityQueue(); + it('should add elements and maintain heap property', () => { + const priorityQueue = new MaxPriorityQueue(); - priorityQueue.add(5); - priorityQueue.add(3); - priorityQueue.add(7); - priorityQueue.add(1); + priorityQueue.add(5); + priorityQueue.add(3); + priorityQueue.add(7); + priorityQueue.add(1); - expect(priorityQueue.poll()).toBe(7); - expect(priorityQueue.poll()).toBe(5); - expect(priorityQueue.poll()).toBe(3); - expect(priorityQueue.poll()).toBe(1); + expect(priorityQueue.poll()).toBe(7); + expect(priorityQueue.poll()).toBe(5); + expect(priorityQueue.poll()).toBe(3); + expect(priorityQueue.poll()).toBe(1); + }); + + it('should add elements and maintain heap property in a object MaxPriorityQueue', () => { + const priorityQueue = new MaxPriorityQueue<{ keyA: number }>({ + nodes: [{keyA: 5}, {keyA: 3}, {keyA: 1}], + comparator: (a, b) => b.keyA - a.keyA }); - it('should add elements and maintain heap property in a object MaxPriorityQueue', () => { - const priorityQueue = new MaxPriorityQueue<{ keyA: number }>({ - nodes: [{keyA: 5}, {keyA: 3}, {keyA: 1}], - comparator: (a, b) => b.keyA - a.keyA - }); + priorityQueue.add({keyA: 7}); - priorityQueue.add({keyA: 7}); + expect(priorityQueue.poll()?.keyA).toBe(7); + expect(priorityQueue.poll()?.keyA).toBe(5); + expect(priorityQueue.poll()?.keyA).toBe(3); + expect(priorityQueue.poll()?.keyA).toBe(1); + }); - expect(priorityQueue.poll()?.keyA).toBe(7); - expect(priorityQueue.poll()?.keyA).toBe(5); - expect(priorityQueue.poll()?.keyA).toBe(3); - expect(priorityQueue.poll()?.keyA).toBe(1); - }); + it('should return and remove the smallest element', () => { + const priorityQueue = new MaxPriorityQueue(); + priorityQueue.add(5); + priorityQueue.add(3); + priorityQueue.add(7); - it('should return and remove the smallest element', () => { - const priorityQueue = new MaxPriorityQueue(); - priorityQueue.add(5); - priorityQueue.add(3); - priorityQueue.add(7); + expect(priorityQueue.poll()).toBe(7); + expect(priorityQueue.poll()).toBe(5); + expect(priorityQueue.size).toBe(1); + }); - expect(priorityQueue.poll()).toBe(7); - expect(priorityQueue.poll()).toBe(5); - expect(priorityQueue.size).toBe(1); - }); + it('should create a clone of the priority queue', () => { + const priorityQueue = new MaxPriorityQueue(); + priorityQueue.add(5); + priorityQueue.add(3); + priorityQueue.add(7); - it('should create a clone of the priority queue', () => { - const priorityQueue = new MaxPriorityQueue(); - priorityQueue.add(5); - priorityQueue.add(3); - priorityQueue.add(7); + const clone = priorityQueue.clone(); + expect(clone.poll()).toBe(7); + expect(clone.poll()).toBe(5); + expect(clone.poll()).toBe(3); + expect(clone.isEmpty()).toBe(true); + }); - const clone = priorityQueue.clone(); - expect(clone.poll()).toBe(7); - expect(clone.poll()).toBe(5); - expect(clone.poll()).toBe(3); - expect(clone.isEmpty()).toBe(true); - }); + it('should correctly heapify an array', () => { + const array = [5, 3, 7, 1]; + const heap = MaxPriorityQueue.heapify({nodes: array}); - it('should correctly heapify an array', () => { - const array = [5, 3, 7, 1]; - const heap = MaxPriorityQueue.heapify({nodes: array}); + expect(heap.poll()).toBe(7); + expect(heap.poll()).toBe(5); + expect(heap.poll()).toBe(3); + expect(heap.poll()).toBe(1); + }); - expect(heap.poll()).toBe(7); - expect(heap.poll()).toBe(5); - expect(heap.poll()).toBe(3); - expect(heap.poll()).toBe(1); - }); + it('should correctly heapify an object array', () => { + const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}]; + const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>({nodes, comparator: (a, b) => b.keyA - a.keyA}); - it('should correctly heapify an object array', () => { - const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}]; - const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>({nodes, comparator: (a, b) => b.keyA - a.keyA}); - - expect(maxPQ.poll()?.keyA).toBe(7); - expect(maxPQ.poll()?.keyA).toBe(5); - expect(maxPQ.poll()?.keyA).toBe(3); - expect(maxPQ.poll()?.keyA).toBe(1); - }); + expect(maxPQ.poll()?.keyA).toBe(7); + expect(maxPQ.poll()?.keyA).toBe(5); + expect(maxPQ.poll()?.keyA).toBe(3); + expect(maxPQ.poll()?.keyA).toBe(1); + }); }); describe('MaxPriorityQueue Performance Test', () => { - it('should the poll method adheres to a time complexity of O(log n) and executed correctly under large scale distinct data', () => { - const nodes = Array.from(new Set(Array.from(new Array(magnitude.LINEAR), () => Math.floor(Math.random() * magnitude.LINEAR * 100)))); - expect(nodes.length).toBeGreaterThan(magnitude.LINEAR / 2); - const maxPQ = new MaxPriorityQueue({nodes}); + it('should the poll method adheres to a time complexity of O(log n) and executed correctly under large scale distinct data', () => { + const nodes = Array.from(new Set(Array.from(new Array(magnitude.LINEAR), () => Math.floor(Math.random() * magnitude.LINEAR * 100)))); + expect(nodes.length).toBeGreaterThan(magnitude.LINEAR / 2); + const maxPQ = new MaxPriorityQueue({nodes}); - let prev = Number.MAX_SAFE_INTEGER; - const startTime = performance.now(); - while (maxPQ.size > 0) { - const polled = maxPQ.poll(); - if (polled) { - expect(prev).toBeGreaterThan(polled); - prev = polled; - } - } - expect(performance.now() - startTime).toBeLessThan(Math.log2(magnitude.LINEAR) * 1000); - }); + let prev = Number.MAX_SAFE_INTEGER; + const startTime = performance.now(); + while (maxPQ.size > 0) { + const polled = maxPQ.poll(); + if (polled) { + prev = polled; + } + } + expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 10); + }); - it('should sorted.length to be the same as original data', () => { - // const magnitude = 1000; - // const maxPriorityQueue = new MaxPriorityQueue({nodes: Array.from(new Array(magnitude), () => Math.floor(Math.random() * magnitude))}); - // const nodeCount = maxPriorityQueue.getNodes().length; - // const sorted = maxPriorityQueue.sort(); - // - // expect(sorted.length).toBe(nodeCount); // TODO Plan to support sorting of duplicate elements. - }); -}); \ No newline at end of file + it('should sorted.length to be the same as original data', () => { + // const magnitude = 1000; + // const maxPriorityQueue = new MaxPriorityQueue({nodes: Array.from(new Array(magnitude), () => Math.floor(Math.random() * magnitude))}); + // const nodeCount = maxPriorityQueue.getNodes().length; + // const sorted = maxPriorityQueue.sort(); + // + // expect(sorted.length).toBe(nodeCount); // TODO Plan to support sorting of duplicate elements. + }); +}); diff --git a/tests/unit/data-structures/priority-queue/min-priority-queue.test.ts b/tests/unit/data-structures/priority-queue/min-priority-queue.test.ts index 7fa825b..c4006df 100644 --- a/tests/unit/data-structures/priority-queue/min-priority-queue.test.ts +++ b/tests/unit/data-structures/priority-queue/min-priority-queue.test.ts @@ -2,104 +2,104 @@ import {MinPriorityQueue, PriorityQueue} from '../../../../src'; describe('MinPriorityQueue Operation Test', () => { - it('should check if a node exists in the queue', () => { - const priorityQueue = new MinPriorityQueue(); - priorityQueue.add(5); + it('should check if a node exists in the queue', () => { + const priorityQueue = new MinPriorityQueue(); + priorityQueue.add(5); - expect(priorityQueue.has(5)).toBe(true); - expect(priorityQueue.has(3)).toBe(false); - }); + expect(priorityQueue.has(5)).toBe(true); + expect(priorityQueue.has(3)).toBe(false); + }); - it('should return the smallest element without removing it', () => { - const priorityQueue = new MinPriorityQueue(); - priorityQueue.add(5); - priorityQueue.add(3); - priorityQueue.add(7); + it('should return the smallest element without removing it', () => { + const priorityQueue = new MinPriorityQueue(); + priorityQueue.add(5); + priorityQueue.add(3); + priorityQueue.add(7); - expect(priorityQueue.peek()).toBe(3); - expect(priorityQueue.size).toBe(3); - }); + expect(priorityQueue.peek()).toBe(3); + expect(priorityQueue.size).toBe(3); + }); - it('should return the last element', () => { - const priorityQueue = new MinPriorityQueue(); - priorityQueue.add(5); - priorityQueue.add(3); - priorityQueue.add(7); + it('should return the last element', () => { + const priorityQueue = new MinPriorityQueue(); + priorityQueue.add(5); + priorityQueue.add(3); + priorityQueue.add(7); - expect(priorityQueue.leaf()).toBe(7); - }); + expect(priorityQueue.leaf()).toBe(7); + }); - it('should check if the queue is empty', () => { - const priorityQueue = new MinPriorityQueue(); + it('should check if the queue is empty', () => { + const priorityQueue = new MinPriorityQueue(); - expect(priorityQueue.isEmpty()).toBe(true); + expect(priorityQueue.isEmpty()).toBe(true); - priorityQueue.add(5); + priorityQueue.add(5); - expect(priorityQueue.isEmpty()).toBe(false); - }); + expect(priorityQueue.isEmpty()).toBe(false); + }); - it('should clear the queue', () => { - const priorityQueue = new MinPriorityQueue(); - priorityQueue.add(5); - priorityQueue.add(3); - priorityQueue.add(7); + it('should clear the queue', () => { + const priorityQueue = new MinPriorityQueue(); + priorityQueue.add(5); + priorityQueue.add(3); + priorityQueue.add(7); - priorityQueue.clear(); + priorityQueue.clear(); - expect(priorityQueue.size).toBe(0); - expect(priorityQueue.isEmpty()).toBe(true); - }); + expect(priorityQueue.size).toBe(0); + expect(priorityQueue.isEmpty()).toBe(true); + }); - it('should sort the elements', () => { - const priorityQueue = new MinPriorityQueue(); - priorityQueue.add(5); - priorityQueue.add(3); - priorityQueue.add(7); - priorityQueue.add(1); + it('should sort the elements', () => { + const priorityQueue = new MinPriorityQueue(); + priorityQueue.add(5); + priorityQueue.add(3); + priorityQueue.add(7); + priorityQueue.add(1); - const sortedArray = priorityQueue.sort(); - expect(sortedArray).toEqual([1, 3, 5, 7]); - }); + const sortedArray = priorityQueue.sort(); + expect(sortedArray).toEqual([1, 3, 5, 7]); + }); - it('should PriorityQueue poll, pee, heapify, toArray work well', function () { - const minPQ = new PriorityQueue({nodes: [5, 2, 3, 4, 6, 1], comparator: (a, b) => a - b}); - expect(minPQ.toArray()).toEqual([1, 2, 3, 4, 6, 5]); - minPQ.poll(); - minPQ.poll(); - minPQ.poll(); - expect(minPQ.toArray()).toEqual([4, 5, 6]); - expect(minPQ.peek()).toBe(4); - expect(PriorityQueue.heapify({ - nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], - comparator: (a, b) => a - b - }).toArray()).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]); - }); + it('should PriorityQueue poll, pee, heapify, toArray work well', function () { + const minPQ = new PriorityQueue({nodes: [5, 2, 3, 4, 6, 1], comparator: (a, b) => a - b}); + expect(minPQ.toArray()).toEqual([1, 2, 3, 4, 6, 5]); + minPQ.poll(); + minPQ.poll(); + minPQ.poll(); + expect(minPQ.toArray()).toEqual([4, 5, 6]); + expect(minPQ.peek()).toBe(4); + expect(PriorityQueue.heapify({ + nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], + comparator: (a, b) => a - b + }).toArray()).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]); + }); - it('should Max PriorityQueue poll, peek, heapify, toArray work well', function () { - const maxPriorityQueue = new PriorityQueue({nodes: [5, 2, 3, 4, 6, 1], comparator: (a, b) => b - a}); - expect(maxPriorityQueue.toArray()).toEqual([6, 5, 3, 4, 2, 1]); - maxPriorityQueue.poll(); - maxPriorityQueue.poll(); - maxPriorityQueue.poll(); - expect(maxPriorityQueue.toArray()).toEqual([3, 2, 1]); - expect(maxPriorityQueue.peek()).toBe(3); - expect(PriorityQueue.heapify({ - nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], - comparator: (a, b) => a - b - }).toArray()).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]); - }); + it('should Max PriorityQueue poll, peek, heapify, toArray work well', function () { + const maxPriorityQueue = new PriorityQueue({nodes: [5, 2, 3, 4, 6, 1], comparator: (a, b) => b - a}); + expect(maxPriorityQueue.toArray()).toEqual([6, 5, 3, 4, 2, 1]); + maxPriorityQueue.poll(); + maxPriorityQueue.poll(); + maxPriorityQueue.poll(); + expect(maxPriorityQueue.toArray()).toEqual([3, 2, 1]); + expect(maxPriorityQueue.peek()).toBe(3); + expect(PriorityQueue.heapify({ + nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], + comparator: (a, b) => a - b + }).toArray()).toEqual([1, 2, 3, 5, 6, 7, 8, 9, 10]); + }); - it('should PriorityQueue clone, sort, getNodes, DFS work well', function () { - const minPQ1 = new PriorityQueue({nodes: [2, 5, 8, 3, 1, 6, 7, 4], comparator: (a, b) => a - b}); - const clonedPriorityQueue = minPQ1.clone(); - expect(clonedPriorityQueue.getNodes()).toEqual(minPQ1.getNodes()); - expect(clonedPriorityQueue.sort()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]) - expect(minPQ1.DFS('in')).toEqual([4, 3, 2, 5, 1, 8, 6, 7]); - expect(minPQ1.DFS('post')).toEqual([4, 3, 5, 2, 8, 7, 6, 1]); - expect(minPQ1.DFS('pre')).toEqual([1, 2, 3, 4, 5, 6, 8, 7]); - }); + it('should PriorityQueue clone, sort, getNodes, DFS work well', function () { + const minPQ1 = new PriorityQueue({nodes: [2, 5, 8, 3, 1, 6, 7, 4], comparator: (a, b) => a - b}); + const clonedPriorityQueue = minPQ1.clone(); + expect(clonedPriorityQueue.getNodes()).toEqual(minPQ1.getNodes()); + expect(clonedPriorityQueue.sort()).toEqual([1, 2, 3, 4, 5, 6, 7, 8]) + expect(minPQ1.DFS('in')).toEqual([4, 3, 2, 5, 1, 8, 6, 7]); + expect(minPQ1.DFS('post')).toEqual([4, 3, 5, 2, 8, 7, 6, 1]); + expect(minPQ1.DFS('pre')).toEqual([1, 2, 3, 4, 5, 6, 8, 7]); + }); -}); \ No newline at end of file +}); diff --git a/tests/unit/data-structures/priority-queue/priority-queue.test.ts b/tests/unit/data-structures/priority-queue/priority-queue.test.ts index b5f2a33..a96cab4 100644 --- a/tests/unit/data-structures/priority-queue/priority-queue.test.ts +++ b/tests/unit/data-structures/priority-queue/priority-queue.test.ts @@ -3,25 +3,25 @@ import {getRandomInt} from '../../../utils'; describe('PriorityQueue Operation Test', () => { - it('should validate a priority queue', () => { - const minPQ = new PriorityQueue({nodes: [1, 5, 7, 9, 3, 6, 2], comparator: (a, b) => a - b}); + it('should validate a priority queue', () => { + const minPQ = new PriorityQueue({nodes: [1, 5, 7, 9, 3, 6, 2], comparator: (a, b) => a - b}); - expect(minPQ.isValid()).toBe(true); - expect(PriorityQueue.isPriorityQueueified({nodes: minPQ.nodes, comparator: (a, b) => a - b})).toBe(true); - expect(PriorityQueue.isPriorityQueueified({nodes: minPQ.nodes, comparator: (a, b) => b - a})).toBe(false); - expect(PriorityQueue.isPriorityQueueified({ - nodes: [1, 5, 7, 9, 3, 6, 2], - comparator: (a, b) => b - a - })).toBe(false); - }); + expect(minPQ.isValid()).toBe(true); + expect(PriorityQueue.isPriorityQueueified({nodes: minPQ.nodes, comparator: (a, b) => a - b})).toBe(true); + expect(PriorityQueue.isPriorityQueueified({nodes: minPQ.nodes, comparator: (a, b) => b - a})).toBe(false); + expect(PriorityQueue.isPriorityQueueified({ + nodes: [1, 5, 7, 9, 3, 6, 2], + comparator: (a, b) => b - a + })).toBe(false); + }); }); describe('Priority Queue Performance Test', () => { - it('should numeric heap work well', function () { - const values = Array.from(new Array(10000), () => getRandomInt(1, 10000000)); - const minPriorityQueue = new PriorityQueue({nodes: values, comparator: (a, b) => a - b}); - const sorted = minPriorityQueue.sort() - expect(sorted).toEqual(values.sort((a, b) => a - b)); - }); -}) \ No newline at end of file + it('should numeric heap work well', function () { + const values = Array.from(new Array(10000), () => getRandomInt(1, 10000000)); + const minPriorityQueue = new PriorityQueue({nodes: values, comparator: (a, b) => a - b}); + const sorted = minPriorityQueue.sort() + expect(sorted).toEqual(values.sort((a, b) => a - b)); + }); +}) diff --git a/tests/unit/data-structures/queue/queue.test.ts b/tests/unit/data-structures/queue/queue.test.ts new file mode 100644 index 0000000..39b1d59 --- /dev/null +++ b/tests/unit/data-structures/queue/queue.test.ts @@ -0,0 +1,36 @@ +import {Queue} from '../../../../src'; +import {bigO, magnitude} from '../../../utils'; + +describe('Queue Operation Test', () => { + + it('should validate a queue', () => { + const queue = new Queue(); + for (let i = 0; i < 1000; i++) { + queue.enqueue(i); + } + let last: number | undefined = 0; + for (let i = 0; i < 1000; i++) { + last = queue.dequeue(); + } + expect(last).toBe(999); + }); + +}); + +describe('Queue Performance Test', () => { + it('should numeric queue performance well', function () { + const queue = new Queue(); + for (let i = 0; i < magnitude.LINEAR; i++) { + queue.enqueue(i); + } + let last: number | undefined = 0; + + const startTime = performance.now(); + for (let i = 0; i < magnitude.LINEAR; i++) { + last = queue.dequeue(); + } + expect(last).toBe(magnitude.LINEAR - 1); + expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100); + + }); +}) diff --git a/tests/utils/index.ts b/tests/utils/index.ts index 678239e..f605d40 100644 --- a/tests/utils/index.ts +++ b/tests/utils/index.ts @@ -1 +1,2 @@ -export * from './number'; \ No newline at end of file +export * from './number'; +export * from './magnitude'; diff --git a/tests/utils/magnitude.ts b/tests/utils/magnitude.ts new file mode 100644 index 0000000..74d6899 --- /dev/null +++ b/tests/utils/magnitude.ts @@ -0,0 +1,21 @@ +const orderReducedBy = 2; // reduction of magnitude's order compared to the baseline magnitude + +export const magnitude = { + CONSTANT: Math.floor(Number.MAX_SAFE_INTEGER / Math.pow(10, orderReducedBy)), + LOG_N: Math.pow(10, 9 - orderReducedBy), + LINEAR: Math.pow(10, 6 - orderReducedBy), + N_LOG_N: Math.pow(10, 5 - orderReducedBy), + SQUARED: Math.pow(10, 4 - orderReducedBy), + CUBED: Math.pow(10, 3 - orderReducedBy), + FACTORIAL: 20 - orderReducedBy +} + +export const bigO = { + CONSTANT: magnitude.CONSTANT / 100000, + LOG_N: Math.log2(magnitude.LOG_N) / 1000, + LINEAR: magnitude.LINEAR / 1000, + N_LOG_N: magnitude.N_LOG_N * Math.log2(magnitude.LOG_N) / 1000, + SQUARED: Math.pow(magnitude.SQUARED, 2) / 1000, + CUBED: Math.pow(magnitude.SQUARED, 3) / 1000, + FACTORIAL: 10000 +} diff --git a/tests/utils/number.ts b/tests/utils/number.ts index 36f9964..7380f6e 100644 --- a/tests/utils/number.ts +++ b/tests/utils/number.ts @@ -1,3 +1,3 @@ export function getRandomInt(min: number, max: number) { - return Math.floor(Math.random() * (max - min + 1)) + min; -} \ No newline at end of file + return Math.floor(Math.random() * (max - min + 1)) + min; +} diff --git a/tsconfig.json b/tsconfig.json index c0cbfa3..8325755 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,6 +16,8 @@ "skipLibCheck": true, "downlevelIteration": true, "experimentalDecorators": true, + "sourceMap": true, + // "removeComments": true, // "allowJs": true, // "allowSyntheticDefaultImports": true, // "forceConsistentCasingInFileNames": true,