linked-list-typed 1.38.6 → 1.38.8

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 (32) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
  2. package/dist/data-structures/binary-tree/avl-tree.js +1 -1
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +45 -33
  4. package/dist/data-structures/binary-tree/binary-tree.js +80 -30
  5. package/dist/data-structures/binary-tree/bst.d.ts +6 -6
  6. package/dist/data-structures/binary-tree/bst.js +1 -1
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
  8. package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
  9. package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
  10. package/dist/interfaces/binary-tree.d.ts +2 -2
  11. package/package.json +2 -2
  12. package/src/data-structures/binary-tree/avl-tree.ts +10 -11
  13. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  14. package/src/data-structures/binary-tree/binary-tree.ts +107 -52
  15. package/src/data-structures/binary-tree/bst.ts +9 -9
  16. package/src/data-structures/binary-tree/rb-tree.ts +4 -7
  17. package/src/data-structures/binary-tree/tree-multiset.ts +10 -14
  18. package/src/data-structures/graph/abstract-graph.ts +10 -11
  19. package/src/data-structures/graph/directed-graph.ts +1 -2
  20. package/src/data-structures/graph/undirected-graph.ts +4 -5
  21. package/src/data-structures/hash/hash-map.ts +1 -1
  22. package/src/data-structures/hash/tree-map.ts +2 -1
  23. package/src/data-structures/hash/tree-set.ts +2 -1
  24. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  25. package/src/data-structures/matrix/matrix.ts +1 -1
  26. package/src/data-structures/matrix/vector2d.ts +2 -1
  27. package/src/data-structures/queue/deque.ts +5 -4
  28. package/src/data-structures/queue/queue.ts +1 -1
  29. package/src/interfaces/binary-tree.ts +2 -2
  30. package/src/types/data-structures/matrix/navigator.ts +1 -1
  31. package/src/types/utils/utils.ts +1 -1
  32. package/src/types/utils/validate-type.ts +2 -2
@@ -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,9 @@ 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<V, AVLTreeNodeNested<V>>>
23
+ extends BST<V, N>
24
+ implements IBinaryTree<V, N> {
26
25
  /**
27
26
  * This is a constructor function for an AVL tree data structure in TypeScript.
28
27
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@@ -38,12 +37,12 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
38
37
  * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
39
38
  * the new node. It is used to determine the position of the node in the binary search tree.
40
39
  * @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
40
+ * type `V`, which means it can be any value that is assignable to the `val` property of the
42
41
  * node type `N`.
43
42
  * @returns a new AVLTreeNode object with the specified key and value.
44
43
  */
45
- override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
46
- return new AVLTreeNode<N['val'], N>(key, val) as N;
44
+ override createNode(key: BinaryTreeNodeKey, val?: V): N {
45
+ return new AVLTreeNode<V, N>(key, val) as N;
47
46
  }
48
47
 
49
48
  /**
@@ -55,7 +54,7 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
55
54
  * are adding to the binary search tree.
56
55
  * @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
57
56
  */
58
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
57
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
59
58
  // TODO support node as a param
60
59
  const inserted = super.add(keyOrNode, val);
61
60
  if (inserted) this._balancePath(inserted);
@@ -161,7 +160,7 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
161
160
  // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
162
161
  switch (
163
162
  this._balanceFactor(A) // second O(1)
164
- ) {
163
+ ) {
165
164
  case -2:
166
165
  if (A && A.left) {
167
166
  if (this._balanceFactor(A.left) <= 0) {
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
17
17
  * @param - - `frequency`: The default frequency value. It is optional and has a default
18
18
  * value of 0.
19
19
  */
20
- constructor({frequency = 0, max}: {frequency?: number; max: number}) {
20
+ constructor({frequency = 0, max}: { frequency?: number; max: number }) {
21
21
  this._freq = frequency;
22
22
  this._max = max;
23
23
  this._freqMap = {0: 0};
@@ -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<V, BinaryTreeNodeNested<V>>> 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
  }
@@ -404,7 +404,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
404
404
  return -1;
405
405
  }
406
406
 
407
- const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
407
+ const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
408
408
  let maxHeight = 0;
409
409
 
410
410
  while (stack.length > 0) {
@@ -887,7 +887,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
887
887
  _traverse(beginRoot);
888
888
  } else {
889
889
  // 0: visit, 1: print
890
- const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
890
+ const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
891
891
 
892
892
  while (stack.length > 0) {
893
893
  const cur = stack.pop();
@@ -930,10 +930,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
930
930
  * @param callback - The `callback` parameter is a function that will be called for each node in the
931
931
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
932
932
  * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
933
- * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
934
- * whether to include the level of each node in the callback function. If `withLevel` is set
935
- * to `true`, the level of each node will be passed as an argument to the callback function. If
936
- * `withLevel` is
937
933
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
938
934
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
939
935
  * will not be performed and an empty array will be returned.
@@ -943,7 +939,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
943
939
  */
944
940
  bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
945
941
  callback: C = this._defaultCallbackByKey as C,
946
- withLevel: boolean = false,
947
942
  beginRoot: N | null = this.root,
948
943
  iterationType = this.iterationType
949
944
  ): ReturnType<C>[] {
@@ -951,9 +946,67 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
951
946
 
952
947
  const ans: BFSCallbackReturn<N>[] = [];
953
948
 
949
+ if (iterationType === IterationType.RECURSIVE) {
950
+ const queue = new Queue<N>([beginRoot]);
951
+
952
+ function traverse(level: number) {
953
+ if (queue.size === 0) return;
954
+
955
+ const current = queue.shift()!;
956
+ ans.push(callback(current));
957
+
958
+ if (current.left) queue.push(current.left);
959
+ if (current.right) queue.push(current.right);
960
+
961
+ traverse(level + 1);
962
+ }
963
+
964
+ traverse(0);
965
+ } else {
966
+ const queue = new Queue<N>([beginRoot]);
967
+ while (queue.size > 0) {
968
+ const levelSize = queue.size;
969
+
970
+ for (let i = 0; i < levelSize; i++) {
971
+ const current = queue.shift()!;
972
+ ans.push(callback(current));
973
+
974
+ if (current.left) queue.push(current.left);
975
+ if (current.right) queue.push(current.right);
976
+ }
977
+
978
+ }
979
+ }
980
+ return ans;
981
+ }
982
+
983
+ /**
984
+ * The `listLevels` function takes a binary tree node and a callback function, and returns an array
985
+ * of arrays representing the levels of the tree.
986
+ * @param {C} callback - The `callback` parameter is a function that will be called on each node in
987
+ * the tree. It takes a node as input and returns a value. The return type of the callback function
988
+ * is determined by the generic type `C`.
989
+ * @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
990
+ * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
991
+ * from the root node of the binary tree.
992
+ * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
993
+ * recursively or iteratively. It can have two possible values:
994
+ * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
995
+ * level in a binary tree. Each inner array contains the return type of the provided callback
996
+ * function `C` applied to the nodes at that level.
997
+ */
998
+ listLevels<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
999
+ callback: C = this._defaultCallbackByKey as C,
1000
+ beginRoot: N | null = this.root,
1001
+ iterationType = this.iterationType
1002
+ ): ReturnType<C>[][] {
1003
+ if (!beginRoot) return [];
1004
+ const levelsNodes: ReturnType<C>[][] = [];
1005
+
954
1006
  if (iterationType === IterationType.RECURSIVE) {
955
1007
  const _recursive = (node: N, level: number) => {
956
- callback && ans.push(callback(node, withLevel ? level : undefined));
1008
+ if (!levelsNodes[level]) levelsNodes[level] = [];
1009
+ levelsNodes[level].push(callback(node));
957
1010
  if (node.left) _recursive(node.left, level + 1);
958
1011
  if (node.right) _recursive(node.right, level + 1);
959
1012
  };
@@ -966,12 +1019,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
966
1019
  const head = stack.pop()!;
967
1020
  const [node, level] = head;
968
1021
 
969
- callback && ans.push(callback(node, withLevel ? level : undefined));
1022
+ if (!levelsNodes[level]) levelsNodes[level] = [];
1023
+ levelsNodes[level].push(callback(node));
970
1024
  if (node.right) stack.push([node.right, level + 1]);
971
1025
  if (node.left) stack.push([node.left, level + 1]);
972
1026
  }
973
1027
  }
974
- return ans;
1028
+
1029
+ return levelsNodes;
975
1030
  }
976
1031
 
977
1032
  /**
@@ -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<V, BSTNodeNested<V>>> 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<V, RBTreeNodeNested<V>>> 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,10 +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>
41
- {
38
+ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
39
+ extends AVLTree<V, N>
40
+ implements IBinaryTree<V, N> {
42
41
  /**
43
42
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
44
43
  * merge duplicated values.
@@ -64,7 +63,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
64
63
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
65
64
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
66
65
  */
67
- override createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N {
66
+ override createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N {
68
67
  return new TreeMultisetNode(key, val, count) as N;
69
68
  }
70
69
 
@@ -81,7 +80,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
81
80
  * count is specified, the default count will be 1.
82
81
  * @returns The function `add` returns a value of type `N | null | undefined`.
83
82
  */
84
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count = 1): N | null | undefined {
83
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count = 1): N | null | undefined {
85
84
  let inserted: N | null | undefined = undefined,
86
85
  newNode: N | null;
87
86
  if (keyOrNode instanceof TreeMultisetNode) {
@@ -187,15 +186,12 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
187
186
  * inserted nodes.
188
187
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
189
188
  * 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
189
+ * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
191
190
  * to the keys or nodes being added to the multiset. It is used to associate additional data with
192
191
  * each key or node.
193
192
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
194
193
  */
195
- override addMany(
196
- keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
197
- data?: N['val'][]
198
- ): (N | null | undefined)[] {
194
+ override addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] {
199
195
  const inserted: (N | null | undefined)[] = [];
200
196
 
201
197
  for (let i = 0; i < keysOrNodes.length; i++) {
@@ -207,7 +203,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
207
203
  }
208
204
 
209
205
  if (keyOrNode === null) {
210
- inserted.push(this.add(NaN, null, 0));
206
+ inserted.push(this.add(NaN, undefined, 0));
211
207
  continue;
212
208
  }
213
209
 
@@ -105,8 +105,7 @@ export abstract class AbstractEdge<V = any> {
105
105
  export abstract class AbstractGraph<
106
106
  V extends AbstractVertex<any> = AbstractVertex<any>,
107
107
  E extends AbstractEdge<any> = AbstractEdge<any>
108
- > implements IGraph<V, E>
109
- {
108
+ > implements IGraph<V, E> {
110
109
  private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
111
110
 
112
111
  get vertices(): Map<VertexKey, V> {
@@ -554,14 +553,14 @@ export abstract class AbstractGraph<
554
553
  }
555
554
 
556
555
  getMinDist &&
557
- distMap.forEach((d, v) => {
558
- if (v !== srcVertex) {
559
- if (d < minDist) {
560
- minDist = d;
561
- if (genPaths) minDest = v;
562
- }
556
+ distMap.forEach((d, v) => {
557
+ if (v !== srcVertex) {
558
+ if (d < minDist) {
559
+ minDist = d;
560
+ if (genPaths) minDest = v;
563
561
  }
564
- });
562
+ }
563
+ });
565
564
 
566
565
  genPaths && getPaths(minDest);
567
566
 
@@ -623,7 +622,7 @@ export abstract class AbstractGraph<
623
622
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
624
623
  }
625
624
 
626
- const heap = new PriorityQueue<{key: number; val: V}>((a, b) => a.key - b.key);
625
+ const heap = new PriorityQueue<{ key: number; val: V }>((a, b) => a.key - b.key);
627
626
  heap.add({key: 0, val: srcVertex});
628
627
 
629
628
  distMap.set(srcVertex, 0);
@@ -852,7 +851,7 @@ export abstract class AbstractGraph<
852
851
  * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
853
852
  * path between vertices in the
854
853
  */
855
- floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
854
+ floyd(): { costs: number[][]; predecessor: (V | null)[][] } {
856
855
  const idAndVertices = [...this._vertices];
857
856
  const n = idAndVertices.length;
858
857
 
@@ -64,8 +64,7 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
64
64
 
65
65
  export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
66
66
  extends AbstractGraph<V, E>
67
- implements IGraph<V, E>
68
- {
67
+ implements IGraph<V, E> {
69
68
  /**
70
69
  * The constructor function initializes an instance of a class.
71
70
  */
@@ -51,12 +51,11 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
51
51
  }
52
52
 
53
53
  export class UndirectedGraph<
54
- V extends UndirectedVertex<any> = UndirectedVertex,
55
- E extends UndirectedEdge<any> = UndirectedEdge
56
- >
54
+ V extends UndirectedVertex<any> = UndirectedVertex,
55
+ E extends UndirectedEdge<any> = UndirectedEdge
56
+ >
57
57
  extends AbstractGraph<V, E>
58
- implements IGraph<V, E>
59
- {
58
+ implements IGraph<V, E> {
60
59
  /**
61
60
  * The constructor initializes a new Map object to store edges.
62
61
  */
@@ -157,7 +157,7 @@ export class HashMap<K, V> {
157
157
  }
158
158
  }
159
159
 
160
- *entries(): IterableIterator<[K, V]> {
160
+ * entries(): IterableIterator<[K, V]> {
161
161
  for (const bucket of this.table) {
162
162
  if (bucket) {
163
163
  for (const [key, value] of bucket) {
@@ -1 +1,2 @@
1
- export class TreeMap {}
1
+ export class TreeMap {
2
+ }
@@ -1 +1,2 @@
1
- export class TreeSet {}
1
+ export class TreeSet {
2
+ }