data-structure-typed 1.49.1 → 1.49.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 +16 -16
- package/README_zh-CN.md +2 -2
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +457 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/cjs/data-structures/base/iterable-base.js +21 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/cjs/data-structures/graph/abstract-graph.js +43 -12
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/cjs/data-structures/hash/hash-map.js +16 -15
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/cjs/data-structures/heap/heap.js +10 -42
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +143 -146
- 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 +16 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -42
- 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 +25 -25
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/cjs/data-structures/queue/deque.js +100 -110
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +37 -38
- package/dist/cjs/data-structures/queue/queue.js +46 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/cjs/data-structures/stack/stack.js +2 -5
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/cjs/data-structures/trie/trie.js +2 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/mjs/data-structures/base/iterable-base.js +21 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/mjs/data-structures/graph/abstract-graph.js +43 -12
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +2 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/mjs/data-structures/hash/hash-map.js +16 -15
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/mjs/data-structures/heap/heap.js +10 -42
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +142 -145
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -43
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/mjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/mjs/data-structures/queue/deque.js +100 -110
- package/dist/mjs/data-structures/queue/queue.d.ts +37 -38
- package/dist/mjs/data-structures/queue/queue.js +45 -48
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/mjs/data-structures/stack/stack.js +2 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/mjs/data-structures/trie/trie.js +2 -5
- package/dist/umd/data-structure-typed.js +531 -532
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -14
- package/src/data-structures/graph/directed-graph.ts +3 -2
- package/src/data-structures/graph/undirected-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +54 -58
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +48 -3
- package/test/unit/data-structures/graph/undirected-graph.test.ts +48 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +6 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/queue/queue.test.ts +4 -4
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
|
@@ -32,6 +32,32 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
32
32
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
33
33
|
*/
|
|
34
34
|
get size(): number;
|
|
35
|
+
/**
|
|
36
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
37
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
38
|
+
*
|
|
39
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
40
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
41
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
42
|
+
*/
|
|
43
|
+
get first(): E | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
46
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
47
|
+
*/
|
|
48
|
+
/**
|
|
49
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
50
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
51
|
+
*
|
|
52
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
53
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
54
|
+
* array is empty, it returns `undefined`.
|
|
55
|
+
*/
|
|
56
|
+
get last(): E | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
59
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
60
|
+
*/
|
|
35
61
|
/**
|
|
36
62
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
37
63
|
* @public
|
|
@@ -42,7 +68,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
42
68
|
*/
|
|
43
69
|
static fromArray<E>(elements: E[]): Queue<E>;
|
|
44
70
|
/**
|
|
45
|
-
* Time Complexity: O(1) - constant time as it
|
|
71
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
46
72
|
* Space Complexity: O(1) - no additional space is used.
|
|
47
73
|
*/
|
|
48
74
|
/**
|
|
@@ -53,9 +79,9 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
53
79
|
* @param {E} element - The `element` parameter represents the element that you want to add to the queue.
|
|
54
80
|
* @returns The `add` method is returning a `Queue<E>` object.
|
|
55
81
|
*/
|
|
56
|
-
push(element: E):
|
|
82
|
+
push(element: E): boolean;
|
|
57
83
|
/**
|
|
58
|
-
* Time Complexity: O(
|
|
84
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
59
85
|
* Space Complexity: O(1) - no additional space is used.
|
|
60
86
|
*/
|
|
61
87
|
/**
|
|
@@ -67,19 +93,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
67
93
|
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
68
94
|
*/
|
|
69
95
|
shift(): E | undefined;
|
|
70
|
-
/**
|
|
71
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
72
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
73
|
-
*/
|
|
74
|
-
/**
|
|
75
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
76
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
77
|
-
*
|
|
78
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
79
|
-
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
80
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
81
|
-
*/
|
|
82
|
-
getFirst(): E | undefined;
|
|
83
96
|
/**
|
|
84
97
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
85
98
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -93,19 +106,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
93
106
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
94
107
|
*/
|
|
95
108
|
peek(): E | undefined;
|
|
96
|
-
/**
|
|
97
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
98
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
99
|
-
*/
|
|
100
|
-
/**
|
|
101
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
102
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
103
|
-
*
|
|
104
|
-
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
105
|
-
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
106
|
-
* array is empty, it returns `undefined`.
|
|
107
|
-
*/
|
|
108
|
-
getLast(): E | undefined;
|
|
109
109
|
/**
|
|
110
110
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
111
111
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -130,7 +130,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
130
130
|
* The enqueue function adds a value to the end of a queue.
|
|
131
131
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
132
132
|
*/
|
|
133
|
-
enqueue(value: E):
|
|
133
|
+
enqueue(value: E): boolean;
|
|
134
134
|
/**
|
|
135
135
|
* Time Complexity: O(n) - same as shift().
|
|
136
136
|
* Space Complexity: O(1) - same as shift().
|
|
@@ -194,7 +194,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
194
194
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
195
195
|
*/
|
|
196
196
|
clone(): Queue<E>;
|
|
197
|
-
print(): void;
|
|
198
197
|
/**
|
|
199
198
|
* Time Complexity: O(n)
|
|
200
199
|
* Space Complexity: O(n)
|
|
@@ -239,7 +238,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
239
238
|
* Time Complexity: O(n)
|
|
240
239
|
* Space Complexity: O(n)
|
|
241
240
|
*/
|
|
242
|
-
protected _getIterator():
|
|
241
|
+
protected _getIterator(): IterableIterator<E>;
|
|
243
242
|
}
|
|
244
243
|
/**
|
|
245
244
|
* 1. First In, First Out (FIFO) Strategy: Like other queue implementations, LinkedListQueue follows the first in, first out principle, meaning the element that is added to the queue first will be the first to be removed.
|
|
@@ -248,21 +247,21 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
|
|
|
248
247
|
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
249
248
|
*/
|
|
250
249
|
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
250
|
+
/**
|
|
251
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
252
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
253
|
+
*/
|
|
254
|
+
get first(): E | undefined;
|
|
251
255
|
/**
|
|
252
256
|
* The enqueue function adds a value to the end of an array.
|
|
253
257
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
254
258
|
*/
|
|
255
|
-
enqueue(value: E):
|
|
259
|
+
enqueue(value: E): boolean;
|
|
256
260
|
/**
|
|
257
261
|
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
258
262
|
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
259
263
|
*/
|
|
260
264
|
dequeue(): E | undefined;
|
|
261
|
-
/**
|
|
262
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
263
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
264
|
-
*/
|
|
265
|
-
getFirst(): E | undefined;
|
|
266
265
|
/**
|
|
267
266
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
268
267
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
@@ -36,6 +36,36 @@ export class Queue extends IterableElementBase {
|
|
|
36
36
|
get size() {
|
|
37
37
|
return this.nodes.length - this.offset;
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
41
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
42
|
+
*
|
|
43
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
44
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
45
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
46
|
+
*/
|
|
47
|
+
get first() {
|
|
48
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
52
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
53
|
+
*/
|
|
54
|
+
/**
|
|
55
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
56
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
57
|
+
*
|
|
58
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
59
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
60
|
+
* array is empty, it returns `undefined`.
|
|
61
|
+
*/
|
|
62
|
+
get last() {
|
|
63
|
+
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
67
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
68
|
+
*/
|
|
39
69
|
/**
|
|
40
70
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
41
71
|
* @public
|
|
@@ -48,7 +78,7 @@ export class Queue extends IterableElementBase {
|
|
|
48
78
|
return new Queue(elements);
|
|
49
79
|
}
|
|
50
80
|
/**
|
|
51
|
-
* Time Complexity: O(1) - constant time as it
|
|
81
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
52
82
|
* Space Complexity: O(1) - no additional space is used.
|
|
53
83
|
*/
|
|
54
84
|
/**
|
|
@@ -61,10 +91,10 @@ export class Queue extends IterableElementBase {
|
|
|
61
91
|
*/
|
|
62
92
|
push(element) {
|
|
63
93
|
this.nodes.push(element);
|
|
64
|
-
return
|
|
94
|
+
return true;
|
|
65
95
|
}
|
|
66
96
|
/**
|
|
67
|
-
* Time Complexity: O(
|
|
97
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
68
98
|
* Space Complexity: O(1) - no additional space is used.
|
|
69
99
|
*/
|
|
70
100
|
/**
|
|
@@ -78,7 +108,7 @@ export class Queue extends IterableElementBase {
|
|
|
78
108
|
shift() {
|
|
79
109
|
if (this.size === 0)
|
|
80
110
|
return undefined;
|
|
81
|
-
const first = this.
|
|
111
|
+
const first = this.first;
|
|
82
112
|
this._offset += 1;
|
|
83
113
|
if (this.offset * 2 < this.nodes.length)
|
|
84
114
|
return first;
|
|
@@ -88,21 +118,6 @@ export class Queue extends IterableElementBase {
|
|
|
88
118
|
this._offset = 0;
|
|
89
119
|
return first;
|
|
90
120
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
93
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
94
|
-
*/
|
|
95
|
-
/**
|
|
96
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
97
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
98
|
-
*
|
|
99
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
100
|
-
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
101
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
102
|
-
*/
|
|
103
|
-
getFirst() {
|
|
104
|
-
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
105
|
-
}
|
|
106
121
|
/**
|
|
107
122
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
108
123
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -116,22 +131,7 @@ export class Queue extends IterableElementBase {
|
|
|
116
131
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
117
132
|
*/
|
|
118
133
|
peek() {
|
|
119
|
-
return this.
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
123
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
124
|
-
*/
|
|
125
|
-
/**
|
|
126
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
127
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
128
|
-
*
|
|
129
|
-
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
130
|
-
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
131
|
-
* array is empty, it returns `undefined`.
|
|
132
|
-
*/
|
|
133
|
-
getLast() {
|
|
134
|
-
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
134
|
+
return this.first;
|
|
135
135
|
}
|
|
136
136
|
/**
|
|
137
137
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
@@ -146,7 +146,7 @@ export class Queue extends IterableElementBase {
|
|
|
146
146
|
* array is empty, it returns `undefined`.
|
|
147
147
|
*/
|
|
148
148
|
peekLast() {
|
|
149
|
-
return this.
|
|
149
|
+
return this.last;
|
|
150
150
|
}
|
|
151
151
|
/**
|
|
152
152
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
@@ -238,9 +238,6 @@ export class Queue extends IterableElementBase {
|
|
|
238
238
|
clone() {
|
|
239
239
|
return new Queue(this.nodes.slice(this.offset));
|
|
240
240
|
}
|
|
241
|
-
print() {
|
|
242
|
-
console.log([...this]);
|
|
243
|
-
}
|
|
244
241
|
/**
|
|
245
242
|
* Time Complexity: O(n)
|
|
246
243
|
* Space Complexity: O(n)
|
|
@@ -316,12 +313,19 @@ export class Queue extends IterableElementBase {
|
|
|
316
313
|
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
317
314
|
*/
|
|
318
315
|
export class LinkedListQueue extends SinglyLinkedList {
|
|
316
|
+
/**
|
|
317
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
318
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
319
|
+
*/
|
|
320
|
+
get first() {
|
|
321
|
+
return this.head?.value;
|
|
322
|
+
}
|
|
319
323
|
/**
|
|
320
324
|
* The enqueue function adds a value to the end of an array.
|
|
321
325
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
322
326
|
*/
|
|
323
327
|
enqueue(value) {
|
|
324
|
-
this.push(value);
|
|
328
|
+
return this.push(value);
|
|
325
329
|
}
|
|
326
330
|
/**
|
|
327
331
|
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
@@ -330,18 +334,11 @@ export class LinkedListQueue extends SinglyLinkedList {
|
|
|
330
334
|
dequeue() {
|
|
331
335
|
return this.shift();
|
|
332
336
|
}
|
|
333
|
-
/**
|
|
334
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
335
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
336
|
-
*/
|
|
337
|
-
getFirst() {
|
|
338
|
-
return this.head?.value;
|
|
339
|
-
}
|
|
340
337
|
/**
|
|
341
338
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
342
339
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
343
340
|
*/
|
|
344
341
|
peek() {
|
|
345
|
-
return this.
|
|
342
|
+
return this.first;
|
|
346
343
|
}
|
|
347
344
|
}
|
|
@@ -73,7 +73,7 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
73
73
|
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
74
74
|
* @returns The `push` method is returning the updated `Stack<E>` object.
|
|
75
75
|
*/
|
|
76
|
-
push(element: E):
|
|
76
|
+
push(element: E): boolean;
|
|
77
77
|
/**
|
|
78
78
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
79
79
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -154,10 +154,9 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
|
154
154
|
* @returns The `map` method is returning a new `Stack` object.
|
|
155
155
|
*/
|
|
156
156
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>;
|
|
157
|
-
print(): void;
|
|
158
157
|
/**
|
|
159
158
|
* Custom iterator for the Stack class.
|
|
160
159
|
* @returns An iterator object.
|
|
161
160
|
*/
|
|
162
|
-
protected _getIterator():
|
|
161
|
+
protected _getIterator(): IterableIterator<E>;
|
|
163
162
|
}
|
|
@@ -87,7 +87,7 @@ export class Stack extends IterableElementBase {
|
|
|
87
87
|
*/
|
|
88
88
|
push(element) {
|
|
89
89
|
this.elements.push(element);
|
|
90
|
-
return
|
|
90
|
+
return true;
|
|
91
91
|
}
|
|
92
92
|
/**
|
|
93
93
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
@@ -103,7 +103,7 @@ export class Stack extends IterableElementBase {
|
|
|
103
103
|
*/
|
|
104
104
|
pop() {
|
|
105
105
|
if (this.isEmpty())
|
|
106
|
-
return
|
|
106
|
+
return;
|
|
107
107
|
return this.elements.pop();
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
@@ -197,9 +197,6 @@ export class Stack extends IterableElementBase {
|
|
|
197
197
|
}
|
|
198
198
|
return newStack;
|
|
199
199
|
}
|
|
200
|
-
print() {
|
|
201
|
-
console.log([...this]);
|
|
202
|
-
}
|
|
203
200
|
/**
|
|
204
201
|
* Custom iterator for the Stack class.
|
|
205
202
|
* @returns An iterator object.
|
|
@@ -172,7 +172,7 @@ export declare class Trie extends IterableElementBase<string> {
|
|
|
172
172
|
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
173
173
|
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
174
174
|
*/
|
|
175
|
-
filter(predicate: ElementCallback<string, boolean>, thisArg?: any):
|
|
175
|
+
filter(predicate: ElementCallback<string, boolean>, thisArg?: any): Trie;
|
|
176
176
|
/**
|
|
177
177
|
* Time Complexity: O(n)
|
|
178
178
|
* Space Complexity: O(n)
|
|
@@ -191,7 +191,6 @@ export declare class Trie extends IterableElementBase<string> {
|
|
|
191
191
|
* @returns The `map` function is returning a new Trie object.
|
|
192
192
|
*/
|
|
193
193
|
map(callback: ElementCallback<string, string>, thisArg?: any): Trie;
|
|
194
|
-
print(): void;
|
|
195
194
|
protected _getIterator(): IterableIterator<string>;
|
|
196
195
|
/**
|
|
197
196
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
@@ -343,11 +343,11 @@ export class Trie extends IterableElementBase {
|
|
|
343
343
|
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
344
344
|
*/
|
|
345
345
|
filter(predicate, thisArg) {
|
|
346
|
-
const results =
|
|
346
|
+
const results = new Trie();
|
|
347
347
|
let index = 0;
|
|
348
348
|
for (const word of this) {
|
|
349
349
|
if (predicate.call(thisArg, word, index, this)) {
|
|
350
|
-
results.
|
|
350
|
+
results.add(word);
|
|
351
351
|
}
|
|
352
352
|
index++;
|
|
353
353
|
}
|
|
@@ -379,9 +379,6 @@ export class Trie extends IterableElementBase {
|
|
|
379
379
|
}
|
|
380
380
|
return newTrie;
|
|
381
381
|
}
|
|
382
|
-
print() {
|
|
383
|
-
console.log([...this]);
|
|
384
|
-
}
|
|
385
382
|
*_getIterator() {
|
|
386
383
|
function* _dfs(node, path) {
|
|
387
384
|
if (node.isEnd) {
|