data-structure-typed 1.49.1 → 1.49.3

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 (106) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +16 -16
  3. package/README_zh-CN.md +2 -2
  4. package/benchmark/report.html +46 -1
  5. package/benchmark/report.json +457 -22
  6. package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
  7. package/dist/cjs/data-structures/base/iterable-base.js +21 -0
  8. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  9. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +7 -7
  10. package/dist/cjs/data-structures/graph/abstract-graph.js +43 -12
  11. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  12. package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
  13. package/dist/cjs/data-structures/graph/directed-graph.js +2 -2
  14. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  15. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
  16. package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
  17. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  18. package/dist/cjs/data-structures/hash/hash-map.d.ts +9 -9
  19. package/dist/cjs/data-structures/hash/hash-map.js +16 -15
  20. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  21. package/dist/cjs/data-structures/heap/heap.d.ts +6 -35
  22. package/dist/cjs/data-structures/heap/heap.js +10 -42
  23. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  24. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
  25. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +143 -146
  26. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  27. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  28. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -42
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  30. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  31. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
  32. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  33. package/dist/cjs/data-structures/queue/deque.d.ts +70 -75
  34. package/dist/cjs/data-structures/queue/deque.js +100 -110
  35. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  36. package/dist/cjs/data-structures/queue/queue.d.ts +37 -38
  37. package/dist/cjs/data-structures/queue/queue.js +46 -49
  38. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  39. package/dist/cjs/data-structures/stack/stack.d.ts +2 -3
  40. package/dist/cjs/data-structures/stack/stack.js +2 -5
  41. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  42. package/dist/cjs/data-structures/trie/trie.d.ts +1 -2
  43. package/dist/cjs/data-structures/trie/trie.js +2 -5
  44. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  45. package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
  46. package/dist/mjs/data-structures/base/iterable-base.js +21 -0
  47. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +7 -7
  48. package/dist/mjs/data-structures/graph/abstract-graph.js +43 -12
  49. package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
  50. package/dist/mjs/data-structures/graph/directed-graph.js +2 -2
  51. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
  52. package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
  53. package/dist/mjs/data-structures/hash/hash-map.d.ts +9 -9
  54. package/dist/mjs/data-structures/hash/hash-map.js +16 -15
  55. package/dist/mjs/data-structures/heap/heap.d.ts +6 -35
  56. package/dist/mjs/data-structures/heap/heap.js +10 -42
  57. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
  58. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +142 -145
  59. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  60. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -43
  61. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
  62. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
  63. package/dist/mjs/data-structures/queue/deque.d.ts +70 -75
  64. package/dist/mjs/data-structures/queue/deque.js +100 -110
  65. package/dist/mjs/data-structures/queue/queue.d.ts +37 -38
  66. package/dist/mjs/data-structures/queue/queue.js +45 -48
  67. package/dist/mjs/data-structures/stack/stack.d.ts +2 -3
  68. package/dist/mjs/data-structures/stack/stack.js +2 -5
  69. package/dist/mjs/data-structures/trie/trie.d.ts +1 -2
  70. package/dist/mjs/data-structures/trie/trie.js +2 -5
  71. package/dist/umd/data-structure-typed.js +531 -532
  72. package/dist/umd/data-structure-typed.min.js +2 -2
  73. package/dist/umd/data-structure-typed.min.js.map +1 -1
  74. package/package.json +1 -1
  75. package/src/data-structures/base/iterable-base.ts +24 -0
  76. package/src/data-structures/graph/abstract-graph.ts +55 -14
  77. package/src/data-structures/graph/directed-graph.ts +3 -2
  78. package/src/data-structures/graph/undirected-graph.ts +1 -1
  79. package/src/data-structures/hash/hash-map.ts +27 -28
  80. package/src/data-structures/heap/heap.ts +19 -57
  81. package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
  82. package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
  83. package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
  84. package/src/data-structures/queue/deque.ts +122 -135
  85. package/src/data-structures/queue/queue.ts +54 -58
  86. package/src/data-structures/stack/stack.ts +4 -8
  87. package/src/data-structures/trie/trie.ts +5 -9
  88. package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
  89. package/test/performance/data-structures/heap/heap.test.ts +2 -2
  90. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  91. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
  92. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
  93. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
  94. package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
  95. package/test/unit/data-structures/graph/directed-graph.test.ts +48 -3
  96. package/test/unit/data-structures/graph/undirected-graph.test.ts +48 -4
  97. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  98. package/test/unit/data-structures/heap/heap.test.ts +6 -1
  99. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  100. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
  101. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  102. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
  103. package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
  104. package/test/unit/data-structures/queue/deque.test.ts +5 -5
  105. package/test/unit/data-structures/queue/queue.test.ts +4 -4
  106. package/test/unit/data-structures/trie/trie.test.ts +1 -1
@@ -138,7 +138,7 @@ export class Trie extends IterableElementBase<string> {
138
138
  * @param{string} word - The word to delete.
139
139
  * @returns {boolean} True if the word was successfully removed.
140
140
  */
141
- delete(word: string) {
141
+ delete(word: string): boolean {
142
142
  word = this._caseProcess(word);
143
143
  let isDeleted = false;
144
144
  const dfs = (cur: TrieNode, i: number): boolean => {
@@ -184,7 +184,7 @@ export class Trie extends IterableElementBase<string> {
184
184
  * Space Complexity: O(1) - Constant space.
185
185
  *
186
186
  */
187
- getHeight() {
187
+ getHeight(): number {
188
188
  const beginRoot = this.root;
189
189
  let maxDepth = 0;
190
190
  if (beginRoot) {
@@ -371,12 +371,12 @@ export class Trie extends IterableElementBase<string> {
371
371
  * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
372
372
  * @returns The `filter` method is returning an array of strings (`string[]`).
373
373
  */
374
- filter(predicate: ElementCallback<string, boolean>, thisArg?: any): string[] {
375
- const results: string[] = [];
374
+ filter(predicate: ElementCallback<string, boolean>, thisArg?: any): Trie {
375
+ const results: Trie = new Trie();
376
376
  let index = 0;
377
377
  for (const word of this) {
378
378
  if (predicate.call(thisArg, word, index, this)) {
379
- results.push(word);
379
+ results.add(word);
380
380
  }
381
381
  index++;
382
382
  }
@@ -411,10 +411,6 @@ export class Trie extends IterableElementBase<string> {
411
411
  return newTrie;
412
412
  }
413
413
 
414
- print() {
415
- console.log([...this]);
416
- }
417
-
418
414
  protected* _getIterator(): IterableIterator<string> {
419
415
  function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
420
416
  if (node.isEnd) {
@@ -47,23 +47,23 @@ if (isCompetitor) {
47
47
  }
48
48
 
49
49
  suite
50
- .add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
50
+ .add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
51
51
  const pq = new SRCPriorityQueue<number>([], { comparator: (a, b) => b - a });
52
52
 
53
53
  for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
54
- for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
54
+ for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
55
55
  })
56
- .add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
56
+ .add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
57
57
  const pq = new CJSPriorityQueue<number>([], { comparator: (a, b) => b - a });
58
58
 
59
59
  for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
60
- for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
60
+ for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
61
61
  })
62
- .add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
62
+ .add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add & poll`, () => {
63
63
  const pq = new MJSPriorityQueue<number>([], { comparator: (a, b) => b - a });
64
64
 
65
65
  for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
66
- for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
66
+ for (let i = 0; i < TEN_THOUSAND; i++) pq.poll();
67
67
  });
68
68
 
69
69
 
@@ -6,7 +6,7 @@ const suite = new Benchmark.Suite();
6
6
  const { HUNDRED_THOUSAND, TEN_THOUSAND } = magnitude;
7
7
 
8
8
  suite
9
- .add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
9
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
10
10
  const heap = new Heap<number>([], { comparator: (a, b) => b - a });
11
11
 
12
12
  for (let i = 0; i < HUNDRED_THOUSAND; i++) {
@@ -14,7 +14,7 @@ suite
14
14
  }
15
15
 
16
16
  for (let i = 0; i < HUNDRED_THOUSAND; i++) {
17
- heap.pop();
17
+ heap.poll();
18
18
  }
19
19
  })
20
20
  .add(`${HUNDRED_THOUSAND.toLocaleString()} add & dfs`, () => {
@@ -55,7 +55,7 @@ suite
55
55
  list.shift();
56
56
  }
57
57
  })
58
- .add(`${LINEAR.toLocaleString()} insertBefore`, () => {
58
+ .add(`${LINEAR.toLocaleString()} addBefore`, () => {
59
59
  const doublyList = new DoublyLinkedList<number>();
60
60
  let midNode: DoublyLinkedListNode | undefined;
61
61
  const midIndex = Math.floor(LINEAR / 2);
@@ -64,7 +64,7 @@ suite
64
64
  if (i === midIndex) {
65
65
  midNode = doublyList.getNode(i);
66
66
  } else if (i > midIndex && midNode) {
67
- doublyList.insertBefore(midNode, i);
67
+ doublyList.addBefore(midNode, i);
68
68
  }
69
69
  }
70
70
  });
@@ -28,7 +28,7 @@ suite
28
28
  list.pop();
29
29
  }
30
30
  })
31
- .add(`${TEN_THOUSAND.toLocaleString()} insertBefore`, () => {
31
+ .add(`${TEN_THOUSAND.toLocaleString()} addBefore`, () => {
32
32
  const singlyList = new SinglyLinkedList<number>();
33
33
  let midSinglyNode: SinglyLinkedListNode | undefined;
34
34
  const midIndex = Math.floor(TEN_THOUSAND / 2);
@@ -37,7 +37,7 @@ suite
37
37
  if (i === midIndex) {
38
38
  midSinglyNode = singlyList.getNode(i);
39
39
  } else if (i > midIndex && midSinglyNode) {
40
- singlyList.insertBefore(midSinglyNode.value, i);
40
+ singlyList.addBefore(midSinglyNode.value, i);
41
41
  }
42
42
  }
43
43
  });
@@ -7,7 +7,7 @@ import { isCompetitor } from '../../../config';
7
7
  const suite = new Benchmark.Suite();
8
8
  const { HUNDRED_THOUSAND } = magnitude;
9
9
 
10
- suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
10
+ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & poll`, () => {
11
11
  const pq = new PriorityQueue<number>([], { comparator: (a, b) => b - a });
12
12
 
13
13
  for (let i = 0; i < HUNDRED_THOUSAND; i++) {
@@ -15,7 +15,7 @@ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
15
15
  }
16
16
 
17
17
  for (let i = 0; i < HUNDRED_THOUSAND; i++) {
18
- pq.pop();
18
+ pq.poll();
19
19
  }
20
20
  });
21
21
  if (isCompetitor) {
@@ -1,8 +1,8 @@
1
1
  import { BinaryTree, BinaryTreeNode, FamilyPosition, IterationType } from '../../../../src';
2
2
  import { getRandomIntArray } from '../../../utils';
3
- import { isDebugTest } from '../../../config';
3
+ // import { isDebugTest } from '../../../config';
4
4
 
5
- const isDebug = isDebugTest;
5
+ // const isDebug = isDebugTest;
6
6
 
7
7
  describe('BinaryTreeNode', () => {
8
8
  it('should create an instance of BinaryTreeNode', () => {
@@ -66,7 +66,7 @@ class MyGraph<
66
66
  return edge ? undefined : undefined;
67
67
  }
68
68
 
69
- protected _addEdgeOnly(edge: EO): boolean {
69
+ protected _addEdge(edge: EO): boolean {
70
70
  return edge ? true : true;
71
71
  }
72
72
  }
@@ -588,7 +588,7 @@ describe('cycles, strongly connected components, bridges, articular points in Di
588
588
  const cutVertexes = graph.getCutVertexes();
589
589
  const dfnMap = graph.getDFNMap();
590
590
  const lowMap = graph.getLowMap();
591
- expect(cycles.size).toBe(2);
591
+ expect(cycles.length).toBe(2);
592
592
  expect(scCs.size).toBe(5);
593
593
  expect(bridges.length).toBe(4);
594
594
  expect(cutVertexes.length).toBe(4);
@@ -688,8 +688,53 @@ describe('DirectedGraph getCycles', () => {
688
688
  graph.addEdge('D', 'E');
689
689
  graph.addEdge('E', 'B');
690
690
  const cycles = graph.getCycles();
691
- expect(cycles.size).toBe(1);
692
- expect(cycles.get(2)).toEqual( [{ "key": "B", "value": undefined }, { "key": "D", "value": undefined }, { "key": "E", "value": undefined }]);
691
+ expect(cycles.length).toBe(1);
692
+ expect(cycles[0]).toEqual(["B", "D", "E"]);
693
693
  })
694
+
695
+ test('should simple cycles graph getCycles return correct result', () => {
696
+ const graph = new DirectedGraph();
697
+
698
+ graph.addVertex('A');
699
+ graph.addVertex('B');
700
+ graph.addVertex('C');
701
+ graph.addVertex('D');
702
+
703
+ graph.addEdge('A', 'B');
704
+ graph.addEdge('B', 'C');
705
+ graph.addEdge('C', 'A');
706
+ graph.addEdge('A', 'D');
707
+ graph.addEdge('D', 'C');
708
+ const cycles = graph.getCycles();
709
+ expect(cycles.length).toBe(2)
710
+ expect(cycles).toEqual([["A", "B", "C"], ["A", "D", "C"]])
711
+ });
712
+
713
+ test('should 3 cycles graph getCycles return correct result', () => {
714
+ const graph = new DirectedGraph();
715
+
716
+ graph.addVertex('A');
717
+ graph.addVertex('B');
718
+ graph.addVertex('C');
719
+ graph.addVertex('D');
720
+ graph.addVertex('E');
721
+ graph.addVertex('F');
722
+ graph.addVertex('G');
723
+
724
+ graph.addEdge('A', 'B');
725
+ graph.addEdge('A', 'C');
726
+ graph.addEdge('B', 'D');
727
+ graph.addEdge('C', 'D');
728
+ graph.addEdge('D', 'E');
729
+ graph.addEdge('E', 'B');
730
+ graph.addEdge('B', 'F');
731
+ graph.addEdge('F', 'E');
732
+ graph.addEdge('C', 'G');
733
+ graph.addEdge('G', 'A');
734
+
735
+ const cycles = graph.getCycles();
736
+ expect(cycles.length).toBe(3)
737
+ expect(cycles).toEqual([["A", "C", "G"], ["B", "D", "E"], ["B", "F", "E"]]);
738
+ });
694
739
  })
695
740
 
@@ -237,7 +237,7 @@ describe('cycles, strongly connected components, bridges, articular points in Un
237
237
  const cutVertexes = graph.getCutVertexes();
238
238
  const dfnMap = graph.getDFNMap();
239
239
  const lowMap = graph.getLowMap();
240
- expect(cycles.size).toBe(2);
240
+ expect(cycles.length).toBe(3);
241
241
  expect(scCs.size).toBe(5);
242
242
  expect(bridges.length).toBe(4);
243
243
  expect(cutVertexes.length).toBe(4);
@@ -277,8 +277,52 @@ describe('UndirectedGraph getCycles', () => {
277
277
  graph.addEdge('D', 'E');
278
278
  graph.addEdge('E', 'B');
279
279
  const cycles = graph.getCycles();
280
- expect(cycles.size).toBe(2);
281
- expect(cycles.get(1)).toEqual([{ "key": "A", "value": "A" }, { "key": "B", "value": "B" }, { "key": "D", "value": "D" }, { "key": "C", "value": "C" }]);
282
- expect(cycles.get(2)).toEqual([{ "key": "B", "value": "B" }, { "key": "D", "value": "D" }, { "key": "E", "value": "E" }]);
280
+ expect(cycles.length).toBe(3);
281
+ expect(cycles).toEqual([["A", "B", "D", "C"], ["A", "B", "E", "D", "C"], ["B", "D", "E"]]);
283
282
  })
283
+
284
+ test('should simple cycles graph getCycles return correct result', () => {
285
+ const graph = new UndirectedGraph();
286
+
287
+ graph.addVertex('A');
288
+ graph.addVertex('B');
289
+ graph.addVertex('C');
290
+ graph.addVertex('D');
291
+
292
+ graph.addEdge('A', 'B');
293
+ graph.addEdge('B', 'C');
294
+ graph.addEdge('C', 'A');
295
+ graph.addEdge('A', 'D');
296
+ graph.addEdge('D', 'C');
297
+ const cycles = graph.getCycles();
298
+ expect(cycles.length).toBe(3)
299
+ expect(cycles).toEqual([["A", "B", "C"], ["A", "B", "C", "D"], ["A", "C", "D"]])
300
+ });
301
+
302
+ test('should 3 cycles graph getCycles return correct result', () => {
303
+ const graph = new UndirectedGraph();
304
+
305
+ graph.addVertex('A');
306
+ graph.addVertex('B');
307
+ graph.addVertex('C');
308
+ graph.addVertex('D');
309
+ graph.addVertex('E');
310
+ graph.addVertex('F');
311
+ graph.addVertex('G');
312
+
313
+ graph.addEdge('A', 'B');
314
+ graph.addEdge('A', 'C');
315
+ graph.addEdge('B', 'D');
316
+ graph.addEdge('C', 'D');
317
+ graph.addEdge('D', 'E');
318
+ graph.addEdge('E', 'B');
319
+ graph.addEdge('B', 'F');
320
+ graph.addEdge('F', 'E');
321
+ graph.addEdge('C', 'G');
322
+ graph.addEdge('G', 'A');
323
+
324
+ const cycles = graph.getCycles();
325
+ expect(cycles.length).toBe(10)
326
+ expect(cycles).toEqual([["A", "B", "D", "C"], ["A", "B", "D", "C", "G"], ["A", "B", "E", "D", "C"], ["A", "B", "E", "D", "C", "G"], ["A", "B", "F", "E", "D", "C"], ["A", "B", "F", "E", "D", "C", "G"], ["A", "C", "G"], ["B", "D", "E"], ["B", "D", "E", "F"], ["B", "E", "F"]]);
327
+ });
284
328
  })
@@ -453,7 +453,7 @@ describe('LinkedHashMap Test2', () => {
453
453
  expect(hashMap.last).toEqual([key, value]);
454
454
  expect(hashMap.reverseBegin().next().value).toEqual([key, value]);
455
455
  } else if (index <= 1000) {
456
- expect(hashMap.getAt(index)).toEqual([key, value]);
456
+ expect(hashMap.getAt(index)).toBe(value);
457
457
  }
458
458
  expect(hashMap.get(key)).toEqual(value);
459
459
  index++;
@@ -502,7 +502,7 @@ describe('LinkedHashMap Test2', () => {
502
502
  test('should get element at specific index', () => {
503
503
  hashMap.set('key1', 'value1');
504
504
  hashMap.set('key2', 'value2');
505
- expect(hashMap.getAt(1)).toEqual(['key2', 'value2']);
505
+ expect(hashMap.getAt(1)).toBe('value2');
506
506
  });
507
507
  });
508
508
 
@@ -5,7 +5,12 @@ import { logBigOMetricsWrap } from '../../../utils';
5
5
  describe('Heap Operation Test', () => {
6
6
  it('should numeric heap work well', function () {
7
7
  const minNumHeap = new MinHeap<number>();
8
- minNumHeap.add(1).add(6).add(2).add(0).add(5).add(9);
8
+ minNumHeap.add(1);
9
+ minNumHeap.add(6);
10
+ minNumHeap.add(2);
11
+ minNumHeap.add(0);
12
+ minNumHeap.add(5);
13
+ minNumHeap.add(9);
9
14
  expect(minNumHeap.has(1)).toBe(true);
10
15
  expect(minNumHeap.has(2)).toBe(true);
11
16
  expect(minNumHeap.poll()).toBe(0);
@@ -55,7 +55,7 @@ describe('MinHeap', () => {
55
55
 
56
56
  it('should push & dfs', () => {
57
57
  for (let i = 0; i < n; i++) {
58
- minHeap.push(i);
58
+ minHeap.add(i);
59
59
  }
60
60
  expect(minHeap.dfs()[0]).toBe(0)
61
61
  expect(minHeap.dfs()[999]).toBe(4126)
@@ -19,11 +19,11 @@ describe('DoublyLinkedList Operation Test', () => {
19
19
  it('should out of bound index', () => {
20
20
  expect(list.getNodeAt(-1)).toBe(undefined);
21
21
  expect(list.getNodeAt(5)).toBe(undefined);
22
- expect(list.insertAt(5, 6)).toBe(true);
22
+ expect(list.addAt(5, 6)).toBe(true);
23
23
  });
24
24
 
25
- it('should insertBefore', () => {
26
- expect(list.insertBefore(1, 0)).toBe(true);
25
+ it('should addBefore', () => {
26
+ expect(list.addBefore(1, 0)).toBe(true);
27
27
  });
28
28
 
29
29
  it('should deleteAt', () => {
@@ -49,11 +49,11 @@ describe('DoublyLinkedList Operation Test', () => {
49
49
  expect(list.findBackward(value => value === 0)).toBe(undefined);
50
50
  });
51
51
 
52
- it('should insertAfter tail', () => {
53
- expect(list.insertAfter(list.tail!, 6)).toBe(true);
52
+ it('should addAfter tail', () => {
53
+ expect(list.addAfter(list.tail!, 6)).toBe(true);
54
54
  });
55
55
 
56
- it('should insertAfter tail', () => {
56
+ it('should addAfter tail', () => {
57
57
  expect([...list]).toEqual([1, 2, 3, 4, 5]);
58
58
  });
59
59
  });
@@ -68,7 +68,7 @@ describe('DoublyLinkedList Operation Test', () => {
68
68
  });
69
69
 
70
70
  it('should initialize an empty list', () => {
71
- expect(list.length).toBe(0);
71
+ expect(list.size).toBe(0);
72
72
  expect(list.head).toBe(undefined);
73
73
  expect(list.tail).toBe(undefined);
74
74
  });
@@ -77,7 +77,7 @@ describe('DoublyLinkedList Operation Test', () => {
77
77
  list.push(1);
78
78
  list.push(2);
79
79
  list.push(3);
80
- expect(list.length).toBe(3);
80
+ expect(list.size).toBe(3);
81
81
  expect(list.head!.value).toBe(1);
82
82
  expect(list.tail!.value).toBe(3);
83
83
  });
@@ -87,7 +87,7 @@ describe('DoublyLinkedList Operation Test', () => {
87
87
  list.push(2);
88
88
  const poppedValue = list.pop();
89
89
  expect(poppedValue).toBe(2);
90
- expect(list.length).toBe(1);
90
+ expect(list.size).toBe(1);
91
91
  expect(list.head!.value).toBe(1);
92
92
  expect(list.tail!.value).toBe(1);
93
93
  });
@@ -97,20 +97,20 @@ describe('DoublyLinkedList Operation Test', () => {
97
97
  list.push(3);
98
98
 
99
99
  // Inserting at the beginning
100
- list.insertAt(0, 0);
101
- expect(list.length).toBe(4);
100
+ list.addAt(0, 0);
101
+ expect(list.size).toBe(4);
102
102
  expect(list.getAt(0)).toBe(0);
103
103
  expect(list.getAt(1)).toBe(1);
104
104
 
105
105
  // Inserting in the middle
106
- list.insertAt(2, 1.5);
107
- expect(list.length).toBe(5);
106
+ list.addAt(2, 1.5);
107
+ expect(list.size).toBe(5);
108
108
  expect(list.getAt(2)).toBe(1.5);
109
109
  expect(list.getAt(3)).toBe(2);
110
110
 
111
111
  // Inserting at the end
112
- list.insertAt(5, 4);
113
- expect(list.length).toBe(6);
112
+ list.addAt(5, 4);
113
+ expect(list.size).toBe(6);
114
114
  expect(list.getAt(5)).toBe(4);
115
115
  expect(list.tail!.value).toBe(4);
116
116
  });
@@ -122,18 +122,18 @@ describe('DoublyLinkedList Operation Test', () => {
122
122
 
123
123
  // Deleting from the beginning
124
124
  const deletedValue = list.deleteAt(0);
125
- expect(deletedValue).toBe(1);
126
- expect(list.length).toBe(2);
125
+ expect(deletedValue).toBe(true);
126
+ expect(list.size).toBe(2);
127
127
  expect(list.head!.value).toBe(2);
128
128
 
129
129
  // Deleting from the middle
130
130
  list.deleteAt(0); // Deleting the second element
131
- expect(list.length).toBe(1);
131
+ expect(list.size).toBe(1);
132
132
  expect(list.head!.value).toBe(3);
133
133
 
134
134
  // Deleting from the end
135
135
  list.deleteAt(0);
136
- expect(list.length).toBe(0);
136
+ expect(list.size).toBe(0);
137
137
  expect(list.head).toBe(undefined);
138
138
  expect(list.tail).toBe(undefined);
139
139
  });
@@ -144,16 +144,16 @@ describe('DoublyLinkedList Operation Test', () => {
144
144
  list.push(3);
145
145
 
146
146
  list.delete(2);
147
- expect(list.length).toBe(2);
147
+ expect(list.size).toBe(2);
148
148
  expect(list.head!.value).toBe(1);
149
149
  expect(list.tail!.value).toBe(3);
150
150
 
151
151
  list.delete(1);
152
- expect(list.length).toBe(1);
152
+ expect(list.size).toBe(1);
153
153
  expect(list.head!.value).toBe(3);
154
154
 
155
155
  list.delete(3);
156
- expect(list.length).toBe(0);
156
+ expect(list.size).toBe(0);
157
157
  expect(list.head).toBe(undefined);
158
158
  expect(list.tail).toBe(undefined);
159
159
  });
@@ -206,7 +206,7 @@ describe('DoublyLinkedList Operation Test', () => {
206
206
  list.push(2);
207
207
  list.push(3);
208
208
 
209
- list.insertAfter(2, 2.5);
209
+ list.addAfter(2, 2.5);
210
210
 
211
211
  expect(list.toArray()).toEqual([1, 2, 2.5, 3]);
212
212
  });
@@ -216,7 +216,7 @@ describe('DoublyLinkedList Operation Test', () => {
216
216
  list.push(2);
217
217
  list.push(3);
218
218
 
219
- list.insertBefore(2, 1.5);
219
+ list.addBefore(2, 1.5);
220
220
 
221
221
  expect(list.toArray()).toEqual([1, 1.5, 2, 3]);
222
222
  });
@@ -258,7 +258,7 @@ describe('DoublyLinkedList Operation Test', () => {
258
258
 
259
259
  list.clear();
260
260
 
261
- expect(list.length).toBe(0);
261
+ expect(list.size).toBe(0);
262
262
  expect(list.head).toBe(undefined);
263
263
  expect(list.tail).toBe(undefined);
264
264
  });
@@ -334,7 +334,7 @@ describe('DoublyLinkedList Operation Test', () => {
334
334
  list.push(2);
335
335
  list.push(3);
336
336
 
337
- const success = list.insertAfter(2, 4);
337
+ const success = list.addAfter(2, 4);
338
338
  expect(success).toBe(true);
339
339
  expect(list.toArray()).toEqual([1, 2, 4, 3]);
340
340
  });
@@ -344,7 +344,7 @@ describe('DoublyLinkedList Operation Test', () => {
344
344
  list.push(2);
345
345
  list.push(3);
346
346
 
347
- const success = list.insertBefore(2, 0);
347
+ const success = list.addBefore(2, 0);
348
348
  expect(success).toBe(true);
349
349
  expect(list.toArray()).toEqual([1, 0, 2, 3]);
350
350
  });
@@ -354,7 +354,7 @@ describe('DoublyLinkedList Operation Test', () => {
354
354
  list.push(2);
355
355
  list.push(3);
356
356
 
357
- const success = list.insertAfter(4, 5);
357
+ const success = list.addAfter(4, 5);
358
358
  expect(success).toBe(false);
359
359
  expect(list.toArray()).toEqual([1, 2, 3]);
360
360
  });
@@ -364,7 +364,7 @@ describe('DoublyLinkedList Operation Test', () => {
364
364
  list.push(2);
365
365
  list.push(3);
366
366
 
367
- const success = list.insertBefore(4, 0);
367
+ const success = list.addBefore(4, 0);
368
368
  expect(success).toBe(false);
369
369
  expect(list.toArray()).toEqual([1, 2, 3]);
370
370
  });
@@ -381,7 +381,7 @@ describe('DoublyLinkedList Operation Test', () => {
381
381
  expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
382
382
 
383
383
  const newObj = { keyA: 25 }; // Corrected newObj value
384
- const insertSuccess = objectList.insertBefore(obj2, newObj);
384
+ const insertSuccess = objectList.addBefore(obj2, newObj);
385
385
  expect(insertSuccess).toBe(true);
386
386
 
387
387
  const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
@@ -2,7 +2,7 @@
2
2
  // import {bigO, magnitude} from '../../../utils';
3
3
 
4
4
  describe('LinkedList Performance Test', () => {
5
- it('should DoublyLinkedList insertBefore faster than SinglyLinkedList', () => {
5
+ it('should DoublyLinkedList addBefore faster than SinglyLinkedList', () => {
6
6
  expect(1).toBe(1);
7
7
  });
8
8
  });