data-structure-typed 1.38.7 → 1.38.8
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/README.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +18 -6
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +58 -8
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +18 -6
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +58 -8
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/umd/{index.global.js → data-structure-typed.min.js} +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -0
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +66 -11
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -3
- package/src/data-structures/graph/abstract-graph.ts +10 -11
- package/src/data-structures/graph/directed-graph.ts +1 -2
- package/src/data-structures/graph/undirected-graph.ts +4 -5
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
- package/test/integration/bst.test.ts +1 -1
- package/test/integration/index.html +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +23 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +4 -10
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- 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/tsup.config.js +11 -4
- package/dist/umd/index.global.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.38.
|
|
3
|
+
"version": "1.38.8",
|
|
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",
|
|
7
7
|
"types": "dist/mjs/index.d.ts",
|
|
8
|
-
"umd:main": "dist/umd/
|
|
8
|
+
"umd:main": "dist/umd/data-structure-typed.min.js",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"import": "./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.7",
|
|
65
65
|
"benchmark": "^2.1.4",
|
|
66
|
-
"binary-tree-typed": "^1.38.
|
|
67
|
-
"bst-typed": "^1.38.
|
|
66
|
+
"binary-tree-typed": "^1.38.7",
|
|
67
|
+
"bst-typed": "^1.38.7",
|
|
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.7",
|
|
75
75
|
"istanbul-badges-readme": "^1.8.5",
|
|
76
76
|
"jest": "^29.7.0",
|
|
77
77
|
"prettier": "^3.0.3",
|
|
@@ -19,10 +19,9 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode
|
|
22
|
+
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
25
|
-
{
|
|
24
|
+
implements IBinaryTree<V, N> {
|
|
26
25
|
/**
|
|
27
26
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
28
27
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -161,7 +160,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode>
|
|
|
161
160
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
162
161
|
switch (
|
|
163
162
|
this._balanceFactor(A) // second O(1)
|
|
164
|
-
|
|
163
|
+
) {
|
|
165
164
|
case -2:
|
|
166
165
|
if (A && A.left) {
|
|
167
166
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
|
|
|
17
17
|
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
18
18
|
* value of 0.
|
|
19
19
|
*/
|
|
20
|
-
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
|
20
|
+
constructor({frequency = 0, max}: { frequency?: number; max: number }) {
|
|
21
21
|
this._freq = frequency;
|
|
22
22
|
this._max = max;
|
|
23
23
|
this._freqMap = {0: 0};
|
|
@@ -115,7 +115,7 @@ 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
|
|
118
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> implements IBinaryTree<V, N> {
|
|
119
119
|
/**
|
|
120
120
|
* Creates a new instance of BinaryTree.
|
|
121
121
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -404,7 +404,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
404
404
|
return -1;
|
|
405
405
|
}
|
|
406
406
|
|
|
407
|
-
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
407
|
+
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
|
|
408
408
|
let maxHeight = 0;
|
|
409
409
|
|
|
410
410
|
while (stack.length > 0) {
|
|
@@ -887,7 +887,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
887
887
|
_traverse(beginRoot);
|
|
888
888
|
} else {
|
|
889
889
|
// 0: visit, 1: print
|
|
890
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
890
|
+
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
|
|
891
891
|
|
|
892
892
|
while (stack.length > 0) {
|
|
893
893
|
const cur = stack.pop();
|
|
@@ -930,10 +930,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
930
930
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
931
931
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
932
932
|
* `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
933
|
-
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
|
|
934
|
-
* whether to include the level of each node in the callback function. If `withLevel` is set
|
|
935
|
-
* to `true`, the level of each node will be passed as an argument to the callback function. If
|
|
936
|
-
* `withLevel` is
|
|
937
933
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
938
934
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
939
935
|
* will not be performed and an empty array will be returned.
|
|
@@ -943,7 +939,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
943
939
|
*/
|
|
944
940
|
bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
|
|
945
941
|
callback: C = this._defaultCallbackByKey as C,
|
|
946
|
-
withLevel: boolean = false,
|
|
947
942
|
beginRoot: N | null = this.root,
|
|
948
943
|
iterationType = this.iterationType
|
|
949
944
|
): ReturnType<C>[] {
|
|
@@ -951,9 +946,67 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
951
946
|
|
|
952
947
|
const ans: BFSCallbackReturn<N>[] = [];
|
|
953
948
|
|
|
949
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
950
|
+
const queue = new Queue<N>([beginRoot]);
|
|
951
|
+
|
|
952
|
+
function traverse(level: number) {
|
|
953
|
+
if (queue.size === 0) return;
|
|
954
|
+
|
|
955
|
+
const current = queue.shift()!;
|
|
956
|
+
ans.push(callback(current));
|
|
957
|
+
|
|
958
|
+
if (current.left) queue.push(current.left);
|
|
959
|
+
if (current.right) queue.push(current.right);
|
|
960
|
+
|
|
961
|
+
traverse(level + 1);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
traverse(0);
|
|
965
|
+
} else {
|
|
966
|
+
const queue = new Queue<N>([beginRoot]);
|
|
967
|
+
while (queue.size > 0) {
|
|
968
|
+
const levelSize = queue.size;
|
|
969
|
+
|
|
970
|
+
for (let i = 0; i < levelSize; i++) {
|
|
971
|
+
const current = queue.shift()!;
|
|
972
|
+
ans.push(callback(current));
|
|
973
|
+
|
|
974
|
+
if (current.left) queue.push(current.left);
|
|
975
|
+
if (current.right) queue.push(current.right);
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
return ans;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/**
|
|
984
|
+
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
|
|
985
|
+
* of arrays representing the levels of the tree.
|
|
986
|
+
* @param {C} callback - The `callback` parameter is a function that will be called on each node in
|
|
987
|
+
* the tree. It takes a node as input and returns a value. The return type of the callback function
|
|
988
|
+
* is determined by the generic type `C`.
|
|
989
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
|
990
|
+
* traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
|
|
991
|
+
* from the root node of the binary tree.
|
|
992
|
+
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
|
|
993
|
+
* recursively or iteratively. It can have two possible values:
|
|
994
|
+
* @returns The function `listLevels` returns an array of arrays, where each inner array represents a
|
|
995
|
+
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
996
|
+
* function `C` applied to the nodes at that level.
|
|
997
|
+
*/
|
|
998
|
+
listLevels<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
|
|
999
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
1000
|
+
beginRoot: N | null = this.root,
|
|
1001
|
+
iterationType = this.iterationType
|
|
1002
|
+
): ReturnType<C>[][] {
|
|
1003
|
+
if (!beginRoot) return [];
|
|
1004
|
+
const levelsNodes: ReturnType<C>[][] = [];
|
|
1005
|
+
|
|
954
1006
|
if (iterationType === IterationType.RECURSIVE) {
|
|
955
1007
|
const _recursive = (node: N, level: number) => {
|
|
956
|
-
|
|
1008
|
+
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1009
|
+
levelsNodes[level].push(callback(node));
|
|
957
1010
|
if (node.left) _recursive(node.left, level + 1);
|
|
958
1011
|
if (node.right) _recursive(node.right, level + 1);
|
|
959
1012
|
};
|
|
@@ -966,12 +1019,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
966
1019
|
const head = stack.pop()!;
|
|
967
1020
|
const [node, level] = head;
|
|
968
1021
|
|
|
969
|
-
|
|
1022
|
+
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1023
|
+
levelsNodes[level].push(callback(node));
|
|
970
1024
|
if (node.right) stack.push([node.right, level + 1]);
|
|
971
1025
|
if (node.left) stack.push([node.left, level + 1]);
|
|
972
1026
|
}
|
|
973
1027
|
}
|
|
974
|
-
|
|
1028
|
+
|
|
1029
|
+
return levelsNodes;
|
|
975
1030
|
}
|
|
976
1031
|
|
|
977
1032
|
/**
|
|
@@ -24,7 +24,7 @@ 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
|
|
27
|
+
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
28
28
|
/**
|
|
29
29
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
30
30
|
* function.
|
|
@@ -19,7 +19,7 @@ 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
|
|
22
|
+
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
23
23
|
constructor(options?: RBTreeOptions) {
|
|
24
24
|
super(options);
|
|
25
25
|
}
|
|
@@ -35,10 +35,9 @@ export class TreeMultisetNode<
|
|
|
35
35
|
/**
|
|
36
36
|
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
37
37
|
*/
|
|
38
|
-
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode
|
|
38
|
+
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
|
|
39
39
|
extends AVLTree<V, N>
|
|
40
|
-
implements IBinaryTree<V, N>
|
|
41
|
-
{
|
|
40
|
+
implements IBinaryTree<V, N> {
|
|
42
41
|
/**
|
|
43
42
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
44
43
|
* merge duplicated values.
|
|
@@ -105,8 +105,7 @@ export abstract class AbstractEdge<V = any> {
|
|
|
105
105
|
export abstract class AbstractGraph<
|
|
106
106
|
V extends AbstractVertex<any> = AbstractVertex<any>,
|
|
107
107
|
E extends AbstractEdge<any> = AbstractEdge<any>
|
|
108
|
-
> implements IGraph<V, E>
|
|
109
|
-
{
|
|
108
|
+
> implements IGraph<V, E> {
|
|
110
109
|
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
|
|
111
110
|
|
|
112
111
|
get vertices(): Map<VertexKey, V> {
|
|
@@ -554,14 +553,14 @@ export abstract class AbstractGraph<
|
|
|
554
553
|
}
|
|
555
554
|
|
|
556
555
|
getMinDist &&
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
}
|
|
556
|
+
distMap.forEach((d, v) => {
|
|
557
|
+
if (v !== srcVertex) {
|
|
558
|
+
if (d < minDist) {
|
|
559
|
+
minDist = d;
|
|
560
|
+
if (genPaths) minDest = v;
|
|
563
561
|
}
|
|
564
|
-
}
|
|
562
|
+
}
|
|
563
|
+
});
|
|
565
564
|
|
|
566
565
|
genPaths && getPaths(minDest);
|
|
567
566
|
|
|
@@ -623,7 +622,7 @@ export abstract class AbstractGraph<
|
|
|
623
622
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
624
623
|
}
|
|
625
624
|
|
|
626
|
-
const heap = new PriorityQueue<{key: number; val: V}>((a, b) => a.key - b.key);
|
|
625
|
+
const heap = new PriorityQueue<{ key: number; val: V }>((a, b) => a.key - b.key);
|
|
627
626
|
heap.add({key: 0, val: srcVertex});
|
|
628
627
|
|
|
629
628
|
distMap.set(srcVertex, 0);
|
|
@@ -852,7 +851,7 @@ export abstract class AbstractGraph<
|
|
|
852
851
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
853
852
|
* path between vertices in the
|
|
854
853
|
*/
|
|
855
|
-
floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
|
|
854
|
+
floyd(): { costs: number[][]; predecessor: (V | null)[][] } {
|
|
856
855
|
const idAndVertices = [...this._vertices];
|
|
857
856
|
const n = idAndVertices.length;
|
|
858
857
|
|
|
@@ -64,8 +64,7 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
|
64
64
|
|
|
65
65
|
export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
|
|
66
66
|
extends AbstractGraph<V, E>
|
|
67
|
-
implements IGraph<V, E>
|
|
68
|
-
{
|
|
67
|
+
implements IGraph<V, E> {
|
|
69
68
|
/**
|
|
70
69
|
* The constructor function initializes an instance of a class.
|
|
71
70
|
*/
|
|
@@ -51,12 +51,11 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export class UndirectedGraph<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
V extends UndirectedVertex<any> = UndirectedVertex,
|
|
55
|
+
E extends UndirectedEdge<any> = UndirectedEdge
|
|
56
|
+
>
|
|
57
57
|
extends AbstractGraph<V, E>
|
|
58
|
-
implements IGraph<V, E>
|
|
59
|
-
{
|
|
58
|
+
implements IGraph<V, E> {
|
|
60
59
|
/**
|
|
61
60
|
* The constructor initializes a new Map object to store edges.
|
|
62
61
|
*/
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeMap {
|
|
1
|
+
export class TreeMap {
|
|
2
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeSet {
|
|
1
|
+
export class TreeSet {
|
|
2
|
+
}
|
|
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
|
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
17
|
+
constructor(options: { row: number; col: number; initialVal?: V }) {
|
|
18
18
|
const {row, col, initialVal} = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
@@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list';
|
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
+
}
|
|
13
14
|
|
|
14
15
|
// O(1) time complexity of obtaining the value
|
|
15
16
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -19,9 +20,9 @@ export class ObjectDeque<E = number> {
|
|
|
19
20
|
if (capacity !== undefined) this._capacity = capacity;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
private _nodes: {[key: number]: E} = {};
|
|
23
|
+
private _nodes: { [key: number]: E } = {};
|
|
23
24
|
|
|
24
|
-
get nodes(): {[p: number]: E} {
|
|
25
|
+
get nodes(): { [p: number]: E } {
|
|
25
26
|
return this._nodes;
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -156,7 +157,7 @@ export class ObjectDeque<E = number> {
|
|
|
156
157
|
return this._size <= 0;
|
|
157
158
|
}
|
|
158
159
|
|
|
159
|
-
protected _seNodes(value: {[p: number]: E}) {
|
|
160
|
+
protected _seNodes(value: { [p: number]: E }) {
|
|
160
161
|
this._nodes = value;
|
|
161
162
|
}
|
|
162
163
|
|
package/src/types/utils/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
2
|
-
export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
|
|
2
|
+
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol };
|
|
3
3
|
export type TrlFn = (...args: any[]) => any;
|
|
4
4
|
export type TrlAsyncFn = (...args: any[]) => any;
|
|
5
5
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type KeyValueObject = {[key: string]: any};
|
|
1
|
+
export type KeyValueObject = { [key: string]: any };
|
|
2
2
|
|
|
3
|
-
export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol};
|
|
3
|
+
export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol };
|
|
4
4
|
|
|
5
5
|
export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
|
|
6
6
|
|
|
@@ -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});
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset='UTF-8'>
|
|
5
5
|
<title>CDN Test</title>
|
|
6
|
-
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/
|
|
6
|
+
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
|
7
7
|
</head>
|
|
8
8
|
<body>
|
|
9
9
|
<div id='app'>
|
|
@@ -110,7 +110,7 @@ describe('AVL Tree Test', () => {
|
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
describe('AVLTree APIs test', () => {
|
|
113
|
-
const avl = new AVLTree<{id: number; text: string}>();
|
|
113
|
+
const avl = new AVLTree<{ id: number; text: string }>();
|
|
114
114
|
beforeEach(() => {
|
|
115
115
|
avl.clear();
|
|
116
116
|
});
|
|
@@ -123,7 +123,7 @@ describe('AVLTree APIs test', () => {
|
|
|
123
123
|
avl.add(node3);
|
|
124
124
|
avl.add(node3, {id: 3, text: 'text33'});
|
|
125
125
|
|
|
126
|
-
const bfsRes = avl.bfs(node => node.key
|
|
126
|
+
const bfsRes = avl.bfs(node => node.key);
|
|
127
127
|
expect(bfsRes[0]).toBe(2);
|
|
128
128
|
});
|
|
129
129
|
});
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode} from '../../../../src';
|
|
1
|
+
import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode, IterationType} from '../../../../src';
|
|
2
|
+
import {isDebugTest} from "../../../config";
|
|
3
|
+
|
|
4
|
+
const isDebug = isDebugTest;
|
|
5
|
+
// const isDebug = true;
|
|
2
6
|
|
|
3
7
|
describe('BinaryTreeNode', () => {
|
|
4
8
|
it('should create an instance of BinaryTreeNode', () => {
|
|
@@ -196,7 +200,7 @@ describe('BinaryTree Morris Traversal', () => {
|
|
|
196
200
|
});
|
|
197
201
|
|
|
198
202
|
describe('BinaryTree APIs test', () => {
|
|
199
|
-
const avl = new AVLTree<{id: number; text: string}>();
|
|
203
|
+
const avl = new AVLTree<{ id: number; text: string }>();
|
|
200
204
|
beforeEach(() => {
|
|
201
205
|
avl.clear();
|
|
202
206
|
});
|
|
@@ -213,3 +217,20 @@ describe('BinaryTree APIs test', () => {
|
|
|
213
217
|
expect(bfsRes[0]?.key).toBe(2);
|
|
214
218
|
});
|
|
215
219
|
});
|
|
220
|
+
|
|
221
|
+
describe('BinaryTree traversals', () => {
|
|
222
|
+
const tree = new BinaryTree<number>();
|
|
223
|
+
|
|
224
|
+
const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
|
|
225
|
+
tree.refill(arr);
|
|
226
|
+
expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
|
|
227
|
+
expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
|
|
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([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
|
|
230
|
+
expect(tree.bfs(node => node.key, tree.root, IterationType.ITERATIVE)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
|
|
231
|
+
|
|
232
|
+
const levels = tree.listLevels(node => node.key);
|
|
233
|
+
expect(levels).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
|
|
234
|
+
isDebug && console.log(levels);
|
|
235
|
+
|
|
236
|
+
})
|
|
@@ -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<{key: number; keyA: number}>();
|
|
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<{key: number; keyA: number}>[] = [];
|
|
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<{key: number; keyA: number}>[] = [];
|
|
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,13 +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[][] =
|
|
439
|
-
bst.bfs((node, level) => {
|
|
440
|
-
if (level !== undefined) {
|
|
441
|
-
if (!arr[level]) arr[level] = [];
|
|
442
|
-
arr[level].push(node.key);
|
|
443
|
-
}
|
|
444
|
-
}, true);
|
|
438
|
+
const arr: number[][] = bst.listLevels((node) => node.key);
|
|
445
439
|
isDebug && console.log('---listLevels', arr);
|
|
446
440
|
isDebug && console.log('---listLevels', performance.now() - startL);
|
|
447
441
|
});
|
|
@@ -29,7 +29,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
29
29
|
bfsIDs[0] === 11; // true
|
|
30
30
|
expect(bfsIDs[0]).toBe(11);
|
|
31
31
|
|
|
32
|
-
const objBST = new BST<{key: number; keyA: number}>();
|
|
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
|
|
|
@@ -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<{key: number; keyA: number}>();
|
|
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((node
|
|
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<{a: string; key: number}>((a, b) => a.key - b.key);
|
|
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<{key: number; a: string}>((a, b) => b.key - a.key);
|
|
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<{keyA: number}>;
|
|
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<{keyA: number}>;
|
|
6
|
+
let objectList: SinglyLinkedList<{ keyA: number }>;
|
|
7
7
|
beforeEach(() => {
|
|
8
8
|
list = new SinglyLinkedList<number>();
|
|
9
|
-
objectList = new SinglyLinkedList<{keyA: number}>();
|
|
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<{keyA: number}>((a, b) => b.keyA - a.keyA);
|
|
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<{keyA: number}>(nodes, (a, b) => b.keyA - a.keyA);
|
|
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);
|