queue-typed 1.47.6 → 1.47.8
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/binary-tree/avl-tree.d.ts +40 -22
- package/dist/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/data-structures/binary-tree/bst.js +137 -154
- package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +30 -30
- package/dist/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/data-structures/graph/directed-graph.js +28 -28
- package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/data-structures/graph/undirected-graph.js +18 -18
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +13 -13
- package/dist/data-structures/stack/stack.d.ts +6 -6
- package/dist/data-structures/stack/stack.js +7 -7
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +6 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
|
@@ -10,13 +10,13 @@ exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
class SinglyLinkedListNode {
|
|
12
12
|
/**
|
|
13
|
-
* The constructor function initializes an instance of a class with a given value and sets the next property to
|
|
13
|
+
* The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
|
|
14
14
|
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
15
15
|
* will be stored in the node of a linked list.
|
|
16
16
|
*/
|
|
17
17
|
constructor(value) {
|
|
18
18
|
this.value = value;
|
|
19
|
-
this.next =
|
|
19
|
+
this.next = undefined;
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
@@ -25,8 +25,8 @@ class SinglyLinkedList {
|
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
26
|
*/
|
|
27
27
|
constructor(elements) {
|
|
28
|
-
this._head =
|
|
29
|
-
this._tail =
|
|
28
|
+
this._head = undefined;
|
|
29
|
+
this._tail = undefined;
|
|
30
30
|
this._length = 0;
|
|
31
31
|
if (elements) {
|
|
32
32
|
for (const el of elements)
|
|
@@ -112,15 +112,15 @@ class SinglyLinkedList {
|
|
|
112
112
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
113
113
|
* pointers accordingly.
|
|
114
114
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
115
|
-
* the linked list is empty, it returns `
|
|
115
|
+
* the linked list is empty, it returns `undefined`.
|
|
116
116
|
*/
|
|
117
117
|
pop() {
|
|
118
118
|
if (!this.head)
|
|
119
119
|
return undefined;
|
|
120
120
|
if (this.head === this.tail) {
|
|
121
121
|
const value = this.head.value;
|
|
122
|
-
this._head =
|
|
123
|
-
this._tail =
|
|
122
|
+
this._head = undefined;
|
|
123
|
+
this._tail = undefined;
|
|
124
124
|
this._length--;
|
|
125
125
|
return value;
|
|
126
126
|
}
|
|
@@ -129,7 +129,7 @@ class SinglyLinkedList {
|
|
|
129
129
|
current = current.next;
|
|
130
130
|
}
|
|
131
131
|
const value = this.tail.value;
|
|
132
|
-
current.next =
|
|
132
|
+
current.next = undefined;
|
|
133
133
|
this._tail = current;
|
|
134
134
|
this._length--;
|
|
135
135
|
return value;
|
|
@@ -145,7 +145,7 @@ class SinglyLinkedList {
|
|
|
145
145
|
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
146
146
|
* pointers accordingly.
|
|
147
147
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
148
|
-
* the linked list is empty, it returns `
|
|
148
|
+
* the linked list is empty, it returns `undefined`.
|
|
149
149
|
*/
|
|
150
150
|
popLast() {
|
|
151
151
|
return this.pop();
|
|
@@ -230,11 +230,11 @@ class SinglyLinkedList {
|
|
|
230
230
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
231
231
|
* Space Complexity: O(1) - Constant space.
|
|
232
232
|
*
|
|
233
|
-
* The function `getAt` returns the value at a specified index in a linked list, or
|
|
233
|
+
* The function `getAt` returns the value at a specified index in a linked list, or undefined if the index is out of range.
|
|
234
234
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
235
235
|
* retrieve from the list.
|
|
236
|
-
* @returns The method `getAt(index: number): E |
|
|
237
|
-
* `
|
|
236
|
+
* @returns The method `getAt(index: number): E | undefined` returns the value at the specified index in the linked list, or
|
|
237
|
+
* `undefined` if the index is out of bounds.
|
|
238
238
|
*/
|
|
239
239
|
getAt(index) {
|
|
240
240
|
if (index < 0 || index >= this.length)
|
|
@@ -257,7 +257,7 @@ class SinglyLinkedList {
|
|
|
257
257
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
258
258
|
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
259
259
|
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
|
|
260
|
-
* specified index exists, or `
|
|
260
|
+
* specified index exists, or `undefined` if the index is out of bounds.
|
|
261
261
|
*/
|
|
262
262
|
getNodeAt(index) {
|
|
263
263
|
let current = this.head;
|
|
@@ -277,7 +277,7 @@ class SinglyLinkedList {
|
|
|
277
277
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
278
278
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
279
279
|
* data structure. It is of type number.
|
|
280
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `
|
|
280
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
281
281
|
* bounds.
|
|
282
282
|
*/
|
|
283
283
|
deleteAt(index) {
|
|
@@ -317,13 +317,13 @@ class SinglyLinkedList {
|
|
|
317
317
|
else {
|
|
318
318
|
value = valueOrNode;
|
|
319
319
|
}
|
|
320
|
-
let current = this.head, prev =
|
|
320
|
+
let current = this.head, prev = undefined;
|
|
321
321
|
while (current) {
|
|
322
322
|
if (current.value === value) {
|
|
323
|
-
if (prev ===
|
|
323
|
+
if (prev === undefined) {
|
|
324
324
|
this._head = current.next;
|
|
325
325
|
if (current === this.tail) {
|
|
326
|
-
this._tail =
|
|
326
|
+
this._tail = undefined;
|
|
327
327
|
}
|
|
328
328
|
}
|
|
329
329
|
else {
|
|
@@ -383,11 +383,11 @@ class SinglyLinkedList {
|
|
|
383
383
|
return this.length === 0;
|
|
384
384
|
}
|
|
385
385
|
/**
|
|
386
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to
|
|
386
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
387
387
|
*/
|
|
388
388
|
clear() {
|
|
389
|
-
this._head =
|
|
390
|
-
this._tail =
|
|
389
|
+
this._head = undefined;
|
|
390
|
+
this._tail = undefined;
|
|
391
391
|
this._length = 0;
|
|
392
392
|
}
|
|
393
393
|
/**
|
|
@@ -424,9 +424,9 @@ class SinglyLinkedList {
|
|
|
424
424
|
reverse() {
|
|
425
425
|
if (!this.head || this.head === this.tail)
|
|
426
426
|
return;
|
|
427
|
-
let prev =
|
|
427
|
+
let prev = undefined;
|
|
428
428
|
let current = this.head;
|
|
429
|
-
let next =
|
|
429
|
+
let next = undefined;
|
|
430
430
|
while (current) {
|
|
431
431
|
next = current.next;
|
|
432
432
|
current.next = prev;
|
|
@@ -447,7 +447,7 @@ class SinglyLinkedList {
|
|
|
447
447
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
448
448
|
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
449
449
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
450
|
-
* the callback function. If no element satisfies the condition, it returns `
|
|
450
|
+
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
451
451
|
*/
|
|
452
452
|
find(callback) {
|
|
453
453
|
let current = this.head;
|
|
@@ -457,7 +457,7 @@ class SinglyLinkedList {
|
|
|
457
457
|
}
|
|
458
458
|
current = current.next;
|
|
459
459
|
}
|
|
460
|
-
return
|
|
460
|
+
return undefined;
|
|
461
461
|
}
|
|
462
462
|
/**
|
|
463
463
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
@@ -493,10 +493,10 @@ class SinglyLinkedList {
|
|
|
493
493
|
* Space Complexity: O(1) - Constant space.
|
|
494
494
|
*
|
|
495
495
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
496
|
-
*
|
|
496
|
+
* undefined.
|
|
497
497
|
* @param {E} value - The value parameter is the value that we want to search for in the linked list.
|
|
498
498
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
499
|
-
* the specified value is found, the function returns `
|
|
499
|
+
* the specified value is found, the function returns `undefined`.
|
|
500
500
|
*/
|
|
501
501
|
getNode(value) {
|
|
502
502
|
let current = this.head;
|
|
@@ -506,7 +506,7 @@ class SinglyLinkedList {
|
|
|
506
506
|
}
|
|
507
507
|
current = current.next;
|
|
508
508
|
}
|
|
509
|
-
return
|
|
509
|
+
return undefined;
|
|
510
510
|
}
|
|
511
511
|
/**
|
|
512
512
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
@@ -25,7 +25,7 @@ class SkipList {
|
|
|
25
25
|
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
26
26
|
*/
|
|
27
27
|
constructor(maxLevel = 16, probability = 0.5) {
|
|
28
|
-
this._head = new SkipListNode(
|
|
28
|
+
this._head = new SkipListNode(undefined, undefined, maxLevel);
|
|
29
29
|
this._level = 0;
|
|
30
30
|
this._maxLevel = maxLevel;
|
|
31
31
|
this._probability = probability;
|
|
@@ -69,7 +69,7 @@ class SkipList {
|
|
|
69
69
|
newNode.forward[i] = update[i].forward[i];
|
|
70
70
|
update[i].forward[i] = newNode;
|
|
71
71
|
}
|
|
72
|
-
if (newNode.forward[0]
|
|
72
|
+
if (!newNode.forward[0]) {
|
|
73
73
|
this._level = Math.max(this.level, newNode.forward.length);
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -140,7 +140,7 @@ class SkipList {
|
|
|
140
140
|
}
|
|
141
141
|
update[i].forward[i] = current.forward[i];
|
|
142
142
|
}
|
|
143
|
-
while (this.level > 0 && this.head.forward[this.level - 1]
|
|
143
|
+
while (this.level > 0 && !this.head.forward[this.level - 1]) {
|
|
144
144
|
this._level--;
|
|
145
145
|
}
|
|
146
146
|
return true;
|
|
@@ -218,7 +218,7 @@ class SkipList {
|
|
|
218
218
|
*/
|
|
219
219
|
lower(key) {
|
|
220
220
|
let current = this.head;
|
|
221
|
-
let lastLess =
|
|
221
|
+
let lastLess = undefined;
|
|
222
222
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
223
223
|
while (current.forward[i] && current.forward[i].key < key) {
|
|
224
224
|
current = current.forward[i];
|
|
@@ -11,8 +11,8 @@ export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
11
11
|
*/
|
|
12
12
|
enqueue(value: E): void;
|
|
13
13
|
/**
|
|
14
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
15
|
-
* @returns The method is returning the element at the front of the queue, or
|
|
14
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
15
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
16
16
|
*/
|
|
17
17
|
dequeue(): E | undefined;
|
|
18
18
|
/**
|
|
@@ -75,7 +75,7 @@ export declare class Queue<E = any> {
|
|
|
75
75
|
*
|
|
76
76
|
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
77
77
|
* necessary to optimize performance.
|
|
78
|
-
* @returns The function `shift()` returns either the first element in the queue or `
|
|
78
|
+
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
79
79
|
*/
|
|
80
80
|
shift(): E | undefined;
|
|
81
81
|
/**
|
|
@@ -86,9 +86,9 @@ export declare class Queue<E = any> {
|
|
|
86
86
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
87
87
|
* Space Complexity: O(1) - no additional space is used.
|
|
88
88
|
*
|
|
89
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
89
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
90
90
|
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
91
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
91
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
92
92
|
*/
|
|
93
93
|
getFirst(): E | undefined;
|
|
94
94
|
/**
|
|
@@ -99,9 +99,9 @@ export declare class Queue<E = any> {
|
|
|
99
99
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
100
100
|
* Space Complexity: O(1) - no additional space is used.
|
|
101
101
|
*
|
|
102
|
-
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
102
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
103
103
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
104
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
104
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
105
105
|
*/
|
|
106
106
|
peek(): E | undefined;
|
|
107
107
|
/**
|
|
@@ -112,9 +112,9 @@ export declare class Queue<E = any> {
|
|
|
112
112
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
113
113
|
* Space Complexity: O(1) - no additional space is used.
|
|
114
114
|
*
|
|
115
|
-
* The `getLast` function returns the last element in an array-like data structure, or
|
|
115
|
+
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
116
116
|
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
117
|
-
* array is empty, it returns `
|
|
117
|
+
* array is empty, it returns `undefined`.
|
|
118
118
|
*/
|
|
119
119
|
getLast(): E | undefined;
|
|
120
120
|
/**
|
|
@@ -125,9 +125,9 @@ export declare class Queue<E = any> {
|
|
|
125
125
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
126
126
|
* Space Complexity: O(1) - no additional space is used.
|
|
127
127
|
*
|
|
128
|
-
* The `peekLast` function returns the last element in an array-like data structure, or
|
|
128
|
+
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
129
129
|
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
130
|
-
* array is empty, it returns `
|
|
130
|
+
* array is empty, it returns `undefined`.
|
|
131
131
|
*/
|
|
132
132
|
peekLast(): E | undefined;
|
|
133
133
|
/**
|
|
@@ -150,8 +150,8 @@ export declare class Queue<E = any> {
|
|
|
150
150
|
* Time Complexity: O(n) - same as shift().
|
|
151
151
|
* Space Complexity: O(1) - same as shift().
|
|
152
152
|
*
|
|
153
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
154
|
-
* @returns The method is returning a value of type E or
|
|
153
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
154
|
+
* @returns The method is returning a value of type E or undefined.
|
|
155
155
|
*/
|
|
156
156
|
dequeue(): E | undefined;
|
|
157
157
|
/**
|
|
@@ -16,8 +16,8 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
|
16
16
|
this.push(value);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
20
|
-
* @returns The method is returning the element at the front of the queue, or
|
|
19
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
20
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
21
21
|
*/
|
|
22
22
|
dequeue() {
|
|
23
23
|
return this.shift();
|
|
@@ -100,7 +100,7 @@ class Queue {
|
|
|
100
100
|
*
|
|
101
101
|
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
102
102
|
* necessary to optimize performance.
|
|
103
|
-
* @returns The function `shift()` returns either the first element in the queue or `
|
|
103
|
+
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
104
104
|
*/
|
|
105
105
|
shift() {
|
|
106
106
|
if (this.size === 0)
|
|
@@ -123,9 +123,9 @@ class Queue {
|
|
|
123
123
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
124
124
|
* Space Complexity: O(1) - no additional space is used.
|
|
125
125
|
*
|
|
126
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
126
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
127
127
|
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
128
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
128
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
129
129
|
*/
|
|
130
130
|
getFirst() {
|
|
131
131
|
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
@@ -138,9 +138,9 @@ class Queue {
|
|
|
138
138
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
139
139
|
* Space Complexity: O(1) - no additional space is used.
|
|
140
140
|
*
|
|
141
|
-
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
141
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
142
142
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
143
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
143
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
144
144
|
*/
|
|
145
145
|
peek() {
|
|
146
146
|
return this.getFirst();
|
|
@@ -153,9 +153,9 @@ class Queue {
|
|
|
153
153
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
154
154
|
* Space Complexity: O(1) - no additional space is used.
|
|
155
155
|
*
|
|
156
|
-
* The `getLast` function returns the last element in an array-like data structure, or
|
|
156
|
+
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
157
157
|
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
158
|
-
* array is empty, it returns `
|
|
158
|
+
* array is empty, it returns `undefined`.
|
|
159
159
|
*/
|
|
160
160
|
getLast() {
|
|
161
161
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
@@ -168,9 +168,9 @@ class Queue {
|
|
|
168
168
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
169
169
|
* Space Complexity: O(1) - no additional space is used.
|
|
170
170
|
*
|
|
171
|
-
* The `peekLast` function returns the last element in an array-like data structure, or
|
|
171
|
+
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
172
172
|
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
173
|
-
* array is empty, it returns `
|
|
173
|
+
* array is empty, it returns `undefined`.
|
|
174
174
|
*/
|
|
175
175
|
peekLast() {
|
|
176
176
|
return this.getLast();
|
|
@@ -197,8 +197,8 @@ class Queue {
|
|
|
197
197
|
* Time Complexity: O(n) - same as shift().
|
|
198
198
|
* Space Complexity: O(1) - same as shift().
|
|
199
199
|
*
|
|
200
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
201
|
-
* @returns The method is returning a value of type E or
|
|
200
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
201
|
+
* @returns The method is returning a value of type E or undefined.
|
|
202
202
|
*/
|
|
203
203
|
dequeue() {
|
|
204
204
|
return this.shift();
|
|
@@ -45,10 +45,10 @@ export declare class Stack<E = any> {
|
|
|
45
45
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
46
46
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
47
47
|
*
|
|
48
|
-
* The `peek` function returns the last element of an array, or
|
|
49
|
-
* @returns The `peek()` function returns the last element of the `_elements` array, or `
|
|
48
|
+
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
49
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
50
50
|
*/
|
|
51
|
-
peek(): E |
|
|
51
|
+
peek(): E | undefined;
|
|
52
52
|
/**
|
|
53
53
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
54
54
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -70,11 +70,11 @@ export declare class Stack<E = any> {
|
|
|
70
70
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
71
71
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
72
72
|
*
|
|
73
|
-
* The `pop` function removes and returns the last element from an array, or returns
|
|
73
|
+
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
74
74
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
75
|
-
* array is empty, it returns `
|
|
75
|
+
* array is empty, it returns `undefined`.
|
|
76
76
|
*/
|
|
77
|
-
pop(): E |
|
|
77
|
+
pop(): E | undefined;
|
|
78
78
|
/**
|
|
79
79
|
* Time Complexity: O(n)
|
|
80
80
|
* Space Complexity: O(n)
|
|
@@ -62,12 +62,12 @@ class Stack {
|
|
|
62
62
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
63
63
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
64
64
|
*
|
|
65
|
-
* The `peek` function returns the last element of an array, or
|
|
66
|
-
* @returns The `peek()` function returns the last element of the `_elements` array, or `
|
|
65
|
+
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
66
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
67
67
|
*/
|
|
68
68
|
peek() {
|
|
69
69
|
if (this.isEmpty())
|
|
70
|
-
return
|
|
70
|
+
return undefined;
|
|
71
71
|
return this.elements[this.elements.length - 1];
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
@@ -94,14 +94,14 @@ class Stack {
|
|
|
94
94
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
95
95
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
96
96
|
*
|
|
97
|
-
* The `pop` function removes and returns the last element from an array, or returns
|
|
97
|
+
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
98
98
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
99
|
-
* array is empty, it returns `
|
|
99
|
+
* array is empty, it returns `undefined`.
|
|
100
100
|
*/
|
|
101
101
|
pop() {
|
|
102
102
|
if (this.isEmpty())
|
|
103
|
-
return
|
|
104
|
-
return this.elements.pop() ||
|
|
103
|
+
return undefined;
|
|
104
|
+
return this.elements.pop() || undefined;
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
107
107
|
* Time Complexity: O(n)
|
|
@@ -20,6 +20,8 @@ export declare class TrieNode {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare class Trie {
|
|
22
22
|
constructor(words?: string[], caseSensitive?: boolean);
|
|
23
|
+
protected _size: number;
|
|
24
|
+
get size(): number;
|
|
23
25
|
protected _caseSensitive: boolean;
|
|
24
26
|
get caseSensitive(): boolean;
|
|
25
27
|
protected _root: TrieNode;
|
|
@@ -145,6 +147,7 @@ export declare class Trie {
|
|
|
145
147
|
filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
|
|
146
148
|
map(callback: (word: string, index: number, trie: this) => string): Trie;
|
|
147
149
|
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
|
|
150
|
+
print(): void;
|
|
148
151
|
/**
|
|
149
152
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
150
153
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -27,12 +27,16 @@ class Trie {
|
|
|
27
27
|
constructor(words, caseSensitive = true) {
|
|
28
28
|
this._root = new TrieNode('');
|
|
29
29
|
this._caseSensitive = caseSensitive;
|
|
30
|
+
this._size = 0;
|
|
30
31
|
if (words) {
|
|
31
|
-
for (const
|
|
32
|
-
this.add(
|
|
32
|
+
for (const word of words) {
|
|
33
|
+
this.add(word);
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
}
|
|
37
|
+
get size() {
|
|
38
|
+
return this._size;
|
|
39
|
+
}
|
|
36
40
|
get caseSensitive() {
|
|
37
41
|
return this._caseSensitive;
|
|
38
42
|
}
|
|
@@ -54,6 +58,7 @@ class Trie {
|
|
|
54
58
|
add(word) {
|
|
55
59
|
word = this._caseProcess(word);
|
|
56
60
|
let cur = this.root;
|
|
61
|
+
let isNewWord = false;
|
|
57
62
|
for (const c of word) {
|
|
58
63
|
let nodeC = cur.children.get(c);
|
|
59
64
|
if (!nodeC) {
|
|
@@ -62,8 +67,12 @@ class Trie {
|
|
|
62
67
|
}
|
|
63
68
|
cur = nodeC;
|
|
64
69
|
}
|
|
65
|
-
cur.isEnd
|
|
66
|
-
|
|
70
|
+
if (!cur.isEnd) {
|
|
71
|
+
isNewWord = true;
|
|
72
|
+
cur.isEnd = true;
|
|
73
|
+
this._size++;
|
|
74
|
+
}
|
|
75
|
+
return isNewWord;
|
|
67
76
|
}
|
|
68
77
|
/**
|
|
69
78
|
* Time Complexity: O(M), where M is the length of the input word.
|
|
@@ -130,6 +139,9 @@ class Trie {
|
|
|
130
139
|
return false;
|
|
131
140
|
};
|
|
132
141
|
dfs(this.root, 0);
|
|
142
|
+
if (isDeleted) {
|
|
143
|
+
this._size--;
|
|
144
|
+
}
|
|
133
145
|
return isDeleted;
|
|
134
146
|
}
|
|
135
147
|
/**
|
|
@@ -352,6 +364,9 @@ class Trie {
|
|
|
352
364
|
}
|
|
353
365
|
return accumulator;
|
|
354
366
|
}
|
|
367
|
+
print() {
|
|
368
|
+
console.log([...this]);
|
|
369
|
+
}
|
|
355
370
|
/**
|
|
356
371
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
357
372
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey,
|
|
2
|
+
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
4
4
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
|
|
7
|
+
addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
9
9
|
}
|
package/dist/types/common.d.ts
CHANGED
|
@@ -19,4 +19,9 @@ export type BinaryTreePrintOptions = {
|
|
|
19
19
|
isShowNull?: boolean;
|
|
20
20
|
isShowRedBlackNIL?: boolean;
|
|
21
21
|
};
|
|
22
|
-
export type
|
|
22
|
+
export type BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
|
|
23
|
+
export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
|
|
24
|
+
export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>;
|
|
25
|
+
export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>;
|
|
26
|
+
export type BTNodePureKeyOrNode<N> = BTNKey | N;
|
|
27
|
+
export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
|
|
@@ -2,9 +2,9 @@ export type VertexKey = string | number;
|
|
|
2
2
|
export type DijkstraResult<V> = {
|
|
3
3
|
distMap: Map<V, number>;
|
|
4
4
|
distPaths?: Map<V, V[]>;
|
|
5
|
-
preMap: Map<V, V |
|
|
5
|
+
preMap: Map<V, V | undefined>;
|
|
6
6
|
seen: Set<V>;
|
|
7
7
|
paths: V[][];
|
|
8
8
|
minDist: number;
|
|
9
9
|
minPath: V[];
|
|
10
|
-
} |
|
|
10
|
+
} | undefined;
|
|
@@ -4,8 +4,7 @@ export type HashMapLinkedNode<K, V> = {
|
|
|
4
4
|
next: HashMapLinkedNode<K, V>;
|
|
5
5
|
prev: HashMapLinkedNode<K, V>;
|
|
6
6
|
};
|
|
7
|
-
export type HashMapOptions<K
|
|
8
|
-
elements: Iterable<[K, V]>;
|
|
7
|
+
export type HashMapOptions<K> = {
|
|
9
8
|
hashFn: (key: K) => string;
|
|
10
9
|
objHashFn: (key: K) => object;
|
|
11
10
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "queue-typed",
|
|
3
|
-
"version": "1.47.
|
|
3
|
+
"version": "1.47.8",
|
|
4
4
|
"description": "Queue, ArrayQueue. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -115,6 +115,6 @@
|
|
|
115
115
|
"typescript": "^4.9.5"
|
|
116
116
|
},
|
|
117
117
|
"dependencies": {
|
|
118
|
-
"data-structure-typed": "^1.47.
|
|
118
|
+
"data-structure-typed": "^1.47.8"
|
|
119
119
|
}
|
|
120
120
|
}
|