data-structure-typed 1.38.8 → 1.38.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +2 -1
- package/dist/cjs/data-structures/trie/trie.js +3 -2
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/trie/trie.d.ts +2 -1
- package/dist/mjs/data-structures/trie/trie.js +3 -2
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/data-structures/binary-tree/bst.ts +3 -1
- package/src/data-structures/binary-tree/rb-tree.ts +3 -1
- package/src/data-structures/trie/trie.ts +4 -2
- package/test/integration/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +9 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/overall.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +2 -2
- package/test/utils/big-o.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.38.
|
|
3
|
+
"version": "1.38.9",
|
|
4
4
|
"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.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -61,17 +61,17 @@
|
|
|
61
61
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
62
62
|
"@typescript-eslint/parser": "^6.7.4",
|
|
63
63
|
"auto-changelog": "^2.4.0",
|
|
64
|
-
"avl-tree-typed": "^1.38.
|
|
64
|
+
"avl-tree-typed": "^1.38.8",
|
|
65
65
|
"benchmark": "^2.1.4",
|
|
66
|
-
"binary-tree-typed": "^1.38.
|
|
67
|
-
"bst-typed": "^1.38.
|
|
66
|
+
"binary-tree-typed": "^1.38.8",
|
|
67
|
+
"bst-typed": "^1.38.8",
|
|
68
68
|
"dependency-cruiser": "^14.1.0",
|
|
69
69
|
"eslint": "^8.50.0",
|
|
70
70
|
"eslint-config-prettier": "^9.0.0",
|
|
71
71
|
"eslint-import-resolver-alias": "^1.1.2",
|
|
72
72
|
"eslint-import-resolver-typescript": "^3.6.1",
|
|
73
73
|
"eslint-plugin-import": "^2.28.1",
|
|
74
|
-
"heap-typed": "^1.38.
|
|
74
|
+
"heap-typed": "^1.38.8",
|
|
75
75
|
"istanbul-badges-readme": "^1.8.5",
|
|
76
76
|
"jest": "^29.7.0",
|
|
77
77
|
"prettier": "^3.0.3",
|
|
@@ -115,7 +115,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
115
115
|
* Represents a binary tree data structure.
|
|
116
116
|
* @template N - The type of the binary tree's nodes.
|
|
117
117
|
*/
|
|
118
|
-
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
118
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
119
|
+
implements IBinaryTree<V, N> {
|
|
119
120
|
/**
|
|
120
121
|
* Creates a new instance of BinaryTree.
|
|
121
122
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -974,7 +975,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
974
975
|
if (current.left) queue.push(current.left);
|
|
975
976
|
if (current.right) queue.push(current.right);
|
|
976
977
|
}
|
|
977
|
-
|
|
978
978
|
}
|
|
979
979
|
}
|
|
980
980
|
return ans;
|
|
@@ -24,7 +24,9 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
27
|
+
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
28
|
+
extends BinaryTree<V, N>
|
|
29
|
+
implements IBinaryTree<V, N> {
|
|
28
30
|
/**
|
|
29
31
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
30
32
|
* function.
|
|
@@ -19,7 +19,9 @@ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
22
|
+
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
23
|
+
extends BST<V, N>
|
|
24
|
+
implements IBinaryTree<V, N> {
|
|
23
25
|
constructor(options?: RBTreeOptions) {
|
|
24
26
|
super(options);
|
|
25
27
|
}
|
|
@@ -241,9 +241,10 @@ export class Trie {
|
|
|
241
241
|
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
242
242
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
243
243
|
* @param {number} max - The max count of words will be found
|
|
244
|
+
* @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
|
|
244
245
|
* @returns {string[]} an array of strings.
|
|
245
246
|
*/
|
|
246
|
-
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER): string[] {
|
|
247
|
+
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
|
|
247
248
|
prefix = this._caseProcess(prefix);
|
|
248
249
|
const words: string[] = [];
|
|
249
250
|
let found = 0;
|
|
@@ -270,7 +271,8 @@ export class Trie {
|
|
|
270
271
|
if (nodeC) startNode = nodeC;
|
|
271
272
|
}
|
|
272
273
|
}
|
|
273
|
-
|
|
274
|
+
|
|
275
|
+
if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
|
|
274
276
|
|
|
275
277
|
return words;
|
|
276
278
|
}
|
|
@@ -183,7 +183,7 @@ describe('Individual package BST operations test', () => {
|
|
|
183
183
|
});
|
|
184
184
|
|
|
185
185
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
186
|
-
const objBST = new BST<{
|
|
186
|
+
const objBST = new BST<{key: number; keyA: number}>();
|
|
187
187
|
expect(objBST).toBeInstanceOf(BST);
|
|
188
188
|
objBST.add(11, {key: 11, keyA: 11});
|
|
189
189
|
objBST.add(3, {key: 3, keyA: 3});
|
|
@@ -110,7 +110,7 @@ describe('AVL Tree Test', () => {
|
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
describe('AVLTree APIs test', () => {
|
|
113
|
-
const avl = new AVLTree<{
|
|
113
|
+
const avl = new AVLTree<{id: number; text: string}>();
|
|
114
114
|
beforeEach(() => {
|
|
115
115
|
avl.clear();
|
|
116
116
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode, IterationType} from '../../../../src';
|
|
2
|
-
import {isDebugTest} from
|
|
2
|
+
import {isDebugTest} from '../../../config';
|
|
3
3
|
|
|
4
4
|
const isDebug = isDebugTest;
|
|
5
5
|
// const isDebug = true;
|
|
@@ -200,7 +200,7 @@ describe('BinaryTree Morris Traversal', () => {
|
|
|
200
200
|
});
|
|
201
201
|
|
|
202
202
|
describe('BinaryTree APIs test', () => {
|
|
203
|
-
const avl = new AVLTree<{
|
|
203
|
+
const avl = new AVLTree<{id: number; text: string}>();
|
|
204
204
|
beforeEach(() => {
|
|
205
205
|
avl.clear();
|
|
206
206
|
});
|
|
@@ -226,11 +226,14 @@ describe('BinaryTree traversals', () => {
|
|
|
226
226
|
expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
|
|
227
227
|
expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
|
|
228
228
|
expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
|
|
229
|
-
expect(tree.bfs(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([
|
|
230
|
-
|
|
229
|
+
expect(tree.bfs(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([
|
|
230
|
+
35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
|
|
231
|
+
]);
|
|
232
|
+
expect(tree.bfs(node => node.key, tree.root, IterationType.ITERATIVE)).toEqual([
|
|
233
|
+
35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
|
|
234
|
+
]);
|
|
231
235
|
|
|
232
236
|
const levels = tree.listLevels(node => node.key);
|
|
233
237
|
expect(levels).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
|
|
234
238
|
isDebug && console.log(levels);
|
|
235
|
-
|
|
236
|
-
})
|
|
239
|
+
});
|
|
@@ -189,7 +189,7 @@ describe('BST operations test', () => {
|
|
|
189
189
|
});
|
|
190
190
|
|
|
191
191
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
192
|
-
const objBST = new BST<{
|
|
192
|
+
const objBST = new BST<{key: number; keyA: number}>();
|
|
193
193
|
expect(objBST).toBeInstanceOf(BST);
|
|
194
194
|
objBST.add(11, {key: 11, keyA: 11});
|
|
195
195
|
objBST.add(3, {key: 3, keyA: 3});
|
|
@@ -260,7 +260,7 @@ describe('BST operations test', () => {
|
|
|
260
260
|
objBST.perfectlyBalance();
|
|
261
261
|
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
|
262
262
|
|
|
263
|
-
const bfsNodesAfterBalanced: BSTNode<{
|
|
263
|
+
const bfsNodesAfterBalanced: BSTNode<{key: number; keyA: number}>[] = [];
|
|
264
264
|
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
|
265
265
|
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
266
266
|
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
@@ -385,7 +385,7 @@ describe('BST operations test', () => {
|
|
|
385
385
|
expect(bfsIDs[1]).toBe(12);
|
|
386
386
|
expect(bfsIDs[2]).toBe(16);
|
|
387
387
|
|
|
388
|
-
const bfsNodes: BSTNode<{
|
|
388
|
+
const bfsNodes: BSTNode<{key: number; keyA: number}>[] = [];
|
|
389
389
|
objBST.bfs(node => bfsNodes.push(node));
|
|
390
390
|
expect(bfsNodes[0].key).toBe(2);
|
|
391
391
|
expect(bfsNodes[1].key).toBe(12);
|
|
@@ -435,7 +435,7 @@ describe('BST Performance test', function () {
|
|
|
435
435
|
bst.addMany(nodes);
|
|
436
436
|
isDebug && console.log('---add', performance.now() - start);
|
|
437
437
|
const startL = performance.now();
|
|
438
|
-
const arr: number[][] = bst.listLevels(
|
|
438
|
+
const arr: number[][] = bst.listLevels(node => node.key);
|
|
439
439
|
isDebug && console.log('---listLevels', arr);
|
|
440
440
|
isDebug && console.log('---listLevels', performance.now() - startL);
|
|
441
441
|
});
|
|
@@ -6,30 +6,30 @@ describe('Overall BinaryTree Test', () => {
|
|
|
6
6
|
bst.add(11);
|
|
7
7
|
bst.add(3);
|
|
8
8
|
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], undefined, false);
|
|
9
|
-
bst.size === 16;
|
|
10
|
-
expect(bst.size).toBe(16);
|
|
11
|
-
bst.has(6);
|
|
12
|
-
expect(bst.has(6)).toBe(true);
|
|
13
|
-
bst.getHeight(6) === 2;
|
|
14
|
-
bst.getHeight() === 5;
|
|
15
|
-
bst.getDepth(6) === 3;
|
|
16
|
-
expect(bst.getHeight(6)).toBe(2);
|
|
17
|
-
expect(bst.getHeight()).toBe(5);
|
|
18
|
-
expect(bst.getDepth(6)).toBe(3);
|
|
9
|
+
bst.size === 16; // true
|
|
10
|
+
expect(bst.size).toBe(16); // true
|
|
11
|
+
bst.has(6); // true
|
|
12
|
+
expect(bst.has(6)).toBe(true); // true
|
|
13
|
+
bst.getHeight(6) === 2; // true
|
|
14
|
+
bst.getHeight() === 5; // true
|
|
15
|
+
bst.getDepth(6) === 3; // true
|
|
16
|
+
expect(bst.getHeight(6)).toBe(2); // true
|
|
17
|
+
expect(bst.getHeight()).toBe(5); // true
|
|
18
|
+
expect(bst.getDepth(6)).toBe(3); // true
|
|
19
19
|
const leftMost = bst.getLeftMost();
|
|
20
|
-
leftMost?.key === 1;
|
|
20
|
+
leftMost?.key === 1; // true
|
|
21
21
|
expect(leftMost?.key).toBe(1);
|
|
22
22
|
bst.delete(6);
|
|
23
|
-
bst.get(6);
|
|
23
|
+
bst.get(6); // null
|
|
24
24
|
expect(bst.get(6)).toBeNull();
|
|
25
|
-
bst.isAVLBalanced();
|
|
25
|
+
bst.isAVLBalanced(); // true or false
|
|
26
26
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
27
27
|
const bfsIDs: number[] = [];
|
|
28
28
|
bst.bfs(node => bfsIDs.push(node.key));
|
|
29
|
-
bfsIDs[0] === 11;
|
|
29
|
+
bfsIDs[0] === 11; // true
|
|
30
30
|
expect(bfsIDs[0]).toBe(11);
|
|
31
31
|
|
|
32
|
-
const objBST = new BST<{
|
|
32
|
+
const objBST = new BST<{key: number; keyA: number}>();
|
|
33
33
|
objBST.add(11, {key: 11, keyA: 11});
|
|
34
34
|
objBST.add(3, {key: 3, keyA: 3});
|
|
35
35
|
|
|
@@ -57,10 +57,10 @@ describe('Overall BinaryTree Test', () => {
|
|
|
57
57
|
|
|
58
58
|
const avlTree = new AVLTree();
|
|
59
59
|
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
60
|
-
avlTree.isAVLBalanced();
|
|
61
|
-
expect(avlTree.isAVLBalanced()).toBe(true);
|
|
60
|
+
avlTree.isAVLBalanced(); // true
|
|
61
|
+
expect(avlTree.isAVLBalanced()).toBe(true); // true
|
|
62
62
|
avlTree.delete(10);
|
|
63
|
-
avlTree.isAVLBalanced();
|
|
64
|
-
expect(avlTree.isAVLBalanced()).toBe(true);
|
|
63
|
+
avlTree.isAVLBalanced(); // true
|
|
64
|
+
expect(avlTree.isAVLBalanced()).toBe(true); // true
|
|
65
65
|
});
|
|
66
66
|
});
|
|
@@ -206,7 +206,7 @@ describe('TreeMultiset operations test', () => {
|
|
|
206
206
|
});
|
|
207
207
|
|
|
208
208
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
209
|
-
const objTreeMultiset = new TreeMultiset<{
|
|
209
|
+
const objTreeMultiset = new TreeMultiset<{key: number; keyA: number}>();
|
|
210
210
|
expect(objTreeMultiset).toBeInstanceOf(TreeMultiset);
|
|
211
211
|
objTreeMultiset.add(11, {key: 11, keyA: 11});
|
|
212
212
|
objTreeMultiset.add(3, {key: 3, keyA: 3});
|
|
@@ -481,7 +481,7 @@ describe('TreeMultiset Performance test', function () {
|
|
|
481
481
|
}
|
|
482
482
|
isDebug && console.log('---add', performance.now() - start);
|
|
483
483
|
const startL = performance.now();
|
|
484
|
-
treeMS.lesserOrGreaterTraverse(
|
|
484
|
+
treeMS.lesserOrGreaterTraverse(node => (node.count += 1), CP.lt, inputSize / 2);
|
|
485
485
|
isDebug && console.log('---lesserOrGreaterTraverse', performance.now() - startL);
|
|
486
486
|
});
|
|
487
487
|
});
|
|
@@ -22,7 +22,7 @@ describe('Heap Operation Test', () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('should object heap work well', function () {
|
|
25
|
-
const minHeap = new MinHeap<{
|
|
25
|
+
const minHeap = new MinHeap<{a: string; key: number}>((a, b) => a.key - b.key);
|
|
26
26
|
minHeap.add({key: 1, a: 'a1'});
|
|
27
27
|
minHeap.add({key: 6, a: 'a6'});
|
|
28
28
|
minHeap.add({key: 2, a: 'a2'});
|
|
@@ -37,7 +37,7 @@ describe('Heap Operation Test', () => {
|
|
|
37
37
|
i++;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
const maxHeap = new MaxHeap<{
|
|
40
|
+
const maxHeap = new MaxHeap<{key: number; a: string}>((a, b) => b.key - a.key);
|
|
41
41
|
maxHeap.add({key: 1, a: 'a1'});
|
|
42
42
|
maxHeap.add({key: 6, a: 'a6'});
|
|
43
43
|
maxHeap.add({key: 5, a: 'a5'});
|
|
@@ -3,7 +3,7 @@ import {bigO, magnitude} from '../../../utils';
|
|
|
3
3
|
|
|
4
4
|
describe('DoublyLinkedList Operation Test', () => {
|
|
5
5
|
let list: DoublyLinkedList<number>;
|
|
6
|
-
let objectList: DoublyLinkedList<{
|
|
6
|
+
let objectList: DoublyLinkedList<{keyA: number}>;
|
|
7
7
|
|
|
8
8
|
beforeEach(() => {
|
|
9
9
|
list = new DoublyLinkedList();
|
|
@@ -3,10 +3,10 @@ import {bigO, magnitude} from '../../../utils';
|
|
|
3
3
|
|
|
4
4
|
describe('SinglyLinkedList Operation Test', () => {
|
|
5
5
|
let list: SinglyLinkedList<number>;
|
|
6
|
-
let objectList: SinglyLinkedList<{
|
|
6
|
+
let objectList: SinglyLinkedList<{keyA: number}>;
|
|
7
7
|
beforeEach(() => {
|
|
8
8
|
list = new SinglyLinkedList<number>();
|
|
9
|
-
objectList = new SinglyLinkedList<{
|
|
9
|
+
objectList = new SinglyLinkedList<{keyA: number}>();
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
describe('push', () => {
|
|
@@ -17,7 +17,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
|
20
|
-
const priorityQueue = new MaxPriorityQueue<{
|
|
20
|
+
const priorityQueue = new MaxPriorityQueue<{keyA: number}>((a, b) => b.keyA - a.keyA);
|
|
21
21
|
priorityQueue.refill([{keyA: 5}, {keyA: 3}, {keyA: 1}]);
|
|
22
22
|
priorityQueue.add({keyA: 7});
|
|
23
23
|
|
|
@@ -64,7 +64,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
64
64
|
|
|
65
65
|
it('should correctly heapify an object array', () => {
|
|
66
66
|
const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}];
|
|
67
|
-
const maxPQ = MaxPriorityQueue.heapify<{
|
|
67
|
+
const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>(nodes, (a, b) => b.keyA - a.keyA);
|
|
68
68
|
|
|
69
69
|
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
70
70
|
expect(maxPQ.poll()?.keyA).toBe(5);
|
package/test/utils/big-o.ts
CHANGED
|
@@ -26,7 +26,7 @@ export const bigO = {
|
|
|
26
26
|
|
|
27
27
|
function findPotentialN(input: any): number {
|
|
28
28
|
let longestArray: any[] = [];
|
|
29
|
-
let mostProperties: {
|
|
29
|
+
let mostProperties: {[key: string]: any} = {};
|
|
30
30
|
|
|
31
31
|
function recurse(obj: any) {
|
|
32
32
|
if (Array.isArray(obj)) {
|