directed-graph-typed 1.52.0 → 1.52.1

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/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +11 -11
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +11 -11
  4. package/dist/data-structures/binary-tree/avl-tree.js +6 -6
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +97 -97
  6. package/dist/data-structures/binary-tree/binary-tree.js +50 -50
  7. package/dist/data-structures/binary-tree/bst.d.ts +35 -35
  8. package/dist/data-structures/binary-tree/bst.js +15 -15
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +8 -8
  10. package/dist/data-structures/binary-tree/rb-tree.js +6 -6
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +10 -10
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
  13. package/dist/data-structures/graph/directed-graph.js +2 -1
  14. package/dist/data-structures/queue/deque.d.ts +7 -0
  15. package/dist/data-structures/queue/deque.js +16 -1
  16. package/dist/data-structures/queue/queue.d.ts +0 -1
  17. package/dist/data-structures/queue/queue.js +0 -1
  18. package/dist/index.d.ts +2 -0
  19. package/dist/index.js +2 -1
  20. package/dist/interfaces/binary-tree.d.ts +3 -3
  21. package/dist/types/common.d.ts +1 -22
  22. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +18 -1
  23. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -0
  24. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  25. package/package.json +2 -2
  26. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +12 -12
  27. package/src/data-structures/binary-tree/avl-tree.ts +11 -11
  28. package/src/data-structures/binary-tree/binary-tree.ts +151 -147
  29. package/src/data-structures/binary-tree/bst.ts +44 -41
  30. package/src/data-structures/binary-tree/rb-tree.ts +10 -10
  31. package/src/data-structures/binary-tree/tree-multi-map.ts +10 -10
  32. package/src/data-structures/graph/directed-graph.ts +2 -1
  33. package/src/data-structures/queue/deque.ts +15 -1
  34. package/src/data-structures/queue/queue.ts +0 -1
  35. package/src/index.ts +2 -1
  36. package/src/interfaces/binary-tree.ts +3 -3
  37. package/src/types/common.ts +2 -24
  38. package/src/types/data-structures/binary-tree/binary-tree.ts +21 -1
  39. package/src/types/data-structures/binary-tree/bst.ts +7 -0
  40. package/src/types/data-structures/queue/deque.ts +4 -1
@@ -11,12 +11,13 @@ import type {
11
11
  BSTNodeNested,
12
12
  BSTOptions,
13
13
  BTNCallback,
14
+ BTNKeyOrNodeOrEntry,
14
15
  BTNPureKeyOrNodeOrEntry,
15
16
  Comparator,
16
17
  CP,
17
18
  DFSOrderPattern,
18
19
  IterationType,
19
- KeyOrNodeOrEntry
20
+ OptBSTN
20
21
  } from '../../types';
21
22
  import { BTNEntry } from '../../types';
22
23
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
@@ -43,16 +44,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
43
44
  * The function returns the value of the `_left` property.
44
45
  * @returns The `_left` property of the current object is being returned.
45
46
  */
46
- override get left(): NODE | undefined {
47
+ override get left(): OptBSTN<NODE> {
47
48
  return this._left;
48
49
  }
49
50
 
50
51
  /**
51
52
  * The function sets the left child of a node and updates the parent reference of the child.
52
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
53
+ * @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be an
53
54
  * instance of the `NODE` class or `undefined`.
54
55
  */
55
- override set left(v: NODE | undefined) {
56
+ override set left(v: OptBSTN<NODE>) {
56
57
  if (v) {
57
58
  v.parent = this as unknown as NODE;
58
59
  }
@@ -66,16 +67,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
66
67
  * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
67
68
  * `undefined`.
68
69
  */
69
- override get right(): NODE | undefined {
70
+ override get right(): OptBSTN<NODE> {
70
71
  return this._right;
71
72
  }
72
73
 
73
74
  /**
74
75
  * The function sets the right child of a node and updates the parent reference of the child.
75
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
76
+ * @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be a
76
77
  * `NODE` object or `undefined`.
77
78
  */
78
- override set right(v: NODE | undefined) {
79
+ override set right(v: OptBSTN<NODE>) {
79
80
  if (v) {
80
81
  v.parent = this as unknown as NODE;
81
82
  }
@@ -110,7 +111,7 @@ export class BST<
110
111
  * It can include a comparator function that defines the order of the elements in the tree.
111
112
  */
112
113
  constructor(
113
- keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
114
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
114
115
  options?: BSTOptions<K, V, R>
115
116
  ) {
116
117
  super([], options);
@@ -129,7 +130,7 @@ export class BST<
129
130
  * The function returns the root node of a tree structure.
130
131
  * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
131
132
  */
132
- override get root(): NODE | undefined {
133
+ override get root(): OptBSTN<NODE> {
133
134
  return this._root;
134
135
  }
135
136
 
@@ -162,17 +163,17 @@ export class BST<
162
163
 
163
164
  /**
164
165
  * The function overrides a method and converts a key, value pair or entry or raw element to a node.
165
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
166
- * type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
166
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
167
+ * type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
167
168
  * element.
168
169
  * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
169
170
  * value associated with a key in a key-value pair.
170
171
  * @returns either a NODE object or undefined.
171
172
  */
172
173
  override keyValueOrEntryOrRawElementToNode(
173
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
174
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
174
175
  value?: V
175
- ): NODE | undefined {
176
+ ): OptBSTN<NODE> {
176
177
  return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
177
178
  }
178
179
 
@@ -182,7 +183,7 @@ export class BST<
182
183
  *
183
184
  * The function ensures the existence of a node in a data structure and returns it, or undefined if
184
185
  * it doesn't exist.
185
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
186
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
186
187
  * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
187
188
  * entry, or raw element that needs to be ensured in the tree.
188
189
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
@@ -192,21 +193,21 @@ export class BST<
192
193
  * not be ensured.
193
194
  */
194
195
  override ensureNode(
195
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
196
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
196
197
  iterationType: IterationType = 'ITERATIVE'
197
- ): NODE | undefined {
198
+ ): OptBSTN<NODE> {
198
199
  return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
199
200
  }
200
201
 
201
202
  /**
202
203
  * The function checks if the input is an instance of the BSTNode class.
203
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
204
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
204
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
205
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
205
206
  * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
206
207
  * an instance of the `BSTNode` class.
207
208
  */
208
209
  override isNode(
209
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
210
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
210
211
  ): keyOrNodeOrEntryOrRawElement is NODE {
211
212
  return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
212
213
  }
@@ -216,13 +217,13 @@ export class BST<
216
217
  * Space Complexity: O(1)
217
218
  *
218
219
  * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
219
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
220
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
220
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
221
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
221
222
  * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
222
223
  * key in the binary search tree. If provided, it will be stored in the node along with the key.
223
224
  * @returns a boolean value.
224
225
  */
225
- override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
226
+ override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
226
227
  const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
227
228
  if (newNode === undefined) return false;
228
229
 
@@ -284,7 +285,7 @@ export class BST<
284
285
  * successfully inserted into the data structure.
285
286
  */
286
287
  override addMany(
287
- keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
288
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
288
289
  values?: Iterable<V | undefined>,
289
290
  isBalanceAdd = true,
290
291
  iterationType: IterationType = this.iterationType
@@ -308,7 +309,9 @@ export class BST<
308
309
 
309
310
  const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
310
311
 
311
- const isRealBTNExemplar = (kve: R | KeyOrNodeOrEntry<K, V, NODE>): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
312
+ const isRealBTNExemplar = (
313
+ kve: R | BTNKeyOrNodeOrEntry<K, V, NODE>
314
+ ): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
312
315
  if (kve === undefined || kve === null) return false;
313
316
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
314
317
  };
@@ -395,7 +398,7 @@ export class BST<
395
398
  * @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
396
399
  * or all matching nodes. If set to true, only the first matching node will be returned. If set to
397
400
  * false, all matching nodes will be returned. The default value is false.
398
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
401
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
399
402
  * point for the search in the binary tree. It can be either a node object, a key-value pair, or an
400
403
  * entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
401
404
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
@@ -406,7 +409,7 @@ export class BST<
406
409
  identifier: ReturnType<C> | undefined,
407
410
  callback: C = this._DEFAULT_CALLBACK as C,
408
411
  onlyOne = false,
409
- beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
412
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
410
413
  iterationType: IterationType = this.iterationType
411
414
  ): NODE[] {
412
415
  beginRoot = this.ensureNode(beginRoot);
@@ -496,7 +499,7 @@ export class BST<
496
499
  callback: C = this._DEFAULT_CALLBACK as C,
497
500
  beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
498
501
  iterationType: IterationType = this.iterationType
499
- ): NODE | undefined {
502
+ ): OptBSTN<NODE> {
500
503
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
501
504
  }
502
505
 
@@ -518,7 +521,7 @@ export class BST<
518
521
  * It has a default value of `'ITERATIVE'`.
519
522
  * @returns The method is returning a NODE object or undefined.
520
523
  */
521
- override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
524
+ override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBSTN<NODE> {
522
525
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
523
526
  }
524
527
 
@@ -539,7 +542,7 @@ export class BST<
539
542
  * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
540
543
  * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
541
544
  * take one of the following values:
542
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
545
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
543
546
  * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
544
547
  * node entry. If not specified, the default value is the root of the tree.
545
548
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@@ -550,7 +553,7 @@ export class BST<
550
553
  override dfs<C extends BTNCallback<NODE>>(
551
554
  callback: C = this._DEFAULT_CALLBACK as C,
552
555
  pattern: DFSOrderPattern = 'IN',
553
- beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
556
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
554
557
  iterationType: IterationType = 'ITERATIVE'
555
558
  ): ReturnType<C>[] {
556
559
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
@@ -570,7 +573,7 @@ export class BST<
570
573
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
571
574
  * visited during the breadth-first search. It should take a single argument, which is the current
572
575
  * node being visited, and it can return a value of any type.
573
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
576
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
574
577
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
575
578
  * object. If no value is provided, the default value is the root of the tree.
576
579
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -580,7 +583,7 @@ export class BST<
580
583
  */
581
584
  override bfs<C extends BTNCallback<NODE>>(
582
585
  callback: C = this._DEFAULT_CALLBACK as C,
583
- beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
586
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
584
587
  iterationType: IterationType = this.iterationType
585
588
  ): ReturnType<C>[] {
586
589
  return super.bfs(callback, beginRoot, iterationType, false);
@@ -600,7 +603,7 @@ export class BST<
600
603
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
601
604
  * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
602
605
  * tree during the iteration process.
603
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
606
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
604
607
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
605
608
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
606
609
  * value is provided, the root of
@@ -611,7 +614,7 @@ export class BST<
611
614
  */
612
615
  override listLevels<C extends BTNCallback<NODE>>(
613
616
  callback: C = this._DEFAULT_CALLBACK as C,
614
- beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
617
+ beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
615
618
  iterationType: IterationType = this.iterationType
616
619
  ): ReturnType<C>[][] {
617
620
  return super.listLevels(callback, beginRoot, iterationType, false);
@@ -634,7 +637,7 @@ export class BST<
634
637
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
635
638
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
636
639
  * 0, or 1, where:
637
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
640
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
638
641
  * the binary tree that you want to start traversing from. It can be specified either by providing
639
642
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
640
643
  * `targetNode` is provided,
@@ -646,7 +649,7 @@ export class BST<
646
649
  lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
647
650
  callback: C = this._DEFAULT_CALLBACK as C,
648
651
  lesserOrGreater: CP = -1,
649
- targetNode: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
652
+ targetNode: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
650
653
  iterationType: IterationType = this.iterationType
651
654
  ): ReturnType<C>[] {
652
655
  const targetNodeEnsured = this.ensureNode(targetNode);
@@ -760,7 +763,7 @@ export class BST<
760
763
  let balanced = true;
761
764
 
762
765
  if (iterationType === 'RECURSIVE') {
763
- const _height = (cur: NODE | undefined): number => {
766
+ const _height = (cur: OptBSTN<NODE>): number => {
764
767
  if (!cur) return 0;
765
768
  const leftHeight = _height(cur.left),
766
769
  rightHeight = _height(cur.right);
@@ -770,8 +773,8 @@ export class BST<
770
773
  _height(this.root);
771
774
  } else {
772
775
  const stack: NODE[] = [];
773
- let node: NODE | undefined = this.root,
774
- last: NODE | undefined = undefined;
776
+ let node: OptBSTN<NODE> = this.root,
777
+ last: OptBSTN<NODE> = undefined;
775
778
  const depths: Map<NODE, number> = new Map();
776
779
 
777
780
  while (stack.length > 0 || node) {
@@ -827,9 +830,9 @@ export class BST<
827
830
  /**
828
831
  * The function sets the root of a tree-like structure and updates the parent property of the new
829
832
  * root.
830
- * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
833
+ * @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
831
834
  */
832
- protected override _setRoot(v: NODE | undefined) {
835
+ protected override _setRoot(v: OptBSTN<NODE>) {
833
836
  if (v) {
834
837
  v.parent = undefined;
835
838
  }
@@ -1,8 +1,8 @@
1
1
  import type {
2
2
  BinaryTreeDeleteResult,
3
3
  BTNCallback,
4
+ BTNKeyOrNodeOrEntry,
4
5
  CRUD,
5
- KeyOrNodeOrEntry,
6
6
  RBTNColor,
7
7
  RBTreeOptions,
8
8
  RedBlackTreeNested,
@@ -72,7 +72,7 @@ export class RedBlackTree<
72
72
  * depend on the implementation
73
73
  */
74
74
  constructor(
75
- keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
75
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
76
76
  options?: RBTreeOptions<K, V, R>
77
77
  ) {
78
78
  super([], options);
@@ -135,13 +135,13 @@ export class RedBlackTree<
135
135
  * Space Complexity: O(1)
136
136
  *
137
137
  * The function checks if the input is an instance of the RedBlackTreeNode class.
138
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
139
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
138
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
139
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
140
140
  * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
141
141
  * an instance of the `RedBlackTreeNode` class.
142
142
  */
143
143
  override isNode(
144
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
144
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
145
145
  ): keyOrNodeOrEntryOrRawElement is NODE {
146
146
  return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
147
147
  }
@@ -157,11 +157,11 @@ export class RedBlackTree<
157
157
  // *
158
158
  // * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
159
159
  // * valid, otherwise it returns undefined.
160
- // * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
160
+ // * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
161
161
  // * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
162
162
  // * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
163
163
  // */
164
- // override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
164
+ // override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
165
165
  //
166
166
  // if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
167
167
  // if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
@@ -210,8 +210,8 @@ export class RedBlackTree<
210
210
  *
211
211
  * The function adds a new node to a binary search tree and returns true if the node was successfully
212
212
  * added.
213
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
214
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
213
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
214
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
215
215
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
216
216
  * the key in the data structure. It represents the value that you want to add or update in the data
217
217
  * structure.
@@ -219,7 +219,7 @@ export class RedBlackTree<
219
219
  * the method returns true. If the node already exists and its value is updated, the method also
220
220
  * returns true. If the node cannot be added or updated, the method returns false.
221
221
  */
222
- override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
222
+ override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
223
223
  const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
224
224
  if (!this.isRealNode(newNode)) return false;
225
225
 
@@ -9,8 +9,8 @@ import type {
9
9
  BinaryTreeDeleteResult,
10
10
  BSTNKeyOrNode,
11
11
  BTNCallback,
12
+ BTNKeyOrNodeOrEntry,
12
13
  IterationType,
13
- KeyOrNodeOrEntry,
14
14
  RBTNColor,
15
15
  TreeMultiMapNested,
16
16
  TreeMultiMapNodeNested,
@@ -81,7 +81,7 @@ export class TreeMultiMap<
81
81
  * `compareValues`, which are functions used to compare keys and values respectively.
82
82
  */
83
83
  constructor(
84
- keysOrNodesOrEntriesOrRawElements: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [],
84
+ keysOrNodesOrEntriesOrRawElements: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
85
85
  options?: TreeMultiMapOptions<K, V, R>
86
86
  ) {
87
87
  super([], options);
@@ -153,8 +153,8 @@ export class TreeMultiMap<
153
153
  /**
154
154
  * The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
155
155
  * node based on the input.
156
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
157
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
156
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
157
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
158
158
  * @param {V} [value] - The `value` parameter is an optional value that represents the value
159
159
  * associated with the key in the node. It is used when creating a new node or updating the value of
160
160
  * an existing node.
@@ -163,7 +163,7 @@ export class TreeMultiMap<
163
163
  * @returns either a NODE object or undefined.
164
164
  */
165
165
  override keyValueOrEntryOrRawElementToNode(
166
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
166
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
167
167
  value?: V,
168
168
  count = 1
169
169
  ): NODE | undefined {
@@ -190,13 +190,13 @@ export class TreeMultiMap<
190
190
 
191
191
  /**
192
192
  * The function checks if the input is an instance of the TreeMultiMapNode class.
193
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
194
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
193
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
194
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
195
195
  * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
196
196
  * an instance of the `TreeMultiMapNode` class.
197
197
  */
198
198
  override isNode(
199
- keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
199
+ keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
200
200
  ): keyOrNodeOrEntryOrRawElement is NODE {
201
201
  return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
202
202
  }
@@ -212,7 +212,7 @@ export class TreeMultiMap<
212
212
  *
213
213
  * The function overrides the add method of a class and adds a new node to a data structure, updating
214
214
  * the count and returning a boolean indicating success.
215
- * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
215
+ * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
216
216
  * `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
217
217
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
218
218
  * data structure. It is an optional parameter, so it can be omitted if not needed.
@@ -222,7 +222,7 @@ export class TreeMultiMap<
222
222
  * @returns The method is returning a boolean value. It returns true if the addition of the new node
223
223
  * was successful, and false otherwise.
224
224
  */
225
- override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
225
+ override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
226
226
  const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
227
227
  const orgCount = newNode?.count || 0;
228
228
  const isSuccessAdded = super.add(newNode);
@@ -261,7 +261,8 @@ export class DirectedGraph<
261
261
  if (vertex) {
262
262
  const neighbors = this.getNeighbors(vertex);
263
263
  for (const neighbor of neighbors) {
264
- this._inEdgeMap.delete(neighbor);
264
+ // this._inEdgeMap.delete(neighbor);
265
+ this.deleteEdgeSrcToDest(vertex, neighbor);
265
266
  }
266
267
  this._outEdgeMap.delete(vertex);
267
268
  this._inEdgeMap.delete(vertex);
@@ -32,8 +32,9 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
32
32
  super(options);
33
33
 
34
34
  if (options) {
35
- const { bucketSize } = options;
35
+ const { bucketSize, maxLen } = options;
36
36
  if (typeof bucketSize === 'number') this._bucketSize = bucketSize;
37
+ if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
37
38
  }
38
39
 
39
40
  let _size: number;
@@ -73,6 +74,17 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
73
74
  return this._bucketSize;
74
75
  }
75
76
 
77
+ protected _maxLen: number = -1;
78
+
79
+ /**
80
+ * The maxLen function returns the max length of the deque.
81
+ *
82
+ * @return The max length of the deque
83
+ */
84
+ get maxLen() {
85
+ return this._maxLen;
86
+ }
87
+
76
88
  protected _bucketFirst = 0;
77
89
 
78
90
  /**
@@ -193,6 +205,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
193
205
  }
194
206
  this._size += 1;
195
207
  this._buckets[this._bucketLast][this._lastInBucket] = element;
208
+ if (this._maxLen > 0 && this._size > this._maxLen) this.shift();
196
209
  return true;
197
210
  }
198
211
 
@@ -257,6 +270,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
257
270
  }
258
271
  this._size += 1;
259
272
  this._buckets[this._bucketFirst][this._firstInBucket] = element;
273
+ if (this._maxLen > 0 && this._size > this._maxLen) this.pop();
260
274
  return true;
261
275
  }
262
276
 
@@ -100,7 +100,6 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
100
100
  *
101
101
  * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
102
102
  * @public
103
- * @static
104
103
  * @param {E[]} elements - The "elements" parameter is an array of elements of type E.
105
104
  * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
106
105
  * array.
package/src/index.ts CHANGED
@@ -5,7 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- // export { DirectedVertex, DirectedEdge, DirectedGraph } from 'data-structure-typed';
9
8
  export * from './data-structures/graph/directed-graph';
9
+ export * from './data-structures/graph/abstract-graph';
10
10
  export * from './types/data-structures/graph/directed-graph';
11
+ export * from './types/data-structures/graph/abstract-graph';
11
12
  export * from './types/common';
@@ -5,7 +5,7 @@ import type {
5
5
  BinaryTreeNodeNested,
6
6
  BinaryTreeOptions,
7
7
  BTNCallback,
8
- KeyOrNodeOrEntry
8
+ BTNKeyOrNodeOrEntry
9
9
  } from '../types';
10
10
 
11
11
  export interface IBinaryTree<
@@ -19,9 +19,9 @@ export interface IBinaryTree<
19
19
 
20
20
  createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
21
21
 
22
- add(keyOrNodeOrEntryOrRawElement: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
22
+ add(keyOrNodeOrEntryOrRawElement: BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
23
23
 
24
- addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
24
+ addMany(nodes: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
25
25
 
26
26
  delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<NODE>[];
27
27
  }
@@ -1,11 +1,5 @@
1
1
  export type CP = 1 | -1 | 0;
2
2
 
3
- /**
4
- * Enum representing different loop types.
5
- *
6
- * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
7
- * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
8
- */
9
3
  export type IterationType = 'ITERATIVE' | 'RECURSIVE';
10
4
 
11
5
  export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
@@ -16,8 +10,6 @@ export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
16
10
 
17
11
  export type NodeDisplayLayout = [string[], number, number, number];
18
12
 
19
- export type BTNCallback<N, D = any> = (node: N) => D;
20
-
21
13
  export interface IterableWithSize<T> extends Iterable<T> {
22
14
  size: number | ((...args: any[]) => number);
23
15
  }
@@ -26,22 +18,8 @@ export interface IterableWithLength<T> extends Iterable<T> {
26
18
  length: number | ((...args: any[]) => number);
27
19
  }
28
20
 
29
- export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
30
-
31
- export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
32
-
33
- export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
34
-
35
- export type BTNKeyOrNode<K, N> = K | null | undefined | N;
21
+ export type OptValue<V> = V | undefined;
36
22
 
37
- export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
38
-
39
- export type BTNodePureKeyOrNode<K, N> = K | N;
40
-
41
- export type BTNPureKeyOrNodeOrEntry<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
42
-
43
- export type BSTNKeyOrNode<K, N> = K | undefined | N;
44
-
45
- export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
23
+ export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
46
24
 
47
25
  export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';
@@ -1,5 +1,5 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
2
- import { BTNEntry, IterationType } from '../../common';
2
+ import { IterationType, OptValue } from '../../common';
3
3
 
4
4
  export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
5
5
 
@@ -9,3 +9,23 @@ export type BinaryTreeOptions<K, V, R> = {
9
9
  iterationType?: IterationType;
10
10
  toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
11
11
  }
12
+
13
+ export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
14
+
15
+ export type OptBTNOrNull<NODE> = NODE | null | undefined;
16
+
17
+ export type OptBTNKeyOrNull<K> = K | null | undefined;
18
+
19
+ export type BTNEntry<K, V> = [OptBTNKeyOrNull<K>, OptValue<V>];
20
+
21
+ export type BTNKeyOrNode<K, NODE> = OptBTNKeyOrNull<K> | NODE;
22
+
23
+ export type BTNKeyOrNodeOrEntry<K, V, NODE> = BTNEntry<K, V> | BTNKeyOrNode<K, NODE>;
24
+
25
+ export type BTNPureKeyOrNode<K, NODE> = K | NODE;
26
+
27
+ export type BTNPureKeyOrNodeOrEntry<K, V, NODE> = [K, OptValue<V>] | BTNPureKeyOrNode<K, NODE>;
28
+
29
+ export type BinaryTreeDeleteResult<NODE> = { deleted: OptBTNOrNull<NODE>; needBalanced: OptBTNOrNull<NODE> };
30
+
31
+ export type BTNCallback<NODE, D = any> = (node: NODE) => D;
@@ -9,3 +9,10 @@ export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R,
9
9
  export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
10
10
  comparator?: Comparator<K>
11
11
  }
12
+
13
+ export type OptBSTNKey<K> = K | undefined;
14
+
15
+ export type OptBSTN<NODE> = NODE | undefined;
16
+
17
+ export type BSTNKeyOrNode<K, NODE> = OptBSTNKey<K> | NODE;
18
+
@@ -1,3 +1,6 @@
1
1
  import { IterableElementBaseOptions } from '../base';
2
2
 
3
- export type DequeOptions<E, R> = { bucketSize?: number } & IterableElementBaseOptions<E, R>;
3
+ export type DequeOptions<E, R> = {
4
+ bucketSize?: number,
5
+ maxLen?: number
6
+ } & IterableElementBaseOptions<E, R>;