diff --git a/.npmignore b/.npmignore index 06a8d71..a15d620 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,5 @@ /.idea -/src /tests /notes /docs diff --git a/package-lock.json b/package-lock.json index f08cae3..c15fc26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,16 @@ { "name": "data-structure-typed", - "version": "1.19.0", + "version": "1.19.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "data-structure-typed", - "version": "1.19.0", + "version": "1.19.4", "license": "MIT", "dependencies": { - "bst-typed": "^1.19.0", + "avl-tree-typed": "^1.19.44", + "bst-typed": "^1.19.45", "zod": "^3.22.2" }, "devDependencies": { @@ -1624,6 +1625,15 @@ "sprintf-js": "~1.0.2" } }, + "node_modules/avl-tree-typed": { + "version": "1.19.44", + "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.19.44.tgz", + "integrity": "sha512-psn/2Lmz4umFa5CedqvfQOH5kqnB+GU4piOZZGTQQ+kX0hR6N9Sdzv10xFj1TqEdMmBjNR+Ng5q4TL/mrb+a2w==", + "dependencies": { + "data-structure-typed": "^1.19.4", + "zod": "^3.22.2" + } + }, "node_modules/babel-jest": { "version": "29.6.3", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.6.3.tgz", @@ -1870,11 +1880,11 @@ } }, "node_modules/bst-typed": { - "version": "1.19.0", - "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.19.0.tgz", - "integrity": "sha512-kfD5ckEQ1GF2p82b+NZo2F9yxoPIw46PfIcyFzIOmKc0W9xFRtImjUmY+b9S0vNtcslhR7nAv7UHveEd+mapLA==", + "version": "1.19.45", + "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.19.45.tgz", + "integrity": "sha512-x/vLIaFDdgv4zcTVgpJmTwW6BanpPPM9eE/P8v72VE9eFgBEspDGOSJsd4Fel7fO8DF+MUqwksMreCln0ECy3g==", "dependencies": { - "data-structure-typed": "^1.19.0", + "data-structure-typed": "^1.19.4", "zod": "^3.22.2" } }, @@ -2136,10 +2146,11 @@ } }, "node_modules/data-structure-typed": { - "version": "1.19.0", - "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.19.0.tgz", - "integrity": "sha512-5V6cJWrebzQMcKPX2gpJs2w0taALVtFZrcaSsfwpW5SWVhihAhunDbD2P0CytY/rvDDzxnsmq5u0ATfcNZIm2Q==", + "version": "1.19.4", + "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.19.4.tgz", + "integrity": "sha512-qZ05QoqIl78GDFCdDFA1WumRX+MX0v60nB/GrP7ZyqPFamOk6TdaIcZqDQCZ+mrQeHmSID2l6jhROWtfiTjBrw==", "dependencies": { + "bst-typed": "^1.19.0", "zod": "^3.22.2" } }, diff --git a/package.json b/package.json index 4ca754e..e5911dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "data-structure-typed", - "version": "1.19.4", + "version": "1.19.5", "description": "Javascript & TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.", "main": "dist/index.js", "scripts": { @@ -65,7 +65,8 @@ "typescript": "^4.9.5" }, "dependencies": { - "bst-typed": "^1.19.0", + "avl-tree-typed": "^1.19.44", + "bst-typed": "^1.19.45", "zod": "^3.22.2" } } diff --git a/tests/integration/avl-tree.test.ts b/tests/integration/avl-tree.test.ts new file mode 100644 index 0000000..9a37fe4 --- /dev/null +++ b/tests/integration/avl-tree.test.ts @@ -0,0 +1,114 @@ +import {AVLTree} from 'avl-tree-typed'; + +describe('AVL Tree Test', () => { + 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(); + + for (const i of arr) tree.add(i, i); + + const node6 = tree.get(6); + + 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 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 subTreeSum = node15 && tree.subTreeSum(node15); + expect(subTreeSum).toBe(70); + + 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); + + 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); + + 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(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(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.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(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(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(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); + + 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 e443cd8..c1c69ff 100644 --- a/tests/integration/bst.test.ts +++ b/tests/integration/bst.test.ts @@ -1,7 +1,7 @@ import {BST, BSTNode} from 'bst-typed'; -describe('BST from package operations test', () => { - it('should perform various operations on a Binary Search Tree from package with numeric values', () => { +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);