data-structure-typed 1.50.2 → 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 +1 -1
- package/README.md +17 -15
- 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 -155
- package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +33 -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 +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 +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/cjs/data-structures/binary-tree/bst.js +46 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +73 -15
- 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 +35 -2
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +38 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +0 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +126 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/cjs/data-structures/hash/hash-map.js +196 -62
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/cjs/data-structures/heap/heap.js +29 -20
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +83 -25
- 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 +26 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +34 -3
- 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 +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js +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 +95 -21
- package/dist/cjs/data-structures/queue/deque.js +100 -16
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/cjs/data-structures/queue/queue.js +65 -45
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/cjs/data-structures/stack/stack.js +36 -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 +100 -36
- package/dist/cjs/data-structures/trie/trie.js +115 -36
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +34 -2
- 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 +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/mjs/data-structures/binary-tree/bst.js +46 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +74 -16
- 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 +35 -2
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +38 -1
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +0 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +127 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/mjs/data-structures/hash/hash-map.js +204 -70
- package/dist/mjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/mjs/data-structures/heap/heap.js +29 -20
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +86 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +36 -5
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/mjs/data-structures/matrix/matrix.js +1 -1
- 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 +95 -21
- package/dist/mjs/data-structures/queue/deque.js +105 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/mjs/data-structures/queue/queue.js +65 -45
- package/dist/mjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/mjs/data-structures/stack/stack.js +36 -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 +100 -36
- package/dist/mjs/data-structures/trie/trie.js +118 -39
- package/dist/umd/data-structure-typed.js +1445 -595
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +59 -58
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/avl-tree.ts +37 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +46 -13
- package/src/data-structures/binary-tree/rb-tree.ts +79 -18
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +42 -3
- package/src/data-structures/graph/abstract-graph.ts +0 -211
- package/src/data-structures/graph/directed-graph.ts +122 -0
- package/src/data-structures/graph/undirected-graph.ts +143 -19
- package/src/data-structures/hash/hash-map.ts +228 -76
- package/src/data-structures/heap/heap.ts +31 -20
- package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
- package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +1 -1
- 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 +118 -22
- package/src/data-structures/queue/queue.ts +68 -45
- package/src/data-structures/stack/stack.ts +39 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +131 -40
- package/test/performance/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/abstract-graph.test.ts +3 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +189 -253
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/queue/queue.test.ts +2 -0
|
@@ -34,7 +34,7 @@ export class Queue extends IterableElementBase {
|
|
|
34
34
|
_offset = 0;
|
|
35
35
|
/**
|
|
36
36
|
* The offset function returns the offset of the current page.
|
|
37
|
-
* @return The value of the
|
|
37
|
+
* @return The value of the protected variable _offset
|
|
38
38
|
*/
|
|
39
39
|
get offset() {
|
|
40
40
|
return this._offset;
|
|
@@ -47,8 +47,12 @@ export class Queue extends IterableElementBase {
|
|
|
47
47
|
return this.elements.length - this.offset;
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
* Time Complexity: O(1)
|
|
51
|
-
* Space Complexity: O(1)
|
|
50
|
+
* Time Complexity: O(1)
|
|
51
|
+
* Space Complexity: O(1)
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* Time Complexity: O(1)
|
|
55
|
+
* Space Complexity: O(1)
|
|
52
56
|
*
|
|
53
57
|
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
54
58
|
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
@@ -58,12 +62,12 @@ export class Queue extends IterableElementBase {
|
|
|
58
62
|
return this.size > 0 ? this.elements[this.offset] : undefined;
|
|
59
63
|
}
|
|
60
64
|
/**
|
|
61
|
-
* Time Complexity: O(1)
|
|
62
|
-
* Space Complexity: O(1)
|
|
65
|
+
* Time Complexity: O(1)
|
|
66
|
+
* Space Complexity: O(1)
|
|
63
67
|
*/
|
|
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
|
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
69
73
|
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
@@ -73,10 +77,13 @@ export class Queue extends IterableElementBase {
|
|
|
73
77
|
return this.size > 0 ? this.elements[this.elements.length - 1] : undefined;
|
|
74
78
|
}
|
|
75
79
|
/**
|
|
76
|
-
* Time Complexity: O(n)
|
|
77
|
-
* Space Complexity: O(
|
|
80
|
+
* Time Complexity: O(n)
|
|
81
|
+
* Space Complexity: O(n)
|
|
78
82
|
*/
|
|
79
83
|
/**
|
|
84
|
+
* Time Complexity: O(n)
|
|
85
|
+
* Space Complexity: O(n)
|
|
86
|
+
*
|
|
80
87
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
81
88
|
* @public
|
|
82
89
|
* @static
|
|
@@ -88,12 +95,12 @@ export class Queue extends IterableElementBase {
|
|
|
88
95
|
return new Queue(elements);
|
|
89
96
|
}
|
|
90
97
|
/**
|
|
91
|
-
* Time Complexity: O(1)
|
|
92
|
-
* Space Complexity: O(1)
|
|
98
|
+
* Time Complexity: O(1)
|
|
99
|
+
* Space Complexity: O(1)
|
|
93
100
|
*/
|
|
94
101
|
/**
|
|
95
|
-
* Time Complexity: O(1)
|
|
96
|
-
* Space Complexity: O(1)
|
|
102
|
+
* Time Complexity: O(1)
|
|
103
|
+
* Space Complexity: O(1)
|
|
97
104
|
*
|
|
98
105
|
* The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
|
|
99
106
|
* @param {E} element - The `element` parameter represents the element that you want to add to the queue.
|
|
@@ -104,12 +111,12 @@ export class Queue extends IterableElementBase {
|
|
|
104
111
|
return true;
|
|
105
112
|
}
|
|
106
113
|
/**
|
|
107
|
-
* Time Complexity: O(1)
|
|
108
|
-
* Space Complexity: O(1)
|
|
114
|
+
* Time Complexity: O(1)
|
|
115
|
+
* Space Complexity: O(1)
|
|
109
116
|
*/
|
|
110
117
|
/**
|
|
111
|
-
* Time Complexity: O(
|
|
112
|
-
* Space Complexity: O(1)
|
|
118
|
+
* Time Complexity: O(1)
|
|
119
|
+
* Space Complexity: O(1)
|
|
113
120
|
*
|
|
114
121
|
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
115
122
|
* necessary to optimize performance.
|
|
@@ -147,12 +154,12 @@ export class Queue extends IterableElementBase {
|
|
|
147
154
|
return spliced.length === 1;
|
|
148
155
|
}
|
|
149
156
|
/**
|
|
150
|
-
* Time Complexity: O(1)
|
|
151
|
-
* Space Complexity: O(1)
|
|
157
|
+
* Time Complexity: O(1)
|
|
158
|
+
* Space Complexity: O(1)
|
|
152
159
|
*/
|
|
153
160
|
/**
|
|
154
|
-
* Time Complexity: O(1)
|
|
155
|
-
* Space Complexity: O(1)
|
|
161
|
+
* Time Complexity: O(1)
|
|
162
|
+
* Space Complexity: O(1)
|
|
156
163
|
*
|
|
157
164
|
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
158
165
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
@@ -162,12 +169,12 @@ export class Queue extends IterableElementBase {
|
|
|
162
169
|
return this.first;
|
|
163
170
|
}
|
|
164
171
|
/**
|
|
165
|
-
* Time Complexity: O(1)
|
|
166
|
-
* Space Complexity: O(1)
|
|
172
|
+
* Time Complexity: O(1)
|
|
173
|
+
* Space Complexity: O(1)
|
|
167
174
|
*/
|
|
168
175
|
/**
|
|
169
|
-
* Time Complexity: O(1)
|
|
170
|
-
* Space Complexity: O(1)
|
|
176
|
+
* Time Complexity: O(1)
|
|
177
|
+
* Space Complexity: O(1)
|
|
171
178
|
*
|
|
172
179
|
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
173
180
|
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
@@ -177,12 +184,12 @@ export class Queue extends IterableElementBase {
|
|
|
177
184
|
return this.last;
|
|
178
185
|
}
|
|
179
186
|
/**
|
|
180
|
-
* Time Complexity: O(1)
|
|
181
|
-
* Space Complexity: O(1)
|
|
187
|
+
* Time Complexity: O(1)
|
|
188
|
+
* Space Complexity: O(1)
|
|
182
189
|
*/
|
|
183
190
|
/**
|
|
184
|
-
* Time Complexity: O(1)
|
|
185
|
-
* Space Complexity: O(1)
|
|
191
|
+
* Time Complexity: O(1)
|
|
192
|
+
* Space Complexity: O(1)
|
|
186
193
|
*
|
|
187
194
|
* The enqueue function adds a value to the end of a queue.
|
|
188
195
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -191,12 +198,12 @@ export class Queue extends IterableElementBase {
|
|
|
191
198
|
return this.push(value);
|
|
192
199
|
}
|
|
193
200
|
/**
|
|
194
|
-
* Time Complexity: O(
|
|
195
|
-
* Space Complexity: O(1)
|
|
201
|
+
* Time Complexity: O(1)
|
|
202
|
+
* Space Complexity: O(1)
|
|
196
203
|
*/
|
|
197
204
|
/**
|
|
198
|
-
* Time Complexity: O(
|
|
199
|
-
* Space Complexity: O(1)
|
|
205
|
+
* Time Complexity: O(1)
|
|
206
|
+
* Space Complexity: O(1)
|
|
200
207
|
*
|
|
201
208
|
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
202
209
|
* @returns The method is returning a value of type E or undefined.
|
|
@@ -205,12 +212,12 @@ export class Queue extends IterableElementBase {
|
|
|
205
212
|
return this.shift();
|
|
206
213
|
}
|
|
207
214
|
/**
|
|
208
|
-
* Time Complexity: O(1)
|
|
209
|
-
* Space Complexity: O(1)
|
|
215
|
+
* Time Complexity: O(1)
|
|
216
|
+
* Space Complexity: O(1)
|
|
210
217
|
*/
|
|
211
218
|
/**
|
|
212
|
-
* Time Complexity: O(1)
|
|
213
|
-
* Space Complexity: O(1)
|
|
219
|
+
* Time Complexity: O(1)
|
|
220
|
+
* Space Complexity: O(1)
|
|
214
221
|
*
|
|
215
222
|
* @param index
|
|
216
223
|
*/
|
|
@@ -218,12 +225,12 @@ export class Queue extends IterableElementBase {
|
|
|
218
225
|
return this.elements[index];
|
|
219
226
|
}
|
|
220
227
|
/**
|
|
221
|
-
* Time Complexity: O(1)
|
|
222
|
-
* Space Complexity: O(1)
|
|
228
|
+
* Time Complexity: O(1)
|
|
229
|
+
* Space Complexity: O(1)
|
|
223
230
|
*/
|
|
224
231
|
/**
|
|
225
|
-
* Time Complexity: O(1)
|
|
226
|
-
* Space Complexity: O(1)
|
|
232
|
+
* Time Complexity: O(1)
|
|
233
|
+
* Space Complexity: O(1)
|
|
227
234
|
*
|
|
228
235
|
* The function checks if a data structure is empty by comparing its size to zero.
|
|
229
236
|
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
@@ -232,12 +239,12 @@ export class Queue extends IterableElementBase {
|
|
|
232
239
|
return this.size === 0;
|
|
233
240
|
}
|
|
234
241
|
/**
|
|
235
|
-
* Time Complexity: O(1)
|
|
236
|
-
* Space Complexity: O(n)
|
|
242
|
+
* Time Complexity: O(1)
|
|
243
|
+
* Space Complexity: O(n)
|
|
237
244
|
*/
|
|
238
245
|
/**
|
|
239
|
-
* Time Complexity: O(1)
|
|
240
|
-
* Space Complexity: O(n)
|
|
246
|
+
* Time Complexity: O(1)
|
|
247
|
+
* Space Complexity: O(n)
|
|
241
248
|
*
|
|
242
249
|
* The toArray() function returns an array of elements from the current offset to the end of the _elements array.
|
|
243
250
|
* @returns An array of type E is being returned.
|
|
@@ -246,6 +253,13 @@ export class Queue extends IterableElementBase {
|
|
|
246
253
|
return this.elements.slice(this.offset);
|
|
247
254
|
}
|
|
248
255
|
/**
|
|
256
|
+
* Time Complexity: O(1)
|
|
257
|
+
* Space Complexity: O(1)
|
|
258
|
+
*/
|
|
259
|
+
/**
|
|
260
|
+
* Time Complexity: O(1)
|
|
261
|
+
* Space Complexity: O(1)
|
|
262
|
+
*
|
|
249
263
|
* The clear function resets the elements array and offset to their initial values.
|
|
250
264
|
*/
|
|
251
265
|
clear() {
|
|
@@ -329,6 +343,12 @@ export class Queue extends IterableElementBase {
|
|
|
329
343
|
* Time Complexity: O(n)
|
|
330
344
|
* Space Complexity: O(n)
|
|
331
345
|
*/
|
|
346
|
+
/**
|
|
347
|
+
* Time Complexity: O(n)
|
|
348
|
+
* Space Complexity: O(n)
|
|
349
|
+
*
|
|
350
|
+
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
351
|
+
*/
|
|
332
352
|
*_getIterator() {
|
|
333
353
|
for (const item of this.elements) {
|
|
334
354
|
yield item;
|
|
@@ -29,18 +29,18 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
29
29
|
* @return An array of elements
|
|
30
30
|
*/
|
|
31
31
|
get elements(): E[];
|
|
32
|
-
/**
|
|
33
|
-
* 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.
|
|
34
|
-
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
35
|
-
*/
|
|
36
32
|
/**
|
|
37
33
|
* The size() function returns the number of elements in an array.
|
|
38
34
|
* @returns The size of the elements array.
|
|
39
35
|
*/
|
|
40
36
|
get size(): number;
|
|
41
37
|
/**
|
|
42
|
-
* Time Complexity: O(n)
|
|
43
|
-
* 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)
|
|
44
44
|
*
|
|
45
45
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
46
46
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -54,24 +54,24 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
54
54
|
*/
|
|
55
55
|
isEmpty(): boolean;
|
|
56
56
|
/**
|
|
57
|
-
* Time Complexity: O(1)
|
|
58
|
-
* Space Complexity: O(1)
|
|
57
|
+
* Time Complexity: O(1)
|
|
58
|
+
* Space Complexity: O(1)
|
|
59
59
|
*/
|
|
60
60
|
/**
|
|
61
|
-
* Time Complexity: O(1)
|
|
62
|
-
* Space Complexity: O(1)
|
|
61
|
+
* Time Complexity: O(1)
|
|
62
|
+
* Space Complexity: O(1)
|
|
63
63
|
*
|
|
64
64
|
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
65
65
|
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
66
66
|
*/
|
|
67
67
|
peek(): E | undefined;
|
|
68
68
|
/**
|
|
69
|
-
* Time Complexity: O(1)
|
|
70
|
-
* Space Complexity: O(1)
|
|
69
|
+
* Time Complexity: O(1)
|
|
70
|
+
* Space Complexity: O(1)
|
|
71
71
|
*/
|
|
72
72
|
/**
|
|
73
|
-
* Time Complexity: O(1)
|
|
74
|
-
* Space Complexity: O(1)
|
|
73
|
+
* Time Complexity: O(1)
|
|
74
|
+
* Space Complexity: O(1)
|
|
75
75
|
*
|
|
76
76
|
* The push function adds an element to the stack and returns the updated stack.
|
|
77
77
|
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
@@ -79,12 +79,12 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
79
79
|
*/
|
|
80
80
|
push(element: E): boolean;
|
|
81
81
|
/**
|
|
82
|
-
* Time Complexity: O(1)
|
|
83
|
-
* Space Complexity: O(1)
|
|
82
|
+
* Time Complexity: O(1)
|
|
83
|
+
* Space Complexity: O(1)
|
|
84
84
|
*/
|
|
85
85
|
/**
|
|
86
|
-
* Time Complexity: O(1)
|
|
87
|
-
* Space Complexity: O(1)
|
|
86
|
+
* Time Complexity: O(1)
|
|
87
|
+
* Space Complexity: O(1)
|
|
88
88
|
*
|
|
89
89
|
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
90
90
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
@@ -116,16 +116,23 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
116
116
|
*/
|
|
117
117
|
toArray(): E[];
|
|
118
118
|
/**
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
|
+
*/
|
|
122
|
+
/**
|
|
123
|
+
* Time Complexity: O(1)
|
|
124
|
+
* Space Complexity: O(1)
|
|
125
|
+
*
|
|
119
126
|
* The clear function clears the elements array.
|
|
120
127
|
*/
|
|
121
128
|
clear(): void;
|
|
122
129
|
/**
|
|
123
|
-
* Time Complexity: O(n)
|
|
124
|
-
* Space Complexity: O(n)
|
|
130
|
+
* Time Complexity: O(n)
|
|
131
|
+
* Space Complexity: O(n)
|
|
125
132
|
*/
|
|
126
133
|
/**
|
|
127
|
-
* Time Complexity: O(n)
|
|
128
|
-
* Space Complexity: O(n)
|
|
134
|
+
* Time Complexity: O(n)
|
|
135
|
+
* Space Complexity: O(n)
|
|
129
136
|
*
|
|
130
137
|
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
131
138
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
@@ -171,6 +178,13 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
171
178
|
*/
|
|
172
179
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>;
|
|
173
180
|
/**
|
|
181
|
+
* Time Complexity: O(n)
|
|
182
|
+
* Space Complexity: O(n)
|
|
183
|
+
*/
|
|
184
|
+
/**
|
|
185
|
+
* Time Complexity: O(n)
|
|
186
|
+
* Space Complexity: O(n)
|
|
187
|
+
*
|
|
174
188
|
* Custom iterator for the Stack class.
|
|
175
189
|
* @returns An iterator object.
|
|
176
190
|
*/
|
|
@@ -29,10 +29,6 @@ export class Stack extends IterableElementBase {
|
|
|
29
29
|
get elements() {
|
|
30
30
|
return this._elements;
|
|
31
31
|
}
|
|
32
|
-
/**
|
|
33
|
-
* 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.
|
|
34
|
-
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
35
|
-
*/
|
|
36
32
|
/**
|
|
37
33
|
* The size() function returns the number of elements in an array.
|
|
38
34
|
* @returns The size of the elements array.
|
|
@@ -41,8 +37,12 @@ export class Stack extends IterableElementBase {
|
|
|
41
37
|
return this.elements.length;
|
|
42
38
|
}
|
|
43
39
|
/**
|
|
44
|
-
* Time Complexity: O(n)
|
|
45
|
-
* 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)
|
|
46
46
|
*
|
|
47
47
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
48
48
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -60,12 +60,12 @@ export class Stack extends IterableElementBase {
|
|
|
60
60
|
return this.elements.length === 0;
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
|
-
* Time Complexity: O(1)
|
|
64
|
-
* Space Complexity: O(1)
|
|
63
|
+
* Time Complexity: O(1)
|
|
64
|
+
* Space Complexity: O(1)
|
|
65
65
|
*/
|
|
66
66
|
/**
|
|
67
|
-
* Time Complexity: O(1)
|
|
68
|
-
* Space Complexity: O(1)
|
|
67
|
+
* Time Complexity: O(1)
|
|
68
|
+
* Space Complexity: O(1)
|
|
69
69
|
*
|
|
70
70
|
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
71
71
|
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
@@ -76,12 +76,12 @@ export class Stack extends IterableElementBase {
|
|
|
76
76
|
return this.elements[this.elements.length - 1];
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
79
|
-
* Time Complexity: O(1)
|
|
80
|
-
* Space Complexity: O(1)
|
|
79
|
+
* Time Complexity: O(1)
|
|
80
|
+
* Space Complexity: O(1)
|
|
81
81
|
*/
|
|
82
82
|
/**
|
|
83
|
-
* Time Complexity: O(1)
|
|
84
|
-
* Space Complexity: O(1)
|
|
83
|
+
* Time Complexity: O(1)
|
|
84
|
+
* Space Complexity: O(1)
|
|
85
85
|
*
|
|
86
86
|
* The push function adds an element to the stack and returns the updated stack.
|
|
87
87
|
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
@@ -92,12 +92,12 @@ export class Stack extends IterableElementBase {
|
|
|
92
92
|
return true;
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
95
|
-
* Time Complexity: O(1)
|
|
96
|
-
* Space Complexity: O(1)
|
|
95
|
+
* Time Complexity: O(1)
|
|
96
|
+
* Space Complexity: O(1)
|
|
97
97
|
*/
|
|
98
98
|
/**
|
|
99
|
-
* Time Complexity: O(1)
|
|
100
|
-
* Space Complexity: O(1)
|
|
99
|
+
* Time Complexity: O(1)
|
|
100
|
+
* Space Complexity: O(1)
|
|
101
101
|
*
|
|
102
102
|
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
103
103
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
@@ -141,18 +141,25 @@ export class Stack extends IterableElementBase {
|
|
|
141
141
|
return this.elements.slice();
|
|
142
142
|
}
|
|
143
143
|
/**
|
|
144
|
+
* Time Complexity: O(1)
|
|
145
|
+
* Space Complexity: O(1)
|
|
146
|
+
*/
|
|
147
|
+
/**
|
|
148
|
+
* Time Complexity: O(1)
|
|
149
|
+
* Space Complexity: O(1)
|
|
150
|
+
*
|
|
144
151
|
* The clear function clears the elements array.
|
|
145
152
|
*/
|
|
146
153
|
clear() {
|
|
147
154
|
this._elements = [];
|
|
148
155
|
}
|
|
149
156
|
/**
|
|
150
|
-
* Time Complexity: O(n)
|
|
151
|
-
* Space Complexity: O(n)
|
|
157
|
+
* Time Complexity: O(n)
|
|
158
|
+
* Space Complexity: O(n)
|
|
152
159
|
*/
|
|
153
160
|
/**
|
|
154
|
-
* Time Complexity: O(n)
|
|
155
|
-
* Space Complexity: O(n)
|
|
161
|
+
* Time Complexity: O(n)
|
|
162
|
+
* Space Complexity: O(n)
|
|
156
163
|
*
|
|
157
164
|
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
158
165
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
@@ -218,6 +225,13 @@ export class Stack extends IterableElementBase {
|
|
|
218
225
|
return newStack;
|
|
219
226
|
}
|
|
220
227
|
/**
|
|
228
|
+
* Time Complexity: O(n)
|
|
229
|
+
* Space Complexity: O(n)
|
|
230
|
+
*/
|
|
231
|
+
/**
|
|
232
|
+
* Time Complexity: O(n)
|
|
233
|
+
* Space Complexity: O(n)
|
|
234
|
+
*
|
|
221
235
|
* Custom iterator for the Stack class.
|
|
222
236
|
* @returns An iterator object.
|
|
223
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
|
};
|