data-structure-typed 1.41.9 → 1.42.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.41.9",
3
+ "version": "1.42.0",
4
4
  "description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
5
5
  "main": "dist/cjs/src/index.js",
6
6
  "module": "dist/mjs/src/index.js",
@@ -108,8 +108,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
108
108
  * @template N - The type of the binary tree's nodes.
109
109
  */
110
110
  export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
111
- implements IBinaryTree<V, N>
112
- {
111
+ implements IBinaryTree<V, N> {
113
112
  iterationType: IterationType = IterationType.ITERATIVE;
114
113
 
115
114
  /**
@@ -391,7 +390,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
391
390
  return -1;
392
391
  }
393
392
 
394
- const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
393
+ const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
395
394
  let maxHeight = 0;
396
395
 
397
396
  while (stack.length > 0) {
@@ -846,6 +845,27 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
846
845
  return this.isSubtreeBST(this.root, iterationType);
847
846
  }
848
847
 
848
+ subTreeTraverse<C extends BTNCallback<N>>(
849
+ callback?: C,
850
+ beginRoot?: BTNKey | N | null,
851
+ iterationType?: IterationType,
852
+ includeNull?: false
853
+ ): ReturnType<C>[]
854
+
855
+ subTreeTraverse<C extends BTNCallback<N>>(
856
+ callback?: C,
857
+ beginRoot?: BTNKey | N | null,
858
+ iterationType?: IterationType,
859
+ includeNull?: undefined
860
+ ): ReturnType<C>[]
861
+
862
+ subTreeTraverse<C extends BTNCallback<N | null>>(
863
+ callback?: C,
864
+ beginRoot?: BTNKey | N | null,
865
+ iterationType?: IterationType,
866
+ includeNull?: true
867
+ ): ReturnType<C>[]
868
+
849
869
  /**
850
870
  * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
851
871
  * node, either recursively or iteratively.
@@ -858,40 +878,79 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
858
878
  * start from the root of the tree.
859
879
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
860
880
  * performed on the binary tree. It can have two possible values:
881
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
861
882
  * @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
862
883
  */
863
- subTreeTraverse<C extends BTNCallback<N>>(
884
+ subTreeTraverse<C extends BTNCallback<N | null>>(
864
885
  callback: C = this.defaultOneParamCallback as C,
865
886
  beginRoot: BTNKey | N | null = this.root,
866
- iterationType = this.iterationType
887
+ iterationType = this.iterationType,
888
+ includeNull = false
867
889
  ): ReturnType<C>[] {
868
890
  if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
869
891
 
870
- const ans: ReturnType<BTNCallback<N>>[] = [];
892
+ const ans: (ReturnType<BTNCallback<N>> | null)[] = [];
871
893
  if (!beginRoot) return ans;
872
894
 
873
895
  if (iterationType === IterationType.RECURSIVE) {
874
- const _traverse = (cur: N) => {
875
- ans.push(callback(cur));
876
- cur.left && _traverse(cur.left);
877
- cur.right && _traverse(cur.right);
896
+ const _traverse = (cur: N | null) => {
897
+ if (cur !== undefined) {
898
+ ans.push(callback(cur));
899
+ if (includeNull) {
900
+ cur !== null && cur.left !== undefined && _traverse(cur.left);
901
+ cur !== null && cur.right !== undefined && _traverse(cur.right);
902
+ } else {
903
+ cur !== null && cur.left && _traverse(cur.left);
904
+ cur !== null && cur.right && _traverse(cur.right);
905
+ }
906
+ }
878
907
  };
879
908
 
880
909
  _traverse(beginRoot);
881
910
  } else {
882
- const stack: N[] = [beginRoot];
911
+ const stack: (N| null)[] = [beginRoot];
883
912
 
884
913
  while (stack.length > 0) {
885
- const cur = stack.pop()!;
886
-
887
- ans.push(callback(cur));
888
- cur.right && stack.push(cur.right);
889
- cur.left && stack.push(cur.left);
914
+ const cur = stack.pop();
915
+ if (cur !== undefined) {
916
+ ans.push(callback(cur));
917
+ if (includeNull) {
918
+ cur !== null && cur.right !== undefined && stack.push(cur.right);
919
+ cur !== null && cur.left !== undefined && stack.push(cur.left);
920
+ } else {
921
+ cur !== null && cur.right && stack.push(cur.right);
922
+ cur !== null && cur.left && stack.push(cur.left);
923
+ }
924
+ }
890
925
  }
891
926
  }
892
927
  return ans;
893
928
  }
894
929
 
930
+ dfs<C extends BTNCallback<N>>(
931
+ callback?: C,
932
+ pattern?: DFSOrderPattern,
933
+ beginRoot?: N | null,
934
+ iterationType?: IterationType,
935
+ includeNull?: false
936
+ ): ReturnType<C>[]
937
+
938
+ dfs<C extends BTNCallback<N>>(
939
+ callback?: C,
940
+ pattern?: DFSOrderPattern,
941
+ beginRoot?: N | null,
942
+ iterationType?: IterationType,
943
+ includeNull?: undefined
944
+ ): ReturnType<C>[]
945
+
946
+ dfs<C extends BTNCallback<N | null>>(
947
+ callback?: C,
948
+ pattern?: DFSOrderPattern,
949
+ beginRoot?: N | null,
950
+ iterationType?: IterationType,
951
+ includeNull?: true
952
+ ): ReturnType<C>[]
953
+
895
954
  /**
896
955
  * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
897
956
  * function on each node according to a specified order pattern.
@@ -905,34 +964,53 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
905
964
  * is `null`, an empty array will be returned.
906
965
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
907
966
  * iteration used in the depth-first search algorithm. It can have two possible values:
967
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
908
968
  * @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
909
969
  */
910
- dfs<C extends BTNCallback<N>>(
970
+ dfs<C extends BTNCallback<N | null>>(
911
971
  callback: C = this.defaultOneParamCallback as C,
912
972
  pattern: DFSOrderPattern = 'in',
913
973
  beginRoot: N | null = this.root,
914
- iterationType: IterationType = IterationType.ITERATIVE
974
+ iterationType: IterationType = IterationType.ITERATIVE,
975
+ includeNull = false
915
976
  ): ReturnType<C>[] {
916
977
  if (!beginRoot) return [];
917
- const ans: ReturnType<BTNCallback<N>>[] = [];
978
+ const ans: ReturnType<C>[] = [];
918
979
  if (iterationType === IterationType.RECURSIVE) {
919
- const _traverse = (node: N) => {
980
+ const _traverse = (node: N | null) => {
920
981
  switch (pattern) {
921
982
  case 'in':
922
- if (node.left) _traverse(node.left);
923
- ans.push(callback(node));
924
- if (node.right) _traverse(node.right);
983
+ if (includeNull) {
984
+ if (node !== null && node.left !== undefined) _traverse(node.left);
985
+ ans.push(callback(node));
986
+ if (node !== null && node.right !== undefined) _traverse(node.right);
987
+ } else {
988
+ if (node !== null && node.left) _traverse(node.left);
989
+ ans.push(callback(node));
990
+ if (node !== null && node.right) _traverse(node.right);
991
+ }
925
992
  break;
926
993
  case 'pre':
927
- ans.push(callback(node));
928
-
929
- if (node.left) _traverse(node.left);
930
- if (node.right) _traverse(node.right);
994
+ if (includeNull) {
995
+ ans.push(callback(node));
996
+ if (node !== null && node.left !== undefined) _traverse(node.left);
997
+ if (node !== null && node.right !== undefined) _traverse(node.right);
998
+ } else {
999
+ ans.push(callback(node));
1000
+ if (node !== null && node.left) _traverse(node.left);
1001
+ if (node !== null && node.right) _traverse(node.right);
1002
+ }
931
1003
  break;
932
1004
  case 'post':
933
- if (node.left) _traverse(node.left);
934
- if (node.right) _traverse(node.right);
935
- ans.push(callback(node));
1005
+ if (includeNull) {
1006
+ if (node !== null && node.left !== undefined) _traverse(node.left);
1007
+ if (node !== null && node.right !== undefined) _traverse(node.right);
1008
+ ans.push(callback(node));
1009
+ } else {
1010
+ if (node !== null && node.left) _traverse(node.left);
1011
+ if (node !== null && node.right) _traverse(node.right);
1012
+ ans.push(callback(node));
1013
+ }
936
1014
 
937
1015
  break;
938
1016
  }
@@ -941,34 +1019,39 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
941
1019
  _traverse(beginRoot);
942
1020
  } else {
943
1021
  // 0: visit, 1: print
944
- const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
1022
+ const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
945
1023
 
946
1024
  while (stack.length > 0) {
947
1025
  const cur = stack.pop();
948
- if (!cur || !cur.node) continue;
1026
+ if (cur === undefined) continue;
1027
+ if (includeNull) {
1028
+ if (cur.node === undefined) continue;
1029
+ } else {
1030
+ if (cur.node === null || cur.node === undefined) continue;
1031
+ }
949
1032
  if (cur.opt === 1) {
950
1033
  ans.push(callback(cur.node));
951
1034
  } else {
952
1035
  switch (pattern) {
953
1036
  case 'in':
954
- stack.push({opt: 0, node: cur.node.right});
1037
+ cur.node && stack.push({opt: 0, node: cur.node.right});
955
1038
  stack.push({opt: 1, node: cur.node});
956
- stack.push({opt: 0, node: cur.node.left});
1039
+ cur.node && stack.push({opt: 0, node: cur.node.left});
957
1040
  break;
958
1041
  case 'pre':
959
- stack.push({opt: 0, node: cur.node.right});
960
- stack.push({opt: 0, node: cur.node.left});
1042
+ cur.node && stack.push({opt: 0, node: cur.node.right});
1043
+ cur.node && stack.push({opt: 0, node: cur.node.left});
961
1044
  stack.push({opt: 1, node: cur.node});
962
1045
  break;
963
1046
  case 'post':
964
1047
  stack.push({opt: 1, node: cur.node});
965
- stack.push({opt: 0, node: cur.node.right});
966
- stack.push({opt: 0, node: cur.node.left});
1048
+ cur.node && stack.push({opt: 0, node: cur.node.right});
1049
+ cur.node && stack.push({opt: 0, node: cur.node.left});
967
1050
  break;
968
1051
  default:
969
- stack.push({opt: 0, node: cur.node.right});
1052
+ cur.node && stack.push({opt: 0, node: cur.node.right});
970
1053
  stack.push({opt: 1, node: cur.node});
971
- stack.push({opt: 0, node: cur.node.left});
1054
+ cur.node && stack.push({opt: 0, node: cur.node.left});
972
1055
  break;
973
1056
  }
974
1057
  }
@@ -978,6 +1061,27 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
978
1061
  return ans;
979
1062
  }
980
1063
 
1064
+ bfs<C extends BTNCallback<N>>(
1065
+ callback?: C,
1066
+ beginRoot?: N | null,
1067
+ iterationType?: IterationType,
1068
+ includeNull?: false
1069
+ ): ReturnType<C>[]
1070
+
1071
+ bfs<C extends BTNCallback<N>>(
1072
+ callback?: C,
1073
+ beginRoot?: N | null,
1074
+ iterationType?: IterationType,
1075
+ includeNull?: undefined
1076
+ ): ReturnType<C>[]
1077
+
1078
+ bfs<C extends BTNCallback<N | null>>(
1079
+ callback?: C,
1080
+ beginRoot?: N | null,
1081
+ iterationType?: IterationType,
1082
+ includeNull?: true
1083
+ ): ReturnType<C>[]
1084
+
981
1085
  /**
982
1086
  * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
983
1087
  * function on each node.
@@ -989,19 +1093,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
989
1093
  * will not be performed and an empty array will be returned.
990
1094
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
991
1095
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
1096
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
992
1097
  * @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
993
1098
  */
994
- bfs<C extends BTNCallback<N>>(
1099
+ bfs<C extends BTNCallback<N | null>>(
995
1100
  callback: C = this.defaultOneParamCallback as C,
996
1101
  beginRoot: N | null = this.root,
997
- iterationType = this.iterationType
1102
+ iterationType = this.iterationType,
1103
+ includeNull = false
998
1104
  ): ReturnType<C>[] {
999
1105
  if (!beginRoot) return [];
1000
1106
 
1001
1107
  const ans: ReturnType<BTNCallback<N>>[] = [];
1002
1108
 
1003
1109
  if (iterationType === IterationType.RECURSIVE) {
1004
- const queue = new Queue<N>([beginRoot]);
1110
+ const queue: Queue<N | null> = new Queue<N | null>([beginRoot]);
1005
1111
 
1006
1112
  const traverse = (level: number) => {
1007
1113
  if (queue.size === 0) return;
@@ -1009,15 +1115,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1009
1115
  const current = queue.shift()!;
1010
1116
  ans.push(callback(current));
1011
1117
 
1012
- if (current.left) queue.push(current.left);
1013
- if (current.right) queue.push(current.right);
1118
+ if (includeNull) {
1119
+ if (current && current.left !== undefined) queue.push(current.left);
1120
+ if (current && current.right !== undefined) queue.push(current.right);
1121
+ } else {
1122
+ if (current.left) queue.push(current.left);
1123
+ if (current.right) queue.push(current.right);
1124
+ }
1125
+
1014
1126
 
1015
1127
  traverse(level + 1);
1016
1128
  };
1017
1129
 
1018
1130
  traverse(0);
1019
1131
  } else {
1020
- const queue = new Queue<N>([beginRoot]);
1132
+ const queue = new Queue<N | null>([beginRoot]);
1021
1133
  while (queue.size > 0) {
1022
1134
  const levelSize = queue.size;
1023
1135
 
@@ -1025,14 +1137,41 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1025
1137
  const current = queue.shift()!;
1026
1138
  ans.push(callback(current));
1027
1139
 
1028
- if (current.left) queue.push(current.left);
1029
- if (current.right) queue.push(current.right);
1140
+ if (includeNull) {
1141
+ if (current !== null && current.left !== undefined) queue.push(current.left);
1142
+ if (current !== null && current.right !== undefined) queue.push(current.right);
1143
+ } else {
1144
+ if (current.left) queue.push(current.left);
1145
+ if (current.right) queue.push(current.right);
1146
+ }
1147
+
1030
1148
  }
1031
1149
  }
1032
1150
  }
1033
1151
  return ans;
1034
1152
  }
1035
1153
 
1154
+ listLevels<C extends BTNCallback<N>>(
1155
+ callback?: C ,
1156
+ beginRoot?: N | null ,
1157
+ iterationType?: IterationType,
1158
+ includeNull?: false
1159
+ ): ReturnType<C>[][]
1160
+
1161
+ listLevels<C extends BTNCallback<N>>(
1162
+ callback?: C ,
1163
+ beginRoot?: N | null ,
1164
+ iterationType?: IterationType,
1165
+ includeNull?: undefined
1166
+ ): ReturnType<C>[][]
1167
+
1168
+ listLevels<C extends BTNCallback<N | null>>(
1169
+ callback?: C ,
1170
+ beginRoot?: N | null ,
1171
+ iterationType?: IterationType,
1172
+ includeNull?: true
1173
+ ): ReturnType<C>[][]
1174
+
1036
1175
  /**
1037
1176
  * The `listLevels` function takes a binary tree node and a callback function, and returns an array
1038
1177
  * of arrays representing the levels of the tree.
@@ -1044,29 +1183,36 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1044
1183
  * from the root node of the binary tree.
1045
1184
  * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
1046
1185
  * recursively or iteratively. It can have two possible values:
1186
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
1047
1187
  * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
1048
1188
  * level in a binary tree. Each inner array contains the return type of the provided callback
1049
1189
  * function `C` applied to the nodes at that level.
1050
1190
  */
1051
- listLevels<C extends BTNCallback<N>>(
1191
+ listLevels<C extends BTNCallback<N | null>>(
1052
1192
  callback: C = this.defaultOneParamCallback as C,
1053
1193
  beginRoot: N | null = this.root,
1054
- iterationType = this.iterationType
1194
+ iterationType = this.iterationType,
1195
+ includeNull = false
1055
1196
  ): ReturnType<C>[][] {
1056
1197
  if (!beginRoot) return [];
1057
1198
  const levelsNodes: ReturnType<C>[][] = [];
1058
1199
 
1059
1200
  if (iterationType === IterationType.RECURSIVE) {
1060
- const _recursive = (node: N, level: number) => {
1201
+ const _recursive = (node: N | null, level: number) => {
1061
1202
  if (!levelsNodes[level]) levelsNodes[level] = [];
1062
1203
  levelsNodes[level].push(callback(node));
1063
- if (node.left) _recursive(node.left, level + 1);
1064
- if (node.right) _recursive(node.right, level + 1);
1204
+ if (includeNull) {
1205
+ if (node && node.left !== undefined) _recursive(node.left, level + 1);
1206
+ if (node && node.right !== undefined) _recursive(node.right, level + 1);
1207
+ } else {
1208
+ if (node && node.left) _recursive(node.left, level + 1);
1209
+ if (node && node.right) _recursive(node.right, level + 1);
1210
+ }
1065
1211
  };
1066
1212
 
1067
1213
  _recursive(beginRoot, 0);
1068
1214
  } else {
1069
- const stack: [N, number][] = [[beginRoot, 0]];
1215
+ const stack: [N | null, number][] = [[beginRoot, 0]];
1070
1216
 
1071
1217
  while (stack.length > 0) {
1072
1218
  const head = stack.pop()!;
@@ -1074,8 +1220,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1074
1220
 
1075
1221
  if (!levelsNodes[level]) levelsNodes[level] = [];
1076
1222
  levelsNodes[level].push(callback(node));
1077
- if (node.right) stack.push([node.right, level + 1]);
1078
- if (node.left) stack.push([node.left, level + 1]);
1223
+
1224
+ if (includeNull) {
1225
+ if (node && node.right !== undefined) stack.push([node.right, level + 1]);
1226
+ if (node && node.left !== undefined) stack.push([node.left, level + 1]);
1227
+ } else {
1228
+ if (node && node.right) stack.push([node.right, level + 1]);
1229
+ if (node && node.left) stack.push([node.left, level + 1]);
1230
+ }
1079
1231
  }
1080
1232
  }
1081
1233
 
@@ -1231,7 +1383,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1231
1383
  * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1232
1384
  * binary tree nodes in a specific order.
1233
1385
  */
1234
- *[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
1386
+ * [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
1235
1387
  if (!node) {
1236
1388
  return;
1237
1389
  }
@@ -174,8 +174,11 @@ describe('BinaryTree', () => {
174
174
  });
175
175
 
176
176
  it('should subTreeTraverse', () => {
177
- tree.addMany([4, 2, 6, 1, 3, 5, 7]);
178
- expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6, 5, 7]);
177
+ tree.addMany([4, 2, 6, null, 1, 3, null, 5, null, 7]);
178
+ expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.ITERATIVE)).toEqual([6,3, 7]);
179
+ expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6,3, 7]);
180
+ expect(tree.subTreeTraverse(node => node === null? null : node.key, tree.getNode(6), IterationType.ITERATIVE, true)).toEqual([6, 3, 7, null]);
181
+ expect(tree.subTreeTraverse(node => node === null? null : node.key, tree.getNode(6), IterationType.RECURSIVE, true)).toEqual([6, 3, 7, null]);
179
182
  });
180
183
 
181
184
  it('should clear the tree', () => {
@@ -266,10 +269,18 @@ describe('BinaryTree traversals', () => {
266
269
 
267
270
  const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
268
271
  tree.refill(arr);
272
+ expect(tree.bfs(node => node , tree.root, IterationType.ITERATIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
273
+ expect(tree.bfs(node => node , tree.root, IterationType.RECURSIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
274
+ expect(tree.bfs(node => node , tree.root, IterationType.ITERATIVE).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
275
+ expect(tree.bfs(node => node , tree.root, IterationType.RECURSIVE).map(node => node === null ? null : node.key)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
276
+
269
277
  expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
270
278
  expect(tree.dfs(node => node.key, 'pre', tree.root, IterationType.RECURSIVE)).toEqual([
271
279
  35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55
272
280
  ]);
281
+ expect(tree.dfs(node => node, 'pre', tree.root, IterationType.ITERATIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
282
+ expect(tree.dfs(node => node, 'pre', tree.root, IterationType.RECURSIVE, true).map(node => node === null ? null : node.key)).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
283
+
273
284
  expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
274
285
  expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
275
286
  expect(tree.dfs(node => node.key, 'post', tree.root, IterationType.RECURSIVE)).toEqual([
@@ -282,9 +293,7 @@ describe('BinaryTree traversals', () => {
282
293
  35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
283
294
  ]);
284
295
 
285
- const levels = tree.listLevels(node => node.key);
286
- expect(levels).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
287
- isDebug && console.log(levels);
296
+ expect(tree.listLevels(node => node.key)).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
288
297
 
289
298
  expect(tree.listLevels(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([
290
299
  [35],
@@ -292,9 +301,21 @@ describe('BinaryTree traversals', () => {
292
301
  [15, 29, 50],
293
302
  [16, 28, 30, 45, 55]
294
303
  ]);
295
- isDebug && console.log(levels);
304
+ expect(tree.listLevels(node => node === null ? null :node.key, tree.root, IterationType.ITERATIVE, true)).toEqual([
305
+ [35],
306
+ [20, 40],
307
+ [15, 29, null,50],
308
+ [null, 16, 28, 30, 45, 55]
309
+ ]);
310
+ expect(tree.listLevels(node => node === null ? null :node.key, tree.root, IterationType.RECURSIVE, true)).toEqual([
311
+ [35],
312
+ [20, 40],
313
+ [15, 29, null,50],
314
+ [null, 16, 28, 30, 45, 55]
315
+ ]);
296
316
  });
297
317
 
318
+
298
319
  describe('BinaryTree', () => {
299
320
  let tree: BinaryTree<string>;
300
321