data-structure-typed 1.50.1 → 1.50.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 +2 -1
- package/README.md +30 -28
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -161
- package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
- 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 +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- 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 +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/cjs/data-structures/hash/hash-map.js +347 -78
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/cjs/data-structures/heap/heap.js +95 -26
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
- 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 +154 -106
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
- 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 +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/cjs/data-structures/queue/deque.js +200 -62
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/cjs/data-structures/queue/queue.js +111 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/cjs/data-structures/stack/stack.js +58 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/cjs/data-structures/trie/trie.js +153 -33
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/mjs/data-structures/hash/hash-map.js +353 -84
- package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/mjs/data-structures/heap/heap.js +95 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/mjs/data-structures/queue/deque.js +205 -67
- package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/mjs/data-structures/queue/queue.js +111 -49
- package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/mjs/data-structures/stack/stack.js +58 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/mjs/data-structures/trie/trie.js +156 -36
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +3121 -1583
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +61 -55
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +24 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
|
@@ -24,19 +24,23 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
24
24
|
*/
|
|
25
25
|
constructor(elements?: Iterable<E>);
|
|
26
26
|
protected _elements: E[];
|
|
27
|
-
get elements(): E[];
|
|
28
27
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
28
|
+
* The elements function returns the elements of this set.
|
|
29
|
+
* @return An array of elements
|
|
31
30
|
*/
|
|
31
|
+
get elements(): E[];
|
|
32
32
|
/**
|
|
33
33
|
* The size() function returns the number of elements in an array.
|
|
34
34
|
* @returns The size of the elements array.
|
|
35
35
|
*/
|
|
36
36
|
get size(): number;
|
|
37
37
|
/**
|
|
38
|
-
* Time Complexity: O(n)
|
|
39
|
-
* Space Complexity: O(n)
|
|
38
|
+
* Time Complexity: O(n)
|
|
39
|
+
* Space Complexity: O(n)
|
|
40
|
+
*/
|
|
41
|
+
/**
|
|
42
|
+
* Time Complexity: O(n)
|
|
43
|
+
* Space Complexity: O(n)
|
|
40
44
|
*
|
|
41
45
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
42
46
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -50,24 +54,24 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
50
54
|
*/
|
|
51
55
|
isEmpty(): boolean;
|
|
52
56
|
/**
|
|
53
|
-
* Time Complexity: O(1)
|
|
54
|
-
* Space Complexity: O(1)
|
|
57
|
+
* Time Complexity: O(1)
|
|
58
|
+
* Space Complexity: O(1)
|
|
55
59
|
*/
|
|
56
60
|
/**
|
|
57
|
-
* Time Complexity: O(1)
|
|
58
|
-
* Space Complexity: O(1)
|
|
61
|
+
* Time Complexity: O(1)
|
|
62
|
+
* Space Complexity: O(1)
|
|
59
63
|
*
|
|
60
64
|
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
61
65
|
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
62
66
|
*/
|
|
63
67
|
peek(): E | undefined;
|
|
64
68
|
/**
|
|
65
|
-
* Time Complexity: O(1)
|
|
66
|
-
* Space Complexity: O(1)
|
|
69
|
+
* Time Complexity: O(1)
|
|
70
|
+
* Space Complexity: O(1)
|
|
67
71
|
*/
|
|
68
72
|
/**
|
|
69
|
-
* Time Complexity: O(1)
|
|
70
|
-
* Space Complexity: O(1)
|
|
73
|
+
* Time Complexity: O(1)
|
|
74
|
+
* Space Complexity: O(1)
|
|
71
75
|
*
|
|
72
76
|
* The push function adds an element to the stack and returns the updated stack.
|
|
73
77
|
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
@@ -75,18 +79,30 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
75
79
|
*/
|
|
76
80
|
push(element: E): boolean;
|
|
77
81
|
/**
|
|
78
|
-
* Time Complexity: O(1)
|
|
79
|
-
* Space Complexity: O(1)
|
|
82
|
+
* Time Complexity: O(1)
|
|
83
|
+
* Space Complexity: O(1)
|
|
80
84
|
*/
|
|
81
85
|
/**
|
|
82
|
-
* Time Complexity: O(1)
|
|
83
|
-
* Space Complexity: O(1)
|
|
86
|
+
* Time Complexity: O(1)
|
|
87
|
+
* Space Complexity: O(1)
|
|
84
88
|
*
|
|
85
89
|
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
86
90
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
87
91
|
* array is empty, it returns `undefined`.
|
|
88
92
|
*/
|
|
89
93
|
pop(): E | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* The delete function removes an element from the stack.
|
|
96
|
+
* @param element: E Specify the element to be deleted
|
|
97
|
+
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
98
|
+
*/
|
|
99
|
+
delete(element: E): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* The deleteAt function deletes the element at a given index.
|
|
102
|
+
* @param index: number Determine the index of the element to be deleted
|
|
103
|
+
* @return A boolean value
|
|
104
|
+
*/
|
|
105
|
+
deleteAt(index: number): boolean;
|
|
90
106
|
/**
|
|
91
107
|
* Time Complexity: O(n)
|
|
92
108
|
* Space Complexity: O(n)
|
|
@@ -100,16 +116,23 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
100
116
|
*/
|
|
101
117
|
toArray(): E[];
|
|
102
118
|
/**
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
|
+
*/
|
|
122
|
+
/**
|
|
123
|
+
* Time Complexity: O(1)
|
|
124
|
+
* Space Complexity: O(1)
|
|
125
|
+
*
|
|
103
126
|
* The clear function clears the elements array.
|
|
104
127
|
*/
|
|
105
128
|
clear(): void;
|
|
106
129
|
/**
|
|
107
|
-
* Time Complexity: O(n)
|
|
108
|
-
* Space Complexity: O(n)
|
|
130
|
+
* Time Complexity: O(n)
|
|
131
|
+
* Space Complexity: O(n)
|
|
109
132
|
*/
|
|
110
133
|
/**
|
|
111
|
-
* Time Complexity: O(n)
|
|
112
|
-
* Space Complexity: O(n)
|
|
134
|
+
* Time Complexity: O(n)
|
|
135
|
+
* Space Complexity: O(n)
|
|
113
136
|
*
|
|
114
137
|
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
115
138
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
@@ -155,6 +178,13 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
155
178
|
*/
|
|
156
179
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>;
|
|
157
180
|
/**
|
|
181
|
+
* Time Complexity: O(n)
|
|
182
|
+
* Space Complexity: O(n)
|
|
183
|
+
*/
|
|
184
|
+
/**
|
|
185
|
+
* Time Complexity: O(n)
|
|
186
|
+
* Space Complexity: O(n)
|
|
187
|
+
*
|
|
158
188
|
* Custom iterator for the Stack class.
|
|
159
189
|
* @returns An iterator object.
|
|
160
190
|
*/
|
|
@@ -22,13 +22,13 @@ export class Stack extends IterableElementBase {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
_elements = [];
|
|
25
|
+
/**
|
|
26
|
+
* The elements function returns the elements of this set.
|
|
27
|
+
* @return An array of elements
|
|
28
|
+
*/
|
|
25
29
|
get elements() {
|
|
26
30
|
return this._elements;
|
|
27
31
|
}
|
|
28
|
-
/**
|
|
29
|
-
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
30
|
-
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
31
|
-
*/
|
|
32
32
|
/**
|
|
33
33
|
* The size() function returns the number of elements in an array.
|
|
34
34
|
* @returns The size of the elements array.
|
|
@@ -37,8 +37,12 @@ export class Stack extends IterableElementBase {
|
|
|
37
37
|
return this.elements.length;
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
|
-
* Time Complexity: O(n)
|
|
41
|
-
* Space Complexity: O(n)
|
|
40
|
+
* Time Complexity: O(n)
|
|
41
|
+
* Space Complexity: O(n)
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* Time Complexity: O(n)
|
|
45
|
+
* Space Complexity: O(n)
|
|
42
46
|
*
|
|
43
47
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
44
48
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -56,12 +60,12 @@ export class Stack extends IterableElementBase {
|
|
|
56
60
|
return this.elements.length === 0;
|
|
57
61
|
}
|
|
58
62
|
/**
|
|
59
|
-
* Time Complexity: O(1)
|
|
60
|
-
* Space Complexity: O(1)
|
|
63
|
+
* Time Complexity: O(1)
|
|
64
|
+
* Space Complexity: O(1)
|
|
61
65
|
*/
|
|
62
66
|
/**
|
|
63
|
-
* Time Complexity: O(1)
|
|
64
|
-
* Space Complexity: O(1)
|
|
67
|
+
* Time Complexity: O(1)
|
|
68
|
+
* Space Complexity: O(1)
|
|
65
69
|
*
|
|
66
70
|
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
67
71
|
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
@@ -72,12 +76,12 @@ export class Stack extends IterableElementBase {
|
|
|
72
76
|
return this.elements[this.elements.length - 1];
|
|
73
77
|
}
|
|
74
78
|
/**
|
|
75
|
-
* Time Complexity: O(1)
|
|
76
|
-
* Space Complexity: O(1)
|
|
79
|
+
* Time Complexity: O(1)
|
|
80
|
+
* Space Complexity: O(1)
|
|
77
81
|
*/
|
|
78
82
|
/**
|
|
79
|
-
* Time Complexity: O(1)
|
|
80
|
-
* Space Complexity: O(1)
|
|
83
|
+
* Time Complexity: O(1)
|
|
84
|
+
* Space Complexity: O(1)
|
|
81
85
|
*
|
|
82
86
|
* The push function adds an element to the stack and returns the updated stack.
|
|
83
87
|
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
@@ -88,12 +92,12 @@ export class Stack extends IterableElementBase {
|
|
|
88
92
|
return true;
|
|
89
93
|
}
|
|
90
94
|
/**
|
|
91
|
-
* Time Complexity: O(1)
|
|
92
|
-
* Space Complexity: O(1)
|
|
95
|
+
* Time Complexity: O(1)
|
|
96
|
+
* Space Complexity: O(1)
|
|
93
97
|
*/
|
|
94
98
|
/**
|
|
95
|
-
* Time Complexity: O(1)
|
|
96
|
-
* Space Complexity: O(1)
|
|
99
|
+
* Time Complexity: O(1)
|
|
100
|
+
* Space Complexity: O(1)
|
|
97
101
|
*
|
|
98
102
|
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
99
103
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
@@ -104,6 +108,24 @@ export class Stack extends IterableElementBase {
|
|
|
104
108
|
return;
|
|
105
109
|
return this.elements.pop();
|
|
106
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* The delete function removes an element from the stack.
|
|
113
|
+
* @param element: E Specify the element to be deleted
|
|
114
|
+
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
115
|
+
*/
|
|
116
|
+
delete(element) {
|
|
117
|
+
const index = this.elements.indexOf(element);
|
|
118
|
+
return this.deleteAt(index);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The deleteAt function deletes the element at a given index.
|
|
122
|
+
* @param index: number Determine the index of the element to be deleted
|
|
123
|
+
* @return A boolean value
|
|
124
|
+
*/
|
|
125
|
+
deleteAt(index) {
|
|
126
|
+
const spliced = this.elements.splice(index, 1);
|
|
127
|
+
return spliced.length === 1;
|
|
128
|
+
}
|
|
107
129
|
/**
|
|
108
130
|
* Time Complexity: O(n)
|
|
109
131
|
* Space Complexity: O(n)
|
|
@@ -119,18 +141,25 @@ export class Stack extends IterableElementBase {
|
|
|
119
141
|
return this.elements.slice();
|
|
120
142
|
}
|
|
121
143
|
/**
|
|
144
|
+
* Time Complexity: O(1)
|
|
145
|
+
* Space Complexity: O(1)
|
|
146
|
+
*/
|
|
147
|
+
/**
|
|
148
|
+
* Time Complexity: O(1)
|
|
149
|
+
* Space Complexity: O(1)
|
|
150
|
+
*
|
|
122
151
|
* The clear function clears the elements array.
|
|
123
152
|
*/
|
|
124
153
|
clear() {
|
|
125
154
|
this._elements = [];
|
|
126
155
|
}
|
|
127
156
|
/**
|
|
128
|
-
* Time Complexity: O(n)
|
|
129
|
-
* Space Complexity: O(n)
|
|
157
|
+
* Time Complexity: O(n)
|
|
158
|
+
* Space Complexity: O(n)
|
|
130
159
|
*/
|
|
131
160
|
/**
|
|
132
|
-
* Time Complexity: O(n)
|
|
133
|
-
* Space Complexity: O(n)
|
|
161
|
+
* Time Complexity: O(n)
|
|
162
|
+
* Space Complexity: O(n)
|
|
134
163
|
*
|
|
135
164
|
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
136
165
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
@@ -196,6 +225,13 @@ export class Stack extends IterableElementBase {
|
|
|
196
225
|
return newStack;
|
|
197
226
|
}
|
|
198
227
|
/**
|
|
228
|
+
* Time Complexity: O(n)
|
|
229
|
+
* Space Complexity: O(n)
|
|
230
|
+
*/
|
|
231
|
+
/**
|
|
232
|
+
* Time Complexity: O(n)
|
|
233
|
+
* Space Complexity: O(n)
|
|
234
|
+
*
|
|
199
235
|
* Custom iterator for the Stack class.
|
|
200
236
|
* @returns An iterator object.
|
|
201
237
|
*/
|
|
@@ -1,8 +1,62 @@
|
|
|
1
1
|
export declare class TreeNode<V = any> {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
/**
|
|
3
|
+
* The constructor function initializes a TreeNode object with a key, optional value, and optional
|
|
4
|
+
* children.
|
|
5
|
+
* @param {string} key - A string representing the key of the tree node.
|
|
6
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
7
|
+
* value associated with the node. If no value is provided, it defaults to `undefined`.
|
|
8
|
+
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
|
|
9
|
+
* objects. It represents the child nodes of the current node. If no children are provided, the
|
|
10
|
+
* default value is an empty array.
|
|
11
|
+
*/
|
|
5
12
|
constructor(key: string, value?: V, children?: TreeNode<V>[]);
|
|
13
|
+
protected _key: string;
|
|
14
|
+
/**
|
|
15
|
+
* The function returns the value of the protected variable _key.
|
|
16
|
+
* @returns The value of the `_key` property, which is a string.
|
|
17
|
+
*/
|
|
18
|
+
get key(): string;
|
|
19
|
+
/**
|
|
20
|
+
* The above function sets the value of a protected variable called "key".
|
|
21
|
+
* @param {string} value - The value parameter is a string that represents the value to be assigned
|
|
22
|
+
* to the key.
|
|
23
|
+
*/
|
|
24
|
+
set key(value: string);
|
|
25
|
+
protected _value?: V | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* The function returns the value stored in a variable, or undefined if the variable is empty.
|
|
28
|
+
* @returns The value of the variable `_value` is being returned.
|
|
29
|
+
*/
|
|
30
|
+
get value(): V | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* The function sets the value of a variable.
|
|
33
|
+
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
|
|
34
|
+
* can accept a value of type "V" or it can be undefined.
|
|
35
|
+
*/
|
|
36
|
+
set value(value: V | undefined);
|
|
37
|
+
protected _children?: TreeNode<V>[] | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* The function returns an array of TreeNode objects or undefined.
|
|
40
|
+
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
|
|
41
|
+
* which means it can either be an array of `TreeNode<V>` objects or `undefined`.
|
|
42
|
+
*/
|
|
43
|
+
get children(): TreeNode<V>[] | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* The function sets the value of the children property of a TreeNode object.
|
|
46
|
+
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
|
|
47
|
+
* undefined. This means that it can accept an array of TreeNode objects or undefined.
|
|
48
|
+
*/
|
|
49
|
+
set children(value: TreeNode<V>[] | undefined);
|
|
50
|
+
/**
|
|
51
|
+
* The function `addChildren` adds one or more child nodes to the current node.
|
|
52
|
+
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
|
|
53
|
+
* `TreeNode<V>` object or an array of `TreeNode<V>` objects.
|
|
54
|
+
*/
|
|
6
55
|
addChildren(children: TreeNode<V> | TreeNode<V>[]): void;
|
|
56
|
+
/**
|
|
57
|
+
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a
|
|
58
|
+
* breadth-first search.
|
|
59
|
+
* @returns the maximum depth or height of the tree.
|
|
60
|
+
*/
|
|
7
61
|
getHeight(): number;
|
|
8
62
|
}
|
|
@@ -1,23 +1,89 @@
|
|
|
1
1
|
export class TreeNode {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
/**
|
|
3
|
+
* The constructor function initializes a TreeNode object with a key, optional value, and optional
|
|
4
|
+
* children.
|
|
5
|
+
* @param {string} key - A string representing the key of the tree node.
|
|
6
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
7
|
+
* value associated with the node. If no value is provided, it defaults to `undefined`.
|
|
8
|
+
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
|
|
9
|
+
* objects. It represents the child nodes of the current node. If no children are provided, the
|
|
10
|
+
* default value is an empty array.
|
|
11
|
+
*/
|
|
5
12
|
constructor(key, value, children) {
|
|
6
|
-
this.
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
13
|
+
this._key = key;
|
|
14
|
+
this._value = value || undefined;
|
|
15
|
+
this._children = children || [];
|
|
9
16
|
}
|
|
17
|
+
_key;
|
|
18
|
+
/**
|
|
19
|
+
* The function returns the value of the protected variable _key.
|
|
20
|
+
* @returns The value of the `_key` property, which is a string.
|
|
21
|
+
*/
|
|
22
|
+
get key() {
|
|
23
|
+
return this._key;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The above function sets the value of a protected variable called "key".
|
|
27
|
+
* @param {string} value - The value parameter is a string that represents the value to be assigned
|
|
28
|
+
* to the key.
|
|
29
|
+
*/
|
|
30
|
+
set key(value) {
|
|
31
|
+
this._key = value;
|
|
32
|
+
}
|
|
33
|
+
_value;
|
|
34
|
+
/**
|
|
35
|
+
* The function returns the value stored in a variable, or undefined if the variable is empty.
|
|
36
|
+
* @returns The value of the variable `_value` is being returned.
|
|
37
|
+
*/
|
|
38
|
+
get value() {
|
|
39
|
+
return this._value;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The function sets the value of a variable.
|
|
43
|
+
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
|
|
44
|
+
* can accept a value of type "V" or it can be undefined.
|
|
45
|
+
*/
|
|
46
|
+
set value(value) {
|
|
47
|
+
this._value = value;
|
|
48
|
+
}
|
|
49
|
+
_children;
|
|
50
|
+
/**
|
|
51
|
+
* The function returns an array of TreeNode objects or undefined.
|
|
52
|
+
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
|
|
53
|
+
* which means it can either be an array of `TreeNode<V>` objects or `undefined`.
|
|
54
|
+
*/
|
|
55
|
+
get children() {
|
|
56
|
+
return this._children;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The function sets the value of the children property of a TreeNode object.
|
|
60
|
+
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
|
|
61
|
+
* undefined. This means that it can accept an array of TreeNode objects or undefined.
|
|
62
|
+
*/
|
|
63
|
+
set children(value) {
|
|
64
|
+
this._children = value;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The function `addChildren` adds one or more child nodes to the current node.
|
|
68
|
+
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
|
|
69
|
+
* `TreeNode<V>` object or an array of `TreeNode<V>` objects.
|
|
70
|
+
*/
|
|
10
71
|
addChildren(children) {
|
|
11
|
-
if (!this.
|
|
12
|
-
this.
|
|
72
|
+
if (!this._children) {
|
|
73
|
+
this._children = [];
|
|
13
74
|
}
|
|
14
75
|
if (children instanceof TreeNode) {
|
|
15
|
-
this.
|
|
76
|
+
this._children.push(children);
|
|
16
77
|
}
|
|
17
78
|
else {
|
|
18
|
-
this.
|
|
79
|
+
this._children = this._children.concat(children);
|
|
19
80
|
}
|
|
20
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a
|
|
84
|
+
* breadth-first search.
|
|
85
|
+
* @returns the maximum depth or height of the tree.
|
|
86
|
+
*/
|
|
21
87
|
getHeight() {
|
|
22
88
|
let maxDepth = 0;
|
|
23
89
|
if (this) {
|
|
@@ -25,10 +91,10 @@ export class TreeNode {
|
|
|
25
91
|
if (level > maxDepth) {
|
|
26
92
|
maxDepth = level;
|
|
27
93
|
}
|
|
28
|
-
const {
|
|
29
|
-
if (
|
|
30
|
-
for (let i = 0, len =
|
|
31
|
-
bfs(
|
|
94
|
+
const { _children } = node;
|
|
95
|
+
if (_children) {
|
|
96
|
+
for (let i = 0, len = _children.length; i < len; i++) {
|
|
97
|
+
bfs(_children[i], level + 1);
|
|
32
98
|
}
|
|
33
99
|
}
|
|
34
100
|
};
|