data-structure-typed 1.51.0 → 1.51.2

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 (48) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +37 -1
  4. package/benchmark/report.json +390 -12
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -1
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js +2 -2
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +17 -11
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js +98 -92
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/bst.d.ts +27 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.js +68 -39
  15. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -48
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js +8 -63
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
  21. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  22. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -1
  23. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
  24. package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -2
  25. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +17 -11
  26. package/dist/mjs/data-structures/binary-tree/binary-tree.js +98 -92
  27. package/dist/mjs/data-structures/binary-tree/bst.d.ts +27 -1
  28. package/dist/mjs/data-structures/binary-tree/bst.js +67 -39
  29. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -48
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.js +8 -62
  31. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  32. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
  33. package/dist/umd/data-structure-typed.js +178 -198
  34. package/dist/umd/data-structure-typed.min.js +2 -2
  35. package/dist/umd/data-structure-typed.min.js.map +1 -1
  36. package/package.json +6 -6
  37. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  38. package/src/data-structures/binary-tree/avl-tree.ts +2 -2
  39. package/src/data-structures/binary-tree/binary-tree.ts +98 -93
  40. package/src/data-structures/binary-tree/bst.ts +69 -34
  41. package/src/data-structures/binary-tree/rb-tree.ts +8 -74
  42. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  43. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -0
  44. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -0
  45. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  46. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +40 -40
  47. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +1 -0
  48. package/test/utils/big-o.ts +12 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.51.0",
3
+ "version": "1.51.2",
4
4
  "description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -66,11 +66,11 @@
66
66
  "@typescript-eslint/eslint-plugin": "^6.7.4",
67
67
  "@typescript-eslint/parser": "^6.7.4",
68
68
  "auto-changelog": "^2.4.0",
69
- "avl-tree-typed": "^1.50.9",
69
+ "avl-tree-typed": "^1.51.1",
70
70
  "benchmark": "^2.1.4",
71
- "binary-tree-typed": "^1.50.9",
72
- "bst-typed": "^1.50.9",
73
- "data-structure-typed": "^1.50.9",
71
+ "binary-tree-typed": "^1.51.1",
72
+ "bst-typed": "^1.51.1",
73
+ "data-structure-typed": "^1.51.1",
74
74
  "dependency-cruiser": "^14.1.0",
75
75
  "doctoc": "^2.2.1",
76
76
  "eslint": "^8.50.0",
@@ -79,7 +79,7 @@
79
79
  "eslint-import-resolver-typescript": "^3.6.1",
80
80
  "eslint-plugin-import": "^2.28.1",
81
81
  "fast-glob": "^3.3.1",
82
- "heap-typed": "^1.50.9",
82
+ "heap-typed": "^1.51.1",
83
83
  "istanbul-badges-readme": "^1.8.5",
84
84
  "jest": "^29.7.0",
85
85
  "js-sdsl": "^4.4.2",
@@ -240,7 +240,7 @@ export class AVLTreeMultiMap<
240
240
  */
241
241
  override delete<C extends BTNCallback<NODE>>(
242
242
  identifier: ReturnType<C>,
243
- callback: C = this._defaultOneParamCallback as C,
243
+ callback: C = this._DEFAULT_CALLBACK as C,
244
244
  ignoreCount = false
245
245
  ): BinaryTreeDeleteResult<NODE>[] {
246
246
  const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
@@ -164,13 +164,13 @@ export class AVLTree<
164
164
  * `callback` function.
165
165
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
166
166
  * that is deleted from the binary tree. It is an optional parameter and if not provided, it will
167
- * default to the `_defaultOneParamCallback` function. The `callback` function should have a single
167
+ * default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
168
168
  * parameter of type `NODE
169
169
  * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
170
170
  */
171
171
  override delete<C extends BTNCallback<NODE>>(
172
172
  identifier: ReturnType<C>,
173
- callback: C = this._defaultOneParamCallback as C
173
+ callback: C = this._DEFAULT_CALLBACK as C
174
174
  ): BinaryTreeDeleteResult<NODE>[] {
175
175
  if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
176
176
  const deletedResults = super.delete(identifier, callback);
@@ -191,6 +191,16 @@ export class BinaryTree<
191
191
  return this._size;
192
192
  }
193
193
 
194
+ protected _NIL: NODE = new BinaryTreeNode<K, V>(NaN as K) as unknown as NODE;
195
+
196
+ /**
197
+ * The function returns the value of the _NIL property.
198
+ * @returns The method is returning the value of the `_NIL` property.
199
+ */
200
+ get NIL(): NODE {
201
+ return this._NIL;
202
+ }
203
+
194
204
  /**
195
205
  * Creates a new instance of BinaryTreeNode with the given key and value.
196
206
  * @param {K} key - The key for the new node.
@@ -268,17 +278,26 @@ export class BinaryTree<
268
278
  keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
269
279
  iterationType: IterationType = 'ITERATIVE'
270
280
  ): NODE | null | undefined {
271
- let res: NODE | null | undefined;
272
281
  if (this.isRealNode(keyOrNodeOrEntry)) {
273
- res = keyOrNodeOrEntry;
282
+ return keyOrNodeOrEntry;
274
283
  } else if (this.isEntry(keyOrNodeOrEntry)) {
275
- if (keyOrNodeOrEntry[0] === null) res = null;
276
- else if (keyOrNodeOrEntry[0] !== undefined) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
284
+ if (keyOrNodeOrEntry[0] === null) return null;
285
+ if (keyOrNodeOrEntry[0] === undefined) return;
286
+ return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
277
287
  } else {
278
- if (keyOrNodeOrEntry === null) res = null;
279
- else if (keyOrNodeOrEntry !== undefined) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
288
+ if (keyOrNodeOrEntry === null) return null;
289
+ if (keyOrNodeOrEntry === undefined) return;
290
+ return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
280
291
  }
281
- return res;
292
+ }
293
+
294
+ /**
295
+ * The function checks if a given node is a real node or null.
296
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
297
+ * @returns a boolean value.
298
+ */
299
+ isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
300
+ return this.isRealNode(node) || node === null;
282
301
  }
283
302
 
284
303
  /**
@@ -290,16 +309,6 @@ export class BinaryTree<
290
309
  return keyOrNodeOrEntry instanceof BinaryTreeNode;
291
310
  }
292
311
 
293
- /**
294
- * The function checks if a given value is an entry in a binary tree node.
295
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
296
- * two type parameters V and NODE, representing the value and node type respectively.
297
- * @returns a boolean value.
298
- */
299
- isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
300
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
301
- }
302
-
303
312
  /**
304
313
  * The function checks if a given node is a real node by verifying if it is an instance of
305
314
  * BinaryTreeNode and its key is not NaN.
@@ -307,7 +316,8 @@ export class BinaryTree<
307
316
  * @returns a boolean value.
308
317
  */
309
318
  isRealNode(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
310
- return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
319
+ if (!this.isNode(node)) return false;
320
+ return node !== this.NIL;
311
321
  }
312
322
 
313
323
  /**
@@ -316,16 +326,17 @@ export class BinaryTree<
316
326
  * @returns a boolean value.
317
327
  */
318
328
  isNIL(node: KeyOrNodeOrEntry<K, V, NODE>) {
319
- return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
329
+ return node === this.NIL;
320
330
  }
321
331
 
322
332
  /**
323
- * The function checks if a given node is a real node or null.
324
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
333
+ * The function checks if a given value is an entry in a binary tree node.
334
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
335
+ * two type parameters V and NODE, representing the value and node type respectively.
325
336
  * @returns a boolean value.
326
337
  */
327
- isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
328
- return this.isRealNode(node) || node === null;
338
+ isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
339
+ return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
329
340
  }
330
341
 
331
342
  /**
@@ -486,16 +497,16 @@ export class BinaryTree<
486
497
  * specific node based on its value or object.
487
498
  * @param {C} callback - The `callback` parameter is a function that is used to determine the
488
499
  * identifier of the node to be deleted. It is optional and has a default value of
489
- * `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
500
+ * `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
490
501
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
491
502
  */
492
503
  delete<C extends BTNCallback<NODE>>(
493
504
  identifier: ReturnType<C> | null | undefined,
494
- callback: C = this._defaultOneParamCallback as C
505
+ callback: C = this._DEFAULT_CALLBACK as C
495
506
  ): BinaryTreeDeleteResult<NODE>[] {
496
507
  const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
497
508
  if (!this.root) return deletedResult;
498
- if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
509
+ if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
499
510
  callback = (node => node) as C;
500
511
 
501
512
  const curr = this.getNode(identifier, callback);
@@ -579,7 +590,7 @@ export class BinaryTree<
579
590
  * specific value.
580
591
  * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
581
592
  * input and returns a value of type `C`. It is used to determine if a node matches the given
582
- * identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
593
+ * identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
583
594
  * default
584
595
  * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
585
596
  * matches the identifier. If set to true, the function will stop iterating once it finds a matching
@@ -594,12 +605,12 @@ export class BinaryTree<
594
605
  */
595
606
  getNodes<C extends BTNCallback<NODE>>(
596
607
  identifier: ReturnType<C> | null | undefined,
597
- callback: C = this._defaultOneParamCallback as C,
608
+ callback: C = this._DEFAULT_CALLBACK as C,
598
609
  onlyOne = false,
599
610
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
600
611
  iterationType: IterationType = this.iterationType
601
612
  ): NODE[] {
602
- if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
613
+ if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
603
614
  callback = (node => node) as C;
604
615
  beginRoot = this.ensureNode(beginRoot);
605
616
  if (!beginRoot) return [];
@@ -607,28 +618,28 @@ export class BinaryTree<
607
618
  const ans: NODE[] = [];
608
619
 
609
620
  if (iterationType === 'RECURSIVE') {
610
- const _traverse = (cur: NODE) => {
621
+ const dfs = (cur: NODE) => {
611
622
  if (callback(cur) === identifier) {
612
623
  ans.push(cur);
613
624
  if (onlyOne) return;
614
625
  }
615
- if (!cur.left && !cur.right) return;
616
- cur.left && _traverse(cur.left);
617
- cur.right && _traverse(cur.right);
626
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
627
+ this.isRealNode(cur.left) && dfs(cur.left);
628
+ this.isRealNode(cur.right) && dfs(cur.right);
618
629
  };
619
630
 
620
- _traverse(beginRoot);
631
+ dfs(beginRoot);
621
632
  } else {
622
- const queue = new Queue<NODE>([beginRoot]);
623
- while (queue.size > 0) {
624
- const cur = queue.shift();
625
- if (cur) {
633
+ const stack = [beginRoot];
634
+ while (stack.length > 0) {
635
+ const cur = stack.pop();
636
+ if (this.isRealNode(cur)) {
626
637
  if (callback(cur) === identifier) {
627
638
  ans.push(cur);
628
639
  if (onlyOne) return ans;
629
640
  }
630
- cur.left && queue.push(cur.left);
631
- cur.right && queue.push(cur.right);
641
+ this.isRealNode(cur.left) && stack.push(cur.left);
642
+ this.isRealNode(cur.right) && stack.push(cur.right);
632
643
  }
633
644
  }
634
645
  }
@@ -685,13 +696,10 @@ export class BinaryTree<
685
696
  */
686
697
  getNode<C extends BTNCallback<NODE>>(
687
698
  identifier: ReturnType<C> | null | undefined,
688
- callback: C = this._defaultOneParamCallback as C,
699
+ callback: C = this._DEFAULT_CALLBACK as C,
689
700
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
690
701
  iterationType: IterationType = this.iterationType
691
702
  ): NODE | null | undefined {
692
- if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
693
- callback = (node => node) as C;
694
-
695
703
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
696
704
  }
697
705
 
@@ -717,23 +725,23 @@ export class BinaryTree<
717
725
  getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
718
726
  if (!this.root) return undefined;
719
727
  if (iterationType === 'RECURSIVE') {
720
- const _dfs = (cur: NODE): NODE | undefined => {
728
+ const dfs = (cur: NODE): NODE | undefined => {
721
729
  if (cur.key === key) return cur;
722
730
 
723
731
  if (!cur.left && !cur.right) return;
724
- if (cur.left) return _dfs(cur.left);
725
- if (cur.right) return _dfs(cur.right);
732
+ if (cur.left) return dfs(cur.left);
733
+ if (cur.right) return dfs(cur.right);
726
734
  };
727
735
 
728
- return _dfs(this.root);
736
+ return dfs(this.root);
729
737
  } else {
730
- const queue = new Queue<NODE>([this.root]);
731
- while (queue.size > 0) {
732
- const cur = queue.shift();
738
+ const stack = [this.root];
739
+ while (stack.length > 0) {
740
+ const cur = stack.pop();
733
741
  if (cur) {
734
742
  if (cur.key === key) return cur;
735
- cur.left && queue.push(cur.left);
736
- cur.right && queue.push(cur.right);
743
+ cur.left && stack.push(cur.left);
744
+ cur.right && stack.push(cur.right);
737
745
  }
738
746
  }
739
747
  }
@@ -789,13 +797,10 @@ export class BinaryTree<
789
797
  */
790
798
  override get<C extends BTNCallback<NODE>>(
791
799
  identifier: ReturnType<C> | null | undefined,
792
- callback: C = this._defaultOneParamCallback as C,
800
+ callback: C = this._DEFAULT_CALLBACK as C,
793
801
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
794
802
  iterationType: IterationType = this.iterationType
795
803
  ): V | undefined {
796
- if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
797
- callback = (node => node) as C;
798
-
799
804
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
800
805
  }
801
806
 
@@ -848,11 +853,11 @@ export class BinaryTree<
848
853
  */
849
854
  override has<C extends BTNCallback<NODE>>(
850
855
  identifier: ReturnType<C> | null | undefined,
851
- callback: C = this._defaultOneParamCallback as C,
856
+ callback: C = this._DEFAULT_CALLBACK as C,
852
857
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
853
858
  iterationType: IterationType = this.iterationType
854
859
  ): boolean {
855
- if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
860
+ if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
856
861
  callback = (node => node) as C;
857
862
 
858
863
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
@@ -1184,20 +1189,20 @@ export class BinaryTree<
1184
1189
  if (!this.isRealNode(beginRoot)) return beginRoot;
1185
1190
 
1186
1191
  if (iterationType === 'RECURSIVE') {
1187
- const _traverse = (cur: NODE): NODE => {
1192
+ const dfs = (cur: NODE): NODE => {
1188
1193
  if (!this.isRealNode(cur.left)) return cur;
1189
- return _traverse(cur.left);
1194
+ return dfs(cur.left);
1190
1195
  };
1191
1196
 
1192
- return _traverse(beginRoot);
1197
+ return dfs(beginRoot);
1193
1198
  } else {
1194
1199
  // Indirect implementation of iteration using tail recursion optimization
1195
- const _traverse = trampoline((cur: NODE) => {
1200
+ const dfs = trampoline((cur: NODE) => {
1196
1201
  if (!this.isRealNode(cur.left)) return cur;
1197
- return _traverse.cont(cur.left);
1202
+ return dfs.cont(cur.left);
1198
1203
  });
1199
1204
 
1200
- return _traverse(beginRoot);
1205
+ return dfs(beginRoot);
1201
1206
  }
1202
1207
  }
1203
1208
 
@@ -1231,20 +1236,20 @@ export class BinaryTree<
1231
1236
  if (!beginRoot) return beginRoot;
1232
1237
 
1233
1238
  if (iterationType === 'RECURSIVE') {
1234
- const _traverse = (cur: NODE): NODE => {
1239
+ const dfs = (cur: NODE): NODE => {
1235
1240
  if (!this.isRealNode(cur.right)) return cur;
1236
- return _traverse(cur.right);
1241
+ return dfs(cur.right);
1237
1242
  };
1238
1243
 
1239
- return _traverse(beginRoot);
1244
+ return dfs(beginRoot);
1240
1245
  } else {
1241
1246
  // Indirect implementation of iteration using tail recursion optimization
1242
- const _traverse = trampoline((cur: NODE) => {
1247
+ const dfs = trampoline((cur: NODE) => {
1243
1248
  if (!this.isRealNode(cur.right)) return cur;
1244
- return _traverse.cont(cur.right);
1249
+ return dfs.cont(cur.right);
1245
1250
  });
1246
1251
 
1247
- return _traverse(beginRoot);
1252
+ return dfs(beginRoot);
1248
1253
  }
1249
1254
  }
1250
1255
 
@@ -1351,7 +1356,7 @@ export class BinaryTree<
1351
1356
  * @returns an array of values that are the return values of the callback function.
1352
1357
  */
1353
1358
  dfs<C extends BTNCallback<NODE | null | undefined>>(
1354
- callback: C = this._defaultOneParamCallback as C,
1359
+ callback: C = this._DEFAULT_CALLBACK as C,
1355
1360
  pattern: DFSOrderPattern = 'IN',
1356
1361
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1357
1362
  iterationType: IterationType = 'ITERATIVE',
@@ -1361,38 +1366,38 @@ export class BinaryTree<
1361
1366
  if (!beginRoot) return [];
1362
1367
  const ans: ReturnType<C>[] = [];
1363
1368
  if (iterationType === 'RECURSIVE') {
1364
- const _traverse = (node: NODE | null | undefined) => {
1369
+ const dfs = (node: NODE | null | undefined) => {
1365
1370
  switch (pattern) {
1366
1371
  case 'IN':
1367
1372
  if (includeNull) {
1368
- if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1373
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
1369
1374
  this.isNodeOrNull(node) && ans.push(callback(node));
1370
- if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1375
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
1371
1376
  } else {
1372
- if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1377
+ if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
1373
1378
  this.isRealNode(node) && ans.push(callback(node));
1374
- if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1379
+ if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
1375
1380
  }
1376
1381
  break;
1377
1382
  case 'PRE':
1378
1383
  if (includeNull) {
1379
1384
  this.isNodeOrNull(node) && ans.push(callback(node));
1380
- if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1381
- if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1385
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
1386
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
1382
1387
  } else {
1383
1388
  this.isRealNode(node) && ans.push(callback(node));
1384
- if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1385
- if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1389
+ if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
1390
+ if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
1386
1391
  }
1387
1392
  break;
1388
1393
  case 'POST':
1389
1394
  if (includeNull) {
1390
- if (this.isRealNode(node) && this.isNodeOrNull(node.left)) _traverse(node.left);
1391
- if (this.isRealNode(node) && this.isNodeOrNull(node.right)) _traverse(node.right);
1395
+ if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
1396
+ if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
1392
1397
  this.isNodeOrNull(node) && ans.push(callback(node));
1393
1398
  } else {
1394
- if (this.isRealNode(node) && this.isRealNode(node.left)) _traverse(node.left);
1395
- if (this.isRealNode(node) && this.isRealNode(node.right)) _traverse(node.right);
1399
+ if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
1400
+ if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
1396
1401
  this.isRealNode(node) && ans.push(callback(node));
1397
1402
  }
1398
1403
 
@@ -1400,7 +1405,7 @@ export class BinaryTree<
1400
1405
  }
1401
1406
  };
1402
1407
 
1403
- _traverse(beginRoot);
1408
+ dfs(beginRoot);
1404
1409
  } else {
1405
1410
  // 0: visit, 1: print
1406
1411
  const stack: { opt: 0 | 1; node: NODE | null | undefined }[] = [{ opt: 0, node: beginRoot }];
@@ -1486,7 +1491,7 @@ export class BinaryTree<
1486
1491
  * the breadth-first traversal of a binary tree.
1487
1492
  */
1488
1493
  bfs<C extends BTNCallback<NODE | null>>(
1489
- callback: C = this._defaultOneParamCallback as C,
1494
+ callback: C = this._DEFAULT_CALLBACK as C,
1490
1495
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1491
1496
  iterationType: IterationType = this.iterationType,
1492
1497
  includeNull = false
@@ -1499,7 +1504,7 @@ export class BinaryTree<
1499
1504
  if (iterationType === 'RECURSIVE') {
1500
1505
  const queue: Queue<NODE | null | undefined> = new Queue<NODE | null | undefined>([beginRoot]);
1501
1506
 
1502
- const traverse = (level: number) => {
1507
+ const dfs = (level: number) => {
1503
1508
  if (queue.size === 0) return;
1504
1509
 
1505
1510
  const current = queue.shift()!;
@@ -1513,10 +1518,10 @@ export class BinaryTree<
1513
1518
  if (this.isRealNode(current.right)) queue.push(current.right);
1514
1519
  }
1515
1520
 
1516
- traverse(level + 1);
1521
+ dfs(level + 1);
1517
1522
  };
1518
1523
 
1519
- traverse(0);
1524
+ dfs(0);
1520
1525
  } else {
1521
1526
  const queue = new Queue<NODE | null | undefined>([beginRoot]);
1522
1527
  while (queue.size > 0) {
@@ -1580,7 +1585,7 @@ export class BinaryTree<
1580
1585
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1581
1586
  */
1582
1587
  listLevels<C extends BTNCallback<NODE | null>>(
1583
- callback: C = this._defaultOneParamCallback as C,
1588
+ callback: C = this._DEFAULT_CALLBACK as C,
1584
1589
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1585
1590
  iterationType: IterationType = this.iterationType,
1586
1591
  includeNull = false
@@ -1651,7 +1656,7 @@ export class BinaryTree<
1651
1656
  * by the return type of the `callback` function.
1652
1657
  */
1653
1658
  morris<C extends BTNCallback<NODE>>(
1654
- callback: C = this._defaultOneParamCallback as C,
1659
+ callback: C = this._DEFAULT_CALLBACK as C,
1655
1660
  pattern: DFSOrderPattern = 'IN',
1656
1661
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root
1657
1662
  ): ReturnType<C>[] {
@@ -1995,7 +2000,7 @@ export class BinaryTree<
1995
2000
  }
1996
2001
  }
1997
2002
 
1998
- protected _defaultOneParamCallback = (node: NODE | null | undefined) => (node ? node.key : undefined);
2003
+ protected _DEFAULT_CALLBACK = (node: NODE | null | undefined) => (node ? node.key : undefined);
1999
2004
 
2000
2005
  /**
2001
2006
  * Swap the data of two nodes in the binary tree.