data-structure-typed 1.38.2 → 1.38.3
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/binary-tree.d.ts +9 -9
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/helpers.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +9 -9
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/helpers.d.ts +1 -1
- package/dist/umd/index.global.js +1 -1
- package/dist/umd/index.global.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +21 -21
- package/src/data-structures/binary-tree/bst.ts +7 -7
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -1
- package/src/data-structures/graph/abstract-graph.ts +11 -10
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/graph/undirected-graph.ts +5 -4
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- 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 +1 -2
- package/src/data-structures/queue/deque.ts +4 -5
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.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 +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +22 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +20 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +1 -1
- 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.3",
|
|
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.2",
|
|
65
65
|
"benchmark": "^2.1.4",
|
|
66
|
-
"binary-tree-typed": "^1.38.
|
|
67
|
-
"bst-typed": "^1.38.
|
|
66
|
+
"binary-tree-typed": "^1.38.2",
|
|
67
|
+
"bst-typed": "^1.38.2",
|
|
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.2",
|
|
75
75
|
"istanbul-badges-readme": "^1.8.5",
|
|
76
76
|
"jest": "^29.7.0",
|
|
77
77
|
"prettier": "^3.0.3",
|
|
@@ -152,7 +152,7 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
152
152
|
// 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:
|
|
153
153
|
switch (
|
|
154
154
|
this._balanceFactor(A) // second O(1)
|
|
155
|
-
|
|
155
|
+
) {
|
|
156
156
|
case -2:
|
|
157
157
|
if (A && A.left) {
|
|
158
158
|
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}: {
|
|
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};
|
|
@@ -407,7 +407,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
407
407
|
return -1;
|
|
408
408
|
}
|
|
409
409
|
|
|
410
|
-
const stack: {
|
|
410
|
+
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
411
411
|
let maxHeight = 0;
|
|
412
412
|
|
|
413
413
|
while (stack.length > 0) {
|
|
@@ -512,9 +512,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
512
512
|
* traverse the binary tree. It can have two possible values:
|
|
513
513
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
514
514
|
*/
|
|
515
|
-
getNodes(
|
|
515
|
+
getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
516
516
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
517
|
-
callback:
|
|
517
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
518
518
|
onlyOne = false,
|
|
519
519
|
beginRoot: N | null = this.root,
|
|
520
520
|
iterationType = this.iterationType
|
|
@@ -570,9 +570,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
570
570
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
571
571
|
* @returns a boolean value.
|
|
572
572
|
*/
|
|
573
|
-
has(
|
|
573
|
+
has<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
574
574
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
575
|
-
callback:
|
|
575
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
576
576
|
beginRoot = this.root,
|
|
577
577
|
iterationType = this.iterationType
|
|
578
578
|
): boolean {
|
|
@@ -595,9 +595,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
595
595
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
596
596
|
* @returns either the found node (of type N) or null if no node is found.
|
|
597
597
|
*/
|
|
598
|
-
get(
|
|
598
|
+
get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
599
599
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
600
|
-
callback:
|
|
600
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
601
601
|
beginRoot = this.root,
|
|
602
602
|
iterationType = this.iterationType
|
|
603
603
|
): N | null {
|
|
@@ -761,11 +761,11 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
761
761
|
* performed on the binary tree. It can have two possible values:
|
|
762
762
|
* @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
763
763
|
*/
|
|
764
|
-
subTreeTraverse(
|
|
765
|
-
callback:
|
|
764
|
+
subTreeTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
765
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
766
766
|
beginRoot: N | BinaryTreeNodeKey | null = this.root,
|
|
767
767
|
iterationType = this.iterationType
|
|
768
|
-
):
|
|
768
|
+
): ReturnType<C>[] {
|
|
769
769
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
770
770
|
|
|
771
771
|
const ans: MapCallbackReturn<N>[] = [];
|
|
@@ -808,12 +808,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
808
808
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
809
809
|
* @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
|
|
810
810
|
*/
|
|
811
|
-
dfs(
|
|
812
|
-
callback:
|
|
811
|
+
dfs<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
812
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
813
813
|
pattern: DFSOrderPattern = 'in',
|
|
814
814
|
beginRoot: N | null = this.root,
|
|
815
815
|
iterationType: IterationType = IterationType.ITERATIVE
|
|
816
|
-
):
|
|
816
|
+
): ReturnType<C>[] {
|
|
817
817
|
if (!beginRoot) return [];
|
|
818
818
|
const ans: MapCallbackReturn<N>[] = [];
|
|
819
819
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -842,7 +842,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
842
842
|
_traverse(beginRoot);
|
|
843
843
|
} else {
|
|
844
844
|
// 0: visit, 1: print
|
|
845
|
-
const stack: {
|
|
845
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
846
846
|
|
|
847
847
|
while (stack.length > 0) {
|
|
848
848
|
const cur = stack.pop();
|
|
@@ -896,12 +896,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
896
896
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
897
897
|
* @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
|
|
898
898
|
*/
|
|
899
|
-
bfs(
|
|
900
|
-
callback:
|
|
899
|
+
bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
|
|
900
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
901
901
|
withLevel: boolean = false,
|
|
902
902
|
beginRoot: N | null = this.root,
|
|
903
903
|
iterationType = this.iterationType
|
|
904
|
-
):
|
|
904
|
+
): ReturnType<C>[] {
|
|
905
905
|
if (!beginRoot) return [];
|
|
906
906
|
|
|
907
907
|
const ans: BFSCallbackReturn<N>[] = [];
|
|
@@ -964,11 +964,11 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
964
964
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
965
965
|
* @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
|
|
966
966
|
*/
|
|
967
|
-
morris(
|
|
968
|
-
callback:
|
|
967
|
+
morris<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
968
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
969
969
|
pattern: DFSOrderPattern = 'in',
|
|
970
970
|
beginRoot: N | null = this.root
|
|
971
|
-
):
|
|
971
|
+
): ReturnType<C>[] {
|
|
972
972
|
if (beginRoot === null) return [];
|
|
973
973
|
const ans: MapCallbackReturn<N>[] = [];
|
|
974
974
|
|
|
@@ -1078,7 +1078,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1078
1078
|
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
1079
1079
|
*/
|
|
1080
1080
|
|
|
1081
|
-
protected _defaultCallbackByKey:
|
|
1081
|
+
protected _defaultCallbackByKey: (node: N) => number = node => node.key;
|
|
1082
1082
|
|
|
1083
1083
|
/**
|
|
1084
1084
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
@@ -238,9 +238,9 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
238
238
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
239
239
|
* matching node is found.
|
|
240
240
|
*/
|
|
241
|
-
override get(
|
|
241
|
+
override get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
242
242
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
243
|
-
callback:
|
|
243
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
244
244
|
beginRoot = this.root,
|
|
245
245
|
iterationType = this.iterationType
|
|
246
246
|
): N | null {
|
|
@@ -289,9 +289,9 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
289
289
|
* traverse the binary tree. It can have one of the following values:
|
|
290
290
|
* @returns an array of nodes (N[]).
|
|
291
291
|
*/
|
|
292
|
-
override getNodes(
|
|
292
|
+
override getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
293
293
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
294
|
-
callback:
|
|
294
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
295
295
|
onlyOne = false,
|
|
296
296
|
beginRoot: N | null = this.root,
|
|
297
297
|
iterationType = this.iterationType
|
|
@@ -363,12 +363,12 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
363
363
|
* done recursively or iteratively. It can have two possible values:
|
|
364
364
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
365
365
|
*/
|
|
366
|
-
lesserOrGreaterTraverse(
|
|
367
|
-
callback:
|
|
366
|
+
lesserOrGreaterTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
|
|
367
|
+
callback: C = this._defaultCallbackByKey as C,
|
|
368
368
|
lesserOrGreater: CP = CP.lt,
|
|
369
369
|
targetNode: N | BinaryTreeNodeKey | null = this.root,
|
|
370
370
|
iterationType = this.iterationType
|
|
371
|
-
):
|
|
371
|
+
): ReturnType<C>[] {
|
|
372
372
|
if (typeof targetNode === 'number') targetNode = this.get(targetNode);
|
|
373
373
|
const ans: MapCallbackReturn<N>[] = [];
|
|
374
374
|
if (!targetNode) return ans;
|
|
@@ -37,7 +37,8 @@ export class TreeMultisetNode<
|
|
|
37
37
|
*/
|
|
38
38
|
export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
|
|
39
39
|
extends AVLTree<N>
|
|
40
|
-
implements IBinaryTree<N>
|
|
40
|
+
implements IBinaryTree<N>
|
|
41
|
+
{
|
|
41
42
|
/**
|
|
42
43
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
43
44
|
* merge duplicated values.
|
|
@@ -105,7 +105,8 @@ 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>
|
|
108
|
+
> implements IGraph<V, E>
|
|
109
|
+
{
|
|
109
110
|
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
|
|
110
111
|
|
|
111
112
|
get vertices(): Map<VertexKey, V> {
|
|
@@ -553,14 +554,14 @@ export abstract class AbstractGraph<
|
|
|
553
554
|
}
|
|
554
555
|
|
|
555
556
|
getMinDist &&
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
557
|
+
distMap.forEach((d, v) => {
|
|
558
|
+
if (v !== srcVertex) {
|
|
559
|
+
if (d < minDist) {
|
|
560
|
+
minDist = d;
|
|
561
|
+
if (genPaths) minDest = v;
|
|
562
|
+
}
|
|
561
563
|
}
|
|
562
|
-
}
|
|
563
|
-
});
|
|
564
|
+
});
|
|
564
565
|
|
|
565
566
|
genPaths && getPaths(minDest);
|
|
566
567
|
|
|
@@ -622,7 +623,7 @@ export abstract class AbstractGraph<
|
|
|
622
623
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
623
624
|
}
|
|
624
625
|
|
|
625
|
-
const heap = new PriorityQueue<{
|
|
626
|
+
const heap = new PriorityQueue<{key: number; val: V}>((a, b) => a.key - b.key);
|
|
626
627
|
heap.add({key: 0, val: srcVertex});
|
|
627
628
|
|
|
628
629
|
distMap.set(srcVertex, 0);
|
|
@@ -851,7 +852,7 @@ export abstract class AbstractGraph<
|
|
|
851
852
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
852
853
|
* path between vertices in the
|
|
853
854
|
*/
|
|
854
|
-
floyd(): {
|
|
855
|
+
floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
|
|
855
856
|
const idAndVertices = [...this._vertices];
|
|
856
857
|
const n = idAndVertices.length;
|
|
857
858
|
|
|
@@ -64,7 +64,8 @@ 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>
|
|
67
|
+
implements IGraph<V, E>
|
|
68
|
+
{
|
|
68
69
|
/**
|
|
69
70
|
* The constructor function initializes an instance of a class.
|
|
70
71
|
*/
|
|
@@ -51,11 +51,12 @@ 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>
|
|
58
|
+
implements IGraph<V, E>
|
|
59
|
+
{
|
|
59
60
|
/**
|
|
60
61
|
* The constructor initializes a new Map object to store edges.
|
|
61
62
|
*/
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export class TreeMap {
|
|
2
|
-
}
|
|
1
|
+
export class TreeMap {}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export class TreeSet {
|
|
2
|
-
}
|
|
1
|
+
export class TreeSet {}
|
|
@@ -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: {
|
|
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,8 +9,7 @@ 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> {
|
|
13
|
-
}
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {}
|
|
14
13
|
|
|
15
14
|
// O(1) time complexity of obtaining the value
|
|
16
15
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -20,9 +19,9 @@ export class ObjectDeque<E = number> {
|
|
|
20
19
|
if (capacity !== undefined) this._capacity = capacity;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
private _nodes: {
|
|
22
|
+
private _nodes: {[key: number]: E} = {};
|
|
24
23
|
|
|
25
|
-
get nodes(): {
|
|
24
|
+
get nodes(): {[p: number]: E} {
|
|
26
25
|
return this._nodes;
|
|
27
26
|
}
|
|
28
27
|
|
|
@@ -157,7 +156,7 @@ export class ObjectDeque<E = number> {
|
|
|
157
156
|
return this._size <= 0;
|
|
158
157
|
}
|
|
159
158
|
|
|
160
|
-
protected _seNodes(value: {
|
|
159
|
+
protected _seNodes(value: {[p: number]: E}) {
|
|
161
160
|
this._nodes = value;
|
|
162
161
|
}
|
|
163
162
|
|
|
@@ -24,7 +24,7 @@ export enum FamilyPosition {
|
|
|
24
24
|
|
|
25
25
|
export type BinaryTreeNodeKey = number;
|
|
26
26
|
|
|
27
|
-
export type BFSCallback<N> = (node: N, level?: number) =>
|
|
27
|
+
export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
|
|
28
28
|
|
|
29
29
|
export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
|
|
30
30
|
|
package/src/types/helpers.ts
CHANGED
|
@@ -2,7 +2,7 @@ export type Comparator<T> = (a: T, b: T) => number;
|
|
|
2
2
|
|
|
3
3
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
4
4
|
|
|
5
|
-
export type MapCallback<N> = (node: N) =>
|
|
5
|
+
export type MapCallback<N, D = any> = (node: N) => D;
|
|
6
6
|
|
|
7
7
|
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
8
8
|
|
package/src/types/utils/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
2
|
-
export type Thunk = () => ReturnType<ToThunkFn> & {
|
|
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 = {
|
|
1
|
+
export type KeyValueObject = {[key: string]: any};
|
|
2
2
|
|
|
3
|
-
export type KeyValueObjectWithKey = {
|
|
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<BSTNode<{
|
|
186
|
+
const objBST = new BST<BSTNode<{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/umd/
|
|
6
|
+
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/index.global.js'></script>
|
|
7
7
|
</head>
|
|
8
8
|
<body>
|
|
9
9
|
<div id='app'>
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
$avlTreeSpan.innerText = 'AVLTree';
|
|
24
24
|
$avlTree.append($avlTreeSpan);
|
|
25
25
|
avlTree.add(1, 1);
|
|
26
|
-
console.log(avlTree.
|
|
26
|
+
console.log(avlTree.bfs());
|
|
27
27
|
$modules.append($avlTree);
|
|
28
28
|
|
|
29
29
|
|
|
@@ -38,8 +38,7 @@ describe('AVL Tree Test', () => {
|
|
|
38
38
|
expect(dfs[dfs.length - 1].key).toBe(16);
|
|
39
39
|
|
|
40
40
|
tree.perfectlyBalance();
|
|
41
|
-
const bfs
|
|
42
|
-
tree.bfs(node => bfs.push(node));
|
|
41
|
+
const bfs = tree.bfs(node => node);
|
|
43
42
|
expect(tree.isPerfectlyBalanced()).toBe(true);
|
|
44
43
|
expect(bfs[0].key).toBe(8);
|
|
45
44
|
expect(bfs[bfs.length - 1].key).toBe(16);
|
|
@@ -98,16 +97,33 @@ describe('AVL Tree Test', () => {
|
|
|
98
97
|
expect(tree.getHeight()).toBe(1);
|
|
99
98
|
|
|
100
99
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
101
|
-
const lastBFSIds =
|
|
102
|
-
tree.bfs(node => lastBFSIds.push(node.key));
|
|
100
|
+
const lastBFSIds = tree.bfs();
|
|
103
101
|
expect(lastBFSIds[0]).toBe(12);
|
|
104
102
|
expect(lastBFSIds[1]).toBe(2);
|
|
105
103
|
expect(lastBFSIds[2]).toBe(16);
|
|
106
104
|
|
|
107
|
-
const lastBFSNodes
|
|
108
|
-
tree.bfs(node => lastBFSNodes.push(node));
|
|
105
|
+
const lastBFSNodes = tree.bfs(node => node);
|
|
109
106
|
expect(lastBFSNodes[0].key).toBe(12);
|
|
110
107
|
expect(lastBFSNodes[1].key).toBe(2);
|
|
111
108
|
expect(lastBFSNodes[2].key).toBe(16);
|
|
112
109
|
});
|
|
113
110
|
});
|
|
111
|
+
|
|
112
|
+
describe('AVLTree APIs test', () => {
|
|
113
|
+
const avl = new AVLTree<AVLTreeNode<{id: number; text: string}>>();
|
|
114
|
+
beforeEach(() => {
|
|
115
|
+
avl.clear();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('add', () => {
|
|
119
|
+
avl.add(1);
|
|
120
|
+
const node2 = new AVLTreeNode(2);
|
|
121
|
+
avl.add(node2);
|
|
122
|
+
const node3 = new AVLTreeNode(3, {id: 3, text: 'text3'});
|
|
123
|
+
avl.add(node3);
|
|
124
|
+
avl.add(node3, {id: 3, text: 'text33'});
|
|
125
|
+
|
|
126
|
+
const bfsRes = avl.bfs(node => node.key, false);
|
|
127
|
+
expect(bfsRes[0]).toBe(2);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {BinaryTree, BinaryTreeNode} from '../../../../src';
|
|
1
|
+
import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode} from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('BinaryTreeNode', () => {
|
|
4
4
|
it('should create an instance of BinaryTreeNode', () => {
|
|
@@ -194,3 +194,22 @@ describe('BinaryTree Morris Traversal', () => {
|
|
|
194
194
|
expect(node1?.right).toBe(node3);
|
|
195
195
|
});
|
|
196
196
|
});
|
|
197
|
+
|
|
198
|
+
describe('BinaryTree APIs test', () => {
|
|
199
|
+
const avl = new AVLTree<AVLTreeNode<{id: number; text: string}>>();
|
|
200
|
+
beforeEach(() => {
|
|
201
|
+
avl.clear();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('add', () => {
|
|
205
|
+
avl.add(1);
|
|
206
|
+
const node2 = new AVLTreeNode(2);
|
|
207
|
+
avl.add(node2);
|
|
208
|
+
const node3 = new AVLTreeNode(3, {id: 3, text: 'text3'});
|
|
209
|
+
avl.add(node3);
|
|
210
|
+
avl.add(node3, {id: 3, text: 'text33'});
|
|
211
|
+
|
|
212
|
+
const bfsRes = avl.bfs(node => node);
|
|
213
|
+
expect(bfsRes[0]?.key).toBe(2);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
@@ -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<BSTNode<{
|
|
192
|
+
const objBST = new BST<BSTNode<{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);
|
|
@@ -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<BSTNode<{
|
|
32
|
+
const objBST = new BST<BSTNode<{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<TreeMultisetNode<{
|
|
209
|
+
const objTreeMultiset = new TreeMultiset<TreeMultisetNode<{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});
|
|
@@ -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', () => {
|