directed-graph-typed 1.48.0 → 1.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +28 -19
  6. package/dist/data-structures/binary-tree/avl-tree.js +22 -11
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -152
  8. package/dist/data-structures/binary-tree/binary-tree.js +241 -215
  9. package/dist/data-structures/binary-tree/bst.d.ts +64 -48
  10. package/dist/data-structures/binary-tree/bst.js +94 -65
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +39 -39
  12. package/dist/data-structures/binary-tree/rb-tree.js +42 -49
  13. package/dist/data-structures/binary-tree/tree-multimap.d.ts +60 -34
  14. package/dist/data-structures/binary-tree/tree-multimap.js +59 -27
  15. package/dist/data-structures/graph/abstract-graph.d.ts +92 -53
  16. package/dist/data-structures/graph/abstract-graph.js +130 -103
  17. package/dist/data-structures/graph/directed-graph.d.ts +70 -52
  18. package/dist/data-structures/graph/directed-graph.js +111 -65
  19. package/dist/data-structures/graph/map-graph.d.ts +5 -5
  20. package/dist/data-structures/graph/map-graph.js +8 -8
  21. package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
  22. package/dist/data-structures/graph/undirected-graph.js +117 -54
  23. package/dist/data-structures/hash/hash-map.d.ts +160 -44
  24. package/dist/data-structures/hash/hash-map.js +314 -82
  25. package/dist/data-structures/heap/heap.d.ts +50 -7
  26. package/dist/data-structures/heap/heap.js +60 -30
  27. package/dist/data-structures/index.d.ts +1 -0
  28. package/dist/data-structures/index.js +1 -0
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +42 -55
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +50 -77
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -55
  32. package/dist/data-structures/linked-list/singly-linked-list.js +44 -77
  33. package/dist/data-structures/queue/deque.d.ts +35 -167
  34. package/dist/data-structures/queue/deque.js +43 -249
  35. package/dist/data-structures/queue/queue.d.ts +49 -48
  36. package/dist/data-structures/queue/queue.js +69 -82
  37. package/dist/data-structures/stack/stack.d.ts +43 -10
  38. package/dist/data-structures/stack/stack.js +50 -31
  39. package/dist/data-structures/trie/trie.d.ts +41 -6
  40. package/dist/data-structures/trie/trie.js +53 -32
  41. package/dist/interfaces/binary-tree.d.ts +6 -6
  42. package/dist/types/common.d.ts +11 -8
  43. package/dist/types/common.js +6 -1
  44. package/dist/types/data-structures/base/base.d.ts +5 -0
  45. package/dist/types/data-structures/base/base.js +2 -0
  46. package/dist/types/data-structures/base/index.d.ts +1 -0
  47. package/dist/types/data-structures/base/index.js +17 -0
  48. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  49. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  50. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
  51. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  52. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  53. package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
  54. package/dist/types/data-structures/index.d.ts +1 -0
  55. package/dist/types/data-structures/index.js +1 -0
  56. package/package.json +2 -2
  57. package/src/data-structures/base/index.ts +1 -0
  58. package/src/data-structures/base/iterable-base.ts +329 -0
  59. package/src/data-structures/binary-tree/avl-tree.ts +37 -25
  60. package/src/data-structures/binary-tree/binary-tree.ts +336 -296
  61. package/src/data-structures/binary-tree/bst.ts +135 -89
  62. package/src/data-structures/binary-tree/rb-tree.ts +60 -69
  63. package/src/data-structures/binary-tree/tree-multimap.ts +86 -49
  64. package/src/data-structures/graph/abstract-graph.ts +136 -104
  65. package/src/data-structures/graph/directed-graph.ts +114 -65
  66. package/src/data-structures/graph/map-graph.ts +8 -8
  67. package/src/data-structures/graph/undirected-graph.ts +124 -56
  68. package/src/data-structures/hash/hash-map.ts +335 -84
  69. package/src/data-structures/heap/heap.ts +63 -36
  70. package/src/data-structures/index.ts +1 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +54 -83
  72. package/src/data-structures/linked-list/singly-linked-list.ts +49 -84
  73. package/src/data-structures/queue/deque.ts +43 -275
  74. package/src/data-structures/queue/queue.ts +71 -86
  75. package/src/data-structures/stack/stack.ts +53 -34
  76. package/src/data-structures/trie/trie.ts +58 -35
  77. package/src/interfaces/binary-tree.ts +5 -6
  78. package/src/types/common.ts +11 -8
  79. package/src/types/data-structures/base/base.ts +6 -0
  80. package/src/types/data-structures/base/index.ts +1 -0
  81. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  82. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  83. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  84. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  85. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  86. package/src/types/data-structures/hash/hash-map.ts +2 -0
  87. package/src/types/data-structures/heap/heap.ts +1 -1
  88. package/src/types/data-structures/index.ts +1 -0
  89. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
@@ -12,16 +12,15 @@ import type {
12
12
  AVLTreeOptions,
13
13
  BiTreeDeleteResult,
14
14
  BSTNodeKeyOrNode,
15
- BTNKey,
16
15
  BTNodeExemplar
17
16
  } from '../../types';
18
- import { BTNCallback } from '../../types';
17
+ import { BTNCallback, BTNodeKeyOrNode } from '../../types';
19
18
  import { IBinaryTree } from '../../interfaces';
20
19
 
21
- export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
20
+ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
22
21
  height: number;
23
22
 
24
- constructor(key: BTNKey, value?: V) {
23
+ constructor(key: K, value?: V) {
25
24
  super(key, value);
26
25
  this.height = 0;
27
26
  }
@@ -37,35 +36,35 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
37
36
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
38
37
  * 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
39
38
  */
40
- export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>>
41
- extends BST<V, N, TREE>
42
- implements IBinaryTree<V, N, TREE> {
39
+ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>>
40
+ extends BST<K, V, N, TREE>
41
+ implements IBinaryTree<K, V, N, TREE> {
43
42
 
44
43
  /**
45
44
  * The constructor function initializes an AVLTree object with optional elements and options.
46
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
45
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
47
46
  * objects. It represents a collection of elements that will be added to the AVL tree during
48
47
  * initialization.
49
48
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
50
49
  * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
51
50
  * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
52
51
  */
53
- constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>) {
52
+ constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
54
53
  super([], options);
55
54
  if (elements) super.addMany(elements);
56
55
  }
57
56
 
58
57
  /**
59
58
  * The function creates a new AVL tree node with the specified key and value.
60
- * @param {BTNKey} key - The key parameter is the key value that will be associated with
59
+ * @param {K} key - The key parameter is the key value that will be associated with
61
60
  * the new node. It is used to determine the position of the node in the binary search tree.
62
61
  * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
63
62
  * type `V`, which means it can be any value that is assignable to the `value` property of the
64
63
  * node type `N`.
65
64
  * @returns a new AVLTreeNode object with the specified key and value.
66
65
  */
67
- override createNode(key: BTNKey, value?: V): N {
68
- return new AVLTreeNode<V, N>(key, value) as N;
66
+ override createNode(key: K, value?: V): N {
67
+ return new AVLTreeNode<K, V, N>(key, value) as N;
69
68
  }
70
69
 
71
70
  /**
@@ -75,40 +74,53 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
75
74
  * being created.
76
75
  * @returns a new AVLTree object.
77
76
  */
78
- override createTree(options?: AVLTreeOptions): TREE {
79
- return new AVLTree<V, N, TREE>([], {
77
+ override createTree(options?: AVLTreeOptions<K>): TREE {
78
+ return new AVLTree<K, V, N, TREE>([], {
80
79
  iterationType: this.iterationType,
81
- comparator: this.comparator, ...options
80
+ variant: this.variant, ...options
82
81
  }) as TREE;
83
82
  }
84
83
 
85
84
  /**
86
85
  * The function checks if an exemplar is an instance of AVLTreeNode.
87
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
86
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
88
87
  * @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
89
88
  */
90
- override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
89
+ override isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N {
91
90
  return exemplar instanceof AVLTreeNode;
92
91
  }
93
92
 
93
+ /**
94
+ * The function "isNotNodeInstance" checks if a potential key is a K.
95
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
96
+ * data type.
97
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
98
+ */
99
+ override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
100
+ return !(potentialKey instanceof AVLTreeNode)
101
+ }
102
+
94
103
  /**
95
104
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
96
105
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
97
106
  */
98
107
 
108
+
99
109
  /**
100
110
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
101
111
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
102
112
  *
103
113
  * The function overrides the add method of a binary tree node and balances the tree after inserting
104
114
  * a new node.
105
- * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
115
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
106
116
  * entry.
107
- * @returns The method is returning either the inserted node or `undefined`.
117
+ * @param {V} [value] - The `value` parameter represents the value associated with the key that is
118
+ * being added to the binary tree.
119
+ * @returns The method is returning either the inserted node or undefined.
108
120
  */
109
- override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
121
+ override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
110
122
  if (keyOrNodeOrEntry === null) return undefined;
111
- const inserted = super.add(keyOrNodeOrEntry);
123
+ const inserted = super.add(keyOrNodeOrEntry, value);
112
124
  if (inserted) this._balancePath(inserted);
113
125
  return inserted;
114
126
  }
@@ -151,14 +163,14 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
151
163
  /**
152
164
  * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
153
165
  * tree.
154
- * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
155
- * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
156
- * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
166
+ * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node that
167
+ * needs to be swapped with the destination node. It can be of type `K`, `N`, or `undefined`.
168
+ * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
157
169
  * node where the values from the source node will be swapped to.
158
170
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
159
171
  * if either `srcNode` or `destNode` is undefined.
160
172
  */
161
- protected override _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined {
173
+ protected override _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined {
162
174
  srcNode = this.ensureNode(srcNode);
163
175
  destNode = this.ensureNode(destNode);
164
176