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.
Files changed (142) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
  2. package/.github/workflows/ci.yml +1 -1
  3. package/.github/workflows/release-package.yml +1 -1
  4. package/CHANGELOG.md +1 -1
  5. package/CODE_OF_CONDUCT.md +32 -10
  6. package/COMMANDS.md +3 -1
  7. package/CONTRIBUTING.md +4 -3
  8. package/README.md +188 -32
  9. package/SECURITY.md +1 -1
  10. package/benchmark/report.html +46 -1
  11. package/benchmark/report.json +563 -8
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
  16. package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
  19. package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
  20. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
  22. package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
  23. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
  25. package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
  26. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  28. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
  29. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  30. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
  31. package/dist/cjs/data-structures/graph/abstract-graph.js +30 -30
  32. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  33. package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
  34. package/dist/cjs/data-structures/graph/directed-graph.js +28 -28
  35. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  36. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
  37. package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
  38. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
  40. package/dist/cjs/data-structures/hash/hash-map.js +5 -8
  41. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  42. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
  43. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
  44. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  45. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
  46. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
  47. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  48. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
  49. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  50. package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
  51. package/dist/cjs/data-structures/queue/queue.js +13 -13
  52. package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
  53. package/dist/cjs/data-structures/stack/stack.js +7 -7
  54. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  55. package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
  56. package/dist/cjs/data-structures/trie/trie.js +19 -4
  57. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  58. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  59. package/dist/cjs/types/common.d.ts +6 -1
  60. package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
  61. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
  62. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
  63. package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
  64. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
  65. package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
  66. package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
  67. package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
  68. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
  69. package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
  70. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
  71. package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
  72. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  73. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
  74. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
  75. package/dist/mjs/data-structures/graph/abstract-graph.js +30 -30
  76. package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
  77. package/dist/mjs/data-structures/graph/directed-graph.js +28 -28
  78. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
  79. package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
  80. package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
  81. package/dist/mjs/data-structures/hash/hash-map.js +5 -8
  82. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
  83. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
  84. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
  85. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
  86. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
  87. package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
  88. package/dist/mjs/data-structures/queue/queue.js +13 -13
  89. package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
  90. package/dist/mjs/data-structures/stack/stack.js +7 -7
  91. package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
  92. package/dist/mjs/data-structures/trie/trie.js +20 -4
  93. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  94. package/dist/mjs/types/common.d.ts +6 -1
  95. package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
  96. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
  97. package/dist/umd/data-structure-typed.js +583 -627
  98. package/dist/umd/data-structure-typed.min.js +2 -2
  99. package/dist/umd/data-structure-typed.min.js.map +1 -1
  100. package/package.json +1 -1
  101. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  102. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  103. package/src/data-structures/binary-tree/bst.ts +157 -154
  104. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  105. package/src/data-structures/binary-tree/segment-tree.ts +10 -10
  106. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  107. package/src/data-structures/graph/abstract-graph.ts +46 -46
  108. package/src/data-structures/graph/directed-graph.ts +40 -40
  109. package/src/data-structures/graph/undirected-graph.ts +26 -26
  110. package/src/data-structures/hash/hash-map.ts +8 -8
  111. package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
  112. package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
  113. package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
  114. package/src/data-structures/queue/queue.ts +13 -13
  115. package/src/data-structures/stack/stack.ts +9 -9
  116. package/src/data-structures/trie/trie.ts +23 -4
  117. package/src/interfaces/binary-tree.ts +3 -3
  118. package/src/types/common.ts +11 -1
  119. package/src/types/data-structures/graph/abstract-graph.ts +2 -2
  120. package/src/types/data-structures/hash/hash-map.ts +1 -2
  121. package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
  122. package/test/integration/index.html +158 -2
  123. package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
  124. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
  125. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
  126. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
  127. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
  128. package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
  129. package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
  130. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
  131. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
  132. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
  133. package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
  134. package/test/unit/data-structures/graph/directed-graph.test.ts +10 -10
  135. package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
  136. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
  137. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
  138. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  139. package/test/unit/data-structures/queue/deque.test.ts +1 -1
  140. package/test/unit/data-structures/stack/stack.test.ts +2 -2
  141. package/test/unit/unrestricted-interconversion.test.ts +61 -5
  142. 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 null.
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 = null;
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 = null;
27
- this._tail = null;
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 `null`.
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 = null;
124
- this._tail = null;
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 = null;
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 `null`.
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 null if the index is out of range.
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 | null` returns the value at the specified index in the linked list, or
238
- * `null` if the index is out of bounds.
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 `null` if the index is out of bounds.
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 `null` if the index is out of
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 = null;
321
+ let current = this.head, prev = undefined;
322
322
  while (current) {
323
323
  if (current.value === value) {
324
- if (prev === null) {
324
+ if (prev === undefined) {
325
325
  this._head = current.next;
326
326
  if (current === this.tail) {
327
- this._tail = null;
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 null and 0 respectively.
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 = null;
391
- this._tail = null;
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 = null;
428
+ let prev = undefined;
429
429
  let current = this.head;
430
- let next = null;
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 `null`.
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 null;
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
- * null.
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 `null`.
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 null;
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(null, null, maxLevel);
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] !== null) {
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] === null) {
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 = null;
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 null if the queue is empty.
15
- * @returns The method is returning the element at the front of the queue, or null if the queue is empty.
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 `null` if the queue is empty.
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 `null`.
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 `null`.
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 `null`.
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 `null`.
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 null if the structure is empty.
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 `null`.
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 null if the structure is empty.
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 `null`.
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 null if the queue is empty.
154
- * @returns The method is returning a value of type E or null.
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 null if the queue is empty.
17
- * @returns The method is returning the element at the front of the queue, or null if the queue is empty.
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 `null` if the queue is empty.
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 `null`.
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 `null`.
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 `null`.
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 `null`.
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 null if the structure is empty.
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 `null`.
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 null if the structure is empty.
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 `null`.
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 null if the queue is empty.
198
- * @returns The method is returning a value of type E or null.
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 null if the array is empty.
49
- * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
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 | null;
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 null if the array is empty.
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 `null`.
75
+ * array is empty, it returns `undefined`.
76
76
  */
77
- pop(): E | null;
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 null if the array is empty.
64
- * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
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 null;
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 null if the array is empty.
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 `null`.
97
+ * array is empty, it returns `undefined`.
98
98
  */
99
99
  pop() {
100
100
  if (this.isEmpty())
101
- return null;
102
- return this.elements.pop() || null;
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 i of words) {
31
- this.add(i);
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 = true;
67
- return true;
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, IterableEntriesOrKeys } from '../types';
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
- init(elements: IterableEntriesOrKeys<V>): void;
7
- add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
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 IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>;
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 | null>;
5
+ preMap: Map<V, V | undefined>;
6
6
  seen: Set<V>;
7
7
  paths: V[][];
8
8
  minDist: number;
9
9
  minPath: V[];
10
- } | null;
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, V> = {
8
- elements: Iterable<[K, V]>;
7
+ export type HashMapOptions<K> = {
9
8
  hashFn: (key: K) => string;
10
9
  objHashFn: (key: K) => object;
11
10
  };