directed-graph-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/dist/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/data-structures/graph/abstract-graph.js +43 -12
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +2 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/data-structures/hash/hash-map.js +16 -15
- package/dist/data-structures/heap/heap.d.ts +6 -35
- package/dist/data-structures/heap/heap.js +10 -42
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/data-structures/linked-list/doubly-linked-list.js +143 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/data-structures/queue/deque.d.ts +70 -75
- package/dist/data-structures/queue/deque.js +100 -110
- package/dist/data-structures/queue/queue.d.ts +37 -38
- package/dist/data-structures/queue/queue.js +46 -49
- package/dist/data-structures/stack/stack.d.ts +2 -3
- package/dist/data-structures/stack/stack.js +2 -5
- package/dist/data-structures/trie/trie.d.ts +1 -2
- package/dist/data-structures/trie/trie.js +2 -5
- package/package.json +2 -2
- 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
|
@@ -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`.
|
|
@@ -37,6 +37,36 @@ class Queue extends base_1.IterableElementBase {
|
|
|
37
37
|
get size() {
|
|
38
38
|
return this.nodes.length - this.offset;
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
42
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
43
|
+
*
|
|
44
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
45
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
46
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
47
|
+
*/
|
|
48
|
+
get first() {
|
|
49
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
53
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
54
|
+
*/
|
|
55
|
+
/**
|
|
56
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
57
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
58
|
+
*
|
|
59
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
60
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
61
|
+
* array is empty, it returns `undefined`.
|
|
62
|
+
*/
|
|
63
|
+
get last() {
|
|
64
|
+
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 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.
|
|
68
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
69
|
+
*/
|
|
40
70
|
/**
|
|
41
71
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
42
72
|
* @public
|
|
@@ -49,7 +79,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
49
79
|
return new Queue(elements);
|
|
50
80
|
}
|
|
51
81
|
/**
|
|
52
|
-
* Time Complexity: O(1) - constant time as it
|
|
82
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
53
83
|
* Space Complexity: O(1) - no additional space is used.
|
|
54
84
|
*/
|
|
55
85
|
/**
|
|
@@ -62,10 +92,10 @@ class Queue extends base_1.IterableElementBase {
|
|
|
62
92
|
*/
|
|
63
93
|
push(element) {
|
|
64
94
|
this.nodes.push(element);
|
|
65
|
-
return
|
|
95
|
+
return true;
|
|
66
96
|
}
|
|
67
97
|
/**
|
|
68
|
-
* Time Complexity: O(
|
|
98
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
69
99
|
* Space Complexity: O(1) - no additional space is used.
|
|
70
100
|
*/
|
|
71
101
|
/**
|
|
@@ -79,7 +109,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
79
109
|
shift() {
|
|
80
110
|
if (this.size === 0)
|
|
81
111
|
return undefined;
|
|
82
|
-
const first = this.
|
|
112
|
+
const first = this.first;
|
|
83
113
|
this._offset += 1;
|
|
84
114
|
if (this.offset * 2 < this.nodes.length)
|
|
85
115
|
return first;
|
|
@@ -89,21 +119,6 @@ class Queue extends base_1.IterableElementBase {
|
|
|
89
119
|
this._offset = 0;
|
|
90
120
|
return first;
|
|
91
121
|
}
|
|
92
|
-
/**
|
|
93
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
94
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
95
|
-
*/
|
|
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
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
101
|
-
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
102
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
103
|
-
*/
|
|
104
|
-
getFirst() {
|
|
105
|
-
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
106
|
-
}
|
|
107
122
|
/**
|
|
108
123
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
109
124
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -117,22 +132,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
117
132
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
118
133
|
*/
|
|
119
134
|
peek() {
|
|
120
|
-
return this.
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
124
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
125
|
-
*/
|
|
126
|
-
/**
|
|
127
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
128
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
129
|
-
*
|
|
130
|
-
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
131
|
-
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
132
|
-
* array is empty, it returns `undefined`.
|
|
133
|
-
*/
|
|
134
|
-
getLast() {
|
|
135
|
-
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
135
|
+
return this.first;
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
138
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
@@ -147,7 +147,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
147
147
|
* array is empty, it returns `undefined`.
|
|
148
148
|
*/
|
|
149
149
|
peekLast() {
|
|
150
|
-
return this.
|
|
150
|
+
return this.last;
|
|
151
151
|
}
|
|
152
152
|
/**
|
|
153
153
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
@@ -239,9 +239,6 @@ class Queue extends base_1.IterableElementBase {
|
|
|
239
239
|
clone() {
|
|
240
240
|
return new Queue(this.nodes.slice(this.offset));
|
|
241
241
|
}
|
|
242
|
-
print() {
|
|
243
|
-
console.log([...this]);
|
|
244
|
-
}
|
|
245
242
|
/**
|
|
246
243
|
* Time Complexity: O(n)
|
|
247
244
|
* Space Complexity: O(n)
|
|
@@ -318,12 +315,20 @@ exports.Queue = Queue;
|
|
|
318
315
|
* 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.
|
|
319
316
|
*/
|
|
320
317
|
class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
318
|
+
/**
|
|
319
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
320
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
321
|
+
*/
|
|
322
|
+
get first() {
|
|
323
|
+
var _a;
|
|
324
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
325
|
+
}
|
|
321
326
|
/**
|
|
322
327
|
* The enqueue function adds a value to the end of an array.
|
|
323
328
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
324
329
|
*/
|
|
325
330
|
enqueue(value) {
|
|
326
|
-
this.push(value);
|
|
331
|
+
return this.push(value);
|
|
327
332
|
}
|
|
328
333
|
/**
|
|
329
334
|
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
@@ -332,20 +337,12 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
|
332
337
|
dequeue() {
|
|
333
338
|
return this.shift();
|
|
334
339
|
}
|
|
335
|
-
/**
|
|
336
|
-
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
337
|
-
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
338
|
-
*/
|
|
339
|
-
getFirst() {
|
|
340
|
-
var _a;
|
|
341
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
342
|
-
}
|
|
343
340
|
/**
|
|
344
341
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
345
342
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
346
343
|
*/
|
|
347
344
|
peek() {
|
|
348
|
-
return this.
|
|
345
|
+
return this.first;
|
|
349
346
|
}
|
|
350
347
|
}
|
|
351
348
|
exports.LinkedListQueue = LinkedListQueue;
|
|
@@ -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
|
}
|
|
@@ -89,7 +89,7 @@ class Stack extends base_1.IterableElementBase {
|
|
|
89
89
|
*/
|
|
90
90
|
push(element) {
|
|
91
91
|
this.elements.push(element);
|
|
92
|
-
return
|
|
92
|
+
return true;
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
95
95
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
@@ -105,7 +105,7 @@ class Stack extends base_1.IterableElementBase {
|
|
|
105
105
|
*/
|
|
106
106
|
pop() {
|
|
107
107
|
if (this.isEmpty())
|
|
108
|
-
return
|
|
108
|
+
return;
|
|
109
109
|
return this.elements.pop();
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
@@ -199,9 +199,6 @@ class Stack extends base_1.IterableElementBase {
|
|
|
199
199
|
}
|
|
200
200
|
return newStack;
|
|
201
201
|
}
|
|
202
|
-
print() {
|
|
203
|
-
console.log([...this]);
|
|
204
|
-
}
|
|
205
202
|
/**
|
|
206
203
|
* Custom iterator for the Stack class.
|
|
207
204
|
* @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.
|
|
@@ -341,11 +341,11 @@ class Trie extends base_1.IterableElementBase {
|
|
|
341
341
|
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
342
342
|
*/
|
|
343
343
|
filter(predicate, thisArg) {
|
|
344
|
-
const results =
|
|
344
|
+
const results = new Trie();
|
|
345
345
|
let index = 0;
|
|
346
346
|
for (const word of this) {
|
|
347
347
|
if (predicate.call(thisArg, word, index, this)) {
|
|
348
|
-
results.
|
|
348
|
+
results.add(word);
|
|
349
349
|
}
|
|
350
350
|
index++;
|
|
351
351
|
}
|
|
@@ -377,9 +377,6 @@ class Trie extends base_1.IterableElementBase {
|
|
|
377
377
|
}
|
|
378
378
|
return newTrie;
|
|
379
379
|
}
|
|
380
|
-
print() {
|
|
381
|
-
console.log([...this]);
|
|
382
|
-
}
|
|
383
380
|
*_getIterator() {
|
|
384
381
|
function* _dfs(node, path) {
|
|
385
382
|
if (node.isEnd) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "directed-graph-typed",
|
|
3
|
-
"version": "1.49.
|
|
3
|
+
"version": "1.49.3",
|
|
4
4
|
"description": "Directed Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -147,6 +147,6 @@
|
|
|
147
147
|
"typescript": "^4.9.5"
|
|
148
148
|
},
|
|
149
149
|
"dependencies": {
|
|
150
|
-
"data-structure-typed": "^1.49.
|
|
150
|
+
"data-structure-typed": "^1.49.3"
|
|
151
151
|
}
|
|
152
152
|
}
|
|
@@ -181,6 +181,21 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
181
181
|
return accumulator;
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
+
hasValue(value: V): boolean {
|
|
185
|
+
for (const [, elementValue] of this) {
|
|
186
|
+
if (elementValue === value) return true;
|
|
187
|
+
}
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Time Complexity: O(n)
|
|
193
|
+
* Space Complexity: O(n)
|
|
194
|
+
*/
|
|
195
|
+
print(): void {
|
|
196
|
+
console.log([...this])
|
|
197
|
+
}
|
|
198
|
+
|
|
184
199
|
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
185
200
|
}
|
|
186
201
|
|
|
@@ -325,5 +340,14 @@ export abstract class IterableElementBase<V> {
|
|
|
325
340
|
return accumulator;
|
|
326
341
|
}
|
|
327
342
|
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Time Complexity: O(n)
|
|
346
|
+
* Space Complexity: O(n)
|
|
347
|
+
*/
|
|
348
|
+
print(): void {
|
|
349
|
+
console.log([...this])
|
|
350
|
+
}
|
|
351
|
+
|
|
328
352
|
protected abstract _getIterator(...args: any[]): IterableIterator<V>;
|
|
329
353
|
}
|
|
@@ -156,10 +156,10 @@ export abstract class AbstractGraph<
|
|
|
156
156
|
|
|
157
157
|
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
|
|
158
158
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
159
|
-
return this.
|
|
159
|
+
return this._addVertex(keyOrVertex);
|
|
160
160
|
} else {
|
|
161
161
|
const newVertex = this.createVertex(keyOrVertex, value);
|
|
162
|
-
return this.
|
|
162
|
+
return this._addVertex(newVertex);
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
|
|
@@ -242,14 +242,14 @@ export abstract class AbstractGraph<
|
|
|
242
242
|
|
|
243
243
|
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
|
|
244
244
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
245
|
-
return this.
|
|
245
|
+
return this._addEdge(srcOrEdge);
|
|
246
246
|
} else {
|
|
247
247
|
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
248
248
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
|
249
249
|
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
|
250
250
|
if (dest instanceof AbstractVertex) dest = dest.key;
|
|
251
251
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
252
|
-
return this.
|
|
252
|
+
return this._addEdge(newEdge);
|
|
253
253
|
} else {
|
|
254
254
|
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
255
255
|
}
|
|
@@ -1147,14 +1147,6 @@ export abstract class AbstractGraph<
|
|
|
1147
1147
|
return this.tarjan(false, false, false, false).lowMap;
|
|
1148
1148
|
}
|
|
1149
1149
|
|
|
1150
|
-
/**
|
|
1151
|
-
* The function `getCycles` returns a map of cycles found using the Tarjan algorithm.
|
|
1152
|
-
* @returns The function `getCycles()` is returning a `Map<number, VO[]>`.
|
|
1153
|
-
*/
|
|
1154
|
-
getCycles(): Map<number, VO[]> {
|
|
1155
|
-
return this.tarjan(false, false, false, true).cycles;
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
1150
|
/**
|
|
1159
1151
|
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
1160
1152
|
* @returns an array of VO objects, specifically the cut vertexes.
|
|
@@ -1180,6 +1172,55 @@ export abstract class AbstractGraph<
|
|
|
1180
1172
|
return this.tarjan(false, true, false, false).bridges;
|
|
1181
1173
|
}
|
|
1182
1174
|
|
|
1175
|
+
/**
|
|
1176
|
+
* O(V+E+C)
|
|
1177
|
+
* O(V+C)
|
|
1178
|
+
*/
|
|
1179
|
+
getCycles(isInclude2Cycle: boolean = false): VertexKey[][] {
|
|
1180
|
+
const cycles: VertexKey[][] = [];
|
|
1181
|
+
const visited: Set<VO> = new Set();
|
|
1182
|
+
|
|
1183
|
+
const dfs = (vertex: VO, currentPath: VertexKey[], visited: Set<VO>) => {
|
|
1184
|
+
if (visited.has(vertex)) {
|
|
1185
|
+
if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
|
|
1186
|
+
cycles.push([...currentPath]);
|
|
1187
|
+
}
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
visited.add(vertex);
|
|
1192
|
+
currentPath.push(vertex.key);
|
|
1193
|
+
|
|
1194
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
1195
|
+
neighbor && dfs(neighbor, currentPath, visited);
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
visited.delete(vertex);
|
|
1199
|
+
currentPath.pop();
|
|
1200
|
+
};
|
|
1201
|
+
|
|
1202
|
+
for (const vertex of this.vertexMap.values()) {
|
|
1203
|
+
dfs(vertex, [], visited);
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// Use a set to eliminate duplicate cycles
|
|
1207
|
+
const uniqueCycles = new Map<string, VertexKey[]>();
|
|
1208
|
+
|
|
1209
|
+
for (const cycle of cycles) {
|
|
1210
|
+
const sorted = [...cycle].sort().toString()
|
|
1211
|
+
|
|
1212
|
+
if (uniqueCycles.has(sorted)) continue
|
|
1213
|
+
else {
|
|
1214
|
+
uniqueCycles.set(sorted, cycle)
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
// Convert the unique cycles back to an array
|
|
1219
|
+
return [...uniqueCycles].map(cycleString =>
|
|
1220
|
+
cycleString[1]
|
|
1221
|
+
);
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1183
1224
|
/**
|
|
1184
1225
|
* Time Complexity: O(n)
|
|
1185
1226
|
* Space Complexity: O(n)
|
|
@@ -1247,9 +1288,9 @@ export abstract class AbstractGraph<
|
|
|
1247
1288
|
}
|
|
1248
1289
|
}
|
|
1249
1290
|
|
|
1250
|
-
protected abstract
|
|
1291
|
+
protected abstract _addEdge(edge: EO): boolean;
|
|
1251
1292
|
|
|
1252
|
-
protected
|
|
1293
|
+
protected _addVertex(newVertex: VO): boolean {
|
|
1253
1294
|
if (this.hasVertex(newVertex)) {
|
|
1254
1295
|
return false;
|
|
1255
1296
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
@@ -596,6 +596,7 @@ export class DirectedGraph<
|
|
|
596
596
|
}
|
|
597
597
|
}
|
|
598
598
|
|
|
599
|
+
|
|
599
600
|
/**
|
|
600
601
|
* Time Complexity: O(1)
|
|
601
602
|
* Space Complexity: O(1)
|
|
@@ -605,13 +606,13 @@ export class DirectedGraph<
|
|
|
605
606
|
* Time Complexity: O(1)
|
|
606
607
|
* Space Complexity: O(1)
|
|
607
608
|
*
|
|
608
|
-
* The function `
|
|
609
|
+
* The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
|
|
609
610
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
610
611
|
* needs to be added to the graph.
|
|
611
612
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
612
613
|
* source or destination vertex does not exist in the graph.
|
|
613
614
|
*/
|
|
614
|
-
protected
|
|
615
|
+
protected _addEdge(edge: EO): boolean {
|
|
615
616
|
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
616
617
|
return false;
|
|
617
618
|
}
|
|
@@ -381,7 +381,7 @@ export class UndirectedGraph<
|
|
|
381
381
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
382
382
|
* @returns a boolean value.
|
|
383
383
|
*/
|
|
384
|
-
protected
|
|
384
|
+
protected _addEdge(edge: EO): boolean {
|
|
385
385
|
for (const end of edge.vertexMap) {
|
|
386
386
|
const endVertex = this._getVertex(end);
|
|
387
387
|
if (endVertex === undefined) return false;
|