data-structure-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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +188 -32
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/cjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- 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 +21 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +13 -13
- package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/cjs/data-structures/stack/stack.js +7 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/mjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +13 -13
- package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/mjs/data-structures/stack/stack.js +7 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +583 -627
- 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/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
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +158 -2
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +10 -10
- package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -9,13 +9,13 @@ export class SinglyLinkedListNode {
|
|
|
9
9
|
value;
|
|
10
10
|
next;
|
|
11
11
|
/**
|
|
12
|
-
* The constructor function initializes an instance of a class with a given value and sets the next property to
|
|
12
|
+
* The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
|
|
13
13
|
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
14
14
|
* will be stored in the node of a linked list.
|
|
15
15
|
*/
|
|
16
16
|
constructor(value) {
|
|
17
17
|
this.value = value;
|
|
18
|
-
this.next =
|
|
18
|
+
this.next = undefined;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
export class SinglyLinkedList {
|
|
@@ -23,8 +23,8 @@ export class SinglyLinkedList {
|
|
|
23
23
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
24
24
|
*/
|
|
25
25
|
constructor(elements) {
|
|
26
|
-
this._head =
|
|
27
|
-
this._tail =
|
|
26
|
+
this._head = undefined;
|
|
27
|
+
this._tail = undefined;
|
|
28
28
|
this._length = 0;
|
|
29
29
|
if (elements) {
|
|
30
30
|
for (const el of elements)
|
|
@@ -113,15 +113,15 @@ export class SinglyLinkedList {
|
|
|
113
113
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
114
114
|
* pointers accordingly.
|
|
115
115
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
116
|
-
* the linked list is empty, it returns `
|
|
116
|
+
* the linked list is empty, it returns `undefined`.
|
|
117
117
|
*/
|
|
118
118
|
pop() {
|
|
119
119
|
if (!this.head)
|
|
120
120
|
return undefined;
|
|
121
121
|
if (this.head === this.tail) {
|
|
122
122
|
const value = this.head.value;
|
|
123
|
-
this._head =
|
|
124
|
-
this._tail =
|
|
123
|
+
this._head = undefined;
|
|
124
|
+
this._tail = undefined;
|
|
125
125
|
this._length--;
|
|
126
126
|
return value;
|
|
127
127
|
}
|
|
@@ -130,7 +130,7 @@ export class SinglyLinkedList {
|
|
|
130
130
|
current = current.next;
|
|
131
131
|
}
|
|
132
132
|
const value = this.tail.value;
|
|
133
|
-
current.next =
|
|
133
|
+
current.next = undefined;
|
|
134
134
|
this._tail = current;
|
|
135
135
|
this._length--;
|
|
136
136
|
return value;
|
|
@@ -146,7 +146,7 @@ export class SinglyLinkedList {
|
|
|
146
146
|
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
147
147
|
* pointers accordingly.
|
|
148
148
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
149
|
-
* the linked list is empty, it returns `
|
|
149
|
+
* the linked list is empty, it returns `undefined`.
|
|
150
150
|
*/
|
|
151
151
|
popLast() {
|
|
152
152
|
return this.pop();
|
|
@@ -231,11 +231,11 @@ export class SinglyLinkedList {
|
|
|
231
231
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
232
232
|
* Space Complexity: O(1) - Constant space.
|
|
233
233
|
*
|
|
234
|
-
* The function `getAt` returns the value at a specified index in a linked list, or
|
|
234
|
+
* The function `getAt` returns the value at a specified index in a linked list, or undefined if the index is out of range.
|
|
235
235
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
236
236
|
* retrieve from the list.
|
|
237
|
-
* @returns The method `getAt(index: number): E |
|
|
238
|
-
* `
|
|
237
|
+
* @returns The method `getAt(index: number): E | undefined` returns the value at the specified index in the linked list, or
|
|
238
|
+
* `undefined` if the index is out of bounds.
|
|
239
239
|
*/
|
|
240
240
|
getAt(index) {
|
|
241
241
|
if (index < 0 || index >= this.length)
|
|
@@ -258,7 +258,7 @@ export class SinglyLinkedList {
|
|
|
258
258
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
259
259
|
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
260
260
|
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
|
|
261
|
-
* specified index exists, or `
|
|
261
|
+
* specified index exists, or `undefined` if the index is out of bounds.
|
|
262
262
|
*/
|
|
263
263
|
getNodeAt(index) {
|
|
264
264
|
let current = this.head;
|
|
@@ -278,7 +278,7 @@ export class SinglyLinkedList {
|
|
|
278
278
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
279
279
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
280
280
|
* data structure. It is of type number.
|
|
281
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `
|
|
281
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
282
282
|
* bounds.
|
|
283
283
|
*/
|
|
284
284
|
deleteAt(index) {
|
|
@@ -318,13 +318,13 @@ export class SinglyLinkedList {
|
|
|
318
318
|
else {
|
|
319
319
|
value = valueOrNode;
|
|
320
320
|
}
|
|
321
|
-
let current = this.head, prev =
|
|
321
|
+
let current = this.head, prev = undefined;
|
|
322
322
|
while (current) {
|
|
323
323
|
if (current.value === value) {
|
|
324
|
-
if (prev ===
|
|
324
|
+
if (prev === undefined) {
|
|
325
325
|
this._head = current.next;
|
|
326
326
|
if (current === this.tail) {
|
|
327
|
-
this._tail =
|
|
327
|
+
this._tail = undefined;
|
|
328
328
|
}
|
|
329
329
|
}
|
|
330
330
|
else {
|
|
@@ -384,11 +384,11 @@ export class SinglyLinkedList {
|
|
|
384
384
|
return this.length === 0;
|
|
385
385
|
}
|
|
386
386
|
/**
|
|
387
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to
|
|
387
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
388
388
|
*/
|
|
389
389
|
clear() {
|
|
390
|
-
this._head =
|
|
391
|
-
this._tail =
|
|
390
|
+
this._head = undefined;
|
|
391
|
+
this._tail = undefined;
|
|
392
392
|
this._length = 0;
|
|
393
393
|
}
|
|
394
394
|
/**
|
|
@@ -425,9 +425,9 @@ export class SinglyLinkedList {
|
|
|
425
425
|
reverse() {
|
|
426
426
|
if (!this.head || this.head === this.tail)
|
|
427
427
|
return;
|
|
428
|
-
let prev =
|
|
428
|
+
let prev = undefined;
|
|
429
429
|
let current = this.head;
|
|
430
|
-
let next =
|
|
430
|
+
let next = undefined;
|
|
431
431
|
while (current) {
|
|
432
432
|
next = current.next;
|
|
433
433
|
current.next = prev;
|
|
@@ -448,7 +448,7 @@ export class SinglyLinkedList {
|
|
|
448
448
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
449
449
|
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
450
450
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
451
|
-
* the callback function. If no element satisfies the condition, it returns `
|
|
451
|
+
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
452
452
|
*/
|
|
453
453
|
find(callback) {
|
|
454
454
|
let current = this.head;
|
|
@@ -458,7 +458,7 @@ export class SinglyLinkedList {
|
|
|
458
458
|
}
|
|
459
459
|
current = current.next;
|
|
460
460
|
}
|
|
461
|
-
return
|
|
461
|
+
return undefined;
|
|
462
462
|
}
|
|
463
463
|
/**
|
|
464
464
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
@@ -494,10 +494,10 @@ export class SinglyLinkedList {
|
|
|
494
494
|
* Space Complexity: O(1) - Constant space.
|
|
495
495
|
*
|
|
496
496
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
497
|
-
*
|
|
497
|
+
* undefined.
|
|
498
498
|
* @param {E} value - The value parameter is the value that we want to search for in the linked list.
|
|
499
499
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
500
|
-
* the specified value is found, the function returns `
|
|
500
|
+
* the specified value is found, the function returns `undefined`.
|
|
501
501
|
*/
|
|
502
502
|
getNode(value) {
|
|
503
503
|
let current = this.head;
|
|
@@ -507,7 +507,7 @@ export class SinglyLinkedList {
|
|
|
507
507
|
}
|
|
508
508
|
current = current.next;
|
|
509
509
|
}
|
|
510
|
-
return
|
|
510
|
+
return undefined;
|
|
511
511
|
}
|
|
512
512
|
/**
|
|
513
513
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
@@ -24,7 +24,7 @@ export class SkipList {
|
|
|
24
24
|
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
25
25
|
*/
|
|
26
26
|
constructor(maxLevel = 16, probability = 0.5) {
|
|
27
|
-
this._head = new SkipListNode(
|
|
27
|
+
this._head = new SkipListNode(undefined, undefined, maxLevel);
|
|
28
28
|
this._level = 0;
|
|
29
29
|
this._maxLevel = maxLevel;
|
|
30
30
|
this._probability = probability;
|
|
@@ -72,7 +72,7 @@ export class SkipList {
|
|
|
72
72
|
newNode.forward[i] = update[i].forward[i];
|
|
73
73
|
update[i].forward[i] = newNode;
|
|
74
74
|
}
|
|
75
|
-
if (newNode.forward[0]
|
|
75
|
+
if (!newNode.forward[0]) {
|
|
76
76
|
this._level = Math.max(this.level, newNode.forward.length);
|
|
77
77
|
}
|
|
78
78
|
}
|
|
@@ -143,7 +143,7 @@ export class SkipList {
|
|
|
143
143
|
}
|
|
144
144
|
update[i].forward[i] = current.forward[i];
|
|
145
145
|
}
|
|
146
|
-
while (this.level > 0 && this.head.forward[this.level - 1]
|
|
146
|
+
while (this.level > 0 && !this.head.forward[this.level - 1]) {
|
|
147
147
|
this._level--;
|
|
148
148
|
}
|
|
149
149
|
return true;
|
|
@@ -221,7 +221,7 @@ export class SkipList {
|
|
|
221
221
|
*/
|
|
222
222
|
lower(key) {
|
|
223
223
|
let current = this.head;
|
|
224
|
-
let lastLess =
|
|
224
|
+
let lastLess = undefined;
|
|
225
225
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
226
226
|
while (current.forward[i] && current.forward[i].key < key) {
|
|
227
227
|
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
|
/**
|
|
@@ -13,8 +13,8 @@ export class LinkedListQueue extends SinglyLinkedList {
|
|
|
13
13
|
this.push(value);
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
17
|
-
* @returns The method is returning the element at the front of the queue, or
|
|
16
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
17
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
18
18
|
*/
|
|
19
19
|
dequeue() {
|
|
20
20
|
return this.shift();
|
|
@@ -97,7 +97,7 @@ export class Queue {
|
|
|
97
97
|
*
|
|
98
98
|
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
99
99
|
* necessary to optimize performance.
|
|
100
|
-
* @returns The function `shift()` returns either the first element in the queue or `
|
|
100
|
+
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
101
101
|
*/
|
|
102
102
|
shift() {
|
|
103
103
|
if (this.size === 0)
|
|
@@ -120,9 +120,9 @@ export class Queue {
|
|
|
120
120
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
121
121
|
* Space Complexity: O(1) - no additional space is used.
|
|
122
122
|
*
|
|
123
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
123
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
124
124
|
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
125
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
125
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
126
126
|
*/
|
|
127
127
|
getFirst() {
|
|
128
128
|
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
@@ -135,9 +135,9 @@ export class Queue {
|
|
|
135
135
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
136
136
|
* Space Complexity: O(1) - no additional space is used.
|
|
137
137
|
*
|
|
138
|
-
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
138
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
139
139
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
140
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
140
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
141
141
|
*/
|
|
142
142
|
peek() {
|
|
143
143
|
return this.getFirst();
|
|
@@ -150,9 +150,9 @@ export class Queue {
|
|
|
150
150
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
151
151
|
* Space Complexity: O(1) - no additional space is used.
|
|
152
152
|
*
|
|
153
|
-
* The `getLast` function returns the last element in an array-like data structure, or
|
|
153
|
+
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
154
154
|
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
155
|
-
* array is empty, it returns `
|
|
155
|
+
* array is empty, it returns `undefined`.
|
|
156
156
|
*/
|
|
157
157
|
getLast() {
|
|
158
158
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
@@ -165,9 +165,9 @@ export class Queue {
|
|
|
165
165
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
166
166
|
* Space Complexity: O(1) - no additional space is used.
|
|
167
167
|
*
|
|
168
|
-
* The `peekLast` function returns the last element in an array-like data structure, or
|
|
168
|
+
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
169
169
|
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
170
|
-
* array is empty, it returns `
|
|
170
|
+
* array is empty, it returns `undefined`.
|
|
171
171
|
*/
|
|
172
172
|
peekLast() {
|
|
173
173
|
return this.getLast();
|
|
@@ -194,8 +194,8 @@ export class Queue {
|
|
|
194
194
|
* Time Complexity: O(n) - same as shift().
|
|
195
195
|
* Space Complexity: O(1) - same as shift().
|
|
196
196
|
*
|
|
197
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
198
|
-
* @returns The method is returning a value of type E or
|
|
197
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
198
|
+
* @returns The method is returning a value of type E or undefined.
|
|
199
199
|
*/
|
|
200
200
|
dequeue() {
|
|
201
201
|
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)
|
|
@@ -60,12 +60,12 @@ export class Stack {
|
|
|
60
60
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
61
61
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
62
62
|
*
|
|
63
|
-
* The `peek` function returns the last element of an array, or
|
|
64
|
-
* @returns The `peek()` function returns the last element of the `_elements` array, or `
|
|
63
|
+
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
64
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
65
65
|
*/
|
|
66
66
|
peek() {
|
|
67
67
|
if (this.isEmpty())
|
|
68
|
-
return
|
|
68
|
+
return undefined;
|
|
69
69
|
return this.elements[this.elements.length - 1];
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
@@ -92,14 +92,14 @@ export class Stack {
|
|
|
92
92
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
93
93
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
94
94
|
*
|
|
95
|
-
* The `pop` function removes and returns the last element from an array, or returns
|
|
95
|
+
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
96
96
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
97
|
-
* array is empty, it returns `
|
|
97
|
+
* array is empty, it returns `undefined`.
|
|
98
98
|
*/
|
|
99
99
|
pop() {
|
|
100
100
|
if (this.isEmpty())
|
|
101
|
-
return
|
|
102
|
-
return this.elements.pop() ||
|
|
101
|
+
return undefined;
|
|
102
|
+
return this.elements.pop() || undefined;
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
105
|
* 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.
|
|
@@ -26,12 +26,17 @@ export class Trie {
|
|
|
26
26
|
constructor(words, caseSensitive = true) {
|
|
27
27
|
this._root = new TrieNode('');
|
|
28
28
|
this._caseSensitive = caseSensitive;
|
|
29
|
+
this._size = 0;
|
|
29
30
|
if (words) {
|
|
30
|
-
for (const
|
|
31
|
-
this.add(
|
|
31
|
+
for (const word of words) {
|
|
32
|
+
this.add(word);
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
}
|
|
36
|
+
_size;
|
|
37
|
+
get size() {
|
|
38
|
+
return this._size;
|
|
39
|
+
}
|
|
35
40
|
_caseSensitive;
|
|
36
41
|
get caseSensitive() {
|
|
37
42
|
return this._caseSensitive;
|
|
@@ -55,6 +60,7 @@ export class Trie {
|
|
|
55
60
|
add(word) {
|
|
56
61
|
word = this._caseProcess(word);
|
|
57
62
|
let cur = this.root;
|
|
63
|
+
let isNewWord = false;
|
|
58
64
|
for (const c of word) {
|
|
59
65
|
let nodeC = cur.children.get(c);
|
|
60
66
|
if (!nodeC) {
|
|
@@ -63,8 +69,12 @@ export class Trie {
|
|
|
63
69
|
}
|
|
64
70
|
cur = nodeC;
|
|
65
71
|
}
|
|
66
|
-
cur.isEnd
|
|
67
|
-
|
|
72
|
+
if (!cur.isEnd) {
|
|
73
|
+
isNewWord = true;
|
|
74
|
+
cur.isEnd = true;
|
|
75
|
+
this._size++;
|
|
76
|
+
}
|
|
77
|
+
return isNewWord;
|
|
68
78
|
}
|
|
69
79
|
/**
|
|
70
80
|
* Time Complexity: O(M), where M is the length of the input word.
|
|
@@ -131,6 +141,9 @@ export class Trie {
|
|
|
131
141
|
return false;
|
|
132
142
|
};
|
|
133
143
|
dfs(this.root, 0);
|
|
144
|
+
if (isDeleted) {
|
|
145
|
+
this._size--;
|
|
146
|
+
}
|
|
134
147
|
return isDeleted;
|
|
135
148
|
}
|
|
136
149
|
/**
|
|
@@ -353,6 +366,9 @@ export class Trie {
|
|
|
353
366
|
}
|
|
354
367
|
return accumulator;
|
|
355
368
|
}
|
|
369
|
+
print() {
|
|
370
|
+
console.log([...this]);
|
|
371
|
+
}
|
|
356
372
|
/**
|
|
357
373
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
358
374
|
* 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
|
}
|
|
@@ -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
|
};
|