data-structure-typed 1.48.5 → 1.48.6

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 (37) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +109 -59
  3. package/README_zh-CN.md +1028 -0
  4. package/benchmark/report.html +16 -16
  5. package/benchmark/report.json +204 -174
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +7 -8
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -9
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/bst.d.ts +15 -11
  10. package/dist/cjs/data-structures/binary-tree/bst.js +21 -13
  11. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  12. package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
  13. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +7 -8
  14. package/dist/mjs/data-structures/binary-tree/binary-tree.js +19 -9
  15. package/dist/mjs/data-structures/binary-tree/bst.d.ts +15 -11
  16. package/dist/mjs/data-structures/binary-tree/bst.js +21 -13
  17. package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
  18. package/dist/umd/data-structure-typed.js +44 -25
  19. package/dist/umd/data-structure-typed.min.js +2 -2
  20. package/dist/umd/data-structure-typed.min.js.map +1 -1
  21. package/package.json +2 -2
  22. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  23. package/src/data-structures/binary-tree/binary-tree.ts +26 -10
  24. package/src/data-structures/binary-tree/bst.ts +35 -20
  25. package/src/data-structures/binary-tree/rb-tree.ts +1 -1
  26. package/src/data-structures/binary-tree/tree-multimap.ts +1 -1
  27. package/src/interfaces/binary-tree.ts +1 -1
  28. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
  29. package/test/performance/data-structures/hash/hash-map.test.ts +8 -8
  30. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  31. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +12 -1
  32. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  33. package/test/performance/data-structures/queue/deque.test.ts +27 -15
  34. package/test/performance/data-structures/queue/queue.test.ts +27 -4
  35. package/test/performance/data-structures/stack/stack.test.ts +2 -2
  36. package/test/unit/data-structures/binary-tree/bst.test.ts +29 -29
  37. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.48.5",
4
- "description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, RedBlack Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack.",
3
+ "version": "1.48.6",
4
+ "description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
7
7
  "types": "dist/mjs/index.d.ts",
@@ -99,7 +99,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
99
99
  /**
100
100
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
101
101
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
102
- *
102
+ *
103
103
  * The function overrides the add method of a binary tree node and balances the tree after inserting
104
104
  * a new node.
105
105
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
@@ -235,7 +235,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
235
235
  /**
236
236
  * Time Complexity O(log n) - O(n)
237
237
  * Space Complexity O(1)
238
- *
238
+ *
239
239
  * The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
240
240
  * existing node with the same key.
241
241
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
@@ -288,22 +288,38 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
288
288
  * Time Complexity: O(k log n) - O(k * n)
289
289
  * Space Complexity: O(1)
290
290
  *
291
- * The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
292
- * current instance, and returns an array of the inserted nodes.
293
- * @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
294
- * `BTNodeExemplar<K, V,N>` objects.
295
- * @returns The function `addMany` returns an array of values, where each value is either of type
296
- * `N`, `null`, or `undefined`.
297
- */
298
- addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>): (N | null | undefined)[] {
291
+ * The `addMany` function takes in a collection of nodes and an optional collection of values, and
292
+ * adds each node with its corresponding value to the data structure.
293
+ * @param nodes - An iterable collection of BTNodeExemplar objects.
294
+ * @param [values] - An optional iterable of values that will be assigned to each node being added.
295
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
296
+ */
297
+ addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[] {
299
298
  // TODO not sure addMany not be run multi times
300
299
  const inserted: (N | null | undefined)[] = [];
300
+
301
+ let valuesIterator: Iterator<V | undefined> | undefined;
302
+ if (values) {
303
+ valuesIterator = values[Symbol.iterator]();
304
+ }
305
+
301
306
  for (const kne of nodes) {
302
- inserted.push(this.add(kne));
307
+ let value: V | undefined | null = undefined;
308
+
309
+ if (valuesIterator) {
310
+ const valueResult = valuesIterator.next();
311
+ if (!valueResult.done) {
312
+ value = valueResult.value;
313
+ }
314
+ }
315
+
316
+ inserted.push(this.add(kne, value));
303
317
  }
318
+
304
319
  return inserted;
305
320
  }
306
321
 
322
+
307
323
  /**
308
324
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
309
325
  * Space Complexity: O(1)
@@ -192,7 +192,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
192
192
  /**
193
193
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
194
194
  * Space Complexity: O(1) - Constant space is used.
195
- *
195
+ *
196
196
  * The `add` function adds a new node to a binary tree, updating the value if the key already exists
197
197
  * or inserting a new node if the key is unique.
198
198
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
@@ -256,31 +256,45 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
256
256
  * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
257
257
  * Space Complexity: O(k) - Additional space is required for the sorted array.
258
258
  *
259
- * The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
260
- * unbalanced manner, and returns an array of the inserted nodes.
261
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
262
- * binary tree.
263
- * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
264
- * adding the nodes. The default value is true.
259
+ * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
260
+ * balancing the tree after each addition.
261
+ * @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
262
+ * the binary tree.
263
+ * @param [values] - An optional iterable of values to be associated with the keys or nodes being
264
+ * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
265
+ * order. If not provided, undefined will be assigned as the value for each key or node.
266
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
267
+ * balanced or not. If set to true, the add operation will be balanced using a binary search tree
268
+ * algorithm. If set to false, the add operation will not be balanced and the elements will be added
269
+ * in the order they appear in the input.
265
270
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
266
- * type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
267
- * value of `this.iterationType`, which means it will use the iteration type specified by the binary
268
- * tree instance.
269
- * @returns The `addMany` function returns an array of `N` or `undefined` values.
271
+ * type of iteration to use when adding multiple keys or nodes. It has a default value of
272
+ * `this.iterationType`, which suggests that it is a property of the current object.
273
+ * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
270
274
  */
271
275
  override addMany(
272
276
  keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>,
277
+ values?: Iterable<V | undefined>,
273
278
  isBalanceAdd = true,
274
279
  iterationType = this.iterationType
275
280
  ): (N | undefined)[] {
276
- const inserted: (N | undefined)[] = []
281
+ const inserted: (N | undefined)[] = [];
282
+
283
+ let valuesIterator: Iterator<V | undefined> | undefined;
284
+
285
+ if (values) {
286
+ valuesIterator = values[Symbol.iterator]();
287
+ }
288
+
277
289
  if (!isBalanceAdd) {
278
290
  for (const kve of keysOrNodesOrEntries) {
279
- const nn = this.add(kve)
291
+ const value = valuesIterator?.next().value;
292
+ const nn = this.add(kve, value);
280
293
  inserted.push(nn);
281
294
  }
282
295
  return inserted;
283
296
  }
297
+
284
298
  const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
285
299
 
286
300
  const isRealBTNExemplar = (kve: BTNodeExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
@@ -292,22 +306,20 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
292
306
  isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
293
307
  }
294
308
 
295
- // TODO this addMany function is inefficient, it should be optimized
296
309
  let sorted: BTNodePureExemplar<K, V, N>[] = [];
297
310
 
298
311
  sorted = realBTNExemplars.sort((a, b) => {
299
312
  let aR: number, bR: number;
300
- if (this.isEntry(a)) aR = this.extractor(a[0])
301
- else if (this.isRealNode(a)) aR = this.extractor(a.key)
313
+ if (this.isEntry(a)) aR = this.extractor(a[0]);
314
+ else if (this.isRealNode(a)) aR = this.extractor(a.key);
302
315
  else aR = this.extractor(a);
303
316
 
304
- if (this.isEntry(b)) bR = this.extractor(b[0])
305
- else if (this.isRealNode(b)) bR = this.extractor(b.key)
317
+ if (this.isEntry(b)) bR = this.extractor(b[0]);
318
+ else if (this.isRealNode(b)) bR = this.extractor(b.key);
306
319
  else bR = this.extractor(b);
307
320
 
308
321
  return aR - bR;
309
- })
310
-
322
+ });
311
323
 
312
324
  const _dfs = (arr: BTNodePureExemplar<K, V, N>[]) => {
313
325
  if (arr.length === 0) return;
@@ -318,6 +330,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
318
330
  _dfs(arr.slice(0, mid));
319
331
  _dfs(arr.slice(mid + 1));
320
332
  };
333
+
321
334
  const _iterate = () => {
322
335
  const n = sorted.length;
323
336
  const stack: [[number, number]] = [[0, n - 1]];
@@ -335,6 +348,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
335
348
  }
336
349
  }
337
350
  };
351
+
338
352
  if (iterationType === IterationType.RECURSIVE) {
339
353
  _dfs(sorted);
340
354
  } else {
@@ -344,6 +358,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
344
358
  return inserted;
345
359
  }
346
360
 
361
+
347
362
  // /**
348
363
  // * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
349
364
  // * Space Complexity: O(n) - Additional space is required for the sorted array.
@@ -153,7 +153,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
153
153
  /**
154
154
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
155
155
  * Space Complexity: O(1)
156
- *
156
+ *
157
157
  * The `add` function adds a new node to a binary search tree and performs necessary rotations and
158
158
  * color changes to maintain the red-black tree properties.
159
159
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
@@ -126,7 +126,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
126
126
  /**
127
127
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
128
128
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
129
- *
129
+ *
130
130
  * The function overrides the add method of a binary tree node and adds a new node to the tree.
131
131
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
132
132
  * entry. It represents the key, node, or entry that you want to add to the binary tree.
@@ -15,7 +15,7 @@ export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V,
15
15
 
16
16
  add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
17
17
 
18
- addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>): (N | null | undefined)[];
18
+ addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
19
19
 
20
20
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
21
21
  }
@@ -18,7 +18,7 @@ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
18
18
  });
19
19
 
20
20
  if (isCompetitor) {
21
- suite.add(`${HUNDRED_THOUSAND.toLocaleString()} CPT add`, () => {
21
+ suite.add(`CPT ${HUNDRED_THOUSAND.toLocaleString()} add`, () => {
22
22
  for (let i = 0; i < arr.length; i++) {
23
23
  cOrderedMap.setElement(arr[i], arr[i]);
24
24
  }
@@ -16,7 +16,7 @@ suite.add(`${MILLION.toLocaleString()} set`, () => {
16
16
  });
17
17
 
18
18
  if (isCompetitor) {
19
- suite.add(`${MILLION.toLocaleString()} CPT set`, () => {
19
+ suite.add(`CPT ${MILLION.toLocaleString()} set`, () => {
20
20
  const hm = new CHashMap<number, number>();
21
21
 
22
22
  for (let i = 0; i < MILLION; i++) {
@@ -25,13 +25,13 @@ if (isCompetitor) {
25
25
  });
26
26
  }
27
27
 
28
- suite.add(`${MILLION.toLocaleString()} Map set`, () => {
28
+ suite.add(`Native Map ${MILLION.toLocaleString()} set`, () => {
29
29
  const hm = new Map<number, number>();
30
30
 
31
31
  for (let i = 0; i < MILLION; i++) hm.set(i, i);
32
32
  });
33
33
 
34
- suite.add(`${MILLION.toLocaleString()} Set add`, () => {
34
+ suite.add(`Native Set ${MILLION.toLocaleString()} add`, () => {
35
35
  const hs = new Set<number>();
36
36
 
37
37
  for (let i = 0; i < MILLION; i++) hs.add(i);
@@ -49,7 +49,7 @@ suite.add(`${MILLION.toLocaleString()} set & get`, () => {
49
49
  });
50
50
 
51
51
  if (isCompetitor) {
52
- suite.add(`${MILLION.toLocaleString()} CPT set & get`, () => {
52
+ suite.add(`CPT ${MILLION.toLocaleString()} set & get`, () => {
53
53
  const hm = new CHashMap<number, number>();
54
54
 
55
55
  for (let i = 0; i < MILLION; i++) {
@@ -61,7 +61,7 @@ if (isCompetitor) {
61
61
  });
62
62
  }
63
63
 
64
- suite.add(`${MILLION.toLocaleString()} Map set & get`, () => {
64
+ suite.add(`Native Map ${MILLION.toLocaleString()} set & get`, () => {
65
65
  const hm = new Map<number, number>();
66
66
 
67
67
  for (let i = 0; i < MILLION; i++) {
@@ -72,7 +72,7 @@ suite.add(`${MILLION.toLocaleString()} Map set & get`, () => {
72
72
  }
73
73
  });
74
74
 
75
- suite.add(`${MILLION.toLocaleString()} Set add & has`, () => {
75
+ suite.add(`Native Set ${MILLION.toLocaleString()} add & has`, () => {
76
76
  const hs = new Set<number>();
77
77
 
78
78
  for (let i = 0; i < MILLION; i++) hs.add(i);
@@ -94,7 +94,7 @@ suite.add(`${MILLION.toLocaleString()} ObjKey set & get`, () => {
94
94
  }
95
95
  });
96
96
 
97
- suite.add(`${MILLION.toLocaleString()} Map ObjKey set & get`, () => {
97
+ suite.add(`Native Map ${MILLION.toLocaleString()} ObjKey set & get`, () => {
98
98
  const hm = new Map<[number, number], number>();
99
99
  const objs: [number, number][] = [];
100
100
  for (let i = 0; i < MILLION; i++) {
@@ -107,7 +107,7 @@ suite.add(`${MILLION.toLocaleString()} Map ObjKey set & get`, () => {
107
107
  }
108
108
  });
109
109
 
110
- suite.add(`${MILLION.toLocaleString()} Set ObjKey add & has`, () => {
110
+ suite.add(`Native Set ${MILLION.toLocaleString()} ObjKey add & has`, () => {
111
111
  const hs = new Set<[number, number]>();
112
112
  const objs: [number, number][] = [];
113
113
  for (let i = 0; i < MILLION; i++) {
@@ -17,7 +17,7 @@ suite.add(`${LINEAR.toLocaleString()} push`, () => {
17
17
 
18
18
 
19
19
  if (isCompetitor) {
20
- suite.add(`${LINEAR.toLocaleString()} CPT push`, () => {
20
+ suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
21
21
  const list = new CLinkedList<number>();
22
22
 
23
23
  for (let i = 0; i < LINEAR; i++) {
@@ -35,7 +35,7 @@ suite.add(`${LINEAR.toLocaleString()} unshift`, () => {
35
35
  });
36
36
 
37
37
  if (isCompetitor) {
38
- suite.add(`${LINEAR.toLocaleString()} CPT unshift`, () => {
38
+ suite.add(`CPT ${LINEAR.toLocaleString()} unshift`, () => {
39
39
  const list = new CLinkedList<number>();
40
40
 
41
41
  for (let i = 0; i < LINEAR; i++) {
@@ -3,9 +3,20 @@ import * as Benchmark from 'benchmark';
3
3
  import { magnitude } from '../../../utils';
4
4
 
5
5
  const suite = new Benchmark.Suite();
6
- const { TEN_THOUSAND } = magnitude;
6
+ const { MILLION, TEN_THOUSAND } = magnitude;
7
7
 
8
8
  suite
9
+ .add(`${MILLION.toLocaleString()} push & shift`, () => {
10
+ const list = new SinglyLinkedList<number>();
11
+
12
+ for (let i = 0; i < MILLION; i++) {
13
+ list.push(i);
14
+ }
15
+
16
+ for (let i = 0; i < MILLION; i++) {
17
+ list.shift();
18
+ }
19
+ })
9
20
  .add(`${TEN_THOUSAND.toLocaleString()} push & pop`, () => {
10
21
  const list = new SinglyLinkedList<number>();
11
22
 
@@ -19,7 +19,7 @@ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
19
19
  }
20
20
  });
21
21
  if (isCompetitor) {
22
- suite.add(`${HUNDRED_THOUSAND.toLocaleString()} CPT add & pop`, () => {
22
+ suite.add(`CPT ${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
23
23
  const pq = new CPriorityQueue<number>();
24
24
 
25
25
  for (let i = 0; i < HUNDRED_THOUSAND; i++) {
@@ -5,7 +5,7 @@ import { magnitude } from '../../../utils';
5
5
  import { isCompetitor } from '../../../config';
6
6
 
7
7
  export const suite = new Benchmark.Suite();
8
- const { LINEAR } = magnitude;
8
+ const { LINEAR, HUNDRED_THOUSAND } = magnitude;
9
9
 
10
10
  suite.add(`${LINEAR.toLocaleString()} push`, () => {
11
11
  const deque = new Deque<number>();
@@ -13,7 +13,7 @@ suite.add(`${LINEAR.toLocaleString()} push`, () => {
13
13
  });
14
14
 
15
15
  if (isCompetitor) {
16
- suite.add(`${LINEAR.toLocaleString()} CPT push`, () => {
16
+ suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
17
17
  const _deque = new CDeque<number>();
18
18
  for (let i = 0; i < LINEAR; i++) _deque.pushBack(i);
19
19
  });
@@ -26,16 +26,28 @@ suite.add(`${LINEAR.toLocaleString()} push & pop`, () => {
26
26
  for (let i = 0; i < LINEAR; i++) _deque.push(i);
27
27
  for (let i = 0; i < LINEAR; i++) _deque.pop();
28
28
 
29
- });
30
- suite.add(`${LINEAR.toLocaleString()} push & shift`, () => {
31
- const _deque = new Deque<number>();
32
-
33
- for (let i = 0; i < LINEAR; i++) _deque.push(i);
34
- for (let i = 0; i < LINEAR; i++) _deque.shift();
35
- });
36
- suite.add(`${LINEAR.toLocaleString()} unshift & shift`, () => {
37
- const _deque = new Deque<number>();
38
-
39
- for (let i = 0; i < LINEAR; i++) _deque.unshift(i);
40
- for (let i = 0; i < LINEAR; i++) _deque.shift();
41
- });
29
+ })
30
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
31
+ const _deque = new Deque<number>();
32
+
33
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) _deque.push(i);
34
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) _deque.shift();
35
+ })
36
+ .add(`Native Array ${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
37
+ const _deque = new Array<number>();
38
+
39
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) _deque.push(i);
40
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) _deque.shift();
41
+ })
42
+ .add(`${HUNDRED_THOUSAND.toLocaleString()} unshift & shift`, () => {
43
+ const _deque = new Deque<number>();
44
+
45
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) _deque.unshift(i);
46
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) _deque.shift();
47
+ })
48
+ .add(`Native Array ${HUNDRED_THOUSAND.toLocaleString()} unshift & shift`, () => {
49
+ const _deque = new Array<number>();
50
+
51
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) _deque.unshift(i);
52
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) _deque.shift();
53
+ });
@@ -5,7 +5,7 @@ import { magnitude } from '../../../utils';
5
5
  import { isCompetitor } from '../../../config';
6
6
 
7
7
  const suite = new Benchmark.Suite();
8
- const { LINEAR } = magnitude;
8
+ const { LINEAR, HUNDRED_THOUSAND } = magnitude;
9
9
 
10
10
  suite.add(`${LINEAR.toLocaleString()} push`, () => {
11
11
  const queue = new Queue<number>();
@@ -15,7 +15,7 @@ suite.add(`${LINEAR.toLocaleString()} push`, () => {
15
15
  }
16
16
  });
17
17
  if (isCompetitor) {
18
- suite.add(`${LINEAR.toLocaleString()} CPT push`, () => {
18
+ suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
19
19
  const queue = new CQueue<number>();
20
20
 
21
21
  for (let i = 0; i < LINEAR; i++) {
@@ -23,13 +23,36 @@ if (isCompetitor) {
23
23
  }
24
24
  });
25
25
  }
26
- suite.add(`${LINEAR.toLocaleString()} push & shift`, () => {
26
+ suite.add(`${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
27
27
  const queue = new Queue<number>();
28
28
 
29
- for (let i = 0; i < LINEAR; i++) {
29
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) {
30
30
  queue.push(i);
31
+ }
32
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) {
31
33
  queue.shift();
32
34
  }
33
35
  });
36
+ suite.add(`Native Array ${HUNDRED_THOUSAND.toLocaleString()} push & shift`, () => {
37
+ const arr = new Array<number>();
38
+
39
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) {
40
+ arr.push(i);
41
+ }
42
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) {
43
+ arr.shift();
44
+ }
45
+ })
46
+ .add(`Native Array ${HUNDRED_THOUSAND.toLocaleString()} push & pop`, () => {
47
+ const arr = new Array<number>();
48
+
49
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) {
50
+ arr.push(i);
51
+ }
52
+
53
+ for (let i = 0; i < HUNDRED_THOUSAND; i++) {
54
+ arr.pop();
55
+ }
56
+ });
34
57
 
35
58
  export { suite };
@@ -15,7 +15,7 @@ suite.add(`${LINEAR.toLocaleString()} push`, () => {
15
15
  }
16
16
  });
17
17
  if (isCompetitor) {
18
- suite.add(`${LINEAR.toLocaleString()} CPT push`, () => {
18
+ suite.add(`CPT ${LINEAR.toLocaleString()} push`, () => {
19
19
  const queue = new CStack<number>();
20
20
 
21
21
  for (let i = 0; i < LINEAR; i++) {
@@ -34,7 +34,7 @@ suite.add(`${LINEAR.toLocaleString()} push & pop`, () => {
34
34
  }
35
35
  });
36
36
  if (isCompetitor) {
37
- suite.add(`${LINEAR.toLocaleString()} CPT push & pop`, () => {
37
+ suite.add(`CPT ${LINEAR.toLocaleString()} push & pop`, () => {
38
38
  const queue = new CStack<number>();
39
39
 
40
40
  for (let i = 0; i < LINEAR; i++) {
@@ -10,7 +10,7 @@ describe('BST operations test', () => {
10
10
  bst.add([11, 11]);
11
11
  bst.add([3, 3]);
12
12
  const idsAndValues: [number, number][] = [[15, 15], [1, 1], [8, 8], [13, 13], [16, 16], [2, 2], [6, 6], [9, 9], [12, 12], [14, 14], [4, 4], [7, 7], [10, 10], [5, 5]];
13
- bst.addMany(idsAndValues, false);
13
+ bst.addMany(idsAndValues, undefined, false);
14
14
  expect(bst.root).toBeInstanceOf(BSTNode);
15
15
 
16
16
  if (bst.root) expect(bst.root.key).toBe(11);
@@ -189,28 +189,27 @@ describe('BST operations test', () => {
189
189
  });
190
190
 
191
191
  it('should perform various operations on a Binary Search Tree with object values', () => {
192
- const objBST = new BST<number, { key: number; keyA: number }>();
192
+ const objBST = new BST<number, { name: string; age: number }>();
193
193
  expect(objBST).toBeInstanceOf(BST);
194
- objBST.add([11, { key: 11, keyA: 11 }]);
195
- objBST.add([3, { key: 3, keyA: 3 }]);
196
- const values: [number, { key: number; keyA: number }][] = [
197
- [15, { key: 15, keyA: 15 }],
198
- [1, { key: 1, keyA: 1 }],
199
- [8, { key: 8, keyA: 8 }],
200
- [13, { key: 13, keyA: 13 }],
201
- [16, { key: 16, keyA: 16 }],
202
- [2, { key: 2, keyA: 2 }],
203
- [6, { key: 6, keyA: 6 }],
204
- [9, { key: 9, keyA: 9 }],
205
- [12, { key: 12, keyA: 12 }],
206
- [14, { key: 14, keyA: 14 }],
207
- [4, { key: 4, keyA: 4 }],
208
- [7, { key: 7, keyA: 7 }],
209
- [10, { key: 10, keyA: 10 }],
210
- [5, { key: 5, keyA: 5 }]
211
- ];
212
-
213
- objBST.addMany(values, false);
194
+ objBST.add([11, { name: '11', age: 11 }]);
195
+ objBST.add([3, { name: '3', age: 3 }]);
196
+
197
+ objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [
198
+ { "name": "Alice", "age": 15 },
199
+ { "name": "Bob", "age": 1 },
200
+ { "name": "Charlie", "age": 8 },
201
+ { "name": "David", "age": 13 },
202
+ { "name": "Emma", "age": 16 },
203
+ { "name": "Frank", "age": 2 },
204
+ { "name": "Grace", "age": 6 },
205
+ { "name": "Hannah", "age": 9 },
206
+ { "name": "Isaac", "age": 12 },
207
+ { "name": "Jack", "age": 14 },
208
+ { "name": "Katie", "age": 4 },
209
+ { "name": "Liam", "age": 7 },
210
+ { "name": "Mia", "age": 10 },
211
+ { "name": "Noah", "age": 5 }
212
+ ], false);
214
213
 
215
214
  expect(objBST.root).toBeInstanceOf(BSTNode);
216
215
 
@@ -232,7 +231,7 @@ describe('BST operations test', () => {
232
231
  expect(leftMost?.key).toBe(1);
233
232
 
234
233
  const node15 = objBST.getNode(15);
235
- expect(node15?.value).toEqual({ key: 15, keyA: 15 });
234
+ expect(node15?.value).toEqual({ name: 'Alice', age: 15 });
236
235
  const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
237
236
  expect(minNodeBySpecificNode?.key).toBe(12);
238
237
 
@@ -256,7 +255,7 @@ describe('BST operations test', () => {
256
255
  objBST.perfectlyBalance();
257
256
  expect(objBST.isPerfectlyBalanced()).toBe(true);
258
257
 
259
- const bfsNodesAfterBalanced: BSTNode<number, { key: number; keyA: number }>[] = [];
258
+ const bfsNodesAfterBalanced: BSTNode<number, { name: string; age: number }>[] = [];
260
259
  objBST.bfs(node => bfsNodesAfterBalanced.push(node));
261
260
  expect(bfsNodesAfterBalanced[0].key).toBe(8);
262
261
  expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
@@ -381,7 +380,7 @@ describe('BST operations test', () => {
381
380
  expect(bfsIDs[1]).toBe(12);
382
381
  expect(bfsIDs[2]).toBe(16);
383
382
 
384
- const bfsNodes: BSTNode<number, { key: number; keyA: number }>[] = [];
383
+ const bfsNodes: BSTNode<number, { name: string; age: number }>[] = [];
385
384
  objBST.bfs(node => bfsNodes.push(node));
386
385
  expect(bfsNodes[0].key).toBe(2);
387
386
  expect(bfsNodes[1].key).toBe(12);
@@ -396,7 +395,7 @@ describe('BST operations test recursively', () => {
396
395
  bst.add([11, 11]);
397
396
  bst.add([3, 3]);
398
397
  const idsAndValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
399
- bst.addMany(idsAndValues, false);
398
+ bst.addMany(idsAndValues, undefined, false);
400
399
  expect(bst.root).toBeInstanceOf(BSTNode);
401
400
 
402
401
  if (bst.root) expect(bst.root.key).toBe(11);
@@ -580,7 +579,7 @@ describe('BST operations test recursively', () => {
580
579
  expect(objBST).toBeInstanceOf(BST);
581
580
  objBST.add([11, { key: 11, keyA: 11 }]);
582
581
  objBST.add([3, { key: 3, keyA: 3 }]);
583
- const values: [number, { key: number; keyA: number }][] = [
582
+ const entries: [number, { key: number; keyA: number }][] = [
584
583
  [15, { key: 15, keyA: 15 }],
585
584
  [1, { key: 1, keyA: 1 }],
586
585
  [8, { key: 8, keyA: 8 }],
@@ -598,7 +597,8 @@ describe('BST operations test recursively', () => {
598
597
  ];
599
598
 
600
599
  objBST.addMany(
601
- values,
600
+ entries,
601
+ undefined,
602
602
  false
603
603
  );
604
604
 
@@ -829,7 +829,7 @@ describe('BST Performance test', function () {
829
829
 
830
830
  it('should the lastKey of a BST to be the largest key', function () {
831
831
  const bst = new BST();
832
- bst.addMany([9, 8, 7, 3, 1, 2, 5, 4, 6], false);
832
+ bst.addMany([9, 8, 7, 3, 1, 2, 5, 4, 6], undefined, false);
833
833
  // TODO
834
834
  // expect(bst.lastKey()).toBe(9);
835
835
  });
@@ -5,7 +5,7 @@ describe('Overall BinaryTree Test', () => {
5
5
  const bst = new BST();
6
6
  bst.add(11);
7
7
  bst.add(3);
8
- bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], false);
8
+ bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], undefined, false);
9
9
  bst.size === 16; // true
10
10
  expect(bst.size).toBe(16); // true
11
11
  bst.has(6); // true