data-structure-typed 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/CONTRIBUTING.md +4 -0
  3. package/README.md +19 -7
  4. package/dist/cjs/index.cjs +1175 -585
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/{index.cjs → cjs-legacy/index.cjs} +1145 -613
  7. package/dist/cjs-legacy/index.cjs.map +1 -0
  8. package/dist/esm/index.mjs +1175 -585
  9. package/dist/esm/index.mjs.map +1 -1
  10. package/dist/{index.js → esm-legacy/index.mjs} +1147 -615
  11. package/dist/esm-legacy/index.mjs.map +1 -0
  12. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +58 -4
  13. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +66 -4
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +59 -5
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +58 -4
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +60 -6
  18. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +58 -4
  19. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +66 -4
  20. package/dist/types/data-structures/heap/heap.d.ts +4 -4
  21. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  22. package/dist/types/interfaces/binary-tree.d.ts +1 -1
  23. package/dist/umd/data-structure-typed.js +711 -228
  24. package/dist/umd/data-structure-typed.js.map +1 -1
  25. package/dist/umd/data-structure-typed.min.js +3 -3
  26. package/dist/umd/data-structure-typed.min.js.map +1 -1
  27. package/jest.integration.config.js +7 -3
  28. package/package.json +29 -7
  29. package/src/data-structures/binary-tree/avl-tree-counter.ts +106 -15
  30. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
  31. package/src/data-structures/binary-tree/avl-tree.ts +107 -16
  32. package/src/data-structures/binary-tree/binary-tree.ts +4 -4
  33. package/src/data-structures/binary-tree/bst.ts +103 -12
  34. package/src/data-structures/binary-tree/red-black-tree.ts +110 -20
  35. package/src/data-structures/binary-tree/tree-counter.ts +105 -14
  36. package/src/data-structures/binary-tree/tree-multi-map.ts +123 -12
  37. package/src/data-structures/graph/abstract-graph.ts +5 -5
  38. package/src/data-structures/graph/directed-graph.ts +5 -5
  39. package/src/data-structures/graph/undirected-graph.ts +5 -5
  40. package/src/data-structures/heap/heap.ts +5 -5
  41. package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
  42. package/src/interfaces/binary-tree.ts +1 -1
  43. package/test/integration/compile.test.mjs +159 -0
  44. package/test/integration/compile.test.ts +176 -0
  45. package/test/integration/heap.test.js +1 -1
  46. package/test/integration/index.html +1 -1
  47. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +5 -4
  48. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +3 -3
  49. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
  50. package/test/unit/data-structures/binary-tree/bst.test.ts +2 -2
  51. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +3 -3
  52. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +5 -4
  53. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +4 -4
  54. package/{tsconfig-base.json → tsconfig.base.json} +0 -1
  55. package/tsconfig.test.json +1 -0
  56. package/{tsconfig-types.json → tsconfig.types.json} +1 -3
  57. package/tsup.config.js +2 -3
  58. package/tsup.node.config.js +71 -0
  59. package/dist/index.cjs.map +0 -1
  60. package/dist/index.js.map +0 -1
  61. package/test/integration/compile.js +0 -144
  62. package/test/integration/compile.mjs +0 -135
  63. package/test/integration/compile.ts +0 -171
  64. package/tsup.node.config.ts +0 -37
@@ -11,6 +11,7 @@ import type {
11
11
  BinaryTreeOptions,
12
12
  BSTNOptKeyOrNode,
13
13
  EntryCallback,
14
+ FamilyPosition,
14
15
  IterationType,
15
16
  OptNode,
16
17
  RBTNColor,
@@ -19,7 +20,7 @@ import type {
19
20
  import { BSTOptions } from '../../types';
20
21
  import { BSTNode } from './bst';
21
22
  import { IBinaryTree } from '../../interfaces';
22
- import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
23
+ import { RedBlackTree } from './red-black-tree';
23
24
 
24
25
  /**
25
26
  * RB-tree node with an extra 'count' field; keeps parent/child links.
@@ -27,8 +28,10 @@ import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
27
28
  * @template K
28
29
  * @template V
29
30
  */
30
- export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
31
- override parent?: TreeCounterNode<K, V> = undefined;
31
+ export class TreeCounterNode<K = any, V = any> {
32
+ key: K;
33
+ value?: V;
34
+ parent?: TreeCounterNode<K, V> = undefined;
32
35
 
33
36
  /**
34
37
  * Create a tree counter node.
@@ -40,18 +43,20 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
40
43
  * @returns New TreeCounterNode instance.
41
44
  */
42
45
  constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') {
43
- super(key, value, color);
46
+ this.key = key;
47
+ this.value = value;
48
+ this.color = color;
44
49
  this.count = count;
45
50
  }
46
51
 
47
- override _left?: TreeCounterNode<K, V> | null | undefined = undefined;
52
+ _left?: TreeCounterNode<K, V> | null | undefined = undefined;
48
53
 
49
54
  /**
50
55
  * Get the left child pointer.
51
56
  * @remarks Time O(1), Space O(1)
52
57
  * @returns Left child node, or null/undefined.
53
58
  */
54
- override get left(): TreeCounterNode<K, V> | null | undefined {
59
+ get left(): TreeCounterNode<K, V> | null | undefined {
55
60
  return this._left;
56
61
  }
57
62
 
@@ -61,21 +66,21 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
61
66
  * @param v - New left child node, or null/undefined.
62
67
  * @returns void
63
68
  */
64
- override set left(v: TreeCounterNode<K, V> | null | undefined) {
69
+ set left(v: TreeCounterNode<K, V> | null | undefined) {
65
70
  if (v) {
66
71
  v.parent = this;
67
72
  }
68
73
  this._left = v;
69
74
  }
70
75
 
71
- override _right?: TreeCounterNode<K, V> | null | undefined = undefined;
76
+ _right?: TreeCounterNode<K, V> | null | undefined = undefined;
72
77
 
73
78
  /**
74
79
  * Get the right child pointer.
75
80
  * @remarks Time O(1), Space O(1)
76
81
  * @returns Right child node, or null/undefined.
77
82
  */
78
- override get right(): TreeCounterNode<K, V> | null | undefined {
83
+ get right(): TreeCounterNode<K, V> | null | undefined {
79
84
  return this._right;
80
85
  }
81
86
 
@@ -85,12 +90,98 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
85
90
  * @param v - New right child node, or null/undefined.
86
91
  * @returns void
87
92
  */
88
- override set right(v: TreeCounterNode<K, V> | null | undefined) {
93
+ set right(v: TreeCounterNode<K, V> | null | undefined) {
89
94
  if (v) {
90
95
  v.parent = this;
91
96
  }
92
97
  this._right = v;
93
98
  }
99
+
100
+ _height: number = 0;
101
+
102
+ /**
103
+ * Gets the height of the node (used in self-balancing trees).
104
+ * @remarks Time O(1), Space O(1)
105
+ *
106
+ * @returns The height.
107
+ */
108
+ get height(): number {
109
+ return this._height;
110
+ }
111
+
112
+ /**
113
+ * Sets the height of the node.
114
+ * @remarks Time O(1), Space O(1)
115
+ *
116
+ * @param value - The new height.
117
+ */
118
+ set height(value: number) {
119
+ this._height = value;
120
+ }
121
+
122
+ _color: RBTNColor = 'BLACK';
123
+
124
+ /**
125
+ * Gets the color of the node (used in Red-Black trees).
126
+ * @remarks Time O(1), Space O(1)
127
+ *
128
+ * @returns The node's color.
129
+ */
130
+ get color(): RBTNColor {
131
+ return this._color;
132
+ }
133
+
134
+ /**
135
+ * Sets the color of the node.
136
+ * @remarks Time O(1), Space O(1)
137
+ *
138
+ * @param value - The new color.
139
+ */
140
+ set color(value: RBTNColor) {
141
+ this._color = value;
142
+ }
143
+
144
+ _count: number = 1;
145
+
146
+ /**
147
+ * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
148
+ * @remarks Time O(1), Space O(1)
149
+ *
150
+ * @returns The subtree node count.
151
+ */
152
+ get count(): number {
153
+ return this._count;
154
+ }
155
+
156
+ /**
157
+ * Sets the count of nodes in the subtree.
158
+ * @remarks Time O(1), Space O(1)
159
+ *
160
+ * @param value - The new count.
161
+ */
162
+ set count(value: number) {
163
+ this._count = value;
164
+ }
165
+
166
+ /**
167
+ * Gets the position of the node relative to its parent.
168
+ * @remarks Time O(1), Space O(1)
169
+ *
170
+ * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
171
+ */
172
+ get familyPosition(): FamilyPosition {
173
+ if (!this.parent) {
174
+ return this.left || this.right ? 'ROOT' : 'ISOLATED';
175
+ }
176
+
177
+ if (this.parent.left === this) {
178
+ return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
179
+ } else if (this.parent.right === this) {
180
+ return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
181
+ }
182
+
183
+ return 'MAL_NODE';
184
+ }
94
185
  }
95
186
 
96
187
  /**
@@ -142,7 +233,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
142
233
  return sum;
143
234
  }
144
235
 
145
- override _createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): TreeCounterNode<K, V> {
236
+ override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): TreeCounterNode<K, V> {
146
237
  return new TreeCounterNode(key, this._isMapMode ? undefined : value, count, color) as TreeCounterNode<K, V>;
147
238
  }
148
239
 
@@ -427,10 +518,10 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
427
518
  const [key, entryValue] = keyNodeOrEntry;
428
519
  if (key === undefined || key === null) return [undefined, undefined];
429
520
  const finalValue = value ?? entryValue;
430
- return [this._createNode(key, finalValue, 'BLACK', count), finalValue];
521
+ return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
431
522
  }
432
523
 
433
- return [this._createNode(keyNodeOrEntry, value, 'BLACK', count), value];
524
+ return [this.createNode(keyNodeOrEntry, value, 'BLACK', count), value];
434
525
  }
435
526
 
436
527
  /**
@@ -448,7 +539,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
448
539
  destNode = this.ensureNode(destNode);
449
540
  if (srcNode && destNode) {
450
541
  const { key, value, count, color } = destNode;
451
- const tempNode = this._createNode(key, value, color, count);
542
+ const tempNode = this.createNode(key, value, color, count);
452
543
  if (tempNode) {
453
544
  tempNode.color = color;
454
545
 
@@ -6,7 +6,15 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import type { BTNOptKeyOrNull, ElemOf, EntryCallback, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
9
+ import type {
10
+ BTNOptKeyOrNull,
11
+ ElemOf,
12
+ EntryCallback,
13
+ FamilyPosition,
14
+ RBTNColor,
15
+ RedBlackTreeOptions,
16
+ TreeMultiMapOptions
17
+ } from '../../types';
10
18
  import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
11
19
  import { IBinaryTree } from '../../interfaces';
12
20
 
@@ -16,8 +24,10 @@ import { IBinaryTree } from '../../interfaces';
16
24
  * @template K
17
25
  * @template V
18
26
  */
19
- export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
20
- override parent?: TreeMultiMapNode<K, V> = undefined;
27
+ export class TreeMultiMapNode<K = any, V = any> {
28
+ key: K;
29
+ value?: V[];
30
+ parent?: TreeMultiMapNode<K, V> = undefined;
21
31
 
22
32
  /**
23
33
  * Create a TreeMultiMap node with an optional value bucket.
@@ -26,18 +36,20 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
26
36
  * @param [value] - Initial array of values.
27
37
  * @returns New TreeMultiMapNode instance.
28
38
  */
29
- constructor(key: K, value?: V[]) {
30
- super(key, value);
39
+ constructor(key: K, value: V[] = [], color: RBTNColor = 'BLACK') {
40
+ this.key = key;
41
+ this.value = value;
42
+ this.color = color;
31
43
  }
32
44
 
33
- override _left?: TreeMultiMapNode<K, V> | null | undefined = undefined;
45
+ _left?: TreeMultiMapNode<K, V> | null | undefined = undefined;
34
46
 
35
47
  /**
36
48
  * Get the left child pointer.
37
49
  * @remarks Time O(1), Space O(1)
38
50
  * @returns Left child node, or null/undefined.
39
51
  */
40
- override get left(): TreeMultiMapNode<K, V> | null | undefined {
52
+ get left(): TreeMultiMapNode<K, V> | null | undefined {
41
53
  return this._left;
42
54
  }
43
55
 
@@ -47,21 +59,21 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
47
59
  * @param v - New left child node, or null/undefined.
48
60
  * @returns void
49
61
  */
50
- override set left(v: TreeMultiMapNode<K, V> | null | undefined) {
62
+ set left(v: TreeMultiMapNode<K, V> | null | undefined) {
51
63
  if (v) {
52
64
  v.parent = this;
53
65
  }
54
66
  this._left = v;
55
67
  }
56
68
 
57
- override _right?: TreeMultiMapNode<K, V> | null | undefined = undefined;
69
+ _right?: TreeMultiMapNode<K, V> | null | undefined = undefined;
58
70
 
59
71
  /**
60
72
  * Get the right child pointer.
61
73
  * @remarks Time O(1), Space O(1)
62
74
  * @returns Right child node, or null/undefined.
63
75
  */
64
- override get right(): TreeMultiMapNode<K, V> | null | undefined {
76
+ get right(): TreeMultiMapNode<K, V> | null | undefined {
65
77
  return this._right;
66
78
  }
67
79
 
@@ -71,12 +83,98 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
71
83
  * @param v - New right child node, or null/undefined.
72
84
  * @returns void
73
85
  */
74
- override set right(v: TreeMultiMapNode<K, V> | null | undefined) {
86
+ set right(v: TreeMultiMapNode<K, V> | null | undefined) {
75
87
  if (v) {
76
88
  v.parent = this;
77
89
  }
78
90
  this._right = v;
79
91
  }
92
+
93
+ _height: number = 0;
94
+
95
+ /**
96
+ * Gets the height of the node (used in self-balancing trees).
97
+ * @remarks Time O(1), Space O(1)
98
+ *
99
+ * @returns The height.
100
+ */
101
+ get height(): number {
102
+ return this._height;
103
+ }
104
+
105
+ /**
106
+ * Sets the height of the node.
107
+ * @remarks Time O(1), Space O(1)
108
+ *
109
+ * @param value - The new height.
110
+ */
111
+ set height(value: number) {
112
+ this._height = value;
113
+ }
114
+
115
+ _color: RBTNColor = 'BLACK';
116
+
117
+ /**
118
+ * Gets the color of the node (used in Red-Black trees).
119
+ * @remarks Time O(1), Space O(1)
120
+ *
121
+ * @returns The node's color.
122
+ */
123
+ get color(): RBTNColor {
124
+ return this._color;
125
+ }
126
+
127
+ /**
128
+ * Sets the color of the node.
129
+ * @remarks Time O(1), Space O(1)
130
+ *
131
+ * @param value - The new color.
132
+ */
133
+ set color(value: RBTNColor) {
134
+ this._color = value;
135
+ }
136
+
137
+ _count: number = 1;
138
+
139
+ /**
140
+ * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
141
+ * @remarks Time O(1), Space O(1)
142
+ *
143
+ * @returns The subtree node count.
144
+ */
145
+ get count(): number {
146
+ return this._count;
147
+ }
148
+
149
+ /**
150
+ * Sets the count of nodes in the subtree.
151
+ * @remarks Time O(1), Space O(1)
152
+ *
153
+ * @param value - The new count.
154
+ */
155
+ set count(value: number) {
156
+ this._count = value;
157
+ }
158
+
159
+ /**
160
+ * Gets the position of the node relative to its parent.
161
+ * @remarks Time O(1), Space O(1)
162
+ *
163
+ * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
164
+ */
165
+ get familyPosition(): FamilyPosition {
166
+ if (!this.parent) {
167
+ return this.left || this.right ? 'ROOT' : 'ISOLATED';
168
+ }
169
+
170
+ if (this.parent.left === this) {
171
+ return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
172
+ } else if (this.parent.right === this) {
173
+ return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
174
+ }
175
+
176
+ return 'MAL_NODE';
177
+ }
80
178
  }
81
179
 
82
180
  /**
@@ -270,10 +368,23 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
270
368
  }
271
369
  }
272
370
 
273
- override _createNode(key: K, value: V[] = []): TreeMultiMapNode<K, V> {
371
+ override createNode(key: K, value: V[] = []): TreeMultiMapNode<K, V> {
274
372
  return new TreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
275
373
  }
276
374
 
375
+ /**
376
+ * Checks if the given item is a `TreeMultiMapNode` instance.
377
+ * @remarks Time O(1), Space O(1)
378
+ *
379
+ * @param keyNodeOrEntry - The item to check.
380
+ * @returns True if it's a TreeMultiMapNode, false otherwise.
381
+ */
382
+ override isNode(
383
+ keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
384
+ ): keyNodeOrEntry is TreeMultiMapNode<K, V> {
385
+ return keyNodeOrEntry instanceof TreeMultiMapNode;
386
+ }
387
+
277
388
  override add(
278
389
  keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
279
390
  ): boolean;
@@ -50,11 +50,11 @@ export abstract class AbstractEdge<E = any> {
50
50
  * @example examples will be generated by unit test
51
51
  */
52
52
  export abstract class AbstractGraph<
53
- V = any,
54
- E = any,
55
- VO extends AbstractVertex<V> = AbstractVertex<V>,
56
- EO extends AbstractEdge<E> = AbstractEdge<E>
57
- >
53
+ V = any,
54
+ E = any,
55
+ VO extends AbstractVertex<V> = AbstractVertex<V>,
56
+ EO extends AbstractEdge<E> = AbstractEdge<E>
57
+ >
58
58
  extends IterableEntryBase<VertexKey, V | undefined>
59
59
  implements IGraph<V, E, VO, EO>
60
60
  {
@@ -38,11 +38,11 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
38
38
  * @example examples will be generated by unit test
39
39
  */
40
40
  export class DirectedGraph<
41
- V = any,
42
- E = any,
43
- VO extends DirectedVertex<V> = DirectedVertex<V>,
44
- EO extends DirectedEdge<E> = DirectedEdge<E>
45
- >
41
+ V = any,
42
+ E = any,
43
+ VO extends DirectedVertex<V> = DirectedVertex<V>,
44
+ EO extends DirectedEdge<E> = DirectedEdge<E>
45
+ >
46
46
  extends AbstractGraph<V, E, VO, EO>
47
47
  implements IGraph<V, E, VO, EO>
48
48
  {
@@ -36,11 +36,11 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
36
36
  * @example examples will be generated by unit test
37
37
  */
38
38
  export class UndirectedGraph<
39
- V = any,
40
- E = any,
41
- VO extends UndirectedVertex<V> = UndirectedVertex<V>,
42
- EO extends UndirectedEdge<E> = UndirectedEdge<E>
43
- >
39
+ V = any,
40
+ E = any,
41
+ VO extends UndirectedVertex<V> = UndirectedVertex<V>,
42
+ EO extends UndirectedEdge<E> = UndirectedEdge<E>
43
+ >
44
44
  extends AbstractGraph<V, E, VO, EO>
45
45
  implements IGraph<V, E, VO, EO>
46
46
  {
@@ -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
+ });