stack-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.
Files changed (56) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
  2. package/dist/data-structures/binary-tree/avl-tree.js +45 -36
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
  4. package/dist/data-structures/binary-tree/binary-tree.js +133 -119
  5. package/dist/data-structures/binary-tree/bst.d.ts +53 -44
  6. package/dist/data-structures/binary-tree/bst.js +137 -154
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
  8. package/dist/data-structures/binary-tree/rb-tree.js +70 -33
  9. package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
  10. package/dist/data-structures/binary-tree/segment-tree.js +7 -7
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  12. package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
  13. package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
  14. package/dist/data-structures/graph/abstract-graph.js +30 -30
  15. package/dist/data-structures/graph/directed-graph.d.ts +24 -24
  16. package/dist/data-structures/graph/directed-graph.js +28 -28
  17. package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
  18. package/dist/data-structures/graph/undirected-graph.js +18 -18
  19. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  20. package/dist/data-structures/hash/hash-map.js +5 -8
  21. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
  22. package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
  23. package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
  24. package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
  25. package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
  26. package/dist/data-structures/queue/queue.d.ts +13 -13
  27. package/dist/data-structures/queue/queue.js +13 -13
  28. package/dist/data-structures/stack/stack.d.ts +6 -6
  29. package/dist/data-structures/stack/stack.js +7 -7
  30. package/dist/data-structures/trie/trie.d.ts +3 -0
  31. package/dist/data-structures/trie/trie.js +19 -4
  32. package/dist/interfaces/binary-tree.d.ts +3 -3
  33. package/dist/types/common.d.ts +6 -1
  34. package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
  35. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  36. package/package.json +2 -2
  37. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  38. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  39. package/src/data-structures/binary-tree/bst.ts +157 -154
  40. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  41. package/src/data-structures/binary-tree/segment-tree.ts +10 -10
  42. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  43. package/src/data-structures/graph/abstract-graph.ts +46 -46
  44. package/src/data-structures/graph/directed-graph.ts +40 -40
  45. package/src/data-structures/graph/undirected-graph.ts +26 -26
  46. package/src/data-structures/hash/hash-map.ts +8 -8
  47. package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
  48. package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
  49. package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
  50. package/src/data-structures/queue/queue.ts +13 -13
  51. package/src/data-structures/stack/stack.ts +9 -9
  52. package/src/data-structures/trie/trie.ts +23 -4
  53. package/src/interfaces/binary-tree.ts +3 -3
  54. package/src/types/common.ts +11 -1
  55. package/src/types/data-structures/graph/abstract-graph.ts +2 -2
  56. package/src/types/data-structures/hash/hash-map.ts +1 -2
@@ -7,16 +7,16 @@
7
7
  */
8
8
  export class SinglyLinkedListNode<E = any> {
9
9
  value: E;
10
- next: SinglyLinkedListNode<E> | null;
10
+ next: SinglyLinkedListNode<E> | undefined;
11
11
 
12
12
  /**
13
- * The constructor function initializes an instance of a class with a given value and sets the next property to null.
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: E) {
18
18
  this.value = value;
19
- this.next = null;
19
+ this.next = undefined;
20
20
  }
21
21
  }
22
22
 
@@ -25,8 +25,8 @@ export class SinglyLinkedList<E = any> {
25
25
  * The constructor initializes the linked list with an empty head, tail, and length.
26
26
  */
27
27
  constructor(elements?: Iterable<E>) {
28
- this._head = null;
29
- this._tail = null;
28
+ this._head = undefined;
29
+ this._tail = undefined;
30
30
  this._length = 0;
31
31
  if (elements) {
32
32
  for (const el of elements)
@@ -34,15 +34,15 @@ export class SinglyLinkedList<E = any> {
34
34
  }
35
35
  }
36
36
 
37
- protected _head: SinglyLinkedListNode<E> | null;
37
+ protected _head: SinglyLinkedListNode<E> | undefined;
38
38
 
39
- get head(): SinglyLinkedListNode<E> | null {
39
+ get head(): SinglyLinkedListNode<E> | undefined {
40
40
  return this._head;
41
41
  }
42
42
 
43
- protected _tail: SinglyLinkedListNode<E> | null;
43
+ protected _tail: SinglyLinkedListNode<E> | undefined;
44
44
 
45
- get tail(): SinglyLinkedListNode<E> | null {
45
+ get tail(): SinglyLinkedListNode<E> | undefined {
46
46
  return this._tail;
47
47
  }
48
48
 
@@ -128,14 +128,14 @@ export class SinglyLinkedList<E = any> {
128
128
  * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
129
129
  * pointers accordingly.
130
130
  * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
131
- * the linked list is empty, it returns `null`.
131
+ * the linked list is empty, it returns `undefined`.
132
132
  */
133
133
  pop(): E | undefined {
134
134
  if (!this.head) return undefined;
135
135
  if (this.head === this.tail) {
136
136
  const value = this.head.value;
137
- this._head = null;
138
- this._tail = null;
137
+ this._head = undefined;
138
+ this._tail = undefined;
139
139
  this._length--;
140
140
  return value;
141
141
  }
@@ -145,7 +145,7 @@ export class SinglyLinkedList<E = any> {
145
145
  current = current.next!;
146
146
  }
147
147
  const value = this.tail!.value;
148
- current.next = null;
148
+ current.next = undefined;
149
149
  this._tail = current;
150
150
  this._length--;
151
151
  return value;
@@ -163,7 +163,7 @@ export class SinglyLinkedList<E = any> {
163
163
  * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
164
164
  * pointers accordingly.
165
165
  * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
166
- * the linked list is empty, it returns `null`.
166
+ * the linked list is empty, it returns `undefined`.
167
167
  */
168
168
  popLast(): E | undefined {
169
169
  return this.pop();
@@ -256,11 +256,11 @@ export class SinglyLinkedList<E = any> {
256
256
  * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
257
257
  * Space Complexity: O(1) - Constant space.
258
258
  *
259
- * The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
259
+ * The function `getAt` returns the value at a specified index in a linked list, or undefined if the index is out of range.
260
260
  * @param {number} index - The index parameter is a number that represents the position of the element we want to
261
261
  * retrieve from the list.
262
- * @returns The method `getAt(index: number): E | null` returns the value at the specified index in the linked list, or
263
- * `null` if the index is out of bounds.
262
+ * @returns The method `getAt(index: number): E | undefined` returns the value at the specified index in the linked list, or
263
+ * `undefined` if the index is out of bounds.
264
264
  */
265
265
  getAt(index: number): E | undefined {
266
266
  if (index < 0 || index >= this.length) return undefined;
@@ -284,9 +284,9 @@ export class SinglyLinkedList<E = any> {
284
284
  * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
285
285
  * retrieve from the linked list. It indicates the zero-based index of the node we want to access.
286
286
  * @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
287
- * specified index exists, or `null` if the index is out of bounds.
287
+ * specified index exists, or `undefined` if the index is out of bounds.
288
288
  */
289
- getNodeAt(index: number): SinglyLinkedListNode<E> | null {
289
+ getNodeAt(index: number): SinglyLinkedListNode<E> | undefined {
290
290
  let current = this.head;
291
291
  for (let i = 0; i < index; i++) {
292
292
  current = current!.next;
@@ -306,7 +306,7 @@ export class SinglyLinkedList<E = any> {
306
306
  * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
307
307
  * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
308
308
  * data structure. It is of type number.
309
- * @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
309
+ * @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
310
310
  * bounds.
311
311
  */
312
312
  deleteAt(index: number): E | undefined {
@@ -336,7 +336,7 @@ export class SinglyLinkedList<E = any> {
336
336
  * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
337
337
  * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
338
338
  */
339
- delete(valueOrNode: E | SinglyLinkedListNode<E> | null | undefined): boolean {
339
+ delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined | undefined): boolean {
340
340
  if (!valueOrNode) return false;
341
341
  let value: E;
342
342
  if (valueOrNode instanceof SinglyLinkedListNode) {
@@ -345,14 +345,14 @@ export class SinglyLinkedList<E = any> {
345
345
  value = valueOrNode;
346
346
  }
347
347
  let current = this.head,
348
- prev = null;
348
+ prev = undefined;
349
349
 
350
350
  while (current) {
351
351
  if (current.value === value) {
352
- if (prev === null) {
352
+ if (prev === undefined) {
353
353
  this._head = current.next;
354
354
  if (current === this.tail) {
355
- this._tail = null;
355
+ this._tail = undefined;
356
356
  }
357
357
  } else {
358
358
  prev.next = current.next;
@@ -416,11 +416,11 @@ export class SinglyLinkedList<E = any> {
416
416
  }
417
417
 
418
418
  /**
419
- * The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
419
+ * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
420
420
  */
421
421
  clear(): void {
422
- this._head = null;
423
- this._tail = null;
422
+ this._head = undefined;
423
+ this._tail = undefined;
424
424
  this._length = 0;
425
425
  }
426
426
 
@@ -461,9 +461,9 @@ export class SinglyLinkedList<E = any> {
461
461
  reverse(): void {
462
462
  if (!this.head || this.head === this.tail) return;
463
463
 
464
- let prev: SinglyLinkedListNode<E> | null = null;
465
- let current: SinglyLinkedListNode<E> | null = this.head;
466
- let next: SinglyLinkedListNode<E> | null = null;
464
+ let prev: SinglyLinkedListNode<E> | undefined = undefined;
465
+ let current: SinglyLinkedListNode<E> | undefined = this.head;
466
+ let next: SinglyLinkedListNode<E> | undefined = undefined;
467
467
 
468
468
  while (current) {
469
469
  next = current.next;
@@ -488,9 +488,9 @@ export class SinglyLinkedList<E = any> {
488
488
  * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
489
489
  * function is used to determine whether a particular value in the linked list satisfies a certain condition.
490
490
  * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
491
- * the callback function. If no element satisfies the condition, it returns `null`.
491
+ * the callback function. If no element satisfies the condition, it returns `undefined`.
492
492
  */
493
- find(callback: (value: E) => boolean): E | null {
493
+ find(callback: (value: E) => boolean): E | undefined {
494
494
  let current = this.head;
495
495
  while (current) {
496
496
  if (callback(current.value)) {
@@ -498,7 +498,7 @@ export class SinglyLinkedList<E = any> {
498
498
  }
499
499
  current = current.next;
500
500
  }
501
- return null;
501
+ return undefined;
502
502
  }
503
503
 
504
504
  /**
@@ -540,12 +540,12 @@ export class SinglyLinkedList<E = any> {
540
540
  * Space Complexity: O(1) - Constant space.
541
541
  *
542
542
  * The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
543
- * null.
543
+ * undefined.
544
544
  * @param {E} value - The value parameter is the value that we want to search for in the linked list.
545
545
  * @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
546
- * the specified value is found, the function returns `null`.
546
+ * the specified value is found, the function returns `undefined`.
547
547
  */
548
- getNode(value: E): SinglyLinkedListNode<E> | null {
548
+ getNode(value: E): SinglyLinkedListNode<E> | undefined {
549
549
  let current = this.head;
550
550
 
551
551
  while (current) {
@@ -555,7 +555,7 @@ export class SinglyLinkedList<E = any> {
555
555
  current = current.next;
556
556
  }
557
557
 
558
- return null;
558
+ return undefined;
559
559
  }
560
560
 
561
561
  /**
@@ -620,7 +620,7 @@ export class SinglyLinkedList<E = any> {
620
620
  * existing value or node, and false if the existing value or node was not found in the linked list.
621
621
  */
622
622
  insertAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean {
623
- let existingNode: E | SinglyLinkedListNode<E> | null;
623
+ let existingNode: E | SinglyLinkedListNode<E> | undefined;
624
624
 
625
625
  if (existingValueOrNode instanceof SinglyLinkedListNode) {
626
626
  existingNode = existingValueOrNode;
@@ -27,7 +27,7 @@ export class SkipList<K, V> {
27
27
  * level in the skip list. It is used to determine the height of each node in the skip list.
28
28
  */
29
29
  constructor(maxLevel = 16, probability = 0.5) {
30
- this._head = new SkipListNode<K, V>(null as any, null as any, maxLevel);
30
+ this._head = new SkipListNode<K, V>(undefined as any, undefined as any, maxLevel);
31
31
  this._level = 0;
32
32
  this._maxLevel = maxLevel;
33
33
  this._probability = probability;
@@ -88,7 +88,7 @@ export class SkipList<K, V> {
88
88
  update[i].forward[i] = newNode;
89
89
  }
90
90
 
91
- if (newNode.forward[0] !== null) {
91
+ if (!newNode.forward[0]) {
92
92
  this._level = Math.max(this.level, newNode.forward.length);
93
93
  }
94
94
  }
@@ -172,7 +172,7 @@ export class SkipList<K, V> {
172
172
  }
173
173
  update[i].forward[i] = current.forward[i];
174
174
  }
175
- while (this.level > 0 && this.head.forward[this.level - 1] === null) {
175
+ while (this.level > 0 && !this.head.forward[this.level - 1]) {
176
176
  this._level--;
177
177
  }
178
178
  return true;
@@ -259,7 +259,7 @@ export class SkipList<K, V> {
259
259
  */
260
260
  lower(key: K): V | undefined {
261
261
  let current = this.head;
262
- let lastLess = null;
262
+ let lastLess = undefined;
263
263
 
264
264
  for (let i = this.level - 1; i >= 0; i--) {
265
265
  while (current.forward[i] && current.forward[i].key < key) {
@@ -15,8 +15,8 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
15
15
  }
16
16
 
17
17
  /**
18
- * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
19
- * @returns The method is returning the element at the front of the queue, or null if the queue is empty.
18
+ * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
19
+ * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
20
20
  */
21
21
  dequeue(): E | undefined {
22
22
  return this.shift();
@@ -112,7 +112,7 @@ export class Queue<E = any> {
112
112
  *
113
113
  * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
114
114
  * necessary to optimize performance.
115
- * @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
115
+ * @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
116
116
  */
117
117
  shift(): E | undefined {
118
118
  if (this.size === 0) return undefined;
@@ -138,9 +138,9 @@ export class Queue<E = any> {
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 `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
141
+ * The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
142
142
  * @returns The `getFirst()` 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 `null`.
143
+ * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
144
144
  */
145
145
  getFirst(): E | undefined {
146
146
  return this.size > 0 ? this.nodes[this.offset] : undefined;
@@ -155,9 +155,9 @@ export class Queue<E = any> {
155
155
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
156
156
  * Space Complexity: O(1) - no additional space is used.
157
157
  *
158
- * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
158
+ * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
159
159
  * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
160
- * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
160
+ * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
161
161
  */
162
162
  peek(): E | undefined {
163
163
  return this.getFirst();
@@ -172,9 +172,9 @@ export class Queue<E = any> {
172
172
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
173
173
  * Space Complexity: O(1) - no additional space is used.
174
174
  *
175
- * The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
175
+ * The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
176
176
  * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
177
- * array is empty, it returns `null`.
177
+ * array is empty, it returns `undefined`.
178
178
  */
179
179
  getLast(): E | undefined {
180
180
  return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
@@ -189,9 +189,9 @@ export class Queue<E = any> {
189
189
  * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
190
190
  * Space Complexity: O(1) - no additional space is used.
191
191
  *
192
- * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
192
+ * The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
193
193
  * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
194
- * array is empty, it returns `null`.
194
+ * array is empty, it returns `undefined`.
195
195
  */
196
196
  peekLast(): E | undefined {
197
197
  return this.getLast();
@@ -222,8 +222,8 @@ export class Queue<E = any> {
222
222
  * Time Complexity: O(n) - same as shift().
223
223
  * Space Complexity: O(1) - same as shift().
224
224
  *
225
- * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
226
- * @returns The method is returning a value of type E or null.
225
+ * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
226
+ * @returns The method is returning a value of type E or undefined.
227
227
  */
228
228
  dequeue(): E | undefined {
229
229
  return this.shift();
@@ -68,11 +68,11 @@ export class Stack<E = any> {
68
68
  * Time Complexity: O(1), as it only involves accessing the last element of the array.
69
69
  * Space Complexity: O(1), as it does not use any additional space.
70
70
  *
71
- * The `peek` function returns the last element of an array, or null if the array is empty.
72
- * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
71
+ * The `peek` function returns the last element of an array, or undefined if the array is empty.
72
+ * @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
73
73
  */
74
- peek(): E | null {
75
- if (this.isEmpty()) return null;
74
+ peek(): E | undefined {
75
+ if (this.isEmpty()) return undefined;
76
76
 
77
77
  return this.elements[this.elements.length - 1];
78
78
  }
@@ -104,14 +104,14 @@ export class Stack<E = any> {
104
104
  * Time Complexity: O(1), as it only involves accessing the last element of the array.
105
105
  * Space Complexity: O(1), as it does not use any additional space.
106
106
  *
107
- * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
107
+ * The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
108
108
  * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
109
- * array is empty, it returns `null`.
109
+ * array is empty, it returns `undefined`.
110
110
  */
111
- pop(): E | null {
112
- if (this.isEmpty()) return null;
111
+ pop(): E | undefined {
112
+ if (this.isEmpty()) return undefined;
113
113
 
114
- return this.elements.pop() || null;
114
+ return this.elements.pop() || undefined;
115
115
  }
116
116
 
117
117
  /**
@@ -29,13 +29,20 @@ export class Trie {
29
29
  constructor(words?: string[], caseSensitive = true) {
30
30
  this._root = new TrieNode('');
31
31
  this._caseSensitive = caseSensitive;
32
+ this._size = 0;
32
33
  if (words) {
33
- for (const i of words) {
34
- this.add(i);
34
+ for (const word of words) {
35
+ this.add(word);
35
36
  }
36
37
  }
37
38
  }
38
39
 
40
+ protected _size: number;
41
+
42
+ get size(): number {
43
+ return this._size;
44
+ }
45
+
39
46
  protected _caseSensitive: boolean;
40
47
 
41
48
  get caseSensitive(): boolean {
@@ -64,6 +71,7 @@ export class Trie {
64
71
  add(word: string): boolean {
65
72
  word = this._caseProcess(word);
66
73
  let cur = this.root;
74
+ let isNewWord = false;
67
75
  for (const c of word) {
68
76
  let nodeC = cur.children.get(c);
69
77
  if (!nodeC) {
@@ -72,8 +80,12 @@ export class Trie {
72
80
  }
73
81
  cur = nodeC;
74
82
  }
75
- cur.isEnd = true;
76
- return true;
83
+ if (!cur.isEnd) {
84
+ isNewWord = true;
85
+ cur.isEnd = true;
86
+ this._size++;
87
+ }
88
+ return isNewWord;
77
89
  }
78
90
 
79
91
  /**
@@ -143,6 +155,9 @@ export class Trie {
143
155
  };
144
156
 
145
157
  dfs(this.root, 0);
158
+ if (isDeleted) {
159
+ this._size--;
160
+ }
146
161
  return isDeleted;
147
162
  }
148
163
 
@@ -377,6 +392,10 @@ export class Trie {
377
392
  return accumulator;
378
393
  }
379
394
 
395
+ print() {
396
+ console.log([...this]);
397
+ }
398
+
380
399
  /**
381
400
  * Time Complexity: O(M), where M is the length of the input string.
382
401
  * Space Complexity: O(1) - Constant space.
@@ -6,7 +6,7 @@ import {
6
6
  BiTreeDeleteResult,
7
7
  BTNCallback,
8
8
  BTNKey,
9
- IterableEntriesOrKeys
9
+ BTNodeExemplar,
10
10
  } from '../types';
11
11
 
12
12
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
@@ -14,9 +14,9 @@ export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTre
14
14
 
15
15
  createTree(options?: Partial<BinaryTreeOptions>): TREE;
16
16
 
17
- init(elements: IterableEntriesOrKeys<V>): void;
17
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
18
18
 
19
- add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
19
+ addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
20
20
 
21
21
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
22
22
  }
@@ -24,4 +24,14 @@ export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLeng
24
24
 
25
25
  export type BinaryTreePrintOptions = { isShowUndefined?: boolean, isShowNull?: boolean, isShowRedBlackNIL?: boolean }
26
26
 
27
- export type IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>
27
+ export type BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
28
+
29
+ export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
30
+
31
+ export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>
32
+
33
+ export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>
34
+
35
+ export type BTNodePureKeyOrNode<N> = BTNKey | N;
36
+
37
+ export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
@@ -3,9 +3,9 @@ export type VertexKey = string | number;
3
3
  export type DijkstraResult<V> = {
4
4
  distMap: Map<V, number>;
5
5
  distPaths?: Map<V, V[]>;
6
- preMap: Map<V, V | null>;
6
+ preMap: Map<V, V | undefined>;
7
7
  seen: Set<V>;
8
8
  paths: V[][];
9
9
  minDist: number;
10
10
  minPath: V[];
11
- } | null;
11
+ } | undefined;
@@ -5,8 +5,7 @@ export type HashMapLinkedNode<K, V> = {
5
5
  prev: HashMapLinkedNode<K, V>;
6
6
  };
7
7
 
8
- export type HashMapOptions<K, V> = {
9
- elements: Iterable<[K, V]>;
8
+ export type HashMapOptions<K> = {
10
9
  hashFn: (key: K) => string;
11
10
  objHashFn: (key: K) => object
12
11
  }