min-heap-typed 1.39.1 → 1.39.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +10 -10
  2. package/dist/data-structures/binary-tree/avl-tree.js +4 -4
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -107
  4. package/dist/data-structures/binary-tree/binary-tree.js +65 -27
  5. package/dist/data-structures/binary-tree/bst.d.ts +22 -22
  6. package/dist/data-structures/binary-tree/bst.js +12 -12
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
  8. package/dist/data-structures/binary-tree/tree-multiset.d.ts +14 -14
  9. package/dist/data-structures/binary-tree/tree-multiset.js +7 -7
  10. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
  11. package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
  12. package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
  13. package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
  14. package/dist/data-structures/queue/deque.d.ts +20 -20
  15. package/dist/data-structures/queue/deque.js +22 -22
  16. package/dist/data-structures/queue/queue.d.ts +3 -3
  17. package/dist/data-structures/queue/queue.js +3 -3
  18. package/dist/interfaces/binary-tree.d.ts +4 -4
  19. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  20. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  21. package/dist/types/helpers.d.ts +1 -1
  22. package/package.json +2 -2
  23. package/src/data-structures/binary-tree/avl-tree.ts +10 -10
  24. package/src/data-structures/binary-tree/binary-tree.ts +165 -53
  25. package/src/data-structures/binary-tree/bst.ts +29 -31
  26. package/src/data-structures/binary-tree/rb-tree.ts +5 -5
  27. package/src/data-structures/binary-tree/tree-multiset.ts +14 -14
  28. package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
  29. package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
  30. package/src/data-structures/queue/deque.ts +22 -22
  31. package/src/data-structures/queue/queue.ts +3 -3
  32. package/src/interfaces/binary-tree.ts +4 -4
  33. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  34. package/src/types/data-structures/binary-tree/bst.ts +2 -2
  35. package/src/types/helpers.ts +1 -1
@@ -87,17 +87,13 @@ export class SinglyLinkedList<E = any> {
87
87
  return singlyLinkedList;
88
88
  }
89
89
 
90
- getLength(): number {
91
- return this._length;
92
- }
93
-
94
90
  /**
95
- * The `push` function adds a new node with the given data to the end of a singly linked list.
96
- * @param {E} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
91
+ * The `push` function adds a new node with the given val to the end of a singly linked list.
92
+ * @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
97
93
  * any type (E) as specified in the generic type declaration of the class or function.
98
94
  */
99
- push(data: E): void {
100
- const newNode = new SinglyLinkedListNode(data);
95
+ push(val: E): void {
96
+ const newNode = new SinglyLinkedListNode(val);
101
97
  if (!this.head) {
102
98
  this.head = newNode;
103
99
  this.tail = newNode;
@@ -108,6 +104,15 @@ export class SinglyLinkedList<E = any> {
108
104
  this._length++;
109
105
  }
110
106
 
107
+ /**
108
+ * The `push` function adds a new node with the given val to the end of a singly linked list.
109
+ * @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
110
+ * any type (E) as specified in the generic type declaration of the class or function.
111
+ */
112
+ addLast(val: E): void {
113
+ this.push(val);
114
+ }
115
+
111
116
  /**
112
117
  * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
113
118
  * pointers accordingly.
@@ -135,6 +140,16 @@ export class SinglyLinkedList<E = any> {
135
140
  return val;
136
141
  }
137
142
 
143
+ /**
144
+ * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
145
+ * pointers accordingly.
146
+ * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
147
+ * the linked list is empty, it returns `null`.
148
+ */
149
+ popLast(): E | undefined {
150
+ return this.pop();
151
+ }
152
+
138
153
  /**
139
154
  * The `shift()` function removes and returns the value of the first node in a linked list.
140
155
  * @returns The value of the node that is being removed from the beginning of the linked list.
@@ -147,6 +162,14 @@ export class SinglyLinkedList<E = any> {
147
162
  return removedNode.val;
148
163
  }
149
164
 
165
+ /**
166
+ * The `popFirst()` function removes and returns the value of the first node in a linked list.
167
+ * @returns The value of the node that is being removed from the beginning of the linked list.
168
+ */
169
+ popFirst(): E | undefined {
170
+ return this.shift();
171
+ }
172
+
150
173
  /**
151
174
  * The unshift function adds a new node with the given value to the beginning of a singly linked list.
152
175
  * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
@@ -164,6 +187,15 @@ export class SinglyLinkedList<E = any> {
164
187
  this._length++;
165
188
  }
166
189
 
190
+ /**
191
+ * The addFirst function adds a new node with the given value to the beginning of a singly linked list.
192
+ * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
193
+ * linked list.
194
+ */
195
+ addFirst(val: E): void {
196
+ this.unshift(val);
197
+ }
198
+
167
199
  /**
168
200
  * The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
169
201
  * @param {number} index - The index parameter is a number that represents the position of the element we want to
@@ -382,7 +414,7 @@ export class SinglyLinkedList<E = any> {
382
414
  * @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
383
415
  * the specified value is found, the function returns `null`.
384
416
  */
385
- findNode(value: E): SinglyLinkedListNode<E> | null {
417
+ getNode(value: E): SinglyLinkedListNode<E> | null {
386
418
  let current = this.head;
387
419
 
388
420
  while (current) {
@@ -432,9 +464,6 @@ export class SinglyLinkedList<E = any> {
432
464
  return false;
433
465
  }
434
466
 
435
- insertAfter(existingValueOrNode: E, newValue: E): boolean;
436
- insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean;
437
-
438
467
  /**
439
468
  * The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
440
469
  * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
@@ -449,7 +478,7 @@ export class SinglyLinkedList<E = any> {
449
478
  if (existingValueOrNode instanceof SinglyLinkedListNode) {
450
479
  existingNode = existingValueOrNode;
451
480
  } else {
452
- existingNode = this.findNode(existingValueOrNode);
481
+ existingNode = this.getNode(existingValueOrNode);
453
482
  }
454
483
 
455
484
  if (existingNode) {
@@ -485,6 +514,82 @@ export class SinglyLinkedList<E = any> {
485
514
  return count;
486
515
  }
487
516
 
517
+ /**
518
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
519
+ * @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
520
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
521
+ * current node in the linked list.
522
+ */
523
+ forEach(callback: (val: E, index: number) => void): void {
524
+ let current = this.head;
525
+ let index = 0;
526
+ while (current) {
527
+ callback(current.val, index);
528
+ current = current.next;
529
+ index++;
530
+ }
531
+ }
532
+
533
+ /**
534
+ * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
535
+ * SinglyLinkedList with the transformed values.
536
+ * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
537
+ * the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
538
+ * SinglyLinkedList).
539
+ * @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
540
+ */
541
+ map<U>(callback: (val: E) => U): SinglyLinkedList<U> {
542
+ const mappedList = new SinglyLinkedList<U>();
543
+ let current = this.head;
544
+ while (current) {
545
+ mappedList.push(callback(current.val));
546
+ current = current.next;
547
+ }
548
+ return mappedList;
549
+ }
550
+
551
+ /**
552
+ * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
553
+ * elements that satisfy the given callback function.
554
+ * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
555
+ * It is used to determine whether a value should be included in the filtered list or not.
556
+ * @returns The filtered list, which is an instance of the SinglyLinkedList class.
557
+ */
558
+ filter(callback: (val: E) => boolean): SinglyLinkedList<E> {
559
+ const filteredList = new SinglyLinkedList<E>();
560
+ let current = this.head;
561
+ while (current) {
562
+ if (callback(current.val)) {
563
+ filteredList.push(current.val);
564
+ }
565
+ current = current.next;
566
+ }
567
+ return filteredList;
568
+ }
569
+
570
+ /**
571
+ * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
572
+ * single value.
573
+ * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
574
+ * used to perform a specific operation on each element of the linked list.
575
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
576
+ * point for the reduction operation.
577
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
578
+ * elements in the linked list.
579
+ */
580
+ reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U {
581
+ let accumulator = initialValue;
582
+ let current = this.head;
583
+ while (current) {
584
+ accumulator = callback(accumulator, current.val);
585
+ current = current.next;
586
+ }
587
+ return accumulator;
588
+ }
589
+
590
+ /**
591
+ * The function returns an iterator that iterates over the values of a linked list.
592
+ */
488
593
  *[Symbol.iterator]() {
489
594
  let current = this.head;
490
595
 
@@ -95,12 +95,12 @@ export class ObjectDeque<E = number> {
95
95
  }
96
96
 
97
97
  /**
98
- * The function `pollFirst()` removes and returns the first element in a data structure.
98
+ * The function `popFirst()` removes and returns the first element in a data structure.
99
99
  * @returns The value of the first element in the data structure.
100
100
  */
101
- pollFirst() {
101
+ popFirst() {
102
102
  if (!this._size) return;
103
- const value = this.peekFirst();
103
+ const value = this.getFirst();
104
104
  delete this._nodes[this._first];
105
105
  this._first++;
106
106
  this._size--;
@@ -108,20 +108,20 @@ export class ObjectDeque<E = number> {
108
108
  }
109
109
 
110
110
  /**
111
- * The `peekFirst` function returns the first element in an array-like data structure if it exists.
111
+ * The `getFirst` function returns the first element in an array-like data structure if it exists.
112
112
  * @returns The element at the first position of the `_nodes` array.
113
113
  */
114
- peekFirst() {
114
+ getFirst() {
115
115
  if (this._size) return this._nodes[this._first];
116
116
  }
117
117
 
118
118
  /**
119
- * The `pollLast()` function removes and returns the last element in a data structure.
119
+ * The `popLast()` function removes and returns the last element in a data structure.
120
120
  * @returns The value that was removed from the data structure.
121
121
  */
122
- pollLast() {
122
+ popLast() {
123
123
  if (!this._size) return;
124
- const value = this.peekLast();
124
+ const value = this.getLast();
125
125
  delete this._nodes[this._last];
126
126
  this._last--;
127
127
  this._size--;
@@ -130,10 +130,10 @@ export class ObjectDeque<E = number> {
130
130
  }
131
131
 
132
132
  /**
133
- * The `peekLast()` function returns the last element in an array-like data structure.
133
+ * The `getLast()` function returns the last element in an array-like data structure.
134
134
  * @returns The last element in the array "_nodes" is being returned.
135
135
  */
136
- peekLast() {
136
+ getLast() {
137
137
  if (this._size) return this._nodes[this._last];
138
138
  }
139
139
 
@@ -188,19 +188,19 @@ export class ArrayDeque<E> {
188
188
  }
189
189
 
190
190
  /**
191
- * The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
192
- * @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
191
+ * The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
192
+ * @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
193
193
  */
194
- pollLast(): E | null {
194
+ popLast(): E | null {
195
195
  return this._nodes.pop() ?? null;
196
196
  }
197
197
 
198
198
  /**
199
- * The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
200
- * @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
199
+ * The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
200
+ * @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
201
201
  * empty.
202
202
  */
203
- pollFirst(): E | null {
203
+ popFirst(): E | null {
204
204
  return this._nodes.shift() ?? null;
205
205
  }
206
206
 
@@ -219,19 +219,19 @@ export class ArrayDeque<E> {
219
219
  }
220
220
 
221
221
  /**
222
- * The `peekFirst` function returns the first element of an array or null if the array is empty.
223
- * @returns The function `peekFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
222
+ * The `getFirst` function returns the first element of an array or null if the array is empty.
223
+ * @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
224
224
  * empty, it will return `null`.
225
225
  */
226
- peekFirst(): E | null {
226
+ getFirst(): E | null {
227
227
  return this._nodes[0] ?? null;
228
228
  }
229
229
 
230
230
  /**
231
- * The `peekLast` function returns the last element of an array or null if the array is empty.
232
- * @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
231
+ * The `getLast` function returns the last element of an array or null if the array is empty.
232
+ * @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
233
233
  */
234
- peekLast(): E | null {
234
+ getLast(): E | null {
235
235
  return this._nodes[this._nodes.length - 1] ?? null;
236
236
  }
237
237
 
@@ -123,11 +123,11 @@ export class Queue<E = any> {
123
123
  }
124
124
 
125
125
  /**
126
- * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
127
- * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
126
+ * The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
127
+ * @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
128
128
  * array is empty, it returns `null`.
129
129
  */
130
- peekLast(): E | undefined {
130
+ getLast(): E | undefined {
131
131
  return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
132
132
  }
133
133
 
@@ -1,10 +1,10 @@
1
1
  import {BinaryTreeNode} from '../data-structures';
2
- import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, OneParamCallback} from '../types';
2
+ import {BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback} from '../types';
3
3
 
4
4
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
5
- createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
5
+ createNode(key: BTNKey, val?: N['val']): N;
6
6
 
7
- add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
7
+ add(keyOrNode: BTNKey | N | null, val?: N['val']): N | null | undefined;
8
8
 
9
- delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
9
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
10
10
  }
@@ -22,7 +22,7 @@ export enum FamilyPosition {
22
22
  MAL_NODE = 'MAL_NODE'
23
23
  }
24
24
 
25
- export type BinaryTreeNodeKey = number;
25
+ export type BTNKey = number;
26
26
 
27
27
  export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
28
28
 
@@ -1,7 +1,7 @@
1
1
  import {BSTNode} from '../../../data-structures';
2
- import type {BinaryTreeNodeKey, BinaryTreeOptions} from './binary-tree';
2
+ import type {BTNKey, BinaryTreeOptions} from './binary-tree';
3
3
 
4
- export type BSTComparator = (a: BinaryTreeNodeKey, b: BinaryTreeNodeKey) => number;
4
+ export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
5
5
 
6
6
  // prettier-ignore
7
7
  export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@@ -2,7 +2,7 @@ export type Comparator<T> = (a: T, b: T) => number;
2
2
 
3
3
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
4
4
 
5
- export type OneParamCallback<N, D = any> = (node: N) => D;
5
+ export type BTNCallback<N, D = any> = (node: N) => D;
6
6
 
7
7
  export enum CP {
8
8
  lt = 'lt',