min-heap-typed 1.50.4 → 1.50.6

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 (28) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +10 -8
  2. package/dist/data-structures/base/iterable-base.js +8 -12
  3. package/dist/data-structures/binary-tree/binary-tree.js +19 -19
  4. package/dist/data-structures/binary-tree/rb-tree.d.ts +158 -135
  5. package/dist/data-structures/binary-tree/rb-tree.js +415 -386
  6. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
  7. package/dist/data-structures/binary-tree/tree-multi-map.js +84 -76
  8. package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
  9. package/dist/data-structures/graph/abstract-graph.js +3 -0
  10. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
  11. package/dist/data-structures/linked-list/doubly-linked-list.js +16 -86
  12. package/dist/data-structures/linked-list/singly-linked-list.d.ts +27 -69
  13. package/dist/data-structures/linked-list/singly-linked-list.js +35 -79
  14. package/dist/data-structures/queue/deque.d.ts +0 -53
  15. package/dist/data-structures/queue/deque.js +0 -61
  16. package/dist/data-structures/queue/queue.d.ts +0 -70
  17. package/dist/data-structures/queue/queue.js +0 -87
  18. package/package.json +2 -2
  19. package/src/data-structures/base/iterable-base.ts +14 -10
  20. package/src/data-structures/binary-tree/binary-tree.ts +19 -19
  21. package/src/data-structures/binary-tree/rb-tree.ts +437 -395
  22. package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
  23. package/src/data-structures/graph/abstract-graph.ts +4 -0
  24. package/src/data-structures/heap/heap.ts +1 -1
  25. package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
  26. package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
  27. package/src/data-structures/queue/deque.ts +0 -67
  28. package/src/data-structures/queue/queue.ts +0 -98
@@ -74,6 +74,24 @@ class SinglyLinkedList extends base_1.IterableElementBase {
74
74
  get tail() {
75
75
  return this._tail;
76
76
  }
77
+ /**
78
+ * The above function returns the value of the first element in a linked list, or undefined if the
79
+ * list is empty.
80
+ * @returns The value of the first node in the linked list, or undefined if the linked list is empty.
81
+ */
82
+ get first() {
83
+ var _a;
84
+ return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
85
+ }
86
+ /**
87
+ * The function returns the value of the last element in a linked list, or undefined if the list is
88
+ * empty.
89
+ * @returns The value of the last node in the linked list, or undefined if the linked list is empty.
90
+ */
91
+ get last() {
92
+ var _a;
93
+ return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.value;
94
+ }
77
95
  /**
78
96
  * The function returns the size of an object.
79
97
  * @returns The size of the object, which is a number.
@@ -106,19 +124,18 @@ class SinglyLinkedList extends base_1.IterableElementBase {
106
124
  /**
107
125
  * Time Complexity: O(1)
108
126
  * Space Complexity: O(1)
109
- * Constant time, as it involves basic pointer adjustments.
110
- * Constant space, as it only creates a new node.
111
127
  */
112
128
  /**
113
129
  * Time Complexity: O(1)
114
130
  * Space Complexity: O(1)
115
131
  *
116
- * The `push` function adds a new node with the given value to the end of a singly linked list.
117
- * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
118
- * any type (E) as specified in the generic type declaration of the class or function.
132
+ * The push function adds a new element to the end of a singly linked list.
133
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
134
+ * add to the linked list.
135
+ * @returns The `push` method is returning a boolean value, `true`.
119
136
  */
120
- push(value) {
121
- const newNode = new SinglyLinkedListNode(value);
137
+ push(element) {
138
+ const newNode = new SinglyLinkedListNode(element);
122
139
  if (!this.head) {
123
140
  this._head = newNode;
124
141
  this._tail = newNode;
@@ -130,21 +147,6 @@ class SinglyLinkedList extends base_1.IterableElementBase {
130
147
  this._size++;
131
148
  return true;
132
149
  }
133
- /**
134
- * Time Complexity: O(1)
135
- * Space Complexity: O(1)
136
- */
137
- /**
138
- * Time Complexity: O(1)
139
- * Space Complexity: O(1)
140
- *
141
- * The `push` function adds a new node with the given value to the end of a singly linked list.
142
- * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
143
- * any type (E) as specified in the generic type declaration of the class or function.
144
- */
145
- addLast(value) {
146
- return this.push(value);
147
- }
148
150
  /**
149
151
  * Time Complexity: O(n)
150
152
  * Space Complexity: O(1)
@@ -154,10 +156,9 @@ class SinglyLinkedList extends base_1.IterableElementBase {
154
156
  * Time Complexity: O(n)
155
157
  * Space Complexity: O(1)
156
158
  *
157
- * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
158
- * pointers accordingly.
159
- * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
160
- * the linked list is empty, it returns `undefined`.
159
+ * The `pop` function removes and returns the value of the last element in a linked list.
160
+ * @returns The method is returning the value of the element that is being popped from the end of the
161
+ * list.
161
162
  */
162
163
  pop() {
163
164
  if (!this.head)
@@ -179,22 +180,6 @@ class SinglyLinkedList extends base_1.IterableElementBase {
179
180
  this._size--;
180
181
  return value;
181
182
  }
182
- /**
183
- * Time Complexity: O(n)
184
- * Space Complexity: O(1)
185
- */
186
- /**
187
- * Time Complexity: O(n)
188
- * Space Complexity: O(1)
189
- *
190
- * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
191
- * pointers accordingly.
192
- * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
193
- * the linked list is empty, it returns `undefined`.
194
- */
195
- pollLast() {
196
- return this.pop();
197
- }
198
183
  /**
199
184
  * Time Complexity: O(1)
200
185
  * Space Complexity: O(1)
@@ -203,8 +188,8 @@ class SinglyLinkedList extends base_1.IterableElementBase {
203
188
  * Time Complexity: O(1)
204
189
  * Space Complexity: O(1)
205
190
  *
206
- * The `shift()` function removes and returns the value of the first node in a linked list.
207
- * @returns The value of the node that is being removed from the beginning of the linked list.
191
+ * The `shift()` function removes and returns the value of the first element in a linked list.
192
+ * @returns The value of the removed node.
208
193
  */
209
194
  shift() {
210
195
  if (!this.head)
@@ -222,26 +207,13 @@ class SinglyLinkedList extends base_1.IterableElementBase {
222
207
  * Time Complexity: O(1)
223
208
  * Space Complexity: O(1)
224
209
  *
225
- * The `pollFirst()` function removes and returns the value of the first node in a linked list.
226
- * @returns The value of the node that is being removed from the beginning of the linked list.
227
- */
228
- pollFirst() {
229
- return this.shift();
230
- }
231
- /**
232
- * Time Complexity: O(1)
233
- * Space Complexity: O(1)
234
- */
235
- /**
236
- * Time Complexity: O(1)
237
- * Space Complexity: O(1)
238
- *
239
- * The unshift function adds a new node with the given value to the beginning of a singly linked list.
240
- * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
241
- * linked list.
210
+ * The unshift function adds a new element to the beginning of a singly linked list.
211
+ * @param {E} element - The "element" parameter represents the value of the element that you want to
212
+ * add to the beginning of the singly linked list.
213
+ * @returns The `unshift` method is returning a boolean value, `true`.
242
214
  */
243
- unshift(value) {
244
- const newNode = new SinglyLinkedListNode(value);
215
+ unshift(element) {
216
+ const newNode = new SinglyLinkedListNode(element);
245
217
  if (!this.head) {
246
218
  this._head = newNode;
247
219
  this._tail = newNode;
@@ -253,25 +225,9 @@ class SinglyLinkedList extends base_1.IterableElementBase {
253
225
  this._size++;
254
226
  return true;
255
227
  }
256
- /**
257
- * Time Complexity: O(1)
258
- * Space Complexity: O(1)
259
- */
260
- /**
261
- * Time Complexity: O(1)
262
- * Space Complexity: O(1)
263
- *
264
- * The addFirst function adds a new node with the given value to the beginning of a singly linked list.
265
- * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
266
- * linked list.
267
- */
268
- addFirst(value) {
269
- return this.unshift(value);
270
- }
271
228
  /**
272
229
  * Time Complexity: O(n)
273
230
  * Space Complexity: O(1)
274
- * Linear time, where n is the index, as it may need to traverse the list to find the desired node.
275
231
  */
276
232
  /**
277
233
  * Time Complexity: O(n)
@@ -432,59 +432,6 @@ export declare class Deque<E> extends IterableElementBase<E> {
432
432
  * @returns a new Deque object with the mapped values.
433
433
  */
434
434
  map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T>;
435
- /**
436
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
437
- * Space Complexity: O(n) - Due to potential resizing.
438
- */
439
- /**
440
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
441
- * Space Complexity: O(n) - Due to potential resizing.
442
- *
443
- * The addLast function adds an element to the end of an array.
444
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
445
- * data structure.
446
- */
447
- addLast(element: E): boolean;
448
- /**
449
- * Time Complexity: O(1)
450
- * Space Complexity: O(1)
451
- */
452
- /**
453
- * Time Complexity: O(1)
454
- * Space Complexity: O(1)
455
- *
456
- * The function "pollLast" removes and returns the last element of an array.
457
- * @returns The last element of the array is being returned.
458
- */
459
- pollLast(): E | undefined;
460
- /**
461
- * Time Complexity: O(1)
462
- * Space Complexity: O(1)
463
- * /
464
-
465
- /**
466
- * Time Complexity: O(1)
467
- * Space Complexity: O(1)
468
- *
469
- * The "addFirst" function adds an element to the beginning of an array.
470
- * @param {E} element - The parameter "element" represents the element that you want to add to the
471
- * beginning of the data structure.
472
- */
473
- addFirst(element: E): boolean;
474
- /**
475
- * Time Complexity: O(1)
476
- * Space Complexity: O(1)
477
- * /
478
-
479
- /**
480
- * Time Complexity: O(1)
481
- * Space Complexity: O(1)
482
- *
483
- * The function "pollFirst" removes and returns the first element of an array.
484
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
485
- * from the beginning. If the array is empty, it will return `undefined`.
486
- */
487
- pollFirst(): E | undefined;
488
435
  /**
489
436
  * Time Complexity: O(n)
490
437
  * Space Complexity: O(1)
@@ -767,67 +767,6 @@ class Deque extends base_1.IterableElementBase {
767
767
  }
768
768
  return newDeque;
769
769
  }
770
- /**
771
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
772
- * Space Complexity: O(n) - Due to potential resizing.
773
- */
774
- /**
775
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
776
- * Space Complexity: O(n) - Due to potential resizing.
777
- *
778
- * The addLast function adds an element to the end of an array.
779
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
780
- * data structure.
781
- */
782
- addLast(element) {
783
- return this.push(element);
784
- }
785
- /**
786
- * Time Complexity: O(1)
787
- * Space Complexity: O(1)
788
- */
789
- /**
790
- * Time Complexity: O(1)
791
- * Space Complexity: O(1)
792
- *
793
- * The function "pollLast" removes and returns the last element of an array.
794
- * @returns The last element of the array is being returned.
795
- */
796
- pollLast() {
797
- return this.pop();
798
- }
799
- /**
800
- * Time Complexity: O(1)
801
- * Space Complexity: O(1)
802
- * /
803
-
804
- /**
805
- * Time Complexity: O(1)
806
- * Space Complexity: O(1)
807
- *
808
- * The "addFirst" function adds an element to the beginning of an array.
809
- * @param {E} element - The parameter "element" represents the element that you want to add to the
810
- * beginning of the data structure.
811
- */
812
- addFirst(element) {
813
- return this.unshift(element);
814
- }
815
- /**
816
- * Time Complexity: O(1)
817
- * Space Complexity: O(1)
818
- * /
819
-
820
- /**
821
- * Time Complexity: O(1)
822
- * Space Complexity: O(1)
823
- *
824
- * The function "pollFirst" removes and returns the first element of an array.
825
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
826
- * from the beginning. If the array is empty, it will return `undefined`.
827
- */
828
- pollFirst() {
829
- return this.shift();
830
- }
831
770
  /**
832
771
  * Time Complexity: O(n)
833
772
  * Space Complexity: O(1)
@@ -120,56 +120,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
120
120
  * @return A boolean value
121
121
  */
122
122
  deleteAt(index: number): boolean;
123
- /**
124
- * Time Complexity: O(1)
125
- * Space Complexity: O(1)
126
- */
127
- /**
128
- * Time Complexity: O(1)
129
- * Space Complexity: O(1)
130
- *
131
- * The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
132
- * @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
133
- * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
134
- */
135
- peek(): E | undefined;
136
- /**
137
- * Time Complexity: O(1)
138
- * Space Complexity: O(1)
139
- */
140
- /**
141
- * Time Complexity: O(1)
142
- * Space Complexity: O(1)
143
- *
144
- * The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
145
- * @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
146
- * array is empty, it returns `undefined`.
147
- */
148
- peekLast(): E | undefined;
149
- /**
150
- * Time Complexity: O(1)
151
- * Space Complexity: O(1)
152
- */
153
- /**
154
- * Time Complexity: O(1)
155
- * Space Complexity: O(1)
156
- *
157
- * The enqueue function adds a value to the end of a queue.
158
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
159
- */
160
- enqueue(value: E): boolean;
161
- /**
162
- * Time Complexity: O(1)
163
- * Space Complexity: O(1)
164
- */
165
- /**
166
- * Time Complexity: O(1)
167
- * Space Complexity: O(1)
168
- *
169
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
170
- * @returns The method is returning a value of type E or undefined.
171
- */
172
- dequeue(): E | undefined;
173
123
  /**
174
124
  * Time Complexity: O(1)
175
125
  * Space Complexity: O(1)
@@ -288,26 +238,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
288
238
  * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
289
239
  */
290
240
  export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
291
- /**
292
- * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
293
- * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
294
- */
295
- get first(): E | undefined;
296
- /**
297
- * The enqueue function adds a value to the end of an array.
298
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
299
- */
300
- enqueue(value: E): boolean;
301
- /**
302
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
303
- * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
304
- */
305
- dequeue(): E | undefined;
306
- /**
307
- * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
308
- * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
309
- */
310
- peek(): E | undefined;
311
241
  /**
312
242
  * Time Complexity: O(n)
313
243
  * Space Complexity: O(n)
@@ -156,64 +156,6 @@ class Queue extends base_1.IterableElementBase {
156
156
  const spliced = this.elements.splice(index, 1);
157
157
  return spliced.length === 1;
158
158
  }
159
- /**
160
- * Time Complexity: O(1)
161
- * Space Complexity: O(1)
162
- */
163
- /**
164
- * Time Complexity: O(1)
165
- * Space Complexity: O(1)
166
- *
167
- * The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
168
- * @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
169
- * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
170
- */
171
- peek() {
172
- return this.first;
173
- }
174
- /**
175
- * Time Complexity: O(1)
176
- * Space Complexity: O(1)
177
- */
178
- /**
179
- * Time Complexity: O(1)
180
- * Space Complexity: O(1)
181
- *
182
- * The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
183
- * @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
184
- * array is empty, it returns `undefined`.
185
- */
186
- peekLast() {
187
- return this.last;
188
- }
189
- /**
190
- * Time Complexity: O(1)
191
- * Space Complexity: O(1)
192
- */
193
- /**
194
- * Time Complexity: O(1)
195
- * Space Complexity: O(1)
196
- *
197
- * The enqueue function adds a value to the end of a queue.
198
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
199
- */
200
- enqueue(value) {
201
- return this.push(value);
202
- }
203
- /**
204
- * Time Complexity: O(1)
205
- * Space Complexity: O(1)
206
- */
207
- /**
208
- * Time Complexity: O(1)
209
- * Space Complexity: O(1)
210
- *
211
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
212
- * @returns The method is returning a value of type E or undefined.
213
- */
214
- dequeue() {
215
- return this.shift();
216
- }
217
159
  /**
218
160
  * Time Complexity: O(1)
219
161
  * Space Complexity: O(1)
@@ -366,35 +308,6 @@ exports.Queue = Queue;
366
308
  * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
367
309
  */
368
310
  class LinkedListQueue extends linked_list_1.SinglyLinkedList {
369
- /**
370
- * The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
371
- * @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
372
- */
373
- get first() {
374
- var _a;
375
- return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
376
- }
377
- /**
378
- * The enqueue function adds a value to the end of an array.
379
- * @param {E} value - The value parameter represents the value that you want to add to the queue.
380
- */
381
- enqueue(value) {
382
- return this.push(value);
383
- }
384
- /**
385
- * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
386
- * @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
387
- */
388
- dequeue() {
389
- return this.shift();
390
- }
391
- /**
392
- * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
393
- * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
394
- */
395
- peek() {
396
- return this.first;
397
- }
398
311
  /**
399
312
  * Time Complexity: O(n)
400
313
  * Space Complexity: O(n)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.50.4",
3
+ "version": "1.50.6",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -132,6 +132,6 @@
132
132
  "typescript": "^4.9.5"
133
133
  },
134
134
  "dependencies": {
135
- "data-structure-typed": "^1.50.4"
135
+ "data-structure-typed": "^1.50.6"
136
136
  }
137
137
  }
@@ -6,6 +6,8 @@ export abstract class IterableEntryBase<K = any, V = any> {
6
6
  * Space Complexity: O(1)
7
7
  */
8
8
 
9
+ abstract get size(): number;
10
+
9
11
  /**
10
12
  * Time Complexity: O(n)
11
13
  * Space Complexity: O(1)
@@ -125,6 +127,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
125
127
  return false;
126
128
  }
127
129
 
130
+ /**
131
+ * Time Complexity: O(n)
132
+ * Space Complexity: O(1)
133
+ */
134
+
128
135
  /**
129
136
  * Time Complexity: O(n)
130
137
  * Space Complexity: O(1)
@@ -248,11 +255,6 @@ export abstract class IterableEntryBase<K = any, V = any> {
248
255
  return;
249
256
  }
250
257
 
251
- /**
252
- * Time Complexity: O(n)
253
- * Space Complexity: O(1)
254
- */
255
-
256
258
  /**
257
259
  * Time Complexity: O(n)
258
260
  * Space Complexity: O(1)
@@ -301,6 +303,8 @@ export abstract class IterableEntryBase<K = any, V = any> {
301
303
  }
302
304
 
303
305
  export abstract class IterableElementBase<E = any, C = any> {
306
+ abstract get size(): number;
307
+
304
308
  /**
305
309
  * Time Complexity: O(n)
306
310
  * Space Complexity: O(1)
@@ -362,6 +366,11 @@ export abstract class IterableElementBase<E = any, C = any> {
362
366
  return true;
363
367
  }
364
368
 
369
+ /**
370
+ * Time Complexity: O(n)
371
+ * Space Complexity: O(1)
372
+ */
373
+
365
374
  /**
366
375
  * Time Complexity: O(n)
367
376
  * Space Complexity: O(1)
@@ -445,11 +454,6 @@ export abstract class IterableElementBase<E = any, C = any> {
445
454
  return;
446
455
  }
447
456
 
448
- /**
449
- * Time Complexity: O(n)
450
- * Space Complexity: O(1)
451
- */
452
-
453
457
  /**
454
458
  * Time Complexity: O(n)
455
459
  * Space Complexity: O(1)
@@ -934,7 +934,7 @@ export class BinaryTree<
934
934
 
935
935
  if (iterationType === IterationType.RECURSIVE) {
936
936
  const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
937
- if (!cur) return true;
937
+ if (!this.isRealNode(cur)) return true;
938
938
  const numKey = this.extractor(cur.key);
939
939
  if (numKey <= min || numKey >= max) return false;
940
940
  return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
@@ -949,14 +949,14 @@ export class BinaryTree<
949
949
  let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
950
950
  // @ts-ignore
951
951
  let curr: NODE | null | undefined = beginRoot;
952
- while (curr || stack.length > 0) {
953
- while (curr) {
952
+ while (this.isRealNode(curr) || stack.length > 0) {
953
+ while (this.isRealNode(curr)) {
954
954
  stack.push(curr);
955
955
  curr = curr.left;
956
956
  }
957
957
  curr = stack.pop()!;
958
958
  const numKey = this.extractor(curr.key);
959
- if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
959
+ if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
960
960
  prev = numKey;
961
961
  curr = curr.right;
962
962
  }
@@ -1025,7 +1025,7 @@ export class BinaryTree<
1025
1025
 
1026
1026
  if (iterationType === IterationType.RECURSIVE) {
1027
1027
  const _getMaxHeight = (cur: NODE | null | undefined): number => {
1028
- if (!cur) return -1;
1028
+ if (!this.isRealNode(cur)) return -1;
1029
1029
  const leftHeight = _getMaxHeight(cur.left);
1030
1030
  const rightHeight = _getMaxHeight(cur.right);
1031
1031
  return Math.max(leftHeight, rightHeight) + 1;
@@ -1039,8 +1039,8 @@ export class BinaryTree<
1039
1039
  while (stack.length > 0) {
1040
1040
  const { node, depth } = stack.pop()!;
1041
1041
 
1042
- if (node.left) stack.push({ node: node.left, depth: depth + 1 });
1043
- if (node.right) stack.push({ node: node.right, depth: depth + 1 });
1042
+ if (this.isRealNode(node.left)) stack.push({ node: node.left, depth: depth + 1 });
1043
+ if (this.isRealNode(node.right)) stack.push({ node: node.right, depth: depth + 1 });
1044
1044
 
1045
1045
  maxHeight = Math.max(maxHeight, depth);
1046
1046
  }
@@ -1354,34 +1354,34 @@ export class BinaryTree<
1354
1354
  switch (pattern) {
1355
1355
  case 'in':
1356
1356
  if (includeNull) {
1357
- if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
1357
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1358
1358
  this.isNodeOrNull(node) && ans.push(callback(node));
1359
- if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
1359
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1360
1360
  } else {
1361
- if (node && node.left) _traverse(node.left);
1361
+ if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1362
1362
  this.isRealNode(node) && ans.push(callback(node));
1363
- if (node && node.right) _traverse(node.right);
1363
+ if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1364
1364
  }
1365
1365
  break;
1366
1366
  case 'pre':
1367
1367
  if (includeNull) {
1368
1368
  this.isNodeOrNull(node) && ans.push(callback(node));
1369
- if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
1370
- if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
1369
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1370
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1371
1371
  } else {
1372
1372
  this.isRealNode(node) && ans.push(callback(node));
1373
- if (node && node.left) _traverse(node.left);
1374
- if (node && node.right) _traverse(node.right);
1373
+ if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1374
+ if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1375
1375
  }
1376
1376
  break;
1377
1377
  case 'post':
1378
1378
  if (includeNull) {
1379
- if (node && this.isNodeOrNull(node.left)) _traverse(node.left);
1380
- if (node && this.isNodeOrNull(node.right)) _traverse(node.right);
1379
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1380
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1381
1381
  this.isNodeOrNull(node) && ans.push(callback(node));
1382
1382
  } else {
1383
- if (node && node.left) _traverse(node.left);
1384
- if (node && node.right) _traverse(node.right);
1383
+ if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1384
+ if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1385
1385
  this.isRealNode(node) && ans.push(callback(node));
1386
1386
  }
1387
1387