data-structure-typed 1.52.8 → 1.53.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.
Files changed (67) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +151 -151
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +64 -47
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +28 -26
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +395 -269
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.d.ts +56 -56
  15. package/dist/cjs/data-structures/binary-tree/bst.js +114 -91
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js +35 -31
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
  21. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +59 -48
  22. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  23. package/dist/cjs/data-structures/trie/trie.js +3 -3
  24. package/dist/cjs/interfaces/binary-tree.d.ts +5 -5
  25. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
  26. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -3
  27. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
  28. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +64 -46
  29. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
  30. package/dist/mjs/data-structures/binary-tree/avl-tree.js +28 -25
  31. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
  32. package/dist/mjs/data-structures/binary-tree/binary-tree.js +395 -268
  33. package/dist/mjs/data-structures/binary-tree/bst.d.ts +56 -56
  34. package/dist/mjs/data-structures/binary-tree/bst.js +114 -89
  35. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
  36. package/dist/mjs/data-structures/binary-tree/rb-tree.js +35 -30
  37. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
  38. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +59 -47
  39. package/dist/mjs/data-structures/trie/trie.js +3 -3
  40. package/dist/mjs/interfaces/binary-tree.d.ts +5 -5
  41. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
  42. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -3
  43. package/dist/umd/data-structure-typed.js +617 -482
  44. package/dist/umd/data-structure-typed.min.js +5 -5
  45. package/dist/umd/data-structure-typed.min.js.map +1 -1
  46. package/package.json +6 -6
  47. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +60 -55
  48. package/src/data-structures/binary-tree/avl-tree.ts +31 -35
  49. package/src/data-structures/binary-tree/binary-tree.ts +461 -385
  50. package/src/data-structures/binary-tree/bst.ts +155 -128
  51. package/src/data-structures/binary-tree/rb-tree.ts +37 -39
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +57 -60
  53. package/src/data-structures/trie/trie.ts +3 -3
  54. package/src/interfaces/binary-tree.ts +6 -6
  55. package/src/types/data-structures/binary-tree/binary-tree.ts +14 -15
  56. package/src/types/data-structures/binary-tree/bst.ts +4 -4
  57. package/test/integration/bst.test.ts +2 -2
  58. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +1 -1
  59. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +2 -2
  60. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +1 -1
  61. package/test/performance/data-structures/binary-tree/bst.test.ts +1 -1
  62. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
  63. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +113 -1
  64. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +107 -1
  65. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +169 -26
  66. package/test/unit/data-structures/binary-tree/bst.test.ts +326 -21
  67. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +139 -1
@@ -678,7 +678,7 @@ describe('AVLTreeMultiMap iterative methods test', () => {
678
678
  expect(treeMM.count).toBe(21);
679
679
  const cloned = treeMM.clone();
680
680
  expect(cloned.root?.left?.key).toBe(1);
681
- expect(cloned.root?.right?.value).toBe('c');
681
+ expect(cloned.get(cloned.root?.right)).toBe('c');
682
682
  });
683
683
 
684
684
  it('should keys', () => {
@@ -761,3 +761,115 @@ describe('AVLTree toEntryFn', () => {
761
761
  expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
762
762
  });
763
763
  });
764
+
765
+ describe('AVLTreeMultiMap map mode count', () => {
766
+ let tm: AVLTreeMultiMap<number>;
767
+ beforeEach(() => {
768
+ tm = new AVLTreeMultiMap<number>([], { isMapMode: true });
769
+ });
770
+ it('Should added isolated node count ', () => {
771
+ tm.addMany([
772
+ [1, 1],
773
+ [2, 2],
774
+ [3, 3],
775
+ [4, 4],
776
+ [5, 5]
777
+ ]);
778
+ const newNode = new AVLTreeMultiMapNode(3, undefined, 10);
779
+ tm.add(newNode, 33);
780
+ expect(tm.count).toBe(15);
781
+ });
782
+ });
783
+
784
+ describe('AVLTreeMultiMap map mode operations test1', () => {
785
+ it('should perform various operations on a Binary Search Tree with numeric values1', () => {
786
+ const treeMultimap = new AVLTreeMultiMap<number>([], { isMapMode: true });
787
+
788
+ expect(treeMultimap instanceof AVLTreeMultiMap);
789
+ treeMultimap.add([11, 11]);
790
+ treeMultimap.add([3, 3]);
791
+ const idAndValues: [number, number][] = [
792
+ [11, 11],
793
+ [3, 3],
794
+ [15, 15],
795
+ [1, 1],
796
+ [8, 8],
797
+ [13, 13],
798
+ [16, 16],
799
+ [2, 2],
800
+ [6, 6],
801
+ [9, 9],
802
+ [12, 12],
803
+ [14, 14],
804
+ [4, 4],
805
+ [7, 7],
806
+ [10, 10],
807
+ [5, 5]
808
+ ];
809
+ treeMultimap.addMany(idAndValues);
810
+ expect(treeMultimap.root instanceof AVLTreeMultiMapNode);
811
+
812
+ if (treeMultimap.root) expect(treeMultimap.root.key == 11);
813
+
814
+ expect(treeMultimap.size).toBe(16);
815
+ expect(treeMultimap.count).toBe(18);
816
+
817
+ expect(treeMultimap.has(6));
818
+
819
+ expect(treeMultimap.getHeight(6)).toBe(4);
820
+ expect(treeMultimap.getDepth(6)).toBe(0);
821
+ const nodeId10 = treeMultimap.getNode(10);
822
+ expect(nodeId10?.key).toBe(10);
823
+
824
+ const nodeVal9 = treeMultimap.getNode(node => node.key === 9);
825
+ expect(nodeVal9?.key).toBe(9);
826
+ });
827
+ });
828
+
829
+ describe('AVLTreeMultiMap map mode operations test recursively1', () => {
830
+ it('should perform various operations on a Binary Search Tree with numeric values1', () => {
831
+ const treeMultimap = new AVLTreeMultiMap<number>([], {
832
+ iterationType: 'RECURSIVE',
833
+ isMapMode: true
834
+ });
835
+
836
+ expect(treeMultimap instanceof AVLTreeMultiMap);
837
+ treeMultimap.add([11, 11]);
838
+ treeMultimap.add([3, 3]);
839
+ const idAndValues: [number, number][] = [
840
+ [11, 11],
841
+ [3, 3],
842
+ [15, 15],
843
+ [1, 1],
844
+ [8, 8],
845
+ [13, 13],
846
+ [16, 16],
847
+ [2, 2],
848
+ [6, 6],
849
+ [9, 9],
850
+ [12, 12],
851
+ [14, 14],
852
+ [4, 4],
853
+ [7, 7],
854
+ [10, 10],
855
+ [5, 5]
856
+ ];
857
+ treeMultimap.addMany(idAndValues);
858
+ expect(treeMultimap.root).toBeInstanceOf(AVLTreeMultiMapNode);
859
+
860
+ if (treeMultimap.root) expect(treeMultimap.root.key).toBe(6);
861
+
862
+ expect(treeMultimap.size).toBe(16);
863
+ expect(treeMultimap.count).toBe(18);
864
+
865
+ expect(treeMultimap.has(6));
866
+
867
+ expect(treeMultimap.getHeight(6)).toBe(4);
868
+ expect(treeMultimap.getDepth(6)).toBe(0);
869
+ const nodeId10 = treeMultimap.getNode(10);
870
+ expect(nodeId10?.key).toBe(10);
871
+
872
+ const nodeVal9 = treeMultimap.getNode(node => node.key === 9);
873
+ expect(nodeVal9?.key).toBe(9);
874
+ });
875
+ });
@@ -33,7 +33,6 @@ describe('AVL Tree Test', () => {
33
33
 
34
34
  // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
35
35
  expect(node15?.value).toBe(15);
36
-
37
36
  const dfs = tree.dfs(node => node, 'IN');
38
37
  expect(dfs[0].key).toBe(1);
39
38
  expect(dfs[dfs.length - 1].key).toBe(16);
@@ -292,6 +291,26 @@ describe('AVLTree', () => {
292
291
  // You can add more specific assertions to check the tree's balance and structure.
293
292
  });
294
293
 
294
+ it('should addMany undefined and null', () => {
295
+ const avl = new AVLTree<number, string>();
296
+ const addManyWithUndefined = avl.addMany([1, undefined, 3]);
297
+ expect(addManyWithUndefined).toEqual([true, false, true]);
298
+ expect(avl.get(undefined)).toBe(undefined);
299
+ const addManyWithNull = avl.addMany([1, null, 3, 4]);
300
+ expect(addManyWithNull).toEqual([true, false, true, true]);
301
+ const addManyEntriesWithNull = avl.addMany([
302
+ [1, '1'],
303
+ [null, 'null'],
304
+ [3, '3'],
305
+ [4, '4']
306
+ ]);
307
+ expect(addManyEntriesWithNull).toEqual([true, false, true, true]);
308
+ expect(avl.get(null)).toBe(undefined);
309
+ const node0 = avl.add(0, '0');
310
+ expect(node0).toBe(true);
311
+ expect(avl.get(0)).toBe('0');
312
+ });
313
+
295
314
  it('should balance the tree using _balanceLR when nodes are deleted', () => {
296
315
  const avlTree = new AVLTree();
297
316
  avlTree.add([10, 'A']);
@@ -417,3 +436,90 @@ describe('AVLTree iterative methods test', () => {
417
436
  expect(leaves).toEqual([1, 3]);
418
437
  });
419
438
  });
439
+
440
+ describe('AVL Tree map mode', () => {
441
+ it('should perform various operations on a AVL Tree', () => {
442
+ const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
443
+ const tree = new AVLTree<number>([], { isMapMode: true });
444
+
445
+ for (const i of arr) tree.add([i, i]);
446
+
447
+ tree.add(null);
448
+ const node6 = tree.getNode(6);
449
+
450
+ expect(node6 && tree.getHeight(node6)).toBe(3);
451
+ expect(node6 && tree.getDepth(node6)).toBe(1);
452
+
453
+ const getNodeById = tree.getNode(10);
454
+ expect(getNodeById?.key).toBe(10);
455
+
456
+ const getMinNodeByRoot = tree.getLeftMost();
457
+ expect(getMinNodeByRoot).toBe(1);
458
+
459
+ const node15 = tree.getNode(15);
460
+ const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node => node, node15);
461
+ expect(getMinNodeBySpecificNode?.key).toBe(12);
462
+
463
+ let subTreeSum = 0;
464
+ if (node15) tree.dfs(node => (subTreeSum += node.key), 'PRE', node15);
465
+ expect(subTreeSum).toBe(70);
466
+
467
+ let lesserSum = 0;
468
+ tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
469
+ expect(lesserSum).toBe(45);
470
+
471
+ // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
472
+ expect(tree.get(node15)).toBe(15);
473
+ });
474
+ });
475
+
476
+ describe('AVL Tree map mode test recursively', () => {
477
+ it('should perform various operations on a AVL Tree', () => {
478
+ const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
479
+ const tree = new AVLTree<number>([], { iterationType: 'RECURSIVE', isMapMode: true });
480
+
481
+ for (const i of arr) tree.add([i, i]);
482
+
483
+ const node6 = tree.getNode(6);
484
+
485
+ expect(node6 && tree.getHeight(node6)).toBe(3);
486
+ expect(node6 && tree.getDepth(node6)).toBe(1);
487
+
488
+ const getNodeById = tree.getNode(10);
489
+ expect(getNodeById?.key).toBe(10);
490
+
491
+ const getMinNodeByRoot = tree.getLeftMost();
492
+ expect(getMinNodeByRoot).toBe(1);
493
+
494
+ const node15 = tree.getNode(15);
495
+ const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node => node, node15);
496
+ expect(getMinNodeBySpecificNode?.key).toBe(12);
497
+
498
+ let subTreeSum = 0;
499
+ if (node15) tree.dfs(node => (subTreeSum += node.key), 'PRE', node15);
500
+ expect(subTreeSum).toBe(70);
501
+
502
+ let lesserSum = 0;
503
+ tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
504
+ expect(lesserSum).toBe(45);
505
+
506
+ // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
507
+ expect(tree.get(node15)).toBe(15);
508
+ });
509
+ });
510
+
511
+ describe('AVLTree iterative methods map mode', () => {
512
+ let avl: AVLTree<number, string>;
513
+ beforeEach(() => {
514
+ avl = new AVLTree<number, string>([], { isMapMode: true });
515
+ avl.add([1, 'a']);
516
+ avl.add([2, 'b']);
517
+ avl.add([3, 'c']);
518
+ });
519
+
520
+ it('should clone work well', () => {
521
+ const cloned = avl.clone();
522
+ expect(cloned.root?.left?.key).toBe(1);
523
+ expect(cloned.get(cloned.root?.right?.key)).toBe('c');
524
+ });
525
+ });
@@ -20,6 +20,8 @@ describe('BinaryTreeNode', () => {
20
20
 
21
21
  it('should set and get the value correctly', () => {
22
22
  const node: BinaryTreeNode<number> = new BinaryTreeNode<number>(1, 42);
23
+ expect(node.key).toBe(1);
24
+
23
25
  expect(node.value).toBe(42);
24
26
 
25
27
  node.value = 55;
@@ -105,6 +107,27 @@ describe('BinaryTree addMany', () => {
105
107
  expect(tree.getNodeByKey(4)?.value).toBe(44);
106
108
  expect(tree.getNodeByKey(1)?.value).toBe(1);
107
109
  });
110
+
111
+ it('should addMany undefined and null', () => {
112
+ const binaryTree = new BinaryTree<number, string>();
113
+ const addManyWithUndefined = binaryTree.addMany([1, undefined, 3]);
114
+ expect(addManyWithUndefined).toEqual([true, false, true]);
115
+ expect(binaryTree.get(undefined)).toBe(undefined);
116
+ const addManyWithNull = binaryTree.addMany([1, null, 3, 4]);
117
+ expect(addManyWithNull).toEqual([true, true, true, true]);
118
+ const addManyEntriesWithNull = binaryTree.addMany([
119
+ [1, '1'],
120
+ [null, 'null'],
121
+ [3, '3'],
122
+ [4, '4']
123
+ ]);
124
+ expect(addManyEntriesWithNull).toEqual([true, true, true, true]);
125
+ expect(binaryTree.get(null)).toBe(undefined);
126
+ expect(binaryTree.getNode(null)).toBe(null);
127
+ const node0 = binaryTree.add(0, '0');
128
+ expect(node0).toBe(true);
129
+ expect(binaryTree.get(0)).toBe('0');
130
+ });
108
131
  });
109
132
 
110
133
  describe('BinaryTree', () => {
@@ -299,7 +322,8 @@ describe('BinaryTree', () => {
299
322
  expect(tree.toVisual()).toBe('');
300
323
  tree.addMany([4, 2, 6, 1, 3, 5, 7, 4]);
301
324
  expect(tree.toVisual()).toBe(
302
- ' ___4___ \n' +
325
+ 'N for null\n' +
326
+ ' ___4___ \n' +
303
327
  ' / \\ \n' +
304
328
  ' _2_ _6_ \n' +
305
329
  ' / \\ / \\ \n' +
@@ -309,9 +333,9 @@ describe('BinaryTree', () => {
309
333
  const visualized = tree.toVisual(undefined, { isShowUndefined: true, isShowNull: true, isShowRedBlackNIL: true });
310
334
  expect(visualized).toBe(
311
335
  'U for undefined\n' +
312
- ' N for null\n' +
313
- ' S for Sentinel Node(NIL)\n' +
314
- ' _______4_______ \n' +
336
+ 'N for null\n' +
337
+ 'S for Sentinel Node(NIL)\n' +
338
+ ' _______4_______ \n' +
315
339
  ' / \\ \n' +
316
340
  ' ___2___ ___6___ \n' +
317
341
  ' / \\ / \\ \n' +
@@ -709,34 +733,40 @@ describe('BinaryTree', () => {
709
733
  ]);
710
734
  });
711
735
 
712
- it('should keyValueOrEntryOrRawElementToNode', () => {
736
+ it('should keyValueNodeEntryRawToNodeAndValue', () => {
713
737
  const tree = new BinaryTree<number>();
714
- const node0 = tree.keyValueOrEntryOrRawElementToNode(0);
715
- expect(node0).toEqual({
716
- _left: undefined,
717
- _right: undefined,
718
- key: 0,
719
- parent: undefined,
720
- value: undefined
721
- });
738
+ const node0 = tree.keyValueNodeEntryRawToNodeAndValue(0);
739
+ expect(node0).toEqual([
740
+ {
741
+ _left: undefined,
742
+ _right: undefined,
743
+ key: 0,
744
+ parent: undefined,
745
+ value: undefined
746
+ },
747
+ undefined
748
+ ]);
722
749
 
723
- const nodeUndefined = tree.keyValueOrEntryOrRawElementToNode(undefined);
724
- expect(nodeUndefined).toBe(undefined);
750
+ const nodeUndefined = tree.keyValueNodeEntryRawToNodeAndValue(undefined);
751
+ expect(nodeUndefined).toEqual([undefined, undefined]);
725
752
 
726
- const nodeNull = tree.keyValueOrEntryOrRawElementToNode(null);
727
- expect(nodeNull).toBe(null);
753
+ const nodeNull = tree.keyValueNodeEntryRawToNodeAndValue(null);
754
+ expect(nodeNull).toEqual([null, undefined]);
728
755
 
729
- const nodeWithSeparateValue = tree.keyValueOrEntryOrRawElementToNode(7, 77);
730
- expect(nodeWithSeparateValue?.value).toBe(77);
756
+ const [, nodeWithSeparateValue] = tree.keyValueNodeEntryRawToNodeAndValue(7, 77);
757
+ expect(nodeWithSeparateValue).toBe(77);
731
758
 
732
- expect(tree.keyValueOrEntryOrRawElementToNode([undefined, 2])).toBe(undefined);
759
+ expect(tree.keyValueNodeEntryRawToNodeAndValue([undefined, 2])).toEqual([undefined, undefined]);
733
760
 
734
- expect(tree.keyValueOrEntryOrRawElementToNode(Symbol('test') as unknown as number)).toBe(undefined);
761
+ expect(tree.keyValueNodeEntryRawToNodeAndValue(Symbol('test') as unknown as number)).toEqual([
762
+ undefined,
763
+ undefined
764
+ ]);
735
765
 
736
766
  const bTree = new BinaryTree<number, number, { obj: { id: number } }>([], {
737
767
  toEntryFn: (ele: { obj: { id: number } }) => [Symbol('test') as unknown as number, ele.obj.id]
738
768
  });
739
- expect(bTree.keyValueOrEntryOrRawElementToNode({ obj: { id: 1 } })).toBe(undefined);
769
+ expect(bTree.keyValueNodeEntryRawToNodeAndValue({ obj: { id: 1 } })).toEqual([undefined, undefined]);
740
770
  });
741
771
  });
742
772
 
@@ -1130,17 +1160,21 @@ describe('BinaryTree', () => {
1130
1160
  tree.add([2, 'B']);
1131
1161
  tree.add([null, 'null']);
1132
1162
 
1133
- const nodes = tree.getNodes(node => node.value === 'B');
1163
+ const nodes = tree.isMapMode ? tree.getNodes(node => node.key === 2) : tree.getNodes(node => node.value === 'B');
1134
1164
 
1135
1165
  expect(nodes.length).toBe(1);
1136
1166
  expect(nodes[0].key).toBe(2);
1137
1167
 
1138
- const nodesRec = tree.getNodes(node => node.value === 'B', false, tree.root, 'RECURSIVE');
1168
+ const nodesRec = tree.isMapMode
1169
+ ? tree.getNodes(node => node.key === 2, false, tree.root, 'RECURSIVE')
1170
+ : tree.getNodes(node => node.value === 'B', false, tree.root, 'RECURSIVE');
1139
1171
 
1140
1172
  expect(nodesRec.length).toBe(1);
1141
1173
  expect(nodesRec[0].key).toBe(2);
1142
1174
 
1143
- const nodesItr = tree.getNodes(node => node.value === 'B', false, tree.root, 'ITERATIVE');
1175
+ const nodesItr = tree.isMapMode
1176
+ ? tree.getNodes(node => node.key === 2, false, tree.root, 'ITERATIVE')
1177
+ : tree.getNodes(node => node.value === 'B', false, tree.root, 'ITERATIVE');
1144
1178
 
1145
1179
  expect(nodesItr.length).toBe(1);
1146
1180
  expect(nodesItr[0].key).toBe(2);
@@ -1186,6 +1220,98 @@ describe('BinaryTree', () => {
1186
1220
  });
1187
1221
  });
1188
1222
 
1223
+ describe('BinaryTree map mode', () => {
1224
+ let tree: BinaryTree<number, string>;
1225
+
1226
+ beforeEach(() => {
1227
+ tree = new BinaryTree<number, string>([], {
1228
+ iterationType: 'RECURSIVE',
1229
+ isMapMode: true
1230
+ });
1231
+ });
1232
+
1233
+ afterEach(() => {
1234
+ tree.clear();
1235
+ });
1236
+
1237
+ it('should add and find nodes', () => {
1238
+ tree.add([1, 1]);
1239
+ tree.add(undefined);
1240
+ tree.add([2, 2]);
1241
+ tree.add([3, 3]);
1242
+
1243
+ expect(tree.has(1)).toBe(true);
1244
+ expect(tree.has(2)).toBe(true);
1245
+ expect(tree.has(3)).toBe(true);
1246
+ expect(tree.has(4)).toBe(false);
1247
+ const node4 = tree.getNode(4);
1248
+ expect(tree.has(node4)).toBe(false);
1249
+ expect(tree.has(node => node === node4)).toBe(false);
1250
+ if (tree.isMapMode) expect(tree.has(node => node.key?.toString() === '3')).toBe(true);
1251
+ else expect(tree.has(node => node.value?.toString() === '3')).toBe(true);
1252
+ });
1253
+
1254
+ it('should isSubtreeBST', () => {
1255
+ tree.addMany([
1256
+ new BinaryTreeNode(4),
1257
+ new BinaryTreeNode(2),
1258
+ new BinaryTreeNode(6),
1259
+ new BinaryTreeNode(1),
1260
+ new BinaryTreeNode(3),
1261
+ new BinaryTreeNode(5),
1262
+ new BinaryTreeNode(7),
1263
+ new BinaryTreeNode(4)
1264
+ ]);
1265
+
1266
+ expect(tree.isBST(tree.getNode(4), 'RECURSIVE')).toBe(true);
1267
+ expect(tree.isBST(tree.getNode(4), 'ITERATIVE')).toBe(true);
1268
+ });
1269
+
1270
+ it('should get nodes by key', () => {
1271
+ tree.add([5, 'A']);
1272
+ tree.add([3, 'B']);
1273
+ tree.add([7, 'C']);
1274
+
1275
+ const nodeA = tree.getNode(5);
1276
+ const nodeB = tree.getNode(3);
1277
+
1278
+ expect(nodeA?.key).toBe(5);
1279
+ expect(tree.get(nodeA)).toBe('A');
1280
+ expect(nodeB?.key).toBe(3);
1281
+ expect(tree.get(nodeB)).toBe('B');
1282
+ });
1283
+
1284
+ it('should get nodes by a custom callback', () => {
1285
+ tree.add([5, 'E']);
1286
+ tree.add([4, 'D']);
1287
+ tree.add([3, 'C']);
1288
+ tree.add([7, 'G']);
1289
+ tree.add([null, 'null']);
1290
+ tree.add([1, 'A']);
1291
+ tree.add([6, 'F']);
1292
+ tree.add([null, 'null']);
1293
+ tree.add([2, 'B']);
1294
+ tree.add([null, 'null']);
1295
+
1296
+ const nodes = tree.getNodes(node => node.key === 2);
1297
+
1298
+ expect(nodes.length).toBe(1);
1299
+ expect(nodes[0].key).toBe(2);
1300
+
1301
+ const nodesRec = tree.getNodes(node => node.key === 2, false, tree.root, 'RECURSIVE');
1302
+
1303
+ expect(nodesRec.length).toBe(1);
1304
+ expect(nodesRec[0].key).toBe(2);
1305
+
1306
+ const nodesItr = tree.getNodes(node => node.key === 2, false, tree.root, 'ITERATIVE');
1307
+
1308
+ expect(nodesItr.length).toBe(1);
1309
+ expect(nodesItr[0].key).toBe(2);
1310
+
1311
+ expect(nodesItr).toEqual(nodesRec);
1312
+ });
1313
+ });
1314
+
1189
1315
  describe('BinaryTree iterative methods test', () => {
1190
1316
  let binaryTree: BinaryTree<number, string>;
1191
1317
  beforeEach(() => {
@@ -1253,7 +1379,8 @@ describe('BinaryTree iterative methods test', () => {
1253
1379
  it('should clone work well', () => {
1254
1380
  const cloned = binaryTree.clone();
1255
1381
  expect(cloned.root?.left?.key).toBe(2);
1256
- expect(cloned.root?.right?.value).toBe('c');
1382
+ if (cloned.isMapMode) expect(cloned.get(cloned.root?.right)).toBe('c');
1383
+ else expect(cloned.root?.right?.value).toBe('c');
1257
1384
  });
1258
1385
 
1259
1386
  it('should keys', () => {
@@ -1295,3 +1422,19 @@ describe('BinaryTree iterative methods test', () => {
1295
1422
  ]);
1296
1423
  });
1297
1424
  });
1425
+
1426
+ describe('BinaryTree map mode iterative methods test', () => {
1427
+ let binaryTree: BinaryTree<number, string>;
1428
+ beforeEach(() => {
1429
+ binaryTree = new BinaryTree<number, string>([], { isMapMode: true });
1430
+ binaryTree.add([1, 'a']);
1431
+ binaryTree.add(2, 'b');
1432
+ binaryTree.add([3, 'c']);
1433
+ });
1434
+
1435
+ it('should clone work well', () => {
1436
+ const cloned = binaryTree.clone();
1437
+ expect(cloned.root?.left?.key).toBe(2);
1438
+ expect(cloned.get(cloned.root?.right)).toBe('c');
1439
+ });
1440
+ });