mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
[binary-tree] Update the testing API to the new version.
This commit is contained in:
parent
c177a4f87d
commit
48e94f29ed
50
package-lock.json
generated
50
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.37.0",
|
||||
"version": "1.37.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.37.0",
|
||||
"version": "1.37.2",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/benchmark": "^2.1.3",
|
||||
|
@ -15,17 +15,17 @@
|
|||
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
||||
"@typescript-eslint/parser": "^6.7.4",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"avl-tree-typed": "^1.36.9",
|
||||
"avl-tree-typed": "^1.37.2",
|
||||
"benchmark": "^2.1.4",
|
||||
"binary-tree-typed": "^1.36.9",
|
||||
"bst-typed": "^1.36.9",
|
||||
"binary-tree-typed": "^1.37.2",
|
||||
"bst-typed": "^1.37.2",
|
||||
"dependency-cruiser": "^14.1.0",
|
||||
"eslint": "^8.50.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-import-resolver-alias": "^1.1.2",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
"eslint-plugin-import": "^2.28.1",
|
||||
"heap-typed": "^1.36.9",
|
||||
"heap-typed": "^1.37.2",
|
||||
"istanbul-badges-readme": "^1.8.5",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.0.3",
|
||||
|
@ -2396,12 +2396,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/avl-tree-typed": {
|
||||
"version": "1.36.9",
|
||||
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.36.9.tgz",
|
||||
"integrity": "sha512-JCcrzwqotdd62ojLQZR74kTcmlgn+Y0w+Ws2l58ZzStW8dWVZUY/KybRxVJnY1XT50DTYY6RzAf1UFgj52anvw==",
|
||||
"version": "1.37.2",
|
||||
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.37.2.tgz",
|
||||
"integrity": "sha512-nwQjVhtKeVnMA6J1ZQwgMo/cubZEIhOFXCZfwu/tzKCklr8WTXAdCyysOVuT52SwKhzkdNjjSeAevDhfH7m05g==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.36.8"
|
||||
"data-structure-typed": "^1.37.2"
|
||||
}
|
||||
},
|
||||
"node_modules/babel-jest": {
|
||||
|
@ -2586,12 +2586,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/binary-tree-typed": {
|
||||
"version": "1.36.9",
|
||||
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.36.9.tgz",
|
||||
"integrity": "sha512-3YBodVs6gpGkgyY7vhDL+m20G/RyRFnu5Yqgf6xYvabwoChc8Yzv+UrxMqD7CVDt2o/WvvEuC/M7U5GYSNLY2A==",
|
||||
"version": "1.37.2",
|
||||
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.37.2.tgz",
|
||||
"integrity": "sha512-bCdPFOAm5eMiuDiJLfnj1g6dR2Ib2p4NLrhTvSzCWVjf/o/LLCjzpS0HfXHZ+sjhqfs7yxthgbW4SeHgDO5MPw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.36.8"
|
||||
"data-structure-typed": "^1.37.2"
|
||||
}
|
||||
},
|
||||
"node_modules/brace-expansion": {
|
||||
|
@ -2670,12 +2670,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/bst-typed": {
|
||||
"version": "1.36.9",
|
||||
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.36.9.tgz",
|
||||
"integrity": "sha512-FAQ6KiVuF6RCakPIgZwtZ/VdfKj2gL64rO/CN6DWP0jWYWoYvsqDW3pr83PvUhjNsxLQW5q92GT17wMOmjwDog==",
|
||||
"version": "1.37.2",
|
||||
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.37.2.tgz",
|
||||
"integrity": "sha512-6WBCmwIX5ibA5i8B/WyL3aXv4m0Pi2xU/6uPTIHfkQAfBSpdMUxrmwRJ5G6aBBJ6wwZ+KkRKezZj6DbklY2FRA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.36.8"
|
||||
"data-structure-typed": "^1.37.2"
|
||||
}
|
||||
},
|
||||
"node_modules/buffer-from": {
|
||||
|
@ -3027,9 +3027,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/data-structure-typed": {
|
||||
"version": "1.36.8",
|
||||
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.36.8.tgz",
|
||||
"integrity": "sha512-mDm4rww/jzmmVtqtb+A63Ri3Aeau3FzNKwxGKtZd20xRPz7FVDm/fciCIxtv1TWTTLjDSpDZcpBGs/tB0i61QA==",
|
||||
"version": "1.37.2",
|
||||
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.37.2.tgz",
|
||||
"integrity": "sha512-eVU6Bd2G2xPkdO3SVqo8R+nUkWrdgDbOTR3OYtoldZbNH6Hp2lbv49goDrMYeFBxbsdJ9+k9bNI6WNTpC728uA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/debug": {
|
||||
|
@ -4362,12 +4362,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/heap-typed": {
|
||||
"version": "1.36.9",
|
||||
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.36.9.tgz",
|
||||
"integrity": "sha512-Dy7W2SooiSAc3+2Kcdb7/GvJXkguUc+hz2qLKvSbdisH6z4siOoNYoVmZJjSrc11udW1JpEINK+LqjOBlLAxiw==",
|
||||
"version": "1.37.2",
|
||||
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.37.2.tgz",
|
||||
"integrity": "sha512-zQXBfpZdCNnWETGKkxQmd14l8hiIk2HZ1rfmzjCb/OgfLDW3GiKVg5SPcn6FacsTqxLx8Y/ClQzpPeXZCOXtAQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.36.8"
|
||||
"data-structure-typed": "^1.37.2"
|
||||
}
|
||||
},
|
||||
"node_modules/html-escaper": {
|
||||
|
|
12
package.json
12
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.37.1",
|
||||
"version": "1.37.2",
|
||||
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
|
||||
"main": "dist/index.js",
|
||||
"module": "lib/index.js",
|
||||
|
@ -28,7 +28,7 @@
|
|||
"fix:test": "npm run lint:test && npm run format:test",
|
||||
"fix": "npm run fix:src && npm run fix:test",
|
||||
"update:individuals": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed --save-dev",
|
||||
"install:individuals": "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",
|
||||
"install:all-individuals": "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",
|
||||
"test": "jest",
|
||||
"check:deps": "dependency-cruiser src",
|
||||
"changelog": "auto-changelog",
|
||||
|
@ -58,17 +58,17 @@
|
|||
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
||||
"@typescript-eslint/parser": "^6.7.4",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"avl-tree-typed": "^1.36.9",
|
||||
"avl-tree-typed": "^1.37.2",
|
||||
"benchmark": "^2.1.4",
|
||||
"binary-tree-typed": "^1.36.9",
|
||||
"bst-typed": "^1.36.9",
|
||||
"binary-tree-typed": "^1.37.2",
|
||||
"bst-typed": "^1.37.2",
|
||||
"dependency-cruiser": "^14.1.0",
|
||||
"eslint": "^8.50.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-import-resolver-alias": "^1.1.2",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
"eslint-plugin-import": "^2.28.1",
|
||||
"heap-typed": "^1.36.9",
|
||||
"heap-typed": "^1.37.2",
|
||||
"istanbul-badges-readme": "^1.8.5",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.0.3",
|
||||
|
|
|
@ -15,12 +15,7 @@ import type {
|
|||
MapCallback,
|
||||
MapCallbackReturn
|
||||
} from '../../types';
|
||||
import {
|
||||
BinaryTreeDeletedResult,
|
||||
DFSOrderPattern,
|
||||
FamilyPosition,
|
||||
LoopType
|
||||
} from '../../types';
|
||||
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, LoopType} from '../../types';
|
||||
import {IBinaryTree} from '../../interfaces';
|
||||
import {trampoline} from '../../utils';
|
||||
import {Queue} from '../queue';
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {AVLTree} from 'avl-tree-typed';
|
||||
import {AVLTree, CP} from 'avl-tree-typed';
|
||||
|
||||
describe('AVL Tree Test', () => {
|
||||
it('should perform various operations on a AVL Tree', () => {
|
||||
|
@ -12,7 +12,7 @@ describe('AVL Tree Test', () => {
|
|||
expect(node6 && tree.getHeight(node6)).toBe(3);
|
||||
expect(node6 && tree.getDepth(node6)).toBe(1);
|
||||
|
||||
const getNodeById = tree.get(10, 'key');
|
||||
const getNodeById = tree.get(10);
|
||||
expect(getNodeById?.key).toBe(10);
|
||||
|
||||
const getMinNodeByRoot = tree.getLeftMost();
|
||||
|
@ -22,75 +22,77 @@ describe('AVL Tree Test', () => {
|
|||
const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
|
||||
expect(getMinNodeBySpecificNode?.key).toBe(12);
|
||||
|
||||
const subTreeSum = node15 && tree.subTreeSum(node15);
|
||||
let subTreeSum = 0;
|
||||
node15 && tree.subTreeTraverse(node => (subTreeSum += node.key), 15);
|
||||
expect(subTreeSum).toBe(70);
|
||||
|
||||
const lesserSum = tree.lesserSum(10);
|
||||
let lesserSum = 0;
|
||||
tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 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);
|
||||
|
||||
const dfs = tree.dfs('in', 'node');
|
||||
const dfs = tree.dfs(node => node, 'in');
|
||||
expect(dfs[0].key).toBe(1);
|
||||
expect(dfs[dfs.length - 1].key).toBe(16);
|
||||
|
||||
tree.perfectlyBalance();
|
||||
const bfs = tree.bfs('node');
|
||||
const bfs = tree.bfs(node => node);
|
||||
expect(tree.isPerfectlyBalanced()).toBe(true);
|
||||
expect(bfs[0].key).toBe(8);
|
||||
expect(bfs[bfs.length - 1].key).toBe(16);
|
||||
|
||||
expect(tree.remove(11)[0].deleted?.key).toBe(11);
|
||||
expect(tree.delete(11)[0].deleted?.key).toBe(11);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(node15 && tree.getHeight(node15)).toBe(2);
|
||||
|
||||
expect(tree.remove(1)[0].deleted?.key).toBe(1);
|
||||
expect(tree.delete(1)[0].deleted?.key).toBe(1);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(4);
|
||||
|
||||
expect(tree.remove(4)[0].deleted?.key).toBe(4);
|
||||
expect(tree.delete(4)[0].deleted?.key).toBe(4);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(4);
|
||||
|
||||
expect(tree.remove(10)[0].deleted?.key).toBe(10);
|
||||
expect(tree.delete(10)[0].deleted?.key).toBe(10);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(15)[0].deleted?.key).toBe(15);
|
||||
expect(tree.delete(15)[0].deleted?.key).toBe(15);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(5)[0].deleted?.key).toBe(5);
|
||||
expect(tree.delete(5)[0].deleted?.key).toBe(5);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(13)[0].deleted?.key).toBe(13);
|
||||
expect(tree.delete(13)[0].deleted?.key).toBe(13);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(3)[0].deleted?.key).toBe(3);
|
||||
expect(tree.delete(3)[0].deleted?.key).toBe(3);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(8)[0].deleted?.key).toBe(8);
|
||||
expect(tree.delete(8)[0].deleted?.key).toBe(8);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(6)[0].deleted?.key).toBe(6);
|
||||
expect(tree.remove(6).length).toBe(0);
|
||||
expect(tree.delete(6)[0].deleted?.key).toBe(6);
|
||||
expect(tree.delete(6).length).toBe(0);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(2);
|
||||
|
||||
expect(tree.remove(7)[0].deleted?.key).toBe(7);
|
||||
expect(tree.delete(7)[0].deleted?.key).toBe(7);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(2);
|
||||
|
||||
expect(tree.remove(9)[0].deleted?.key).toBe(9);
|
||||
expect(tree.delete(9)[0].deleted?.key).toBe(9);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(2);
|
||||
expect(tree.remove(14)[0].deleted?.key).toBe(14);
|
||||
expect(tree.delete(14)[0].deleted?.key).toBe(14);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(1);
|
||||
|
||||
|
@ -100,7 +102,7 @@ describe('AVL Tree Test', () => {
|
|||
expect(lastBFSIds[1]).toBe(2);
|
||||
expect(lastBFSIds[2]).toBe(16);
|
||||
|
||||
const lastBFSNodes = tree.bfs('node');
|
||||
const lastBFSNodes = tree.bfs(node => node);
|
||||
expect(lastBFSNodes[0].key).toBe(12);
|
||||
expect(lastBFSNodes[1].key).toBe(2);
|
||||
expect(lastBFSNodes[2].key).toBe(16);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {BST, BSTNode} from 'bst-typed';
|
||||
import {BST, BSTNode, CP} from 'bst-typed';
|
||||
|
||||
describe('Individual package BST operations test', () => {
|
||||
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
||||
|
@ -7,7 +7,7 @@ describe('Individual package BST operations test', () => {
|
|||
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);
|
||||
bst.addMany(idsOrVals, idsOrVals, false);
|
||||
expect(bst.root).toBeInstanceOf(BSTNode);
|
||||
|
||||
if (bst.root) expect(bst.root.key).toBe(11);
|
||||
|
@ -23,7 +23,7 @@ describe('Individual package BST operations test', () => {
|
|||
const nodeId10 = bst.get(10);
|
||||
expect(nodeId10?.key).toBe(10);
|
||||
|
||||
const nodeVal9 = bst.get(9, 'val');
|
||||
const nodeVal9 = bst.get(9, node => node.val);
|
||||
expect(nodeVal9?.key).toBe(9);
|
||||
|
||||
const leftMost = bst.getLeftMost();
|
||||
|
@ -33,10 +33,12 @@ describe('Individual package BST operations test', () => {
|
|||
const minNodeBySpecificNode = node15 && bst.getLeftMost(node15);
|
||||
expect(minNodeBySpecificNode?.key).toBe(12);
|
||||
|
||||
const subTreeSum = node15 && bst.subTreeSum(15);
|
||||
let subTreeSum = 0;
|
||||
node15 && bst.subTreeTraverse(node => (subTreeSum += node.key), 15);
|
||||
expect(subTreeSum).toBe(70);
|
||||
|
||||
const lesserSum = bst.lesserSum(10);
|
||||
let lesserSum = 0;
|
||||
bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10);
|
||||
expect(lesserSum).toBe(45);
|
||||
|
||||
expect(node15).toBeInstanceOf(BSTNode);
|
||||
|
@ -44,18 +46,18 @@ describe('Individual package BST operations test', () => {
|
|||
const node11 = bst.get(11);
|
||||
expect(node11).toBeInstanceOf(BSTNode);
|
||||
|
||||
const dfsInorderNodes = bst.dfs('in', 'node');
|
||||
const dfsInorderNodes = bst.dfs(node => node, 'in');
|
||||
expect(dfsInorderNodes[0].key).toBe(1);
|
||||
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
||||
|
||||
bst.perfectlyBalance();
|
||||
expect(bst.isPerfectlyBalanced()).toBe(true);
|
||||
|
||||
const bfsNodesAfterBalanced = bst.bfs('node');
|
||||
const bfsNodesAfterBalanced = bst.bfs(node => node);
|
||||
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
||||
|
||||
const removed11 = bst.remove(11);
|
||||
const removed11 = bst.delete(11);
|
||||
expect(removed11).toBeInstanceOf(Array);
|
||||
expect(removed11[0]).toBeDefined();
|
||||
expect(removed11[0].deleted).toBeDefined();
|
||||
|
@ -66,7 +68,7 @@ describe('Individual package BST operations test', () => {
|
|||
|
||||
expect(bst.getHeight(15)).toBe(1);
|
||||
|
||||
const removed1 = bst.remove(1);
|
||||
const removed1 = bst.delete(1);
|
||||
expect(removed1).toBeInstanceOf(Array);
|
||||
expect(removed1[0]).toBeDefined();
|
||||
expect(removed1[0].deleted).toBeDefined();
|
||||
|
@ -76,7 +78,7 @@ describe('Individual package BST operations test', () => {
|
|||
|
||||
expect(bst.getHeight()).toBe(4);
|
||||
|
||||
const removed4 = bst.remove(4);
|
||||
const removed4 = bst.delete(4);
|
||||
expect(removed4).toBeInstanceOf(Array);
|
||||
expect(removed4[0]).toBeDefined();
|
||||
expect(removed4[0].deleted).toBeDefined();
|
||||
|
@ -84,7 +86,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(4);
|
||||
|
||||
const removed10 = bst.remove(10);
|
||||
const removed10 = bst.delete(10);
|
||||
expect(removed10).toBeInstanceOf(Array);
|
||||
expect(removed10[0]).toBeDefined();
|
||||
expect(removed10[0].deleted).toBeDefined();
|
||||
|
@ -92,7 +94,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(4);
|
||||
|
||||
const removed15 = bst.remove(15);
|
||||
const removed15 = bst.delete(15);
|
||||
expect(removed15).toBeInstanceOf(Array);
|
||||
expect(removed15[0]).toBeDefined();
|
||||
expect(removed15[0].deleted).toBeDefined();
|
||||
|
@ -101,7 +103,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
const removed5 = bst.remove(5);
|
||||
const removed5 = bst.delete(5);
|
||||
expect(removed5).toBeInstanceOf(Array);
|
||||
expect(removed5[0]).toBeDefined();
|
||||
expect(removed5[0].deleted).toBeDefined();
|
||||
|
@ -110,7 +112,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
const removed13 = bst.remove(13);
|
||||
const removed13 = bst.delete(13);
|
||||
expect(removed13).toBeInstanceOf(Array);
|
||||
expect(removed13[0]).toBeDefined();
|
||||
expect(removed13[0].deleted).toBeDefined();
|
||||
|
@ -118,7 +120,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
const removed3 = bst.remove(3);
|
||||
const removed3 = bst.delete(3);
|
||||
expect(removed3).toBeInstanceOf(Array);
|
||||
expect(removed3[0]).toBeDefined();
|
||||
expect(removed3[0].deleted).toBeDefined();
|
||||
|
@ -126,7 +128,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
const removed8 = bst.remove(8);
|
||||
const removed8 = bst.delete(8);
|
||||
expect(removed8).toBeInstanceOf(Array);
|
||||
expect(removed8[0]).toBeDefined();
|
||||
expect(removed8[0].deleted).toBeDefined();
|
||||
|
@ -134,16 +136,16 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
const removed6 = bst.remove(6);
|
||||
const removed6 = bst.delete(6);
|
||||
expect(removed6).toBeInstanceOf(Array);
|
||||
expect(removed6[0]).toBeDefined();
|
||||
expect(removed6[0].deleted).toBeDefined();
|
||||
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
||||
expect(bst.remove(6).length).toBe(0);
|
||||
expect(bst.delete(6).length).toBe(0);
|
||||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
const removed7 = bst.remove(7);
|
||||
const removed7 = bst.delete(7);
|
||||
expect(removed7).toBeInstanceOf(Array);
|
||||
expect(removed7[0]).toBeDefined();
|
||||
expect(removed7[0].deleted).toBeDefined();
|
||||
|
@ -151,7 +153,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
const removed9 = bst.remove(9);
|
||||
const removed9 = bst.delete(9);
|
||||
expect(removed9).toBeInstanceOf(Array);
|
||||
expect(removed9[0]).toBeDefined();
|
||||
expect(removed9[0].deleted).toBeDefined();
|
||||
|
@ -159,7 +161,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
const removed14 = bst.remove(14);
|
||||
const removed14 = bst.delete(14);
|
||||
expect(removed14).toBeInstanceOf(Array);
|
||||
expect(removed14[0]).toBeDefined();
|
||||
expect(removed14[0].deleted).toBeDefined();
|
||||
|
@ -174,7 +176,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bfsIDs[1]).toBe(12);
|
||||
expect(bfsIDs[2]).toBe(16);
|
||||
|
||||
const bfsNodes = bst.bfs('node');
|
||||
const bfsNodes = bst.bfs(node => node);
|
||||
expect(bfsNodes[0].key).toBe(2);
|
||||
expect(bfsNodes[1].key).toBe(12);
|
||||
expect(bfsNodes[2].key).toBe(16);
|
||||
|
@ -204,7 +206,8 @@ describe('Individual package BST operations test', () => {
|
|||
|
||||
objBST.addMany(
|
||||
values.map(item => item.key),
|
||||
values
|
||||
values,
|
||||
false
|
||||
);
|
||||
|
||||
expect(objBST.root).toBeInstanceOf(BSTNode);
|
||||
|
@ -217,10 +220,10 @@ describe('Individual package BST operations test', () => {
|
|||
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
||||
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
||||
|
||||
const nodeId10 = objBST.get(10, 'key');
|
||||
const nodeId10 = objBST.get(10);
|
||||
expect(nodeId10?.key).toBe(10);
|
||||
|
||||
const nodeVal9 = objBST.get(9, 'key');
|
||||
const nodeVal9 = objBST.get(9);
|
||||
expect(nodeVal9?.key).toBe(9);
|
||||
|
||||
const leftMost = objBST.getLeftMost();
|
||||
|
@ -231,10 +234,12 @@ describe('Individual package BST operations test', () => {
|
|||
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
||||
expect(minNodeBySpecificNode?.key).toBe(12);
|
||||
|
||||
const subTreeSum = node15 && objBST.subTreeSum(node15);
|
||||
let subTreeSum = 0;
|
||||
node15 && objBST.subTreeTraverse(node => (subTreeSum += node.key), node15);
|
||||
expect(subTreeSum).toBe(70);
|
||||
|
||||
const lesserSum = objBST.lesserSum(10);
|
||||
let lesserSum = 0;
|
||||
objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10);
|
||||
expect(lesserSum).toBe(45);
|
||||
|
||||
expect(node15).toBeInstanceOf(BSTNode);
|
||||
|
@ -242,18 +247,18 @@ describe('Individual package BST operations test', () => {
|
|||
const node11 = objBST.get(11);
|
||||
expect(node11).toBeInstanceOf(BSTNode);
|
||||
|
||||
const dfsInorderNodes = objBST.dfs('in', 'node');
|
||||
const dfsInorderNodes = objBST.dfs(node => node, 'in');
|
||||
expect(dfsInorderNodes[0].key).toBe(1);
|
||||
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
||||
|
||||
objBST.perfectlyBalance();
|
||||
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
||||
|
||||
const bfsNodesAfterBalanced = objBST.bfs('node');
|
||||
const bfsNodesAfterBalanced = objBST.bfs(node => node);
|
||||
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
||||
|
||||
const removed11 = objBST.remove(11);
|
||||
const removed11 = objBST.delete(11);
|
||||
expect(removed11).toBeInstanceOf(Array);
|
||||
expect(removed11[0]).toBeDefined();
|
||||
expect(removed11[0].deleted).toBeDefined();
|
||||
|
@ -264,7 +269,7 @@ describe('Individual package BST operations test', () => {
|
|||
|
||||
expect(node15 && objBST.getHeight(node15)).toBe(2);
|
||||
|
||||
const removed1 = objBST.remove(1);
|
||||
const removed1 = objBST.delete(1);
|
||||
expect(removed1).toBeInstanceOf(Array);
|
||||
expect(removed1[0]).toBeDefined();
|
||||
expect(removed1[0].deleted).toBeDefined();
|
||||
|
@ -274,7 +279,7 @@ describe('Individual package BST operations test', () => {
|
|||
|
||||
expect(objBST.getHeight()).toBe(4);
|
||||
|
||||
const removed4 = objBST.remove(4);
|
||||
const removed4 = objBST.delete(4);
|
||||
expect(removed4).toBeInstanceOf(Array);
|
||||
expect(removed4[0]).toBeDefined();
|
||||
expect(removed4[0].deleted).toBeDefined();
|
||||
|
@ -282,7 +287,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(4);
|
||||
|
||||
const removed10 = objBST.remove(10);
|
||||
const removed10 = objBST.delete(10);
|
||||
expect(removed10).toBeInstanceOf(Array);
|
||||
expect(removed10[0]).toBeDefined();
|
||||
expect(removed10[0].deleted).toBeDefined();
|
||||
|
@ -290,7 +295,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(4);
|
||||
|
||||
const removed15 = objBST.remove(15);
|
||||
const removed15 = objBST.delete(15);
|
||||
expect(removed15).toBeInstanceOf(Array);
|
||||
expect(removed15[0]).toBeDefined();
|
||||
expect(removed15[0].deleted).toBeDefined();
|
||||
|
@ -299,7 +304,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
const removed5 = objBST.remove(5);
|
||||
const removed5 = objBST.delete(5);
|
||||
expect(removed5).toBeInstanceOf(Array);
|
||||
expect(removed5[0]).toBeDefined();
|
||||
expect(removed5[0].deleted).toBeDefined();
|
||||
|
@ -308,7 +313,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
const removed13 = objBST.remove(13);
|
||||
const removed13 = objBST.delete(13);
|
||||
expect(removed13).toBeInstanceOf(Array);
|
||||
expect(removed13[0]).toBeDefined();
|
||||
expect(removed13[0].deleted).toBeDefined();
|
||||
|
@ -316,7 +321,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
const removed3 = objBST.remove(3);
|
||||
const removed3 = objBST.delete(3);
|
||||
expect(removed3).toBeInstanceOf(Array);
|
||||
expect(removed3[0]).toBeDefined();
|
||||
expect(removed3[0].deleted).toBeDefined();
|
||||
|
@ -324,7 +329,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
const removed8 = objBST.remove(8);
|
||||
const removed8 = objBST.delete(8);
|
||||
expect(removed8).toBeInstanceOf(Array);
|
||||
expect(removed8[0]).toBeDefined();
|
||||
expect(removed8[0].deleted).toBeDefined();
|
||||
|
@ -332,16 +337,16 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
const removed6 = objBST.remove(6);
|
||||
const removed6 = objBST.delete(6);
|
||||
expect(removed6).toBeInstanceOf(Array);
|
||||
expect(removed6[0]).toBeDefined();
|
||||
expect(removed6[0].deleted).toBeDefined();
|
||||
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
||||
expect(objBST.remove(6).length).toBe(0);
|
||||
expect(objBST.delete(6).length).toBe(0);
|
||||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
const removed7 = objBST.remove(7);
|
||||
const removed7 = objBST.delete(7);
|
||||
expect(removed7).toBeInstanceOf(Array);
|
||||
expect(removed7[0]).toBeDefined();
|
||||
expect(removed7[0].deleted).toBeDefined();
|
||||
|
@ -349,7 +354,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
const removed9 = objBST.remove(9);
|
||||
const removed9 = objBST.delete(9);
|
||||
expect(removed9).toBeInstanceOf(Array);
|
||||
expect(removed9[0]).toBeDefined();
|
||||
expect(removed9[0].deleted).toBeDefined();
|
||||
|
@ -357,7 +362,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
const removed14 = objBST.remove(14);
|
||||
const removed14 = objBST.delete(14);
|
||||
expect(removed14).toBeInstanceOf(Array);
|
||||
expect(removed14[0]).toBeDefined();
|
||||
expect(removed14[0].deleted).toBeDefined();
|
||||
|
@ -372,7 +377,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bfsIDs[1]).toBe(12);
|
||||
expect(bfsIDs[2]).toBe(16);
|
||||
|
||||
const bfsNodes = objBST.bfs('node');
|
||||
const bfsNodes = objBST.bfs(node => node);
|
||||
expect(bfsNodes[0].key).toBe(2);
|
||||
expect(bfsNodes[1].key).toBe(12);
|
||||
expect(bfsNodes[2].key).toBe(16);
|
||||
|
|
Loading…
Reference in a new issue