data-structure-typed 1.39.6 → 1.40.0
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/avl-tree.js +0 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- 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 +5 -20
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +8 -29
- 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 +3 -3
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +1 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/cjs/data-structures/binary-tree/segment-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 +6 -6
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/cjs/data-structures/graph/abstract-graph.js +4 -43
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/cjs/data-structures/graph/directed-graph.js +2 -20
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/cjs/data-structures/graph/map-graph.js +2 -20
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/cjs/data-structures/graph/undirected-graph.js +1 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/cjs/data-structures/hash/coordinate-map.js +0 -3
- package/dist/cjs/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/cjs/data-structures/hash/coordinate-set.js +0 -3
- package/dist/cjs/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/cjs/data-structures/hash/hash-map.js +4 -22
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/cjs/data-structures/hash/hash-table.js +0 -9
- package/dist/cjs/data-structures/hash/hash-table.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/heap/heap.d.ts +12 -6
- package/dist/cjs/data-structures/heap/heap.js +40 -22
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +18 -42
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +17 -35
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/cjs/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +8 -12
- package/dist/cjs/data-structures/queue/deque.js +31 -43
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +20 -5
- package/dist/cjs/data-structures/queue/queue.js +35 -18
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +3 -9
- package/dist/cjs/data-structures/tree/tree.js +3 -21
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +6 -12
- package/dist/cjs/data-structures/trie/trie.js +6 -24
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -1
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -20
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +8 -29
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +3 -3
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -8
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +6 -6
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/mjs/data-structures/graph/abstract-graph.js +8 -47
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/mjs/data-structures/graph/directed-graph.js +4 -22
- package/dist/mjs/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/mjs/data-structures/graph/map-graph.js +4 -22
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/mjs/data-structures/graph/undirected-graph.js +2 -15
- package/dist/mjs/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/mjs/data-structures/hash/coordinate-map.js +0 -3
- package/dist/mjs/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/mjs/data-structures/hash/coordinate-set.js +0 -3
- package/dist/mjs/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/mjs/data-structures/hash/hash-map.js +4 -22
- package/dist/mjs/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/mjs/data-structures/hash/hash-table.js +0 -9
- package/dist/mjs/data-structures/heap/heap.d.ts +12 -6
- package/dist/mjs/data-structures/heap/heap.js +44 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +21 -45
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +19 -37
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/mjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/mjs/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/mjs/data-structures/queue/deque.d.ts +8 -12
- package/dist/mjs/data-structures/queue/deque.js +31 -43
- package/dist/mjs/data-structures/queue/queue.d.ts +20 -5
- package/dist/mjs/data-structures/queue/queue.js +34 -17
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +11 -8
- package/dist/mjs/data-structures/tree/tree.d.ts +3 -9
- package/dist/mjs/data-structures/tree/tree.js +6 -24
- package/dist/mjs/data-structures/trie/trie.d.ts +6 -12
- package/dist/mjs/data-structures/trie/trie.js +10 -28
- package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- 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/avl-tree.ts +2 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +17 -42
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/rb-tree.ts +13 -21
- package/src/data-structures/binary-tree/segment-tree.ts +16 -83
- package/src/data-structures/binary-tree/tree-multiset.ts +8 -9
- package/src/data-structures/graph/abstract-graph.ts +21 -67
- package/src/data-structures/graph/directed-graph.ts +13 -39
- package/src/data-structures/graph/map-graph.ts +7 -32
- package/src/data-structures/graph/undirected-graph.ts +9 -26
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +6 -18
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +26 -60
- package/src/data-structures/linked-list/singly-linked-list.ts +24 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.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/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -6
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/map-graph.test.ts +2 -2
- package/test/unit/data-structures/hash/coordinate-map.test.ts +1 -1
- package/test/unit/data-structures/hash/coordinate-set.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +0 -1
- package/test/unit/data-structures/hash/hash-table.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +31 -0
- package/test/unit/data-structures/queue/queue.test.ts +8 -8
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
export class Stack<E = any> {
|
|
7
|
-
protected _elements: E[];
|
|
8
|
-
|
|
9
7
|
/**
|
|
10
8
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
11
9
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -16,6 +14,12 @@ export class Stack<E = any> {
|
|
|
16
14
|
this._elements = Array.isArray(elements) ? elements : [];
|
|
17
15
|
}
|
|
18
16
|
|
|
17
|
+
protected _elements: E[];
|
|
18
|
+
|
|
19
|
+
get elements(): E[] {
|
|
20
|
+
return this._elements;
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
/**
|
|
20
24
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
21
25
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -31,7 +35,7 @@ export class Stack<E = any> {
|
|
|
31
35
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
32
36
|
*/
|
|
33
37
|
isEmpty(): boolean {
|
|
34
|
-
return this.
|
|
38
|
+
return this.elements.length === 0;
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
/**
|
|
@@ -39,7 +43,7 @@ export class Stack<E = any> {
|
|
|
39
43
|
* @returns The size of the elements array.
|
|
40
44
|
*/
|
|
41
45
|
size(): number {
|
|
42
|
-
return this.
|
|
46
|
+
return this.elements.length;
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
/**
|
|
@@ -49,7 +53,7 @@ export class Stack<E = any> {
|
|
|
49
53
|
peek(): E | null {
|
|
50
54
|
if (this.isEmpty()) return null;
|
|
51
55
|
|
|
52
|
-
return this.
|
|
56
|
+
return this.elements[this.elements.length - 1];
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
/**
|
|
@@ -58,7 +62,7 @@ export class Stack<E = any> {
|
|
|
58
62
|
* @returns The `push` method is returning the updated `Stack<E>` object.
|
|
59
63
|
*/
|
|
60
64
|
push(element: E): Stack<E> {
|
|
61
|
-
this.
|
|
65
|
+
this.elements.push(element);
|
|
62
66
|
return this;
|
|
63
67
|
}
|
|
64
68
|
|
|
@@ -70,7 +74,7 @@ export class Stack<E = any> {
|
|
|
70
74
|
pop(): E | null {
|
|
71
75
|
if (this.isEmpty()) return null;
|
|
72
76
|
|
|
73
|
-
return this.
|
|
77
|
+
return this.elements.pop() || null;
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
/**
|
|
@@ -78,7 +82,7 @@ export class Stack<E = any> {
|
|
|
78
82
|
* @returns An array of type E.
|
|
79
83
|
*/
|
|
80
84
|
toArray(): E[] {
|
|
81
|
-
return this.
|
|
85
|
+
return this.elements.slice();
|
|
82
86
|
}
|
|
83
87
|
|
|
84
88
|
/**
|
|
@@ -93,6 +97,6 @@ export class Stack<E = any> {
|
|
|
93
97
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
94
98
|
*/
|
|
95
99
|
clone(): Stack<E> {
|
|
96
|
-
return new Stack(this.
|
|
100
|
+
return new Stack(this.elements.slice());
|
|
97
101
|
}
|
|
98
102
|
}
|
|
@@ -1,38 +1,12 @@
|
|
|
1
1
|
export class TreeNode<V = any> {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
this._children = children || [];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
private _key: string;
|
|
9
|
-
|
|
10
|
-
get key(): string {
|
|
11
|
-
return this._key;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
set key(value: string) {
|
|
15
|
-
this._key = value;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
private _value?: V | undefined;
|
|
2
|
+
key: string;
|
|
3
|
+
value?: V | undefined;
|
|
4
|
+
children?: TreeNode<V>[] | undefined;
|
|
19
5
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
set value(value: V | undefined) {
|
|
25
|
-
this._value = value;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private _children?: TreeNode<V>[] | undefined;
|
|
29
|
-
|
|
30
|
-
get children(): TreeNode<V>[] | undefined {
|
|
31
|
-
return this._children;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
set children(value: TreeNode<V>[] | undefined) {
|
|
35
|
-
this._children = value;
|
|
6
|
+
constructor(key: string, value?: V, children?: TreeNode<V>[]) {
|
|
7
|
+
this.key = key;
|
|
8
|
+
this.value = value || undefined;
|
|
9
|
+
this.children = children || [];
|
|
36
10
|
}
|
|
37
11
|
|
|
38
12
|
addChildren(children: TreeNode<V> | TreeNode<V>[]) {
|
|
@@ -11,40 +11,14 @@
|
|
|
11
11
|
* and a flag indicating whether it's the end of a word.
|
|
12
12
|
*/
|
|
13
13
|
export class TrieNode {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this._children = new Map<string, TrieNode>();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private _key;
|
|
21
|
-
|
|
22
|
-
get key(): string {
|
|
23
|
-
return this._key;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
set key(v: string) {
|
|
27
|
-
this._key = v;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
protected _children: Map<string, TrieNode>;
|
|
31
|
-
|
|
32
|
-
get children(): Map<string, TrieNode> {
|
|
33
|
-
return this._children;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
set children(v: Map<string, TrieNode>) {
|
|
37
|
-
this._children = v;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
protected _isEnd: boolean;
|
|
41
|
-
|
|
42
|
-
get isEnd(): boolean {
|
|
43
|
-
return this._isEnd;
|
|
44
|
-
}
|
|
14
|
+
key: string;
|
|
15
|
+
children: Map<string, TrieNode>;
|
|
16
|
+
isEnd: boolean;
|
|
45
17
|
|
|
46
|
-
|
|
47
|
-
this.
|
|
18
|
+
constructor(key: string) {
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.isEnd = false;
|
|
21
|
+
this.children = new Map<string, TrieNode>();
|
|
48
22
|
}
|
|
49
23
|
}
|
|
50
24
|
|
|
@@ -52,8 +26,6 @@ export class TrieNode {
|
|
|
52
26
|
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
53
27
|
*/
|
|
54
28
|
export class Trie {
|
|
55
|
-
private readonly _caseSensitive: boolean;
|
|
56
|
-
|
|
57
29
|
constructor(words?: string[], caseSensitive = true) {
|
|
58
30
|
this._root = new TrieNode('');
|
|
59
31
|
this._caseSensitive = caseSensitive;
|
|
@@ -64,16 +36,18 @@ export class Trie {
|
|
|
64
36
|
}
|
|
65
37
|
}
|
|
66
38
|
|
|
39
|
+
protected _caseSensitive: boolean;
|
|
40
|
+
|
|
41
|
+
get caseSensitive(): boolean {
|
|
42
|
+
return this._caseSensitive;
|
|
43
|
+
}
|
|
44
|
+
|
|
67
45
|
protected _root: TrieNode;
|
|
68
46
|
|
|
69
47
|
get root() {
|
|
70
48
|
return this._root;
|
|
71
49
|
}
|
|
72
50
|
|
|
73
|
-
set root(v: TrieNode) {
|
|
74
|
-
this._root = v;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
51
|
/**
|
|
78
52
|
* Add a word to the Trie structure.
|
|
79
53
|
* @param {string} word - The word to add.
|
|
@@ -277,7 +251,7 @@ export class Trie {
|
|
|
277
251
|
return words;
|
|
278
252
|
}
|
|
279
253
|
|
|
280
|
-
|
|
254
|
+
protected _caseProcess(str: string) {
|
|
281
255
|
if (!this._caseSensitive) {
|
|
282
256
|
str = str.toLowerCase(); // Convert str to lowercase if case-insensitive
|
|
283
257
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {BinaryTreeDeletedResult,
|
|
2
|
+
import {BinaryTreeDeletedResult, BinaryTreeNodeNested, BTNCallback, BTNKey} from '../types';
|
|
3
3
|
|
|
4
4
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
5
|
createNode(key: BTNKey, value?: N['value']): N;
|
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
|
|
|
@@ -31,7 +31,7 @@ describe('AVL Tree Test', () => {
|
|
|
31
31
|
expect(lesserSum).toBe(45);
|
|
32
32
|
|
|
33
33
|
// 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.
|
|
34
|
-
expect(node15?.
|
|
34
|
+
expect(node15?.value).toBe(15);
|
|
35
35
|
|
|
36
36
|
const dfs = tree.dfs(node => node, 'in');
|
|
37
37
|
expect(dfs[0].key).toBe(1);
|
|
@@ -23,7 +23,7 @@ describe('Individual package BST operations test', () => {
|
|
|
23
23
|
const nodeId10 = bst.get(10);
|
|
24
24
|
expect(nodeId10?.key).toBe(10);
|
|
25
25
|
|
|
26
|
-
const nodeVal9 = bst.get(9, node => node.
|
|
26
|
+
const nodeVal9 = bst.get(9, node => node.value);
|
|
27
27
|
expect(nodeVal9?.key).toBe(9);
|
|
28
28
|
|
|
29
29
|
const leftMost = bst.getLeftMost();
|
|
@@ -230,7 +230,7 @@ describe('Individual package BST operations test', () => {
|
|
|
230
230
|
expect(leftMost?.key).toBe(1);
|
|
231
231
|
|
|
232
232
|
const node15 = objBST.get(15);
|
|
233
|
-
expect(node15?.
|
|
233
|
+
expect(node15?.value).toEqual({key: 15, keyA: 15});
|
|
234
234
|
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
|
235
235
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
236
236
|
|
|
@@ -286,8 +286,8 @@ function loopLowerBoundTests(bit: BinaryIndexedTree, values: number[]) {
|
|
|
286
286
|
|
|
287
287
|
describe('', () => {
|
|
288
288
|
class NumArrayDC {
|
|
289
|
-
|
|
290
|
-
|
|
289
|
+
protected _tree: BinaryIndexedTree;
|
|
290
|
+
protected readonly _nums: number[];
|
|
291
291
|
|
|
292
292
|
constructor(nums: number[]) {
|
|
293
293
|
this._nums = nums;
|
|
@@ -19,7 +19,6 @@ class MyEdge<E = any> extends AbstractEdge<E> {
|
|
|
19
19
|
this.src = srcOrV1;
|
|
20
20
|
this.dest = destOrV2;
|
|
21
21
|
this.data = value;
|
|
22
|
-
this._setHashCode('');
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
24
|
|
|
@@ -54,12 +53,12 @@ class MyGraph<
|
|
|
54
53
|
}
|
|
55
54
|
|
|
56
55
|
edgesOf(vertexOrKey: VO | VertexKey): EO[] {
|
|
57
|
-
const a = typeof vertexOrKey ===
|
|
56
|
+
const a = typeof vertexOrKey === 'string' ? vertexOrKey : 'a';
|
|
58
57
|
return [new MyEdge(a, 'b') as EO];
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
|
|
62
|
-
const a = typeof vertexOrKey ===
|
|
61
|
+
const a = typeof vertexOrKey === 'string' ? vertexOrKey : 'a';
|
|
63
62
|
return [new MyVertex(a, 'b') as VO];
|
|
64
63
|
}
|
|
65
64
|
|
|
@@ -75,8 +74,7 @@ class MyGraph<
|
|
|
75
74
|
describe('AbstractGraph Operation Test', () => {
|
|
76
75
|
const myGraph: MyGraph<number, string> = new MyGraph<number, string>();
|
|
77
76
|
|
|
78
|
-
beforeEach(() => {
|
|
79
|
-
});
|
|
77
|
+
beforeEach(() => {});
|
|
80
78
|
it('should edge cases', function () {
|
|
81
79
|
myGraph.addVertex('A', 1);
|
|
82
80
|
myGraph.addVertex('B', 2);
|
|
@@ -97,6 +95,6 @@ describe('AbstractGraph Operation Test', () => {
|
|
|
97
95
|
eAB!.value = eAB.value;
|
|
98
96
|
const hs = eAB.hashCode;
|
|
99
97
|
|
|
100
|
-
expect(hs).toBe(
|
|
98
|
+
expect(hs.length).toBe(36);
|
|
101
99
|
});
|
|
102
100
|
});
|
|
@@ -98,7 +98,7 @@ class MyVertex<V = any> extends DirectedVertex<V> {
|
|
|
98
98
|
this._data = value;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
protected _data: V | undefined;
|
|
102
102
|
|
|
103
103
|
get data(): V | undefined {
|
|
104
104
|
return this._data;
|
|
@@ -115,7 +115,7 @@ class MyEdge<E = any> extends DirectedEdge<E> {
|
|
|
115
115
|
this._data = value;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
protected _data: E | undefined;
|
|
119
119
|
|
|
120
120
|
get data(): E | undefined {
|
|
121
121
|
return this._data;
|
|
@@ -141,11 +141,11 @@ class MyDirectedGraph<
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
setInEdgeMap(value: Map<VO, EO[]>) {
|
|
144
|
-
|
|
144
|
+
this._inEdgeMap = value;
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
setOutEdgeMap(value: Map<VO, EO[]>) {
|
|
148
|
-
|
|
148
|
+
this._outEdgeMap = value;
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
|
|
@@ -88,8 +88,8 @@ describe('MapGraph', () => {
|
|
|
88
88
|
const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B');
|
|
89
89
|
const edgeBC = new MapEdge('B', 'C', 60, 'Edge from B to C');
|
|
90
90
|
|
|
91
|
-
mapGraph.origin
|
|
92
|
-
mapGraph.bottomRight
|
|
91
|
+
expect(mapGraph.origin).toEqual([0, 0]);
|
|
92
|
+
expect(mapGraph.bottomRight).toEqual([100, 100]);
|
|
93
93
|
|
|
94
94
|
mapGraph.addVertex(locationA);
|
|
95
95
|
mapGraph.addVertex(locationB);
|
|
@@ -28,7 +28,6 @@ describe('HashMap', () => {
|
|
|
28
28
|
|
|
29
29
|
it('should handle key collisions', () => {
|
|
30
30
|
// Force a collision by setting two different keys to the same bucket
|
|
31
|
-
hashMap.hashFn = () => 0; // Override hash function to return the same index
|
|
32
31
|
hashMap.set('key1', 1);
|
|
33
32
|
hashMap.set('key2', 2);
|
|
34
33
|
|
|
@@ -15,9 +15,9 @@ describe('HashNode', () => {
|
|
|
15
15
|
describe('HashTable', () => {
|
|
16
16
|
it('should initialize with default capacity', () => {
|
|
17
17
|
const hashTable = new HashTable<string, string>();
|
|
18
|
-
hashTable.capacity
|
|
19
|
-
hashTable.buckets
|
|
20
|
-
hashTable.hashFn
|
|
18
|
+
expect(hashTable.capacity).toBe(16);
|
|
19
|
+
expect(hashTable.buckets).toEqual(new Array(16).fill(null));
|
|
20
|
+
expect(hashTable.hashFn('a')).toBe(6);
|
|
21
21
|
expect(hashTable.capacity).toBe(16);
|
|
22
22
|
expect(hashTable.size).toBe(0);
|
|
23
23
|
expect(hashTable.buckets.length).toBe(16);
|
|
@@ -53,3 +53,34 @@ describe('SkipList', () => {
|
|
|
53
53
|
expect(skipList.get(4)).toBe('Four');
|
|
54
54
|
});
|
|
55
55
|
});
|
|
56
|
+
|
|
57
|
+
describe('SkipList', () => {
|
|
58
|
+
let skipList: SkipList<number, string>;
|
|
59
|
+
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
skipList = new SkipList();
|
|
62
|
+
skipList.add(1, 'One');
|
|
63
|
+
skipList.add(2, 'Two');
|
|
64
|
+
skipList.add(3, 'Three');
|
|
65
|
+
skipList.add(4, 'Four');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('getFirst() should return the getFirst element', () => {
|
|
69
|
+
expect(skipList.getFirst()).toBe('One');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('getLast() should return the getLast element', () => {
|
|
73
|
+
expect(skipList.getLast()).toBe('Four');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('higher(key) should return the getFirst element greater than the given key', () => {
|
|
77
|
+
expect(skipList.higher(2)).toBe('Three');
|
|
78
|
+
expect(skipList.higher(3)).toBe('Four');
|
|
79
|
+
expect(skipList.higher(4)).toBeUndefined();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('lower(key) should return the getLast element less than the given key', () => {
|
|
83
|
+
expect(skipList.lower(2)).toBe('One');
|
|
84
|
+
expect(skipList.lower(1)).toBe(null);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {LinkedListQueue, Queue} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
import {isDebugTest} from '../../../config';
|
|
4
4
|
|
|
@@ -168,11 +168,11 @@ describe('Queue', () => {
|
|
|
168
168
|
expect(values).toEqual([1, 2, 3]);
|
|
169
169
|
});
|
|
170
170
|
});
|
|
171
|
-
describe('
|
|
172
|
-
let queue:
|
|
171
|
+
describe('LinkedListQueue', () => {
|
|
172
|
+
let queue: LinkedListQueue<string>;
|
|
173
173
|
|
|
174
174
|
beforeEach(() => {
|
|
175
|
-
queue = new
|
|
175
|
+
queue = new LinkedListQueue<string>();
|
|
176
176
|
});
|
|
177
177
|
|
|
178
178
|
it('should enqueue elements to the end of the queue', () => {
|
|
@@ -197,7 +197,7 @@ describe('SkipQueue', () => {
|
|
|
197
197
|
expect(queue.peek()).toBe('A');
|
|
198
198
|
});
|
|
199
199
|
|
|
200
|
-
// Add more test cases for other methods of
|
|
200
|
+
// Add more test cases for other methods of LinkedListQueue.
|
|
201
201
|
});
|
|
202
202
|
|
|
203
203
|
describe('Queue Performance Test', () => {
|
|
@@ -228,16 +228,16 @@ describe('Queue Performance Test', () => {
|
|
|
228
228
|
expect(performance.now() - startTime2).toBeLessThan(bigO.CUBED * 100);
|
|
229
229
|
});
|
|
230
230
|
|
|
231
|
-
it('should numeric
|
|
231
|
+
it('should numeric LinkedListQueue be efficient', function () {
|
|
232
232
|
const startTime = performance.now();
|
|
233
|
-
const queue = new
|
|
233
|
+
const queue = new LinkedListQueue<number>();
|
|
234
234
|
for (let i = 0; i < dataSize; i++) {
|
|
235
235
|
queue.enqueue(i);
|
|
236
236
|
}
|
|
237
237
|
for (let i = 0; i < dataSize; i++) {
|
|
238
238
|
queue.dequeue();
|
|
239
239
|
}
|
|
240
|
-
console.log(`
|
|
240
|
+
console.log(`LinkedListQueue Performance Test: ${performance.now() - startTime} ms`);
|
|
241
241
|
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100);
|
|
242
242
|
});
|
|
243
243
|
});
|