data-structure-typed 2.1.1 → 2.1.2

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 (51) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +19 -7
  3. package/dist/cjs/index.cjs +22 -22
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +22 -22
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  8. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  12. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
  13. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  14. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  15. package/dist/types/data-structures/heap/heap.d.ts +4 -4
  16. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  17. package/dist/types/interfaces/binary-tree.d.ts +1 -1
  18. package/dist/umd/data-structure-typed.js +87 -165
  19. package/dist/umd/data-structure-typed.js.map +1 -1
  20. package/dist/umd/data-structure-typed.min.js +2 -2
  21. package/dist/umd/data-structure-typed.min.js.map +1 -1
  22. package/jest.integration.config.js +7 -3
  23. package/package.json +11 -7
  24. package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
  25. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  26. package/src/data-structures/binary-tree/avl-tree.ts +2 -2
  27. package/src/data-structures/binary-tree/binary-tree.ts +4 -4
  28. package/src/data-structures/binary-tree/bst.ts +1 -1
  29. package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
  30. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  31. package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
  32. package/src/data-structures/heap/heap.ts +5 -5
  33. package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
  34. package/src/interfaces/binary-tree.ts +1 -1
  35. package/test/integration/compile.test.mjs +159 -0
  36. package/test/integration/compile.test.ts +176 -0
  37. package/test/integration/heap.test.js +1 -1
  38. package/test/integration/index.html +1 -1
  39. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
  40. package/{tsconfig-base.json → tsconfig.base.json} +0 -1
  41. package/tsconfig.test.json +1 -0
  42. package/{tsconfig-types.json → tsconfig.types.json} +1 -3
  43. package/tsup.config.js +2 -3
  44. package/dist/index.cjs +0 -13091
  45. package/dist/index.cjs.map +0 -1
  46. package/dist/index.js +0 -13013
  47. package/dist/index.js.map +0 -1
  48. package/test/integration/compile.js +0 -144
  49. package/test/integration/compile.mjs +0 -135
  50. package/test/integration/compile.ts +0 -171
  51. /package/{tsup.node.config.ts → tsup.node.config.js} +0 -0
@@ -1,8 +1,12 @@
1
1
  module.exports = {
2
2
  preset: 'ts-jest',
3
3
  testEnvironment: 'node',
4
- testMatch: ['<rootDir>/test/integration/**/*.test.ts', '<rootDir>/test/integration/**/*.test.js'],
4
+ testMatch: [
5
+ '<rootDir>/test/integration/**/*.test.ts',
6
+ '<rootDir>/test/integration/**/*.test.js',
7
+ '<rootDir>/test/integration/**/*.test.mjs'
8
+ ],
5
9
  transform: {
6
- '^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: 'tsconfig.test.json' }],
7
- },
10
+ '^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: 'tsconfig.test.json' }]
11
+ }
8
12
  };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Standard data structure",
5
5
  "browser": "dist/umd/data-structure-typed.min.js",
6
6
  "umd:main": "dist/umd/data-structure-typed.min.js",
7
7
  "main": "dist/cjs/index.cjs",
8
- "module": "./dist/esm/index.mjs",
8
+ "module": "dist/esm/index.mjs",
9
9
  "types": "dist/types/index.d.ts",
10
10
  "exports": {
11
11
  ".": {
@@ -15,18 +15,22 @@
15
15
  }
16
16
  },
17
17
  "sideEffects": false,
18
+ "engines": {
19
+ "node": ">=12.20.0",
20
+ "npm": ">=6.14.0"
21
+ },
18
22
  "scripts": {
19
23
  "build": "npm run build:ecu && npm run build:docs-class",
20
- "build:node": "tsup --config tsup.node.config.ts",
21
- "build:types": "rm -rf dist/types && tsc -p tsconfig-types.json",
24
+ "build:node": "tsup --config tsup.node.config.js",
25
+ "build:types": "rm -rf dist/types && tsc -p tsconfig.types.json",
22
26
  "build:umd": "tsup",
23
27
  "build:ecu": "npm run build:node && npm run build:types && npm run build:umd",
24
28
  "build:docs": "npm run gen:examples && typedoc --out docs ./src",
25
29
  "build:docs-class": "npm run gen:examples && typedoc --out docs ./src/data-structures",
26
- "gen:examples": "ts-node scripts/testToExample.ts",
30
+ "gen:examples": "ts-node scripts/test-to-example.ts",
27
31
  "test:in-band": "jest --runInBand",
28
32
  "test": "npm run test:in-band",
29
- "test:integration": "npm run update:subs && jest --config jest.integration.config.js && tsc test/integration/compile.ts && node test/integration/compile.mjs",
33
+ "test:integration": "npm run update:subs && jest --config jest.integration.config.js && tsc test/integration/compile.test.ts && node test/integration/compile.mjs",
30
34
  "test:perf": "npm run build:ecu && ts-node test/performance/benchmark-runner.ts --isolate --gc --cooldown-ms=80",
31
35
  "check": "tsc --noEmit",
32
36
  "check:circular-refs": "dependency-cruiser src",
@@ -77,7 +81,7 @@
77
81
  "benchmark": "^2.1.4",
78
82
  "binary-tree-typed": "^1.54.3",
79
83
  "bst-typed": "^1.54.3",
80
- "data-structure-typed": "^1.54.3",
84
+ "data-structure-typed": "^2.1.1",
81
85
  "dependency-cruiser": "^16.5.0",
82
86
  "doctoc": "^2.2.1",
83
87
  "eslint": "^9.13.0",
@@ -130,7 +130,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
130
130
  return sum;
131
131
  }
132
132
 
133
- override _createNode(key: K, value?: V, count?: number): AVLTreeCounterNode<K, V> {
133
+ override createNode(key: K, value?: V, count?: number): AVLTreeCounterNode<K, V> {
134
134
  return new AVLTreeCounterNode(key, this._isMapMode ? undefined : value, count) as AVLTreeCounterNode<K, V>;
135
135
  }
136
136
 
@@ -391,10 +391,10 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
391
391
  const [key, entryValue] = keyNodeOrEntry;
392
392
  if (key === undefined || key === null) return [undefined, undefined];
393
393
  const finalValue = value ?? entryValue;
394
- return [this._createNode(key, finalValue, count), finalValue];
394
+ return [this.createNode(key, finalValue, count), finalValue];
395
395
  }
396
396
 
397
- return [this._createNode(keyNodeOrEntry, value, count), value];
397
+ return [this.createNode(keyNodeOrEntry, value, count), value];
398
398
  }
399
399
 
400
400
  /**
@@ -412,7 +412,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
412
412
  destNode = this.ensureNode(destNode);
413
413
  if (srcNode && destNode) {
414
414
  const { key, value, count, height } = destNode;
415
- const tempNode = this._createNode(key, value, count);
415
+ const tempNode = this.createNode(key, value, count);
416
416
  if (tempNode) {
417
417
  tempNode.height = height;
418
418
 
@@ -113,7 +113,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
113
113
  }
114
114
  }
115
115
 
116
- override _createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
116
+ override createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
117
117
  return new AVLTreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
118
118
  }
119
119
 
@@ -196,7 +196,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
196
196
  * @param [value] - The value for the new node.
197
197
  * @returns The newly created AVLTreeNode.
198
198
  */
199
- override _createNode(key: K, value?: V): AVLTreeNode<K, V> {
199
+ override createNode(key: K, value?: V): AVLTreeNode<K, V> {
200
200
  return new AVLTreeNode<K, V>(key, this._isMapMode ? undefined : value) as AVLTreeNode<K, V>;
201
201
  }
202
202
 
@@ -370,7 +370,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
370
370
 
371
371
  if (srcNodeEnsured && destNodeEnsured) {
372
372
  const { key, value, height } = destNodeEnsured;
373
- const tempNode = this._createNode(key, value);
373
+ const tempNode = this.createNode(key, value);
374
374
 
375
375
  if (tempNode) {
376
376
  tempNode.height = height;
@@ -390,7 +390,7 @@ export class BinaryTree<K = any, V = any, R = any>
390
390
  * @param [value] - The value for the new node (used if not in Map mode).
391
391
  * @returns The newly created node.
392
392
  */
393
- _createNode(key: K, value?: V): BinaryTreeNode<K, V> {
393
+ createNode(key: K, value?: V): BinaryTreeNode<K, V> {
394
394
  return new BinaryTreeNode<K, V>(key, this._isMapMode ? undefined : value);
395
395
  }
396
396
 
@@ -2019,10 +2019,10 @@ export class BinaryTree<K = any, V = any, R = any>
2019
2019
  if (key === undefined) return [undefined, undefined];
2020
2020
  else if (key === null) return [null, undefined];
2021
2021
  const finalValue = value ?? entryValue;
2022
- return [this._createNode(key, finalValue), finalValue];
2022
+ return [this.createNode(key, finalValue), finalValue];
2023
2023
  }
2024
2024
 
2025
- return [this._createNode(keyNodeOrEntry, value), value];
2025
+ return [this.createNode(keyNodeOrEntry, value), value];
2026
2026
  }
2027
2027
 
2028
2028
  /**
@@ -2147,7 +2147,7 @@ export class BinaryTree<K = any, V = any, R = any>
2147
2147
 
2148
2148
  if (srcNode && destNode) {
2149
2149
  const { key, value } = destNode;
2150
- const tempNode = this._createNode(key, value); // Use a temp node to hold dest properties
2150
+ const tempNode = this.createNode(key, value); // Use a temp node to hold dest properties
2151
2151
 
2152
2152
  if (tempNode) {
2153
2153
  // Copy src to dest
@@ -268,7 +268,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
268
268
  * @param [value] - The value for the new node (used if not in Map mode).
269
269
  * @returns The newly created BSTNode.
270
270
  */
271
- override _createNode(key: K, value?: V): BSTNode<K, V> {
271
+ override createNode(key: K, value?: V): BSTNode<K, V> {
272
272
  return new BSTNode<K, V>(key, this._isMapMode ? undefined : value);
273
273
  }
274
274
 
@@ -89,7 +89,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
89
89
  }
90
90
 
91
91
  /**
92
- * RRRRRRRRRRRRRRRRRRRed-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
92
+ * Represents a Red-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
93
93
  * @remarks Time O(1), Space O(1)
94
94
  * @template K
95
95
  * @template V
@@ -176,7 +176,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
176
176
  * @param color - See parameter type for details.
177
177
  * @returns A new RedBlackTreeNode instance.
178
178
  */
179
- override _createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): RedBlackTreeNode<K, V> {
179
+ override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): RedBlackTreeNode<K, V> {
180
180
  return new RedBlackTreeNode<K, V>(key, this._isMapMode ? undefined : value, color);
181
181
  }
182
182
 
@@ -142,7 +142,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
142
142
  return sum;
143
143
  }
144
144
 
145
- override _createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): TreeCounterNode<K, V> {
145
+ override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): TreeCounterNode<K, V> {
146
146
  return new TreeCounterNode(key, this._isMapMode ? undefined : value, count, color) as TreeCounterNode<K, V>;
147
147
  }
148
148
 
@@ -427,10 +427,10 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
427
427
  const [key, entryValue] = keyNodeOrEntry;
428
428
  if (key === undefined || key === null) return [undefined, undefined];
429
429
  const finalValue = value ?? entryValue;
430
- return [this._createNode(key, finalValue, 'BLACK', count), finalValue];
430
+ return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
431
431
  }
432
432
 
433
- return [this._createNode(keyNodeOrEntry, value, 'BLACK', count), value];
433
+ return [this.createNode(keyNodeOrEntry, value, 'BLACK', count), value];
434
434
  }
435
435
 
436
436
  /**
@@ -448,7 +448,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
448
448
  destNode = this.ensureNode(destNode);
449
449
  if (srcNode && destNode) {
450
450
  const { key, value, count, color } = destNode;
451
- const tempNode = this._createNode(key, value, color, count);
451
+ const tempNode = this.createNode(key, value, color, count);
452
452
  if (tempNode) {
453
453
  tempNode.color = color;
454
454
 
@@ -270,7 +270,7 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
270
270
  }
271
271
  }
272
272
 
273
- override _createNode(key: K, value: V[] = []): TreeMultiMapNode<K, V> {
273
+ override createNode(key: K, value: V[] = []): TreeMultiMapNode<K, V> {
274
274
  return new TreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
275
275
  }
276
276
 
@@ -190,7 +190,7 @@ import { IterableElementBase } from '../base';
190
190
  * ]);
191
191
  * console.log(scheduleTasks(tasks, 2)); // expectedMap
192
192
  */
193
- export class Heap<E = unknown, R = never> extends IterableElementBase<E, R> {
193
+ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
194
194
  protected _equals: (a: E, b: E) => boolean = Object.is;
195
195
 
196
196
  /**
@@ -255,7 +255,7 @@ export class Heap<E = unknown, R = never> extends IterableElementBase<E, R> {
255
255
  * @returns A new heap instance of this class.
256
256
  */
257
257
 
258
- static from<T, R = never, S extends Heap<T, R> = Heap<T, R>>(
258
+ static from<T, R = any, S extends Heap<T, R> = Heap<T, R>>(
259
259
  this: new (elements?: Iterable<T | R>, options?: HeapOptions<T, R>) => S,
260
260
  elements?: Iterable<T | R>,
261
261
  options?: HeapOptions<T, R>
@@ -273,7 +273,7 @@ export class Heap<E = unknown, R = never> extends IterableElementBase<E, R> {
273
273
  * @returns A new Heap built from elements.
274
274
  */
275
275
 
276
- static heapify<EE = unknown, RR = never>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR> {
276
+ static heapify<EE = any, RR = any>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR> {
277
277
  return new Heap<EE, RR>(elements, options);
278
278
  }
279
279
 
@@ -788,7 +788,7 @@ export class FibonacciHeap<E> {
788
788
  */
789
789
 
790
790
  push(element: E): this {
791
- const node = this._createNode(element);
791
+ const node = this.createNode(element);
792
792
  node.left = node;
793
793
  node.right = node;
794
794
  this.mergeWithRoot(node);
@@ -900,7 +900,7 @@ export class FibonacciHeap<E> {
900
900
  heapToMerge.clear();
901
901
  }
902
902
 
903
- _createNode(element: E): FibonacciHeapNode<E> {
903
+ createNode(element: E): FibonacciHeapNode<E> {
904
904
  return new FibonacciHeapNode<E>(element);
905
905
  }
906
906
 
@@ -781,7 +781,7 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
781
781
  * @returns A new SinglyLinkedListNode instance.
782
782
  */
783
783
 
784
- protected _createNode(value: E): SinglyLinkedListNode<E> {
784
+ protected createNode(value: E): SinglyLinkedListNode<E> {
785
785
  return new SinglyLinkedListNode<E>(value);
786
786
  }
787
787
 
@@ -807,7 +807,7 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
807
807
 
808
808
  protected _ensureNode(elementOrNode: E | SinglyLinkedListNode<E>) {
809
809
  if (this.isNode(elementOrNode)) return elementOrNode;
810
- return this._createNode(elementOrNode);
810
+ return this.createNode(elementOrNode);
811
811
  }
812
812
 
813
813
  /**
@@ -32,7 +32,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
32
32
  readonly isDuplicate: boolean;
33
33
 
34
34
  // ---- construction / mutation ----
35
- _createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
35
+ createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
36
36
 
37
37
  createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R>;
38
38
 
@@ -0,0 +1,159 @@
1
+ import {
2
+ AVLTree,
3
+ BinaryTree,
4
+ BST,
5
+ Deque,
6
+ DoublyLinkedList,
7
+ HashMap,
8
+ Heap,
9
+ MaxPriorityQueue,
10
+ MinHeap,
11
+ MinPriorityQueue,
12
+ Queue,
13
+ RedBlackTree,
14
+ SinglyLinkedList,
15
+ Stack,
16
+ TreeMultiMap,
17
+ Trie
18
+ } from 'data-structure-typed';
19
+
20
+ describe('', () => {
21
+ it('', () => {
22
+ const orgArr = [6, 1, 2, 7, 5, 3, 4, 9, 8];
23
+ const orgStrArr = ['trie', 'trial', 'trick', 'trip', 'tree', 'trend', 'triangle', 'track', 'trace', 'transmit'];
24
+ const entries = [
25
+ [6, '6'],
26
+ [1, '1'],
27
+ [2, '2'],
28
+ [7, '7'],
29
+ [5, '5'],
30
+ [3, '3'],
31
+ [4, '4'],
32
+ [9, '9'],
33
+ [8, '8']
34
+ ];
35
+ const queue = new Queue(orgArr);
36
+
37
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
38
+ const deque = new Deque(orgArr);
39
+
40
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
41
+ const sList = new SinglyLinkedList(orgArr);
42
+
43
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
44
+ const dList = new DoublyLinkedList(orgArr);
45
+
46
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
47
+ const stack = new Stack(orgArr);
48
+
49
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
50
+ const minHeap = new MinHeap(orgArr);
51
+
52
+ // [1, 5, 2, 7, 6, 3, 4, 9, 8]
53
+ const maxPQ = new MaxPriorityQueue(orgArr);
54
+
55
+ // [9, 8, 4, 7, 5, 2, 3, 1, 6]
56
+ const biTree = new BinaryTree(entries);
57
+
58
+ // ___6___
59
+ // / \
60
+ // ___1_ _2_
61
+ // / \ / \
62
+ // _7_ 5 3 4
63
+ // / \
64
+ // 9 8
65
+ const bst = new BST(entries);
66
+
67
+ // _____5___
68
+ // / \
69
+ // _2_ _7_
70
+ // / \ / \
71
+ // 1 3_ 6 8_
72
+ // \ \
73
+ // 4 9
74
+ const rbTree = new RedBlackTree(entries);
75
+
76
+ // ___4___
77
+ // / \
78
+ // _2_ _6___
79
+ // / \ / \
80
+ // 1 3 5 _8_
81
+ // / \
82
+ // 7 9
83
+ const avl = new AVLTree(entries);
84
+
85
+ // ___4___
86
+ // / \
87
+ // _2_ _6___
88
+ // / \ / \
89
+ // 1 3 5 _8_
90
+ // / \
91
+ // 7 9
92
+ const treeMulti = new TreeMultiMap(entries);
93
+
94
+ // ___4___
95
+ // / \
96
+ // _2_ _6___
97
+ // / \ / \
98
+ // 1 3 5 _8_
99
+ // / \
100
+ // 7 9
101
+ const hm = new HashMap(entries);
102
+
103
+ // [[6, "6"], [1, "1"], [2, "2"], [7, "7"], [5, "5"], [3, "3"], [4, "4"], [9, "9"], [8, "8"]]
104
+ const rbTreeH = new RedBlackTree(hm);
105
+
106
+ // ___4___
107
+ // / \
108
+ // _2_ _6___
109
+ // / \ / \
110
+ // 1 3 5 _8_
111
+ // / \
112
+ // 7 9
113
+ const pq = new MinPriorityQueue(orgArr);
114
+
115
+ // [1, 5, 2, 7, 6, 3, 4, 9, 8]
116
+ const bst1 = new BST(pq);
117
+
118
+ // _____5___
119
+ // / \
120
+ // _2_ _7_
121
+ // / \ / \
122
+ // 1 3_ 6 8_
123
+ // \ \
124
+ // 4 9
125
+ const dq1 = new Deque(orgArr);
126
+
127
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
128
+ const rbTree1 = new RedBlackTree(dq1);
129
+
130
+ // _____5___
131
+ // / \
132
+ // _2___ _7___
133
+ // / \ / \
134
+ // 1 _4 6 _9
135
+ // / /
136
+ // 3 8
137
+ const trie2 = new Trie(orgStrArr);
138
+
139
+ // ['trie', 'trial', 'triangle', 'trick', 'trip', 'tree', 'trend', 'track', 'trace', 'transmit']
140
+ const heap2 = new Heap(trie2, { comparator: (a, b) => Number(a) - Number(b) });
141
+
142
+ // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
143
+ const dq2 = new Deque(heap2);
144
+
145
+ // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
146
+ const entries2 = dq2.map((el, i) => [i, el]);
147
+ const avl2 = new AVLTree(entries2);
148
+ avl2.print();
149
+ // ___3_______
150
+ // / \
151
+ // _1_ ___7_
152
+ // / \ / \
153
+ // 0 2 _5_ 8_
154
+ // / \ \
155
+ // 4 6 9
156
+
157
+ expect(avl2.size).toBe(10);
158
+ });
159
+ });
@@ -0,0 +1,176 @@
1
+ import {
2
+ AVLTree,
3
+ BinaryTree,
4
+ BST,
5
+ Deque,
6
+ DoublyLinkedList,
7
+ HashMap,
8
+ Heap,
9
+ MaxPriorityQueue,
10
+ MinHeap,
11
+ MinPriorityQueue,
12
+ Queue,
13
+ RedBlackTree,
14
+ SinglyLinkedList,
15
+ Stack,
16
+ TreeMultiMap,
17
+ Trie
18
+ } from 'data-structure-typed';
19
+
20
+ describe('compile', () => {
21
+ it('compiles an empty array', () => {
22
+ const orgArr = [6, 1, 2, 7, 5, 3, 4, 9, 8];
23
+ const orgStrArr = ['trie', 'trial', 'trick', 'trip', 'tree', 'trend', 'triangle', 'track', 'trace', 'transmit'];
24
+ const entries: [number, string][] = [
25
+ [6, '6'],
26
+ [1, '1'],
27
+ [2, '2'],
28
+ [7, '7'],
29
+ [5, '5'],
30
+ [3, '3'],
31
+ [4, '4'],
32
+ [9, '9'],
33
+ [8, '8']
34
+ ];
35
+
36
+ const queue = new Queue(orgArr);
37
+ queue.print();
38
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
39
+
40
+ const deque = new Deque(orgArr);
41
+ deque.print();
42
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
43
+
44
+ const sList = new SinglyLinkedList(orgArr);
45
+ sList.print();
46
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
47
+
48
+ const dList = new DoublyLinkedList(orgArr);
49
+ dList.print();
50
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
51
+
52
+ const stack = new Stack(orgArr);
53
+ stack.print();
54
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
55
+
56
+ const minHeap = new MinHeap(orgArr);
57
+ minHeap.print();
58
+ // [1, 5, 2, 7, 6, 3, 4, 9, 8]
59
+
60
+ const maxPQ = new MaxPriorityQueue(orgArr);
61
+ maxPQ.print();
62
+ // [9, 8, 4, 7, 5, 2, 3, 1, 6]
63
+
64
+ const biTree = new BinaryTree(entries);
65
+ biTree.print();
66
+ // ___6___
67
+ // / \
68
+ // ___1_ _2_
69
+ // / \ / \
70
+ // _7_ 5 3 4
71
+ // / \
72
+ // 9 8
73
+
74
+ const bst = new BST(entries);
75
+ bst.print();
76
+ // _____5___
77
+ // / \
78
+ // _2_ _7_
79
+ // / \ / \
80
+ // 1 3_ 6 8_
81
+ // \ \
82
+ // 4 9
83
+
84
+ const rbTree = new RedBlackTree(entries);
85
+ rbTree.print();
86
+ // ___4___
87
+ // / \
88
+ // _2_ _6___
89
+ // / \ / \
90
+ // 1 3 5 _8_
91
+ // / \
92
+ // 7 9
93
+
94
+ const avl = new AVLTree(entries);
95
+ avl.print();
96
+ // ___4___
97
+ // / \
98
+ // _2_ _6___
99
+ // / \ / \
100
+ // 1 3 5 _8_
101
+ // / \
102
+ // 7 9
103
+
104
+ const treeMulti = new TreeMultiMap(entries);
105
+ treeMulti.print();
106
+ // ___4___
107
+ // / \
108
+ // _2_ _6___
109
+ // / \ / \
110
+ // 1 3 5 _8_
111
+ // / \
112
+ // 7 9
113
+
114
+ const hm = new HashMap(entries);
115
+ hm.print();
116
+ // [[6, "6"], [1, "1"], [2, "2"], [7, "7"], [5, "5"], [3, "3"], [4, "4"], [9, "9"], [8, "8"]]
117
+
118
+ const rbTreeH = new RedBlackTree(hm);
119
+ rbTreeH.print();
120
+ // ___4___
121
+ // / \
122
+ // _2_ _6___
123
+ // / \ / \
124
+ // 1 3 5 _8_
125
+ // / \
126
+ // 7 9
127
+
128
+ const pq = new MinPriorityQueue(orgArr);
129
+ pq.print();
130
+ // [1, 5, 2, 7, 6, 3, 4, 9, 8]
131
+
132
+ const bst1 = new BST(pq);
133
+ bst1.print();
134
+ // _____5___
135
+ // / \
136
+ // _2_ _7_
137
+ // / \ / \
138
+ // 1 3_ 6 8_
139
+ // \ \
140
+ // 4 9
141
+
142
+ const dq1 = new Deque(orgArr);
143
+ dq1.print();
144
+ // [6, 1, 2, 7, 5, 3, 4, 9, 8]
145
+ const rbTree1 = new RedBlackTree(dq1);
146
+ rbTree1.print();
147
+ // _____5___
148
+ // / \
149
+ // _2___ _7___
150
+ // / \ / \
151
+ // 1 _4 6 _9
152
+ // / /
153
+ // 3 8
154
+
155
+ const trie2 = new Trie(orgStrArr);
156
+ trie2.print();
157
+ // ['trie', 'trial', 'triangle', 'trick', 'trip', 'tree', 'trend', 'track', 'trace', 'transmit']
158
+ const heap2 = new Heap(trie2, { comparator: (a, b) => Number(a) - Number(b) });
159
+ heap2.print();
160
+ // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
161
+ const dq2 = new Deque(heap2);
162
+ dq2.print();
163
+ // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
164
+ const entries2 = dq2.map((el, i) => [i, el]);
165
+ const avl2 = new AVLTree(entries2);
166
+ avl2.print();
167
+ // ___3_______
168
+ // / \
169
+ // _1_ ___7_
170
+ // / \ / \
171
+ // 0 2 _5_ 8_
172
+ // / \ \
173
+ // 4 6 9
174
+ expect(avl2.size).toBe(10);
175
+ });
176
+ });
@@ -1,4 +1,4 @@
1
- const { MinHeap } = require('heap-typed');
1
+ const { MinHeap } = require('data-structure-typed');
2
2
 
3
3
  describe('JS Heap Operation Test', () => {
4
4
  it('should numeric heap work well', function () {
@@ -86,7 +86,7 @@
86
86
  tree.print(undefined, { isShowUndefined: true });
87
87
 
88
88
  const node3 = tree.getNode(3);
89
- if (node3) node3.right = tree._createNode(1);
89
+ if (node3) node3.right = tree.createNode(1);
90
90
  console.log(tree.isPerfectlyBalanced(), `tree.isPerfectlyBalanced()`);
91
91
  tree.print();
92
92