data-structure-typed 1.46.8 → 1.46.9

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 (68) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +1 -1
  3. package/benchmark/report.html +1 -46
  4. package/benchmark/report.json +11 -422
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +4 -2
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js +10 -0
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +19 -13
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +121 -55
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.d.ts +10 -9
  12. package/dist/cjs/data-structures/binary-tree/bst.js +16 -14
  13. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +7 -5
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js +32 -23
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +5 -3
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +11 -2
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  20. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  21. package/dist/cjs/types/common.d.ts +5 -4
  22. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -1
  23. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +3 -1
  24. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
  25. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
  26. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -1
  27. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -1
  28. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +2 -1
  29. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +4 -2
  30. package/dist/mjs/data-structures/binary-tree/avl-tree.js +11 -0
  31. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +19 -13
  32. package/dist/mjs/data-structures/binary-tree/binary-tree.js +122 -55
  33. package/dist/mjs/data-structures/binary-tree/bst.d.ts +10 -9
  34. package/dist/mjs/data-structures/binary-tree/bst.js +17 -14
  35. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +7 -5
  36. package/dist/mjs/data-structures/binary-tree/rb-tree.js +34 -24
  37. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +5 -3
  38. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +12 -2
  39. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  40. package/dist/mjs/types/common.d.ts +5 -4
  41. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -1
  42. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +3 -1
  43. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +0 -6
  44. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -1
  45. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -1
  46. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +2 -1
  47. package/dist/umd/data-structure-typed.js +198 -103
  48. package/dist/umd/data-structure-typed.min.js +4 -1
  49. package/dist/umd/data-structure-typed.min.js.map +1 -1
  50. package/package.json +5 -5
  51. package/src/data-structures/binary-tree/avl-tree.ts +17 -5
  52. package/src/data-structures/binary-tree/binary-tree.ts +143 -62
  53. package/src/data-structures/binary-tree/bst.ts +23 -19
  54. package/src/data-structures/binary-tree/rb-tree.ts +38 -27
  55. package/src/data-structures/binary-tree/tree-multimap.ts +19 -6
  56. package/src/interfaces/binary-tree.ts +3 -3
  57. package/src/types/common.ts +2 -5
  58. package/src/types/data-structures/binary-tree/avl-tree.ts +5 -1
  59. package/src/types/data-structures/binary-tree/binary-tree.ts +5 -7
  60. package/src/types/data-structures/binary-tree/bst.ts +3 -1
  61. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -1
  62. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -1
  63. package/test/integration/index.html +30 -28
  64. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +7 -0
  65. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +57 -1
  66. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +62 -7
  67. package/test/unit/data-structures/binary-tree/bst.test.ts +56 -1
  68. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +98 -42
@@ -189,9 +189,9 @@ describe('BinaryTree', () => {
189
189
  tree.add(4);
190
190
  tree.add(2);
191
191
  expect(tree.getHeight()).toBe(1);
192
- tree.iterationType = IterationType.RECURSIVE;
192
+ tree.options.iterationType = IterationType.RECURSIVE;
193
193
  expect(tree.getHeight()).toBe(1);
194
- tree.iterationType = IterationType.ITERATIVE;
194
+ tree.options.iterationType = IterationType.ITERATIVE;
195
195
 
196
196
  tree.add(6);
197
197
  tree.add(1);
@@ -541,11 +541,11 @@ describe('BinaryTree', () => {
541
541
  tree.add(3, 'B');
542
542
  tree.add(7, 'C');
543
543
 
544
- tree.iterationType = IterationType.ITERATIVE;
545
- expect([...tree]).toEqual([3, 5, 7]);
546
- tree.iterationType = IterationType.RECURSIVE;
547
- expect([...tree]).toEqual([3, 5, 7]);
548
- tree.iterationType = IterationType.ITERATIVE;
544
+ tree.options.iterationType = IterationType.ITERATIVE;
545
+ expect([...tree]).toEqual([[3, "B"], [5, "A"], [7, "C"]]);
546
+ tree.options.iterationType = IterationType.RECURSIVE;
547
+ expect([...tree]).toEqual([[3, "B"], [5, "A"], [7, "C"]]);
548
+ tree.options.iterationType = IterationType.ITERATIVE;
549
549
 
550
550
  const result = tree.morris();
551
551
  expect(result).toEqual([3, 5, 7]);
@@ -564,3 +564,58 @@ describe('BinaryTree', () => {
564
564
  expect(tree.getHeight()).toBe(-1);
565
565
  });
566
566
  });
567
+
568
+
569
+ describe('BinaryTree iterative methods test', () => {
570
+ let binaryTree: BinaryTree<string>;
571
+ beforeEach(() => {
572
+ binaryTree = new BinaryTree();
573
+ binaryTree.add(1, 'a');
574
+ binaryTree.add(2, 'b');
575
+ binaryTree.add(3, 'c');
576
+ });
577
+
578
+ test('The node obtained by get Node should match the node type', () => {
579
+ const node3 = binaryTree.getNode(3);
580
+ expect(node3).toBeInstanceOf(BinaryTreeNode);
581
+ });
582
+
583
+ test('forEach should iterate over all elements', () => {
584
+ const mockCallback = jest.fn();
585
+ binaryTree.forEach((entry) => {
586
+ mockCallback(entry);
587
+ });
588
+
589
+ expect(mockCallback.mock.calls.length).toBe(3);
590
+ expect(mockCallback.mock.calls[0][0]).toEqual([2, 'b']);
591
+ expect(mockCallback.mock.calls[1][0]).toEqual([1, 'a']);
592
+ expect(mockCallback.mock.calls[2][0]).toEqual([3, 'c']);
593
+ });
594
+
595
+ test('filter should return a new tree with filtered elements', () => {
596
+ const filteredTree = binaryTree.filter(([key]) => key > 1);
597
+ expect(filteredTree.size).toBe(2);
598
+ expect([...filteredTree]).toEqual([[3, 'c'], [2, 'b']]);
599
+ });
600
+
601
+ test('map should return a new tree with modified elements', () => {
602
+ const mappedTree = binaryTree.map(([key]) => (key * 2).toString());
603
+ expect(mappedTree.size).toBe(3);
604
+ expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
605
+ });
606
+
607
+ test('reduce should accumulate values', () => {
608
+ const sum = binaryTree.reduce((acc, [key]) => acc + key, 0);
609
+ expect(sum).toBe(6);
610
+ });
611
+
612
+ test('[Symbol.iterator] should provide an iterator', () => {
613
+ const entries = [];
614
+ for (const entry of binaryTree) {
615
+ entries.push(entry);
616
+ }
617
+
618
+ expect(entries.length).toBe(3);
619
+ expect(entries).toEqual([[2, 'b'], [1, 'a'], [3, 'c']]);
620
+ });
621
+ });
@@ -1,4 +1,4 @@
1
- import { BST, BSTNode, CP, IterationType } from '../../../../src';
1
+ import { BinaryTreeNode, BST, BSTNode, CP, IterationType } from '../../../../src';
2
2
  import { isDebugTest } from '../../../config';
3
3
 
4
4
  const isDebug = isDebugTest;
@@ -851,3 +851,58 @@ describe('BST Performance test', function () {
851
851
  ]);
852
852
  });
853
853
  });
854
+
855
+ describe('BST iterative methods test', () => {
856
+ let bst: BST<string>;
857
+ beforeEach(() => {
858
+ bst = new BST();
859
+ bst.add(1, 'a');
860
+ bst.add(2, 'b');
861
+ bst.add(3, 'c');
862
+ });
863
+
864
+ test('The node obtained by get Node should match the node type', () => {
865
+ const node3 = bst.getNode(3);
866
+ expect(node3).toBeInstanceOf(BinaryTreeNode);
867
+ expect(node3).toBeInstanceOf(BSTNode);
868
+ });
869
+
870
+ test('forEach should iterate over all elements', () => {
871
+ const mockCallback = jest.fn();
872
+ bst.forEach((entry) => {
873
+ mockCallback(entry);
874
+ });
875
+
876
+ expect(mockCallback.mock.calls.length).toBe(3);
877
+ expect(mockCallback.mock.calls[0][0]).toEqual([1, 'a']);
878
+ expect(mockCallback.mock.calls[1][0]).toEqual([2, 'b']);
879
+ expect(mockCallback.mock.calls[2][0]).toEqual([3, 'c']);
880
+ });
881
+
882
+ test('filter should return a new tree with filtered elements', () => {
883
+ const filteredTree = bst.filter(([key]) => key > 1);
884
+ expect(filteredTree.size).toBe(2);
885
+ expect([...filteredTree]).toEqual([[2, 'b'], [3, 'c']]);
886
+ });
887
+
888
+ test('map should return a new tree with modified elements', () => {
889
+ const mappedTree = bst.map(([key]) => (key * 2).toString());
890
+ expect(mappedTree.size).toBe(3);
891
+ expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
892
+ });
893
+
894
+ test('reduce should accumulate values', () => {
895
+ const sum = bst.reduce((acc, [key]) => acc + key, 0);
896
+ expect(sum).toBe(6);
897
+ });
898
+
899
+ test('[Symbol.iterator] should provide an iterator', () => {
900
+ const entries = [];
901
+ for (const entry of bst) {
902
+ entries.push(entry);
903
+ }
904
+
905
+ expect(entries.length).toBe(3);
906
+ expect(entries).toEqual([[1, 'a'], [2, 'b'], [3, 'c']]);
907
+ });
908
+ });
@@ -1,4 +1,4 @@
1
- import { IterationType, RBTNColor, RedBlackTree, RedBlackTreeNode } from '../../../../src';
1
+ import { BinaryTreeNode, BSTNode, IterationType, RBTNColor, RedBlackTree, RedBlackTreeNode } from '../../../../src';
2
2
  import { getRandomInt, getRandomIntArray, magnitude } from '../../../utils';
3
3
  import { isDebugTest } from '../../../config';
4
4
  import { OrderedMap } from 'js-sdsl';
@@ -67,7 +67,7 @@ describe('RedBlackTree', () => {
67
67
 
68
68
  it('should handle an empty tree', () => {
69
69
  const minNode = tree.getLeftMost(tree.root);
70
- expect(minNode).toBe(tree.NIL);
70
+ expect(minNode).toBe(tree.Sentinel);
71
71
  });
72
72
  });
73
73
 
@@ -85,7 +85,7 @@ describe('RedBlackTree', () => {
85
85
 
86
86
  it('should handle an empty tree', () => {
87
87
  const maxNode = tree.getRightMost(tree.root);
88
- expect(maxNode).toBe(tree.NIL);
88
+ expect(maxNode).toBe(tree.Sentinel);
89
89
  });
90
90
  });
91
91
 
@@ -109,7 +109,7 @@ describe('RedBlackTree', () => {
109
109
 
110
110
  const node = tree.getNode(10);
111
111
  const successorNode = tree.getSuccessor(node!);
112
- // TODO not sure if it should be undefined or tree.NIL
112
+ // TODO not sure if it should be undefined or tree.Sentinel
113
113
  expect(successorNode).toBe(undefined);
114
114
  });
115
115
  });
@@ -134,7 +134,7 @@ describe('RedBlackTree', () => {
134
134
 
135
135
  const node = tree.getNode(20);
136
136
  const predecessorNode = tree.getPredecessor(node!);
137
- // TODO not sure if it should be tree.NIL or something else.
137
+ // TODO not sure if it should be tree.Sentinel or something else.
138
138
  expect(predecessorNode).toBe(tree.getNode(10));
139
139
  });
140
140
  });
@@ -235,28 +235,28 @@ describe('RedBlackTree', () => {
235
235
  expect(node5F?.parent).toBe(node10F);
236
236
  expect(node15F?.key).toBe(15);
237
237
  expect(node15F?.color).toBe(RBTNColor.RED);
238
- expect(node15F?.left).toBe(tree.NIL);
239
- expect(node15F?.right).toBe(tree.NIL);
238
+ expect(node15F?.left).toBe(tree.Sentinel);
239
+ expect(node15F?.right).toBe(tree.Sentinel);
240
240
  expect(node15F?.parent).toBe(node20F);
241
241
  expect(node21F?.key).toBe(21);
242
242
  expect(node21F?.color).toBe(RBTNColor.RED);
243
- expect(node21F?.left).toBe(tree.NIL);
244
- expect(node21F?.right).toBe(tree.NIL);
243
+ expect(node21F?.left).toBe(tree.Sentinel);
244
+ expect(node21F?.right).toBe(tree.Sentinel);
245
245
  expect(node21F?.parent).toBe(node20F);
246
246
  expect(node6F?.key).toBe(6);
247
247
  expect(node6F?.color).toBe(RBTNColor.RED);
248
- expect(node6F?.left).toBe(tree.NIL);
249
- expect(node6F?.right).toBe(tree.NIL);
248
+ expect(node6F?.left).toBe(tree.Sentinel);
249
+ expect(node6F?.right).toBe(tree.Sentinel);
250
250
  expect(node6F?.parent).toBe(node5F);
251
251
  expect(node2F?.key).toBe(2);
252
252
  expect(node2F?.color).toBe(RBTNColor.RED);
253
- expect(node2F?.left).toBe(tree.NIL);
254
- expect(node2F?.right).toBe(tree.NIL);
253
+ expect(node2F?.left).toBe(tree.Sentinel);
254
+ expect(node2F?.right).toBe(tree.Sentinel);
255
255
  expect(node2F?.parent).toBe(node5F);
256
256
  expect(node15F?.key).toBe(15);
257
257
  expect(node15F?.color).toBe(RBTNColor.RED);
258
- expect(node15F?.left).toBe(tree.NIL);
259
- expect(node15F?.right).toBe(tree.NIL);
258
+ expect(node15F?.left).toBe(tree.Sentinel);
259
+ expect(node15F?.right).toBe(tree.Sentinel);
260
260
  expect(node15F?.parent).toBe(node20F);
261
261
  tree.delete(5);
262
262
  node10F = tree.getNode(10);
@@ -279,28 +279,28 @@ describe('RedBlackTree', () => {
279
279
  expect(node5F).toBe(undefined);
280
280
  expect(node15F?.key).toBe(15);
281
281
  expect(node15F?.color).toBe(RBTNColor.RED);
282
- expect(node15F?.left).toBe(tree.NIL);
283
- expect(node15F?.right).toBe(tree.NIL);
282
+ expect(node15F?.left).toBe(tree.Sentinel);
283
+ expect(node15F?.right).toBe(tree.Sentinel);
284
284
  expect(node15F?.parent).toBe(node20F);
285
285
  expect(node21F?.key).toBe(21);
286
286
  expect(node21F?.color).toBe(RBTNColor.RED);
287
- expect(node21F?.left).toBe(tree.NIL);
288
- expect(node21F?.right).toBe(tree.NIL);
287
+ expect(node21F?.left).toBe(tree.Sentinel);
288
+ expect(node21F?.right).toBe(tree.Sentinel);
289
289
  expect(node21F?.parent).toBe(node20F);
290
290
  expect(node6F?.key).toBe(6);
291
291
  expect(node6F?.color).toBe(RBTNColor.BLACK);
292
292
  expect(node6F?.left).toBe(node2F);
293
- expect(node6F?.right).toBe(tree.NIL);
293
+ expect(node6F?.right).toBe(tree.Sentinel);
294
294
  expect(node6F?.parent).toBe(node10F);
295
295
  expect(node2F?.key).toBe(2);
296
296
  expect(node2F?.color).toBe(RBTNColor.RED);
297
- expect(node2F?.left).toBe(tree.NIL);
298
- expect(node2F?.right).toBe(tree.NIL);
297
+ expect(node2F?.left).toBe(tree.Sentinel);
298
+ expect(node2F?.right).toBe(tree.Sentinel);
299
299
  expect(node2F?.parent).toBe(node6F);
300
300
  expect(node15F?.key).toBe(15);
301
301
  expect(node15F?.color).toBe(RBTNColor.RED);
302
- expect(node15F?.left).toBe(tree.NIL);
303
- expect(node15F?.right).toBe(tree.NIL);
302
+ expect(node15F?.left).toBe(tree.Sentinel);
303
+ expect(node15F?.right).toBe(tree.Sentinel);
304
304
  expect(node15F?.parent).toBe(node20F);
305
305
  tree.delete(20);
306
306
  node10F = tree.getNode(10);
@@ -319,28 +319,28 @@ describe('RedBlackTree', () => {
319
319
  expect(node5F).toBe(undefined);
320
320
  expect(node15F?.key).toBe(15);
321
321
  expect(node15F?.color).toBe(RBTNColor.RED);
322
- expect(node15F?.left).toBe(tree.NIL);
323
- expect(node15F?.right).toBe(tree.NIL);
322
+ expect(node15F?.left).toBe(tree.Sentinel);
323
+ expect(node15F?.right).toBe(tree.Sentinel);
324
324
  expect(node15F?.parent).toBe(node21F);
325
325
  expect(node21F?.key).toBe(21);
326
326
  expect(node21F?.color).toBe(RBTNColor.BLACK);
327
327
  expect(node21F?.left).toBe(node15F);
328
- expect(node21F?.right).toBe(tree.NIL);
328
+ expect(node21F?.right).toBe(tree.Sentinel);
329
329
  expect(node21F?.parent).toBe(node10F);
330
330
  expect(node6F?.key).toBe(6);
331
331
  expect(node6F?.color).toBe(RBTNColor.BLACK);
332
332
  expect(node6F?.left).toBe(node2F);
333
- expect(node6F?.right).toBe(tree.NIL);
333
+ expect(node6F?.right).toBe(tree.Sentinel);
334
334
  expect(node6F?.parent).toBe(node10F);
335
335
  expect(node2F?.key).toBe(2);
336
336
  expect(node2F?.color).toBe(RBTNColor.RED);
337
- expect(node2F?.left).toBe(tree.NIL);
338
- expect(node2F?.right).toBe(tree.NIL);
337
+ expect(node2F?.left).toBe(tree.Sentinel);
338
+ expect(node2F?.right).toBe(tree.Sentinel);
339
339
  expect(node2F?.parent).toBe(node6F);
340
340
  expect(node15F?.key).toBe(15);
341
341
  expect(node15F?.color).toBe(RBTNColor.RED);
342
- expect(node15F?.left).toBe(tree.NIL);
343
- expect(node15F?.right).toBe(tree.NIL);
342
+ expect(node15F?.left).toBe(tree.Sentinel);
343
+ expect(node15F?.right).toBe(tree.Sentinel);
344
344
  expect(node15F?.parent).toBe(node21F);
345
345
  });
346
346
 
@@ -350,8 +350,8 @@ describe('RedBlackTree', () => {
350
350
  tree.add(5);
351
351
  tree.add(15);
352
352
  const node15F = tree.getNode(15);
353
- expect(node15F?.left).toBe(tree.NIL);
354
- expect(node15F?.right).toBe(tree.NIL);
353
+ expect(node15F?.left).toBe(tree.Sentinel);
354
+ expect(node15F?.right).toBe(tree.Sentinel);
355
355
  expect(node15F?.parent).toBe(tree.getNode(5));
356
356
 
357
357
  tree.add(25);
@@ -366,8 +366,8 @@ describe('RedBlackTree', () => {
366
366
  tree.add(155);
367
367
  tree.add(225);
368
368
  const node225F = tree.getNode(225);
369
- expect(node225F?.left).toBe(tree.NIL);
370
- expect(node225F?.right).toBe(tree.NIL);
369
+ expect(node225F?.left).toBe(tree.Sentinel);
370
+ expect(node225F?.right).toBe(tree.Sentinel);
371
371
  expect(node225F?.parent?.key).toBe(155);
372
372
  tree.add(7);
373
373
 
@@ -393,14 +393,14 @@ describe('RedBlackTree', () => {
393
393
  const node50 = tree.getNode(50);
394
394
  expect(node50?.key).toBe(50);
395
395
  expect(node50?.left?.key).toBe(33);
396
- expect(node50?.right).toBe(tree.NIL);
396
+ expect(node50?.right).toBe(tree.Sentinel);
397
397
  const node15Fo = tree.getNode(15);
398
398
 
399
399
  expect(node15Fo?.key).toBe(15);
400
- expect(node15Fo?.left).toBe(tree.NIL);
400
+ expect(node15Fo?.left).toBe(tree.Sentinel);
401
401
  const node225S = tree.getNode(225);
402
- expect(node225S?.left).toBe(tree.NIL);
403
- expect(node225S?.right).toBe(tree.NIL);
402
+ expect(node225S?.left).toBe(tree.Sentinel);
403
+ expect(node225S?.right).toBe(tree.Sentinel);
404
404
  expect(node225S?.parent?.key).toBe(155);
405
405
  // TODO
406
406
  // expect(tree.getNode(0)).toBe(undefined);
@@ -465,7 +465,7 @@ describe('RedBlackTree', () => {
465
465
  tree.delete(getRandomInt(-100, 1000));
466
466
  }
467
467
 
468
- // TODO there is a bug when dfs the tree with NIL node
468
+ // TODO there is a bug when dfs the tree with Sentinel node
469
469
  // expect(tree.isBST()).toBe(true);
470
470
  });
471
471
  const { HUNDRED_THOUSAND } = magnitude;
@@ -490,7 +490,7 @@ describe('RedBlackTree', () => {
490
490
 
491
491
  it('duplicates', () => {
492
492
  tree.addMany([9, 8, 7, 8, 8, 8, 2, 3, 6, 5, 5, 4]);
493
- tree.print();
493
+ isDebug && tree.print();
494
494
 
495
495
  expect(tree.size).toBe(8);
496
496
  expect(tree.isBST()).toBe(true);
@@ -505,3 +505,59 @@ describe('RedBlackTree', () => {
505
505
  expect(tree.isAVLBalanced()).toBe(false);
506
506
  })
507
507
  });
508
+
509
+ describe('RedBlackTree iterative methods test', () => {
510
+ let rbTree: RedBlackTree<string>;
511
+ beforeEach(() => {
512
+ rbTree = new RedBlackTree();
513
+ rbTree.add(1, 'a');
514
+ rbTree.add(2, 'b');
515
+ rbTree.add(3, 'c');
516
+ });
517
+
518
+ test('The node obtained by get Node should match the node type', () => {
519
+ const node3 = rbTree.getNode(3);
520
+ expect(node3).toBeInstanceOf(BinaryTreeNode);
521
+ expect(node3).toBeInstanceOf(BSTNode);
522
+ expect(node3).toBeInstanceOf(RedBlackTreeNode);
523
+ });
524
+
525
+ test('forEach should iterate over all elements', () => {
526
+ const mockCallback = jest.fn();
527
+ rbTree.forEach((entry) => {
528
+ mockCallback(entry);
529
+ });
530
+
531
+ expect(mockCallback.mock.calls.length).toBe(3);
532
+ expect(mockCallback.mock.calls[0][0]).toEqual([1, 'a']);
533
+ expect(mockCallback.mock.calls[1][0]).toEqual([2, 'b']);
534
+ expect(mockCallback.mock.calls[2][0]).toEqual([3, 'c']);
535
+ });
536
+
537
+ test('filter should return a new tree with filtered elements', () => {
538
+ const filteredTree = rbTree.filter(([key]) => key > 1);
539
+ expect(filteredTree.size).toBe(2);
540
+ expect([...filteredTree]).toEqual([[2, 'b'], [3, 'c']]);
541
+ });
542
+
543
+ test('map should return a new tree with modified elements', () => {
544
+ const mappedTree = rbTree.map(([key]) => (key * 2).toString());
545
+ expect(mappedTree.size).toBe(3);
546
+ expect([...mappedTree]).toEqual([[1, '2'], [2, '4'], [3, '6']]);
547
+ });
548
+
549
+ test('reduce should accumulate values', () => {
550
+ const sum = rbTree.reduce((acc, [key]) => acc + key, 0);
551
+ expect(sum).toBe(6);
552
+ });
553
+
554
+ test('[Symbol.iterator] should provide an iterator', () => {
555
+ const entries = [];
556
+ for (const entry of rbTree) {
557
+ entries.push(entry);
558
+ }
559
+
560
+ expect(entries.length).toBe(3);
561
+ expect(entries).toEqual([[1, 'a'], [2, 'b'], [3, 'c']]);
562
+ });
563
+ });