queue-typed 1.47.4 → 1.47.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 (72) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +8 -8
  2. package/dist/data-structures/binary-tree/avl-tree.js +23 -15
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
  4. package/dist/data-structures/binary-tree/binary-tree.js +66 -82
  5. package/dist/data-structures/binary-tree/bst.d.ts +38 -37
  6. package/dist/data-structures/binary-tree/bst.js +56 -40
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
  8. package/dist/data-structures/binary-tree/rb-tree.js +26 -17
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  10. package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +3 -3
  13. package/dist/data-structures/hash/hash-map.js +10 -4
  14. package/dist/data-structures/hash/hash-table.d.ts +9 -4
  15. package/dist/data-structures/hash/hash-table.js +50 -5
  16. package/dist/data-structures/heap/heap.d.ts +25 -22
  17. package/dist/data-structures/heap/heap.js +101 -41
  18. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/max-heap.js +2 -2
  20. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  21. package/dist/data-structures/heap/min-heap.js +2 -2
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  25. package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
  26. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  30. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  31. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  32. package/dist/data-structures/queue/deque.d.ts +50 -49
  33. package/dist/data-structures/queue/deque.js +81 -71
  34. package/dist/data-structures/queue/queue.d.ts +46 -0
  35. package/dist/data-structures/queue/queue.js +80 -0
  36. package/dist/data-structures/stack/stack.d.ts +20 -6
  37. package/dist/data-structures/stack/stack.js +65 -8
  38. package/dist/data-structures/trie/trie.d.ts +5 -0
  39. package/dist/data-structures/trie/trie.js +47 -0
  40. package/dist/interfaces/binary-tree.d.ts +3 -1
  41. package/dist/types/common.d.ts +2 -0
  42. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  43. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  44. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  45. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  46. package/package.json +2 -2
  47. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  48. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  49. package/src/data-structures/binary-tree/bst.ts +67 -47
  50. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  51. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  52. package/src/data-structures/graph/abstract-graph.ts +1 -1
  53. package/src/data-structures/hash/hash-map.ts +13 -7
  54. package/src/data-structures/hash/hash-table.ts +59 -9
  55. package/src/data-structures/heap/heap.ts +115 -46
  56. package/src/data-structures/heap/max-heap.ts +5 -5
  57. package/src/data-structures/heap/min-heap.ts +5 -5
  58. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  59. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  60. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  61. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  62. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  63. package/src/data-structures/queue/deque.ts +86 -75
  64. package/src/data-structures/queue/queue.ts +88 -0
  65. package/src/data-structures/stack/stack.ts +75 -10
  66. package/src/data-structures/trie/trie.ts +53 -0
  67. package/src/interfaces/binary-tree.ts +13 -1
  68. package/src/types/common.ts +5 -1
  69. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  70. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  71. package/src/types/data-structures/heap/heap.ts +3 -1
  72. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -13,6 +13,7 @@ import {
13
13
  BiTreeDeleteResult,
14
14
  DFSOrderPattern,
15
15
  FamilyPosition,
16
+ IterableEntriesOrKeys,
16
17
  IterationType,
17
18
  NodeDisplayLayout
18
19
  } from '../../types';
@@ -118,20 +119,24 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
118
119
  export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>>
119
120
  implements IBinaryTree<V, N, TREE> {
120
121
 
121
- options: BinaryTreeOptions;
122
+ iterationType = IterationType.ITERATIVE
122
123
 
123
124
  /**
124
125
  * Creates a new instance of BinaryTree.
125
126
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
126
127
  */
127
- constructor(options?: BinaryTreeOptions) {
128
+ constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<BinaryTreeOptions>) {
129
+
128
130
  if (options) {
129
- this.options = { iterationType: IterationType.ITERATIVE, ...options }
130
- } else {
131
- this.options = { iterationType: IterationType.ITERATIVE };
131
+ const { iterationType } = options;
132
+ if (iterationType) {
133
+ this.iterationType = iterationType;
134
+ }
132
135
  }
133
136
 
134
137
  this._size = 0;
138
+
139
+ if (elements) this.init(elements);
135
140
  }
136
141
 
137
142
  protected _root?: N | null;
@@ -162,16 +167,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
162
167
  return new BinaryTreeNode<V, N>(key, value) as N;
163
168
  }
164
169
 
165
- createTree(options?: BinaryTreeOptions): TREE {
166
- return new BinaryTree<V, N, TREE>({ ...this.options, ...options }) as TREE;
170
+ createTree(options?: Partial<BinaryTreeOptions>): TREE {
171
+ return new BinaryTree<V, N, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
167
172
  }
168
173
 
169
- /**
170
- * Time Complexity: O(n)
171
- * Space Complexity: O(1)
172
- * Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
173
- */
174
-
175
174
  /**
176
175
  * Time Complexity: O(n)
177
176
  * Space Complexity: O(1)
@@ -228,8 +227,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
228
227
  }
229
228
 
230
229
  /**
231
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
230
+ * Time Complexity: O(n)
232
231
  * Space Complexity: O(1)
232
+ * Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
233
233
  */
234
234
 
235
235
  /**
@@ -284,17 +284,17 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
284
284
  return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
285
285
  }
286
286
 
287
+ /**
288
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
289
+ * Space Complexity: O(1)
290
+ */
291
+
287
292
  delete<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C): BiTreeDeleteResult<N>[];
288
293
 
289
294
  delete<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
290
295
 
291
296
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BiTreeDeleteResult<N>[];
292
297
 
293
- /**
294
- * Time Complexity: O(n)
295
- * Space Complexity: O(1)
296
- */
297
-
298
298
  /**
299
299
  * Time Complexity: O(n)
300
300
  * Space Complexity: O(1)
@@ -394,8 +394,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
394
394
 
395
395
  /**
396
396
  * Time Complexity: O(n)
397
- * Space Complexity: O(log n)
398
- * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
397
+ * Space Complexity: O(1)
399
398
  */
400
399
 
401
400
  /**
@@ -412,7 +411,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
412
411
  * values:
413
412
  * @returns the height of the binary tree.
414
413
  */
415
- getHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.options.iterationType): number {
414
+ getHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): number {
416
415
  beginRoot = this.ensureNotKey(beginRoot);
417
416
  if (!beginRoot) return -1;
418
417
 
@@ -461,7 +460,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
461
460
  * to calculate the minimum height of a binary tree. It can have two possible values:
462
461
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
463
462
  */
464
- getMinHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.options.iterationType): number {
463
+ getMinHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): number {
465
464
  beginRoot = this.ensureNotKey(beginRoot);
466
465
  if (!beginRoot) return -1;
467
466
 
@@ -507,6 +506,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
507
506
  /**
508
507
  * Time Complexity: O(n)
509
508
  * Space Complexity: O(log n)
509
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
510
510
  */
511
511
 
512
512
  /**
@@ -524,6 +524,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
524
524
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
525
525
  }
526
526
 
527
+ /**
528
+ * Time Complexity: O(n)
529
+ * Space Complexity: O(log n)
530
+ */
531
+
527
532
  getNodes<C extends BTNCallback<N, BTNKey>>(
528
533
  identifier: BTNKey,
529
534
  callback?: C,
@@ -548,11 +553,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
548
553
  iterationType?: IterationType
549
554
  ): N[];
550
555
 
551
- /**
552
- * Time Complexity: O(n)
553
- * Space Complexity: O(log n).
554
- */
555
-
556
556
  /**
557
557
  * Time Complexity: O(n)
558
558
  * Space Complexity: O(log n).
@@ -583,7 +583,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
583
583
  callback: C = this._defaultOneParamCallback as C,
584
584
  onlyOne = false,
585
585
  beginRoot: BTNKey | N | null | undefined = this.root,
586
- iterationType = this.options.iterationType
586
+ iterationType = this.iterationType
587
587
  ): N[] {
588
588
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
589
589
  callback = (node => node) as C;
@@ -622,6 +622,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
622
622
  return ans;
623
623
  }
624
624
 
625
+ /**
626
+ * Time Complexity: O(n)
627
+ * Space Complexity: O(log n).
628
+ */
629
+
625
630
  has<C extends BTNCallback<N, BTNKey>>(
626
631
  identifier: BTNKey,
627
632
  callback?: C,
@@ -643,11 +648,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
643
648
  iterationType?: IterationType
644
649
  ): boolean;
645
650
 
646
- /**
647
- * Time Complexity: O(n)
648
- * Space Complexity: O(log n).
649
- */
650
-
651
651
  /**
652
652
  * Time Complexity: O(n)
653
653
  *
@@ -672,7 +672,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
672
672
  identifier: ReturnType<C> | null | undefined,
673
673
  callback: C = this._defaultOneParamCallback as C,
674
674
  beginRoot: BTNKey | N | null | undefined = this.root,
675
- iterationType = this.options.iterationType
675
+ iterationType = this.iterationType
676
676
  ): boolean {
677
677
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
678
678
  callback = (node => node) as C;
@@ -680,6 +680,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
680
680
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
681
681
  }
682
682
 
683
+ /**
684
+ * Time Complexity: O(n)
685
+ * Space Complexity: O(log n).
686
+ */
687
+
683
688
  getNode<C extends BTNCallback<N, BTNKey>>(
684
689
  identifier: BTNKey,
685
690
  callback?: C,
@@ -701,11 +706,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
701
706
  iterationType?: IterationType
702
707
  ): N | null | undefined;
703
708
 
704
- /**
705
- * Time Complexity: O(n)
706
- * Space Complexity: O(log n)
707
- */
708
-
709
709
  /**
710
710
  * Time Complexity: O(n)
711
711
  * Space Complexity: O(log n)
@@ -731,7 +731,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
731
731
  identifier: ReturnType<C> | null | undefined,
732
732
  callback: C = this._defaultOneParamCallback as C,
733
733
  beginRoot: BTNKey | N | null | undefined = this.root,
734
- iterationType = this.options.iterationType
734
+ iterationType = this.iterationType
735
735
  ): N | null | undefined {
736
736
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
737
737
  callback = (node => node) as C;
@@ -783,6 +783,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
783
783
  }
784
784
  }
785
785
 
786
+ /**
787
+ * Time Complexity: O(n)
788
+ * Space Complexity: O(log n)
789
+ */
790
+
786
791
  /**
787
792
  * The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
788
793
  * key, otherwise it returns the key itself.
@@ -819,11 +824,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
819
824
  iterationType?: IterationType
820
825
  ): V | undefined;
821
826
 
822
- /**
823
- * Time Complexity: O(n)
824
- * Space Complexity: O(log n)
825
- */
826
-
827
827
  /**
828
828
  * Time Complexity: O(n)
829
829
  * Space Complexity: O(log n)
@@ -850,7 +850,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
850
850
  identifier: ReturnType<C> | null | undefined,
851
851
  callback: C = this._defaultOneParamCallback as C,
852
852
  beginRoot: BTNKey | N | null | undefined = this.root,
853
- iterationType = this.options.iterationType
853
+ iterationType = this.iterationType
854
854
  ): V | undefined {
855
855
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
856
856
  callback = (node => node) as C;
@@ -858,6 +858,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
858
858
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
859
859
  }
860
860
 
861
+ /**
862
+ * Time Complexity: O(n)
863
+ * Space Complexity: O(log n)
864
+ */
865
+
861
866
  /**
862
867
  * Clear the binary tree, removing all nodes.
863
868
  */
@@ -874,11 +879,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
874
879
  return this.size === 0;
875
880
  }
876
881
 
877
- /**
878
- * Time Complexity: O(log n)
879
- * Space Complexity: O(log n)
880
- */
881
-
882
882
  /**
883
883
  * Time Complexity: O(log n)
884
884
  * Space Complexity: O(log n)
@@ -912,7 +912,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
912
912
 
913
913
  /**
914
914
  * Time Complexity: O(log n)
915
- * Space Complexity: O(1)
915
+ * Space Complexity: O(log n)
916
916
  */
917
917
 
918
918
  /**
@@ -931,7 +931,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
931
931
  */
932
932
  getLeftMost(
933
933
  beginRoot: BTNKey | N | null | undefined = this.root,
934
- iterationType = this.options.iterationType
934
+ iterationType = this.iterationType
935
935
  ): N | null | undefined {
936
936
  beginRoot = this.ensureNotKey(beginRoot);
937
937
 
@@ -977,7 +977,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
977
977
  */
978
978
  getRightMost(
979
979
  beginRoot: BTNKey | N | null | undefined = this.root,
980
- iterationType = this.options.iterationType
980
+ iterationType = this.iterationType
981
981
  ): N | null | undefined {
982
982
  // TODO support get right most by passing key in
983
983
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1002,7 +1002,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1002
1002
  }
1003
1003
 
1004
1004
  /**
1005
- * Time Complexity: O(n)
1005
+ * Time Complexity: O(log n)
1006
1006
  * Space Complexity: O(1)
1007
1007
  */
1008
1008
 
@@ -1018,7 +1018,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1018
1018
  * possible values:
1019
1019
  * @returns a boolean value.
1020
1020
  */
1021
- isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType = this.options.iterationType): boolean {
1021
+ isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType = this.iterationType): boolean {
1022
1022
  // TODO there is a bug
1023
1023
  beginRoot = this.ensureNotKey(beginRoot);
1024
1024
  if (!beginRoot) return true;
@@ -1065,11 +1065,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1065
1065
  * expected to be
1066
1066
  * @returns a boolean value.
1067
1067
  */
1068
- isBST(iterationType = this.options.iterationType): boolean {
1068
+ isBST(iterationType = this.iterationType): boolean {
1069
1069
  if (this.root === null) return true;
1070
1070
  return this.isSubtreeBST(this.root, iterationType);
1071
1071
  }
1072
1072
 
1073
+ /**
1074
+ * Time Complexity: O(n)
1075
+ * Space Complexity: O(1)
1076
+ */
1077
+
1073
1078
  subTreeTraverse<C extends BTNCallback<N>>(
1074
1079
  callback?: C,
1075
1080
  beginRoot?: BTNKey | N | null | undefined,
@@ -1091,11 +1096,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1091
1096
  includeNull?: true
1092
1097
  ): ReturnType<C>[];
1093
1098
 
1094
- /**
1095
- * Time complexity: O(n)
1096
- * Space complexity: O(log n)
1097
- */
1098
-
1099
1099
  /**
1100
1100
  * Time complexity: O(n)
1101
1101
  * Space complexity: O(log n)
@@ -1121,7 +1121,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1121
1121
  subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
1122
1122
  callback: C = this._defaultOneParamCallback as C,
1123
1123
  beginRoot: BTNKey | N | null | undefined = this.root,
1124
- iterationType = this.options.iterationType,
1124
+ iterationType = this.iterationType,
1125
1125
  includeNull = false
1126
1126
  ): ReturnType<C>[] {
1127
1127
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1164,6 +1164,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1164
1164
  return ans;
1165
1165
  }
1166
1166
 
1167
+ /**
1168
+ * Time complexity: O(n)
1169
+ * Space complexity: O(log n)
1170
+ */
1171
+
1167
1172
  /**
1168
1173
  * The function checks if a given node is a real node by verifying if it is an instance of
1169
1174
  * BinaryTreeNode and its key is not NaN.
@@ -1226,11 +1231,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1226
1231
  includeNull?: true
1227
1232
  ): ReturnType<C>[];
1228
1233
 
1229
- /**
1230
- * Time complexity: O(n)
1231
- * Space complexity: O(n)
1232
- */
1233
-
1234
1234
  /**
1235
1235
  * Time complexity: O(n)
1236
1236
  * Space complexity: O(n)
@@ -1349,6 +1349,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1349
1349
  return ans;
1350
1350
  }
1351
1351
 
1352
+ /**
1353
+ * Time complexity: O(n)
1354
+ * Space complexity: O(n)
1355
+ */
1356
+
1352
1357
  bfs<C extends BTNCallback<N>>(
1353
1358
  callback?: C,
1354
1359
  beginRoot?: BTNKey | N | null | undefined,
@@ -1370,11 +1375,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1370
1375
  includeNull?: true
1371
1376
  ): ReturnType<C>[];
1372
1377
 
1373
- /**
1374
- * Time complexity: O(n)
1375
- * Space complexity: O(n)
1376
- */
1377
-
1378
1378
  /**
1379
1379
  * Time complexity: O(n)
1380
1380
  * Space complexity: O(n)
@@ -1399,7 +1399,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1399
1399
  bfs<C extends BTNCallback<N | null | undefined>>(
1400
1400
  callback: C = this._defaultOneParamCallback as C,
1401
1401
  beginRoot: BTNKey | N | null | undefined = this.root,
1402
- iterationType = this.options.iterationType,
1402
+ iterationType = this.iterationType,
1403
1403
  includeNull = false
1404
1404
  ): ReturnType<C>[] {
1405
1405
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1450,6 +1450,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1450
1450
  return ans;
1451
1451
  }
1452
1452
 
1453
+ /**
1454
+ * Time complexity: O(n)
1455
+ * Space complexity: O(n)
1456
+ */
1457
+
1453
1458
  listLevels<C extends BTNCallback<N>>(
1454
1459
  callback?: C,
1455
1460
  beginRoot?: BTNKey | N | null | undefined,
@@ -1471,11 +1476,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1471
1476
  includeNull?: true
1472
1477
  ): ReturnType<C>[][];
1473
1478
 
1474
- /**
1475
- * Time complexity: O(n)
1476
- * Space complexity: O(n)
1477
- */
1478
-
1479
1479
  /**
1480
1480
  * Time complexity: O(n)
1481
1481
  * Space complexity: O(n)
@@ -1500,7 +1500,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1500
1500
  listLevels<C extends BTNCallback<N | null | undefined>>(
1501
1501
  callback: C = this._defaultOneParamCallback as C,
1502
1502
  beginRoot: BTNKey | N | null | undefined = this.root,
1503
- iterationType = this.options.iterationType,
1503
+ iterationType = this.iterationType,
1504
1504
  includeNull = false
1505
1505
  ): ReturnType<C>[][] {
1506
1506
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1544,6 +1544,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1544
1544
  return levelsNodes;
1545
1545
  }
1546
1546
 
1547
+ /**
1548
+ * Time complexity: O(n)
1549
+ * Space complexity: O(n)
1550
+ */
1551
+
1547
1552
  getPredecessor(node: N): N;
1548
1553
 
1549
1554
  /**
@@ -1591,11 +1596,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1591
1596
  return y;
1592
1597
  }
1593
1598
 
1594
- /**
1595
- * Time complexity: O(n)
1596
- * Space complexity: O(1)
1597
- */
1598
-
1599
1599
  /**
1600
1600
  * Time complexity: O(n)
1601
1601
  * Space complexity: O(1)
@@ -1700,6 +1700,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1700
1700
  return ans;
1701
1701
  }
1702
1702
 
1703
+ /**
1704
+ * Time complexity: O(n)
1705
+ * Space complexity: O(1)
1706
+ */
1707
+
1703
1708
  /**
1704
1709
  * The `forEach` function iterates over each entry in a tree and calls a callback function with the
1705
1710
  * entry and the tree as arguments.
@@ -1730,15 +1735,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1730
1735
  return newTree;
1731
1736
  }
1732
1737
 
1733
- // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1734
- // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1735
- // const newTree = this.createTree();
1736
- // for (const [key, value] of this) {
1737
- // newTree.add(key, callback([key, value], this));
1738
- // }
1739
- // return newTree;
1740
- // }
1741
-
1742
1738
  /**
1743
1739
  * The `map` function creates a new tree by applying a callback function to each entry in the current
1744
1740
  * tree.
@@ -1753,6 +1749,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1753
1749
  return newTree;
1754
1750
  }
1755
1751
 
1752
+ // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1753
+ // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1754
+ // const newTree = this.createTree();
1755
+ // for (const [key, value] of this) {
1756
+ // newTree.add(key, callback([key, value], this));
1757
+ // }
1758
+ // return newTree;
1759
+ // }
1760
+
1756
1761
  /**
1757
1762
  * The `reduce` function iterates over the entries of a tree and applies a callback function to each
1758
1763
  * entry, accumulating a single value.
@@ -1773,7 +1778,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1773
1778
  return accumulator;
1774
1779
  }
1775
1780
 
1776
-
1777
1781
  /**
1778
1782
  * The above function is an iterator for a binary tree that can be used to traverse the tree in
1779
1783
  * either an iterative or recursive manner.
@@ -1786,7 +1790,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1786
1790
  * [Symbol.iterator](node = this.root): Generator<[BTNKey, V | undefined], void, undefined> {
1787
1791
  if (!node) return;
1788
1792
 
1789
- if (this.options.iterationType === IterationType.ITERATIVE) {
1793
+ if (this.iterationType === IterationType.ITERATIVE) {
1790
1794
  const stack: (N | null | undefined)[] = [];
1791
1795
  let current: N | null | undefined = node;
1792
1796
 
@@ -1843,6 +1847,19 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1843
1847
  display(beginRoot);
1844
1848
  }
1845
1849
 
1850
+ init(elements: IterableEntriesOrKeys<V>): void {
1851
+ if (elements) {
1852
+ for (const entryOrKey of elements) {
1853
+ if (Array.isArray(entryOrKey)) {
1854
+ const [key, value] = entryOrKey;
1855
+ this.add(key, value);
1856
+ } else {
1857
+ this.add(entryOrKey);
1858
+ }
1859
+ }
1860
+ }
1861
+ }
1862
+
1846
1863
  protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout {
1847
1864
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1848
1865
  const emptyDisplayLayout = <NodeDisplayLayout>[['─'], 1, 0, 0];