data-structure-typed 1.38.6 → 1.38.7

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 (40) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.js +1 -1
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +28 -28
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +22 -22
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
  9. package/dist/cjs/data-structures/binary-tree/bst.js +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
  14. package/dist/cjs/data-structures/binary-tree/tree-multiset.js +2 -2
  15. package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
  16. package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
  17. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
  18. package/dist/mjs/data-structures/binary-tree/avl-tree.js +1 -1
  19. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +28 -28
  20. package/dist/mjs/data-structures/binary-tree/binary-tree.js +22 -22
  21. package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
  22. package/dist/mjs/data-structures/binary-tree/bst.js +1 -1
  23. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
  24. package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
  25. package/dist/mjs/data-structures/binary-tree/tree-multiset.js +2 -2
  26. package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
  27. package/dist/umd/index.global.js +1 -1
  28. package/dist/umd/index.global.js.map +1 -1
  29. package/package.json +5 -5
  30. package/src/data-structures/binary-tree/avl-tree.ts +10 -10
  31. package/src/data-structures/binary-tree/binary-tree.ts +42 -42
  32. package/src/data-structures/binary-tree/bst.ts +9 -9
  33. package/src/data-structures/binary-tree/rb-tree.ts +4 -7
  34. package/src/data-structures/binary-tree/tree-multiset.ts +10 -13
  35. package/src/interfaces/binary-tree.ts +2 -2
  36. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  37. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
  38. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  39. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  40. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.38.6",
3
+ "version": "1.38.7",
4
4
  "description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -61,17 +61,17 @@
61
61
  "@typescript-eslint/eslint-plugin": "^6.7.4",
62
62
  "@typescript-eslint/parser": "^6.7.4",
63
63
  "auto-changelog": "^2.4.0",
64
- "avl-tree-typed": "^1.38.5",
64
+ "avl-tree-typed": "^1.38.6",
65
65
  "benchmark": "^2.1.4",
66
- "binary-tree-typed": "^1.38.5",
67
- "bst-typed": "^1.38.5",
66
+ "binary-tree-typed": "^1.38.6",
67
+ "bst-typed": "^1.38.6",
68
68
  "dependency-cruiser": "^14.1.0",
69
69
  "eslint": "^8.50.0",
70
70
  "eslint-config-prettier": "^9.0.0",
71
71
  "eslint-import-resolver-alias": "^1.1.2",
72
72
  "eslint-import-resolver-typescript": "^3.6.1",
73
73
  "eslint-plugin-import": "^2.28.1",
74
- "heap-typed": "^1.38.5",
74
+ "heap-typed": "^1.38.6",
75
75
  "istanbul-badges-readme": "^1.8.5",
76
76
  "jest": "^29.7.0",
77
77
  "prettier": "^3.0.3",
@@ -7,13 +7,10 @@
7
7
  */
8
8
  import {BST, BSTNode} from './bst';
9
9
  import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
10
- import {IBinaryTree} from '../../interfaces';
11
10
  import {MapCallback} from '../../types';
11
+ import {IBinaryTree} from '../../interfaces';
12
12
 
13
- export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>> extends BSTNode<
14
- V,
15
- FAMILY
16
- > {
13
+ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
17
14
  height: number;
18
15
 
19
16
  constructor(key: BinaryTreeNodeKey, val?: V) {
@@ -22,7 +19,10 @@ export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTre
22
19
  }
23
20
  }
24
21
 
25
- export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IBinaryTree<N> {
22
+ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode>
23
+ extends BST<V, N>
24
+ implements IBinaryTree<V, N>
25
+ {
26
26
  /**
27
27
  * This is a constructor function for an AVL tree data structure in TypeScript.
28
28
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@@ -38,12 +38,12 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
38
38
  * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
39
39
  * the new node. It is used to determine the position of the node in the binary search tree.
40
40
  * @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
41
- * type `N['val']`, which means it can be any value that is assignable to the `val` property of the
41
+ * type `V`, which means it can be any value that is assignable to the `val` property of the
42
42
  * node type `N`.
43
43
  * @returns a new AVLTreeNode object with the specified key and value.
44
44
  */
45
- override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
46
- return new AVLTreeNode<N['val'], N>(key, val) as N;
45
+ override createNode(key: BinaryTreeNodeKey, val?: V): N {
46
+ return new AVLTreeNode<V, N>(key, val) as N;
47
47
  }
48
48
 
49
49
  /**
@@ -55,7 +55,7 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
55
55
  * are adding to the binary search tree.
56
56
  * @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
57
57
  */
58
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
58
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
59
59
  // TODO support node as a param
60
60
  const inserted = super.add(keyOrNode, val);
61
61
  if (inserted) this._balancePath(inserted);
@@ -23,9 +23,9 @@ import {Queue} from '../queue';
23
23
  /**
24
24
  * Represents a node in a binary tree.
25
25
  * @template V - The type of data stored in the node.
26
- * @template FAMILY - The type of the family relationship in the binary tree.
26
+ * @template N - The type of the family relationship in the binary tree.
27
27
  */
28
- export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
28
+ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> {
29
29
  /**
30
30
  * The key associated with the node.
31
31
  */
@@ -39,7 +39,7 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
39
39
  /**
40
40
  * The parent node of the current node.
41
41
  */
42
- parent: FAMILY | null | undefined;
42
+ parent: N | null | undefined;
43
43
 
44
44
  /**
45
45
  * Creates a new instance of BinaryTreeNode.
@@ -51,42 +51,42 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
51
51
  this.val = val;
52
52
  }
53
53
 
54
- private _left: FAMILY | null | undefined;
54
+ private _left: N | null | undefined;
55
55
 
56
56
  /**
57
57
  * Get the left child node.
58
58
  */
59
- get left(): FAMILY | null | undefined {
59
+ get left(): N | null | undefined {
60
60
  return this._left;
61
61
  }
62
62
 
63
63
  /**
64
64
  * Set the left child node.
65
- * @param {FAMILY | null | undefined} v - The left child node.
65
+ * @param {N | null | undefined} v - The left child node.
66
66
  */
67
- set left(v: FAMILY | null | undefined) {
67
+ set left(v: N | null | undefined) {
68
68
  if (v) {
69
- v.parent = this as unknown as FAMILY;
69
+ v.parent = this as unknown as N;
70
70
  }
71
71
  this._left = v;
72
72
  }
73
73
 
74
- private _right: FAMILY | null | undefined;
74
+ private _right: N | null | undefined;
75
75
 
76
76
  /**
77
77
  * Get the right child node.
78
78
  */
79
- get right(): FAMILY | null | undefined {
79
+ get right(): N | null | undefined {
80
80
  return this._right;
81
81
  }
82
82
 
83
83
  /**
84
84
  * Set the right child node.
85
- * @param {FAMILY | null | undefined} v - The right child node.
85
+ * @param {N | null | undefined} v - The right child node.
86
86
  */
87
- set right(v: FAMILY | null | undefined) {
87
+ set right(v: N | null | undefined) {
88
88
  if (v) {
89
- v.parent = this as unknown as FAMILY;
89
+ v.parent = this as unknown as N;
90
90
  }
91
91
  this._right = v;
92
92
  }
@@ -96,7 +96,7 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
96
96
  * @returns {FamilyPosition} - The family position of the node.
97
97
  */
98
98
  get familyPosition(): FamilyPosition {
99
- const that = this as unknown as FAMILY;
99
+ const that = this as unknown as N;
100
100
  if (!this.parent) {
101
101
  return this.left || this.right ? FamilyPosition.ROOT : FamilyPosition.ISOLATED;
102
102
  }
@@ -115,9 +115,7 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
115
115
  * Represents a binary tree data structure.
116
116
  * @template N - The type of the binary tree's nodes.
117
117
  */
118
- export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
119
- private _loopType: IterationType = IterationType.ITERATIVE;
120
-
118
+ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode> implements IBinaryTree<V, N> {
121
119
  /**
122
120
  * Creates a new instance of BinaryTree.
123
121
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
@@ -125,10 +123,27 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
125
123
  constructor(options?: BinaryTreeOptions) {
126
124
  if (options !== undefined) {
127
125
  const {iterationType = IterationType.ITERATIVE} = options;
128
- this._loopType = iterationType;
126
+ this._iterationType = iterationType;
129
127
  }
130
128
  }
131
129
 
130
+ private _iterationType: IterationType = IterationType.ITERATIVE;
131
+
132
+ /**
133
+ * Get the iteration type used in the binary tree.
134
+ */
135
+ get iterationType(): IterationType {
136
+ return this._iterationType;
137
+ }
138
+
139
+ /**
140
+ * Set the iteration type for the binary tree.
141
+ * @param {IterationType} v - The new iteration type to set.
142
+ */
143
+ set iterationType(v: IterationType) {
144
+ this._iterationType = v;
145
+ }
146
+
132
147
  private _root: N | null = null;
133
148
 
134
149
  /**
@@ -147,29 +162,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
147
162
  return this._size;
148
163
  }
149
164
 
150
- /**
151
- * Get the iteration type used in the binary tree.
152
- */
153
- get iterationType(): IterationType {
154
- return this._loopType;
155
- }
156
-
157
- /**
158
- * Set the iteration type for the binary tree.
159
- * @param {IterationType} v - The new iteration type to set.
160
- */
161
- set iterationType(v: IterationType) {
162
- this._loopType = v;
163
- }
164
-
165
165
  /**
166
166
  * Creates a new instance of BinaryTreeNode with the given key and value.
167
167
  * @param {BinaryTreeNodeKey} key - The key for the new node.
168
- * @param {N['val']} val - The value for the new node.
168
+ * @param {V} val - The value for the new node.
169
169
  * @returns {N} - The newly created BinaryTreeNode.
170
170
  */
171
- createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
172
- return new BinaryTreeNode<N['val'], N>(key, val) as N;
171
+ createNode(key: BinaryTreeNodeKey, val?: V): N {
172
+ return new BinaryTreeNode<V, N>(key, val) as N;
173
173
  }
174
174
 
175
175
  /**
@@ -191,10 +191,10 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
191
191
  /**
192
192
  * Add a node with the given key and value to the binary tree.
193
193
  * @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
194
- * @param {N['val']} val - The value for the new node (optional).
194
+ * @param {V} val - The value for the new node (optional).
195
195
  * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
196
196
  */
197
- add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
197
+ add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
198
198
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
199
199
  const queue = new Queue<N | null>([root]);
200
200
  while (queue.size > 0) {
@@ -249,12 +249,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
249
249
  * values, and adds them to the binary tree.
250
250
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
251
251
  * objects, or null values.
252
- * @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to
252
+ * @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
253
253
  * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
254
254
  * the value of the nodes will be `undefined`.
255
255
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
256
256
  */
257
- addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: N['val'][]): (N | null | undefined)[] {
257
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[] {
258
258
  // TODO not sure addMany not be run multi times
259
259
  return keysOrNodes.map((keyOrNode, i) => {
260
260
  if (keyOrNode instanceof BinaryTreeNode) {
@@ -274,12 +274,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
274
274
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
275
275
  * @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
276
276
  * `BinaryTreeNodeKey` or `N` values.
277
- * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
277
+ * @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
278
278
  * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
279
279
  * array. Each value in the `data` array will be assigned to the
280
280
  * @returns The method is returning a boolean value.
281
281
  */
282
- refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean {
282
+ refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean {
283
283
  this.clear();
284
284
  return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
285
285
  }
@@ -18,13 +18,13 @@ import {BinaryTree, BinaryTreeNode} from './binary-tree';
18
18
  import {IBinaryTree} from '../../interfaces';
19
19
  import {Queue} from '../queue';
20
20
 
21
- export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> {
21
+ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
22
22
  constructor(key: BinaryTreeNodeKey, val?: V) {
23
23
  super(key, val);
24
24
  }
25
25
  }
26
26
 
27
- export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> {
27
+ export class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
28
28
  /**
29
29
  * The constructor function initializes a binary search tree object with an optional comparator
30
30
  * function.
@@ -49,8 +49,8 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
49
49
  * represents the value associated with the node in a binary search tree.
50
50
  * @returns a new instance of the BSTNode class with the specified key and value.
51
51
  */
52
- override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
53
- return new BSTNode<N['val'], N>(key, val) as N;
52
+ override createNode(key: BinaryTreeNodeKey, val?: V): N {
53
+ return new BSTNode<V, N>(key, val) as N;
54
54
  }
55
55
 
56
56
  /**
@@ -63,7 +63,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
63
63
  * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
64
64
  * was not added or if the parameters were invalid, it returns null or undefined.
65
65
  */
66
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
66
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
67
67
  // TODO support node as a parameter
68
68
  let inserted: N | null = null;
69
69
  let newNode: N | null = null;
@@ -136,7 +136,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
136
136
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
137
137
  * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
138
138
  * `null
139
- * @param {N['val'][]} data - The values of tree nodes
139
+ * @param {V[]} data - The values of tree nodes
140
140
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
141
141
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
142
142
  * It can have two possible values:
@@ -145,7 +145,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
145
145
 
146
146
  override addMany(
147
147
  keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
148
- data?: N['val'][],
148
+ data?: V[],
149
149
  isBalanceAdd = true,
150
150
  iterationType = this.iterationType
151
151
  ): (N | null | undefined)[] {
@@ -174,7 +174,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
174
174
  }
175
175
 
176
176
  let sortedKeysOrNodes: (number | N | null)[] = [],
177
- sortedData: (N['val'] | undefined)[] | undefined = [];
177
+ sortedData: (V | undefined)[] | undefined = [];
178
178
 
179
179
  if (isNodeOrNullTuple(combinedArr)) {
180
180
  sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
@@ -185,7 +185,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
185
185
  }
186
186
  sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
187
187
  sortedData = sorted.map(([, val]) => val);
188
- const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: N['val'][]) => {
188
+ const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: (V | undefined)[]) => {
189
189
  if (arr.length === 0) return;
190
190
 
191
191
  const mid = Math.floor((arr.length - 1) / 2);
@@ -2,10 +2,7 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../..
2
2
  import {IBinaryTree} from '../../interfaces';
3
3
  import {BST, BSTNode} from './bst';
4
4
 
5
- export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<
6
- V,
7
- FAMILY
8
- > {
5
+ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
9
6
  constructor(key: BinaryTreeNodeKey, val?: V) {
10
7
  super(key, val);
11
8
  this._color = RBColor.RED;
@@ -22,16 +19,16 @@ export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNo
22
19
  }
23
20
  }
24
21
 
25
- export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IBinaryTree<N> {
22
+ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
26
23
  constructor(options?: RBTreeOptions) {
27
24
  super(options);
28
25
  }
29
26
 
30
- override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
27
+ override createNode(key: BinaryTreeNodeKey, val?: V): N {
31
28
  return new RBTreeNode(key, val) as N;
32
29
  }
33
30
 
34
- // override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
31
+ // override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
35
32
  // const inserted = super.add(keyOrNode, val);
36
33
  // if (inserted) this._fixInsertViolation(inserted);
37
34
  // return inserted;
@@ -12,8 +12,8 @@ import {AVLTree, AVLTreeNode} from './avl-tree';
12
12
 
13
13
  export class TreeMultisetNode<
14
14
  V = any,
15
- FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>
16
- > extends AVLTreeNode<V, FAMILY> {
15
+ N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>
16
+ > extends AVLTreeNode<V, N> {
17
17
  count: number;
18
18
 
19
19
  /**
@@ -35,9 +35,9 @@ export class TreeMultisetNode<
35
35
  /**
36
36
  * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
37
37
  */
38
- export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
39
- extends AVLTree<N>
40
- implements IBinaryTree<N>
38
+ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode>
39
+ extends AVLTree<V, N>
40
+ implements IBinaryTree<V, N>
41
41
  {
42
42
  /**
43
43
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
@@ -64,7 +64,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
64
64
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
65
65
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
66
66
  */
67
- override createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N {
67
+ override createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N {
68
68
  return new TreeMultisetNode(key, val, count) as N;
69
69
  }
70
70
 
@@ -81,7 +81,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
81
81
  * count is specified, the default count will be 1.
82
82
  * @returns The function `add` returns a value of type `N | null | undefined`.
83
83
  */
84
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count = 1): N | null | undefined {
84
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count = 1): N | null | undefined {
85
85
  let inserted: N | null | undefined = undefined,
86
86
  newNode: N | null;
87
87
  if (keyOrNode instanceof TreeMultisetNode) {
@@ -187,15 +187,12 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
187
187
  * inserted nodes.
188
188
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
189
189
  * added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
190
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
190
+ * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
191
191
  * to the keys or nodes being added to the multiset. It is used to associate additional data with
192
192
  * each key or node.
193
193
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
194
194
  */
195
- override addMany(
196
- keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
197
- data?: N['val'][]
198
- ): (N | null | undefined)[] {
195
+ override addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] {
199
196
  const inserted: (N | null | undefined)[] = [];
200
197
 
201
198
  for (let i = 0; i < keysOrNodes.length; i++) {
@@ -207,7 +204,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
207
204
  }
208
205
 
209
206
  if (keyOrNode === null) {
210
- inserted.push(this.add(NaN, null, 0));
207
+ inserted.push(this.add(NaN, undefined, 0));
211
208
  continue;
212
209
  }
213
210
 
@@ -1,7 +1,7 @@
1
1
  import {BinaryTreeNode} from '../data-structures';
2
- import {BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback} from '../types';
2
+ import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback} from '../types';
3
3
 
4
- export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
4
+ export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
5
5
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
6
6
 
7
7
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
@@ -3,7 +3,7 @@ import {AVLTree, AVLTreeNode, CP} from '../../../../src';
3
3
  describe('AVL Tree Test', () => {
4
4
  it('should perform various operations on a AVL Tree', () => {
5
5
  const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
6
- const tree = new AVLTree<AVLTreeNode<number>>();
6
+ const tree = new AVLTree<number>();
7
7
 
8
8
  for (const i of arr) tree.add(i, i);
9
9
 
@@ -110,7 +110,7 @@ describe('AVL Tree Test', () => {
110
110
  });
111
111
 
112
112
  describe('AVLTree APIs test', () => {
113
- const avl = new AVLTree<AVLTreeNode<{id: number; text: string}>>();
113
+ const avl = new AVLTree<{id: number; text: string}>();
114
114
  beforeEach(() => {
115
115
  avl.clear();
116
116
  });
@@ -196,7 +196,7 @@ describe('BinaryTree Morris Traversal', () => {
196
196
  });
197
197
 
198
198
  describe('BinaryTree APIs test', () => {
199
- const avl = new AVLTree<AVLTreeNode<{id: number; text: string}>>();
199
+ const avl = new AVLTree<{id: number; text: string}>();
200
200
  beforeEach(() => {
201
201
  avl.clear();
202
202
  });
@@ -189,7 +189,7 @@ 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<BSTNode<{key: number; keyA: number}>>();
192
+ const objBST = new BST<{key: number; keyA: number}>();
193
193
  expect(objBST).toBeInstanceOf(BST);
194
194
  objBST.add(11, {key: 11, keyA: 11});
195
195
  objBST.add(3, {key: 3, keyA: 3});
@@ -1,4 +1,4 @@
1
- import {AVLTree, BST, BSTNode} from '../../../../src';
1
+ import {AVLTree, BST} from '../../../../src';
2
2
 
3
3
  describe('Overall BinaryTree Test', () => {
4
4
  it('should perform various operations on BinaryTree', () => {
@@ -6,30 +6,30 @@ describe('Overall BinaryTree Test', () => {
6
6
  bst.add(11);
7
7
  bst.add(3);
8
8
  bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], undefined, false);
9
- bst.size === 16; // true
10
- expect(bst.size).toBe(16); // true
11
- bst.has(6); // true
12
- expect(bst.has(6)).toBe(true); // true
13
- bst.getHeight(6) === 2; // true
14
- bst.getHeight() === 5; // true
15
- bst.getDepth(6) === 3; // true
16
- expect(bst.getHeight(6)).toBe(2); // true
17
- expect(bst.getHeight()).toBe(5); // true
18
- expect(bst.getDepth(6)).toBe(3); // true
9
+ bst.size === 16; // true
10
+ expect(bst.size).toBe(16); // true
11
+ bst.has(6); // true
12
+ expect(bst.has(6)).toBe(true); // true
13
+ bst.getHeight(6) === 2; // true
14
+ bst.getHeight() === 5; // true
15
+ bst.getDepth(6) === 3; // true
16
+ expect(bst.getHeight(6)).toBe(2); // true
17
+ expect(bst.getHeight()).toBe(5); // true
18
+ expect(bst.getDepth(6)).toBe(3); // true
19
19
  const leftMost = bst.getLeftMost();
20
- leftMost?.key === 1; // true
20
+ leftMost?.key === 1; // true
21
21
  expect(leftMost?.key).toBe(1);
22
22
  bst.delete(6);
23
- bst.get(6); // null
23
+ bst.get(6); // null
24
24
  expect(bst.get(6)).toBeNull();
25
- bst.isAVLBalanced(); // true or false
25
+ bst.isAVLBalanced(); // true or false
26
26
  expect(bst.isAVLBalanced()).toBe(true);
27
27
  const bfsIDs: number[] = [];
28
28
  bst.bfs(node => bfsIDs.push(node.key));
29
- bfsIDs[0] === 11; // true
29
+ bfsIDs[0] === 11; // true
30
30
  expect(bfsIDs[0]).toBe(11);
31
31
 
32
- const objBST = new BST<BSTNode<{key: number; keyA: number}>>();
32
+ const objBST = new BST<{key: number; keyA: number}>();
33
33
  objBST.add(11, {key: 11, keyA: 11});
34
34
  objBST.add(3, {key: 3, keyA: 3});
35
35
 
@@ -57,10 +57,10 @@ describe('Overall BinaryTree Test', () => {
57
57
 
58
58
  const avlTree = new AVLTree();
59
59
  avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
60
- avlTree.isAVLBalanced(); // true
61
- expect(avlTree.isAVLBalanced()).toBe(true); // true
60
+ avlTree.isAVLBalanced(); // true
61
+ expect(avlTree.isAVLBalanced()).toBe(true); // true
62
62
  avlTree.delete(10);
63
- avlTree.isAVLBalanced(); // true
64
- expect(avlTree.isAVLBalanced()).toBe(true); // true
63
+ avlTree.isAVLBalanced(); // true
64
+ expect(avlTree.isAVLBalanced()).toBe(true); // true
65
65
  });
66
66
  });
@@ -206,7 +206,7 @@ describe('TreeMultiset operations test', () => {
206
206
  });
207
207
 
208
208
  it('should perform various operations on a Binary Search Tree with object values', () => {
209
- const objTreeMultiset = new TreeMultiset<TreeMultisetNode<{key: number; keyA: number}>>();
209
+ const objTreeMultiset = new TreeMultiset<{key: number; keyA: number}>();
210
210
  expect(objTreeMultiset).toBeInstanceOf(TreeMultiset);
211
211
  objTreeMultiset.add(11, {key: 11, keyA: 11});
212
212
  objTreeMultiset.add(3, {key: 3, keyA: 3});