data-structure-typed 1.41.9 → 1.42.1

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.
@@ -714,9 +714,10 @@ class BinaryTree {
714
714
  * start from the root of the tree.
715
715
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
716
716
  * performed on the binary tree. It can have two possible values:
717
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
717
718
  * @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
718
719
  */
719
- subTreeTraverse(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
720
+ subTreeTraverse(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
720
721
  if (typeof beginRoot === 'number')
721
722
  beginRoot = this.getNode(beginRoot);
722
723
  const ans = [];
@@ -724,9 +725,17 @@ class BinaryTree {
724
725
  return ans;
725
726
  if (iterationType === types_1.IterationType.RECURSIVE) {
726
727
  const _traverse = (cur) => {
727
- ans.push(callback(cur));
728
- cur.left && _traverse(cur.left);
729
- cur.right && _traverse(cur.right);
728
+ if (cur !== undefined) {
729
+ ans.push(callback(cur));
730
+ if (includeNull) {
731
+ cur !== null && cur.left !== undefined && _traverse(cur.left);
732
+ cur !== null && cur.right !== undefined && _traverse(cur.right);
733
+ }
734
+ else {
735
+ cur !== null && cur.left && _traverse(cur.left);
736
+ cur !== null && cur.right && _traverse(cur.right);
737
+ }
738
+ }
730
739
  };
731
740
  _traverse(beginRoot);
732
741
  }
@@ -734,9 +743,17 @@ class BinaryTree {
734
743
  const stack = [beginRoot];
735
744
  while (stack.length > 0) {
736
745
  const cur = stack.pop();
737
- ans.push(callback(cur));
738
- cur.right && stack.push(cur.right);
739
- cur.left && stack.push(cur.left);
746
+ if (cur !== undefined) {
747
+ ans.push(callback(cur));
748
+ if (includeNull) {
749
+ cur !== null && cur.right !== undefined && stack.push(cur.right);
750
+ cur !== null && cur.left !== undefined && stack.push(cur.left);
751
+ }
752
+ else {
753
+ cur !== null && cur.right && stack.push(cur.right);
754
+ cur !== null && cur.left && stack.push(cur.left);
755
+ }
756
+ }
740
757
  }
741
758
  }
742
759
  return ans;
@@ -754,9 +771,10 @@ class BinaryTree {
754
771
  * is `null`, an empty array will be returned.
755
772
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
756
773
  * iteration used in the depth-first search algorithm. It can have two possible values:
774
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
757
775
  * @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
758
776
  */
759
- dfs(callback = this.defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
777
+ dfs(callback = this.defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
760
778
  if (!beginRoot)
761
779
  return [];
762
780
  const ans = [];
@@ -764,25 +782,52 @@ class BinaryTree {
764
782
  const _traverse = (node) => {
765
783
  switch (pattern) {
766
784
  case 'in':
767
- if (node.left)
768
- _traverse(node.left);
769
- ans.push(callback(node));
770
- if (node.right)
771
- _traverse(node.right);
785
+ if (includeNull) {
786
+ if (node !== null && node.left !== undefined)
787
+ _traverse(node.left);
788
+ ans.push(callback(node));
789
+ if (node !== null && node.right !== undefined)
790
+ _traverse(node.right);
791
+ }
792
+ else {
793
+ if (node !== null && node.left)
794
+ _traverse(node.left);
795
+ ans.push(callback(node));
796
+ if (node !== null && node.right)
797
+ _traverse(node.right);
798
+ }
772
799
  break;
773
800
  case 'pre':
774
- ans.push(callback(node));
775
- if (node.left)
776
- _traverse(node.left);
777
- if (node.right)
778
- _traverse(node.right);
801
+ if (includeNull) {
802
+ ans.push(callback(node));
803
+ if (node !== null && node.left !== undefined)
804
+ _traverse(node.left);
805
+ if (node !== null && node.right !== undefined)
806
+ _traverse(node.right);
807
+ }
808
+ else {
809
+ ans.push(callback(node));
810
+ if (node !== null && node.left)
811
+ _traverse(node.left);
812
+ if (node !== null && node.right)
813
+ _traverse(node.right);
814
+ }
779
815
  break;
780
816
  case 'post':
781
- if (node.left)
782
- _traverse(node.left);
783
- if (node.right)
784
- _traverse(node.right);
785
- ans.push(callback(node));
817
+ if (includeNull) {
818
+ if (node !== null && node.left !== undefined)
819
+ _traverse(node.left);
820
+ if (node !== null && node.right !== undefined)
821
+ _traverse(node.right);
822
+ ans.push(callback(node));
823
+ }
824
+ else {
825
+ if (node !== null && node.left)
826
+ _traverse(node.left);
827
+ if (node !== null && node.right)
828
+ _traverse(node.right);
829
+ ans.push(callback(node));
830
+ }
786
831
  break;
787
832
  }
788
833
  };
@@ -793,32 +838,40 @@ class BinaryTree {
793
838
  const stack = [{ opt: 0, node: beginRoot }];
794
839
  while (stack.length > 0) {
795
840
  const cur = stack.pop();
796
- if (!cur || !cur.node)
841
+ if (cur === undefined)
797
842
  continue;
843
+ if (includeNull) {
844
+ if (cur.node === undefined)
845
+ continue;
846
+ }
847
+ else {
848
+ if (cur.node === null || cur.node === undefined)
849
+ continue;
850
+ }
798
851
  if (cur.opt === 1) {
799
852
  ans.push(callback(cur.node));
800
853
  }
801
854
  else {
802
855
  switch (pattern) {
803
856
  case 'in':
804
- stack.push({ opt: 0, node: cur.node.right });
857
+ cur.node && stack.push({ opt: 0, node: cur.node.right });
805
858
  stack.push({ opt: 1, node: cur.node });
806
- stack.push({ opt: 0, node: cur.node.left });
859
+ cur.node && stack.push({ opt: 0, node: cur.node.left });
807
860
  break;
808
861
  case 'pre':
809
- stack.push({ opt: 0, node: cur.node.right });
810
- stack.push({ opt: 0, node: cur.node.left });
862
+ cur.node && stack.push({ opt: 0, node: cur.node.right });
863
+ cur.node && stack.push({ opt: 0, node: cur.node.left });
811
864
  stack.push({ opt: 1, node: cur.node });
812
865
  break;
813
866
  case 'post':
814
867
  stack.push({ opt: 1, node: cur.node });
815
- stack.push({ opt: 0, node: cur.node.right });
816
- stack.push({ opt: 0, node: cur.node.left });
868
+ cur.node && stack.push({ opt: 0, node: cur.node.right });
869
+ cur.node && stack.push({ opt: 0, node: cur.node.left });
817
870
  break;
818
871
  default:
819
- stack.push({ opt: 0, node: cur.node.right });
872
+ cur.node && stack.push({ opt: 0, node: cur.node.right });
820
873
  stack.push({ opt: 1, node: cur.node });
821
- stack.push({ opt: 0, node: cur.node.left });
874
+ cur.node && stack.push({ opt: 0, node: cur.node.left });
822
875
  break;
823
876
  }
824
877
  }
@@ -837,9 +890,10 @@ class BinaryTree {
837
890
  * will not be performed and an empty array will be returned.
838
891
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
839
892
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
893
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
840
894
  * @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
841
895
  */
842
- bfs(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
896
+ bfs(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
843
897
  if (!beginRoot)
844
898
  return [];
845
899
  const ans = [];
@@ -850,10 +904,18 @@ class BinaryTree {
850
904
  return;
851
905
  const current = queue.shift();
852
906
  ans.push(callback(current));
853
- if (current.left)
854
- queue.push(current.left);
855
- if (current.right)
856
- queue.push(current.right);
907
+ if (includeNull) {
908
+ if (current && current.left !== undefined)
909
+ queue.push(current.left);
910
+ if (current && current.right !== undefined)
911
+ queue.push(current.right);
912
+ }
913
+ else {
914
+ if (current.left)
915
+ queue.push(current.left);
916
+ if (current.right)
917
+ queue.push(current.right);
918
+ }
857
919
  traverse(level + 1);
858
920
  };
859
921
  traverse(0);
@@ -865,10 +927,18 @@ class BinaryTree {
865
927
  for (let i = 0; i < levelSize; i++) {
866
928
  const current = queue.shift();
867
929
  ans.push(callback(current));
868
- if (current.left)
869
- queue.push(current.left);
870
- if (current.right)
871
- queue.push(current.right);
930
+ if (includeNull) {
931
+ if (current !== null && current.left !== undefined)
932
+ queue.push(current.left);
933
+ if (current !== null && current.right !== undefined)
934
+ queue.push(current.right);
935
+ }
936
+ else {
937
+ if (current.left)
938
+ queue.push(current.left);
939
+ if (current.right)
940
+ queue.push(current.right);
941
+ }
872
942
  }
873
943
  }
874
944
  }
@@ -885,11 +955,12 @@ class BinaryTree {
885
955
  * from the root node of the binary tree.
886
956
  * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
887
957
  * recursively or iteratively. It can have two possible values:
958
+ * @param includeNull - The choice to output null values during binary tree traversal should be provided.
888
959
  * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
889
960
  * level in a binary tree. Each inner array contains the return type of the provided callback
890
961
  * function `C` applied to the nodes at that level.
891
962
  */
892
- listLevels(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
963
+ listLevels(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
893
964
  if (!beginRoot)
894
965
  return [];
895
966
  const levelsNodes = [];
@@ -898,10 +969,18 @@ class BinaryTree {
898
969
  if (!levelsNodes[level])
899
970
  levelsNodes[level] = [];
900
971
  levelsNodes[level].push(callback(node));
901
- if (node.left)
902
- _recursive(node.left, level + 1);
903
- if (node.right)
904
- _recursive(node.right, level + 1);
972
+ if (includeNull) {
973
+ if (node && node.left !== undefined)
974
+ _recursive(node.left, level + 1);
975
+ if (node && node.right !== undefined)
976
+ _recursive(node.right, level + 1);
977
+ }
978
+ else {
979
+ if (node && node.left)
980
+ _recursive(node.left, level + 1);
981
+ if (node && node.right)
982
+ _recursive(node.right, level + 1);
983
+ }
905
984
  };
906
985
  _recursive(beginRoot, 0);
907
986
  }
@@ -913,10 +992,18 @@ class BinaryTree {
913
992
  if (!levelsNodes[level])
914
993
  levelsNodes[level] = [];
915
994
  levelsNodes[level].push(callback(node));
916
- if (node.right)
917
- stack.push([node.right, level + 1]);
918
- if (node.left)
919
- stack.push([node.left, level + 1]);
995
+ if (includeNull) {
996
+ if (node && node.right !== undefined)
997
+ stack.push([node.right, level + 1]);
998
+ if (node && node.left !== undefined)
999
+ stack.push([node.left, level + 1]);
1000
+ }
1001
+ else {
1002
+ if (node && node.right)
1003
+ stack.push([node.right, level + 1]);
1004
+ if (node && node.left)
1005
+ stack.push([node.left, level + 1]);
1006
+ }
920
1007
  }
921
1008
  }
922
1009
  return levelsNodes;