min-heap-typed 1.50.1 → 1.50.3

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 (85) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +120 -9
  2. package/dist/data-structures/base/iterable-base.js +143 -7
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
  4. package/dist/data-structures/binary-tree/avl-tree.js +101 -72
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
  8. package/dist/data-structures/binary-tree/binary-tree.js +484 -376
  9. package/dist/data-structures/binary-tree/bst.d.ts +92 -79
  10. package/dist/data-structures/binary-tree/bst.js +68 -76
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
  12. package/dist/data-structures/binary-tree/rb-tree.js +152 -99
  13. package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
  14. package/dist/data-structures/binary-tree/segment-tree.js +127 -10
  15. package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  16. package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
  17. package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
  18. package/dist/data-structures/graph/abstract-graph.js +3 -189
  19. package/dist/data-structures/graph/directed-graph.d.ts +73 -0
  20. package/dist/data-structures/graph/directed-graph.js +131 -0
  21. package/dist/data-structures/graph/map-graph.d.ts +8 -0
  22. package/dist/data-structures/graph/map-graph.js +14 -0
  23. package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
  24. package/dist/data-structures/graph/undirected-graph.js +151 -18
  25. package/dist/data-structures/hash/hash-map.d.ts +254 -28
  26. package/dist/data-structures/hash/hash-map.js +347 -78
  27. package/dist/data-structures/heap/heap.d.ts +95 -25
  28. package/dist/data-structures/heap/heap.js +95 -26
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  32. package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
  33. package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  34. package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
  35. package/dist/data-structures/matrix/matrix.d.ts +35 -4
  36. package/dist/data-structures/matrix/matrix.js +50 -11
  37. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  38. package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
  39. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  40. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
  41. package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
  42. package/dist/data-structures/priority-queue/priority-queue.js +8 -0
  43. package/dist/data-structures/queue/deque.d.ts +139 -35
  44. package/dist/data-structures/queue/deque.js +200 -62
  45. package/dist/data-structures/queue/queue.d.ts +103 -49
  46. package/dist/data-structures/queue/queue.js +111 -49
  47. package/dist/data-structures/stack/stack.d.ts +51 -21
  48. package/dist/data-structures/stack/stack.js +58 -22
  49. package/dist/data-structures/tree/tree.d.ts +57 -3
  50. package/dist/data-structures/tree/tree.js +77 -11
  51. package/dist/data-structures/trie/trie.d.ts +135 -34
  52. package/dist/data-structures/trie/trie.js +153 -33
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  54. package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
  55. package/dist/types/utils/utils.d.ts +1 -0
  56. package/package.json +2 -2
  57. package/src/data-structures/base/iterable-base.ts +184 -19
  58. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  59. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  60. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  61. package/src/data-structures/binary-tree/bst.ts +127 -136
  62. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  63. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  64. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  65. package/src/data-structures/graph/abstract-graph.ts +4 -211
  66. package/src/data-structures/graph/directed-graph.ts +152 -0
  67. package/src/data-structures/graph/map-graph.ts +15 -0
  68. package/src/data-structures/graph/undirected-graph.ts +171 -19
  69. package/src/data-structures/hash/hash-map.ts +389 -96
  70. package/src/data-structures/heap/heap.ts +97 -26
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  72. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  73. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  74. package/src/data-structures/matrix/matrix.ts +52 -12
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  78. package/src/data-structures/queue/deque.ts +225 -70
  79. package/src/data-structures/queue/queue.ts +118 -49
  80. package/src/data-structures/stack/stack.ts +63 -23
  81. package/src/data-structures/tree/tree.ts +89 -15
  82. package/src/data-structures/trie/trie.ts +173 -38
  83. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  84. package/src/types/data-structures/hash/hash-map.ts +4 -3
  85. package/src/types/utils/utils.ts +2 -0
@@ -9,17 +9,118 @@
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.SegmentTree = exports.SegmentTreeNode = void 0;
11
11
  class SegmentTreeNode {
12
+ /**
13
+ * The constructor initializes the properties of a SegmentTreeNode object.
14
+ * @param {number} start - The `start` parameter represents the starting index of the segment covered
15
+ * by this node in a segment tree.
16
+ * @param {number} end - The `end` parameter represents the end index of the segment covered by this
17
+ * node in a segment tree.
18
+ * @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
19
+ * the segment tree node.
20
+ * @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
21
+ * of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
22
+ */
12
23
  constructor(start, end, sum, value) {
13
- this.start = 0;
14
- this.end = 0;
15
- this.value = undefined;
16
- this.sum = 0;
17
- this.left = undefined;
18
- this.right = undefined;
19
- this.start = start;
20
- this.end = end;
21
- this.sum = sum;
22
- this.value = value || undefined;
24
+ this._start = 0;
25
+ this._end = 0;
26
+ this._value = undefined;
27
+ this._sum = 0;
28
+ this._left = undefined;
29
+ this._right = undefined;
30
+ this._start = start;
31
+ this._end = end;
32
+ this._sum = sum;
33
+ this._value = value || undefined;
34
+ }
35
+ /**
36
+ * The function returns the value of the protected variable _start.
37
+ * @returns The start value, which is of type number.
38
+ */
39
+ get start() {
40
+ return this._start;
41
+ }
42
+ /**
43
+ * The above function sets the value of the "start" property.
44
+ * @param {number} value - The value parameter is of type number.
45
+ */
46
+ set start(value) {
47
+ this._start = value;
48
+ }
49
+ /**
50
+ * The function returns the value of the protected variable `_end`.
51
+ * @returns The value of the protected property `_end`.
52
+ */
53
+ get end() {
54
+ return this._end;
55
+ }
56
+ /**
57
+ * The above function sets the value of the "end" property.
58
+ * @param {number} value - The value parameter is a number that represents the new value for the end
59
+ * property.
60
+ */
61
+ set end(value) {
62
+ this._end = value;
63
+ }
64
+ /**
65
+ * The function returns the value of a segment tree node.
66
+ * @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
67
+ */
68
+ get value() {
69
+ return this._value;
70
+ }
71
+ /**
72
+ * The function sets the value of a segment tree node.
73
+ * @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
74
+ * `SegmentTreeNodeVal` or `undefined`.
75
+ */
76
+ set value(value) {
77
+ this._value = value;
78
+ }
79
+ /**
80
+ * The function returns the value of the sum property.
81
+ * @returns The method is returning the value of the variable `_sum`.
82
+ */
83
+ get sum() {
84
+ return this._sum;
85
+ }
86
+ /**
87
+ * The above function sets the value of the sum property.
88
+ * @param {number} value - The parameter "value" is of type "number".
89
+ */
90
+ set sum(value) {
91
+ this._sum = value;
92
+ }
93
+ /**
94
+ * The function returns the left child of a segment tree node.
95
+ * @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
96
+ * `SegmentTreeNode` or `undefined`.
97
+ */
98
+ get left() {
99
+ return this._left;
100
+ }
101
+ /**
102
+ * The function sets the value of the left property of a SegmentTreeNode object.
103
+ * @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
104
+ * undefined.
105
+ */
106
+ set left(value) {
107
+ this._left = value;
108
+ }
109
+ /**
110
+ * The function returns the right child of a segment tree node.
111
+ * @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
112
+ */
113
+ get right() {
114
+ return this._right;
115
+ }
116
+ /**
117
+ * The function sets the right child of a segment tree node.
118
+ * @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
119
+ * undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
120
+ * value.
121
+ */
122
+ set right(value) {
123
+ this._right = value;
23
124
  }
24
125
  }
25
126
  exports.SegmentTreeNode = SegmentTreeNode;
@@ -49,15 +150,31 @@ class SegmentTree {
49
150
  this._values = [];
50
151
  }
51
152
  }
153
+ /**
154
+ * The function returns an array of numbers.
155
+ * @returns An array of numbers is being returned.
156
+ */
52
157
  get values() {
53
158
  return this._values;
54
159
  }
160
+ /**
161
+ * The function returns the value of the protected variable _start.
162
+ * @returns The start value, which is of type number.
163
+ */
55
164
  get start() {
56
165
  return this._start;
57
166
  }
167
+ /**
168
+ * The function returns the value of the protected variable `_end`.
169
+ * @returns The value of the protected property `_end`.
170
+ */
58
171
  get end() {
59
172
  return this._end;
60
173
  }
174
+ /**
175
+ * The function returns the root node of a segment tree.
176
+ * @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
177
+ */
61
178
  get root() {
62
179
  return this._root;
63
180
  }
@@ -9,8 +9,7 @@ import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEnt
9
9
  import { IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
- export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
13
- count: number;
12
+ export declare class TreeMultimapNode<K = any, V = any, NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
14
13
  /**
15
14
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
16
15
  * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
@@ -22,48 +21,73 @@ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNo
22
21
  * parameter when creating a new instance of the `BinaryTreeNode` class.
23
22
  */
24
23
  constructor(key: K, value?: V, count?: number);
24
+ protected _count: number;
25
+ /**
26
+ * The function returns the value of the protected variable _count.
27
+ * @returns The count property of the object, which is of type number.
28
+ */
29
+ get count(): number;
30
+ /**
31
+ * The above function sets the value of the count property.
32
+ * @param {number} value - The value parameter is of type number, which means it can accept any
33
+ * numeric value.
34
+ */
35
+ set count(value: number);
25
36
  }
26
37
  /**
27
38
  * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
28
39
  */
29
- export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
30
- constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: TreeMultimapOptions<K>);
31
- private _count;
40
+ export declare class TreeMultimap<K = any, V = any, NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, NODE, TREE> = TreeMultimap<K, V, NODE, TreeMultimapNested<K, V, NODE>>> extends AVLTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
41
+ constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: TreeMultimapOptions<K>);
42
+ protected _count: number;
43
+ /**
44
+ * The function calculates the sum of the count property of all nodes in a tree using depth-first
45
+ * search.
46
+ * @returns the sum of the count property of all nodes in the tree.
47
+ */
32
48
  get count(): number;
33
49
  /**
34
50
  * The function creates a new BSTNode with the given key, value, and count.
35
51
  * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
36
52
  * distinguish one node from another in the tree.
37
- * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
53
+ * @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
38
54
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
39
55
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
40
56
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
41
57
  */
42
- createNode(key: K, value?: V, count?: number): N;
58
+ createNode(key: K, value?: V, count?: number): NODE;
59
+ /**
60
+ * The function creates a new TreeMultimap object with the specified options and returns it.
61
+ * @param [options] - The `options` parameter is an optional object that contains additional
62
+ * configuration options for creating the `TreeMultimap` object. It can include properties such as
63
+ * `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
64
+ * the tree, respectively. These properties can be
65
+ * @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
66
+ * default options. The returned value is casted as `TREE`.
67
+ */
43
68
  createTree(options?: TreeMultimapOptions<K>): TREE;
44
69
  /**
45
- * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
46
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
70
+ * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
71
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
47
72
  * can be one of the following:
48
73
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
49
74
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
50
75
  * it defaults to `undefined`.
51
76
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
52
77
  * times the value should be added to the node. If not provided, it defaults to 1.
53
- * @returns a node of type `N` or `undefined`.
78
+ * @returns a node of type `NODE` or `undefined`.
54
79
  */
55
- exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): N | undefined;
80
+ keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): NODE | undefined;
56
81
  /**
57
82
  * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
58
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
83
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
59
84
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
60
85
  * class.
61
86
  */
62
- isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
87
+ isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
63
88
  /**
64
89
  * Time Complexity: O(log n)
65
90
  * Space Complexity: O(1)
66
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
67
91
  */
68
92
  /**
69
93
  * Time Complexity: O(log n)
@@ -81,47 +105,13 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
81
105
  * @returns The method is returning either the newly inserted node or `undefined` if the insertion
82
106
  * was not successful.
83
107
  */
84
- add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
85
- /**
86
- * Time Complexity: O(k log n)
87
- * Space Complexity: O(1)
88
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
89
- */
90
- /**
91
- * Time Complexity: O(k log n)
92
- * Space Complexity: O(1)
93
- *
94
- * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
95
- * structure.
96
- * @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
97
- * either keys, nodes, or entries.
98
- * @returns The method is returning an array of type `N | undefined`.
99
- */
100
- addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[];
101
- /**
102
- * Time Complexity: O(n log n)
103
- * Space Complexity: O(n)
104
- * logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
105
- */
106
- /**
107
- * Time Complexity: O(n log n)
108
- * Space Complexity: O(n)
109
- *
110
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
111
- * tree using either a recursive or iterative approach.
112
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
113
- * type of iteration to use when building the balanced binary search tree. It can have two possible
114
- * values:
115
- * @returns a boolean value.
116
- */
117
- perfectlyBalance(iterationType?: IterationType): boolean;
108
+ add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
118
109
  /**
119
- * Time Complexity: O(k log n)
110
+ * Time Complexity: O(log n)
120
111
  * Space Complexity: O(1)
121
- * logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
122
112
  */
123
113
  /**
124
- * Time Complexity: O(k log n)
114
+ * Time Complexity: O(log n)
125
115
  * Space Complexity: O(1)
126
116
  *
127
117
  * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
@@ -137,9 +127,9 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
137
127
  * being deleted. If set to true, the count of the node will not be considered and the node will be
138
128
  * deleted regardless of its count. If set to false (default), the count of the node will be
139
129
  * decremented by 1 and
140
- * @returns an array of `BinaryTreeDeleteResult<N>`.
130
+ * @returns an array of `BinaryTreeDeleteResult<NODE>`.
141
131
  */
142
- delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<N>[];
132
+ delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[];
143
133
  /**
144
134
  * Time Complexity: O(1)
145
135
  * Space Complexity: O(1)
@@ -151,6 +141,22 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
151
141
  * The clear() function clears the contents of a data structure and sets the count to zero.
152
142
  */
153
143
  clear(): void;
144
+ /**
145
+ * Time Complexity: O(n log n)
146
+ * Space Complexity: O(log n)
147
+ */
148
+ /**
149
+ * Time Complexity: O(n log n)
150
+ * Space Complexity: O(log n)
151
+ *
152
+ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
153
+ * tree using either a recursive or iterative approach.
154
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
155
+ * type of iteration to use when building the balanced binary search tree. It can have two possible
156
+ * values:
157
+ * @returns a boolean value.
158
+ */
159
+ perfectlyBalance(iterationType?: IterationType): boolean;
154
160
  /**
155
161
  * Time complexity: O(n)
156
162
  * Space complexity: O(n)
@@ -165,13 +171,21 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
165
171
  clone(): TREE;
166
172
  /**
167
173
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
168
- * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
169
- * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
170
- * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
174
+ * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
175
+ * which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
176
+ * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
171
177
  * node where the values from the source node will be swapped to.
172
178
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
173
179
  * if either `srcNode` or `destNode` is undefined.
174
180
  */
175
- protected _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined;
176
- protected _replaceNode(oldNode: N, newNode: N): N;
181
+ protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined;
182
+ /**
183
+ * The function replaces an old node with a new node and updates the count property of the new node.
184
+ * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
185
+ * needs to be replaced in a data structure.
186
+ * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
187
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
188
+ * superclass, after updating the `count` property of the `newNode` object.
189
+ */
190
+ protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
177
191
  }
@@ -16,8 +16,24 @@ class TreeMultimapNode extends avl_tree_1.AVLTreeNode {
16
16
  */
17
17
  constructor(key, value, count = 1) {
18
18
  super(key, value);
19
+ this._count = 1;
19
20
  this.count = count;
20
21
  }
22
+ /**
23
+ * The function returns the value of the protected variable _count.
24
+ * @returns The count property of the object, which is of type number.
25
+ */
26
+ get count() {
27
+ return this._count;
28
+ }
29
+ /**
30
+ * The above function sets the value of the count property.
31
+ * @param {number} value - The value parameter is of type number, which means it can accept any
32
+ * numeric value.
33
+ */
34
+ set count(value) {
35
+ this._count = value;
36
+ }
21
37
  }
22
38
  exports.TreeMultimapNode = TreeMultimapNode;
23
39
  /**
@@ -31,6 +47,11 @@ class TreeMultimap extends avl_tree_1.AVLTree {
31
47
  this.addMany(keysOrNodesOrEntries);
32
48
  }
33
49
  // TODO the _count is not accurate after nodes count modified
50
+ /**
51
+ * The function calculates the sum of the count property of all nodes in a tree using depth-first
52
+ * search.
53
+ * @returns the sum of the count property of all nodes in the tree.
54
+ */
34
55
  get count() {
35
56
  let sum = 0;
36
57
  this.dfs(node => (sum += node.count));
@@ -40,7 +61,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
40
61
  * The function creates a new BSTNode with the given key, value, and count.
41
62
  * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
42
63
  * distinguish one node from another in the tree.
43
- * @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
64
+ * @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
44
65
  * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
45
66
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
46
67
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
@@ -48,21 +69,30 @@ class TreeMultimap extends avl_tree_1.AVLTree {
48
69
  createNode(key, value, count) {
49
70
  return new TreeMultimapNode(key, value, count);
50
71
  }
72
+ /**
73
+ * The function creates a new TreeMultimap object with the specified options and returns it.
74
+ * @param [options] - The `options` parameter is an optional object that contains additional
75
+ * configuration options for creating the `TreeMultimap` object. It can include properties such as
76
+ * `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
77
+ * the tree, respectively. These properties can be
78
+ * @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
79
+ * default options. The returned value is casted as `TREE`.
80
+ */
51
81
  createTree(options) {
52
82
  return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
53
83
  }
54
84
  /**
55
- * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
56
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
85
+ * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
86
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
57
87
  * can be one of the following:
58
88
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
59
89
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
60
90
  * it defaults to `undefined`.
61
91
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
62
92
  * times the value should be added to the node. If not provided, it defaults to 1.
63
- * @returns a node of type `N` or `undefined`.
93
+ * @returns a node of type `NODE` or `undefined`.
64
94
  */
65
- exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
95
+ keyValueOrEntryToNode(keyOrNodeOrEntry, value, count = 1) {
66
96
  let node;
67
97
  if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
68
98
  return;
@@ -89,7 +119,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
89
119
  }
90
120
  /**
91
121
  * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
92
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
122
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
93
123
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
94
124
  * class.
95
125
  */
@@ -99,7 +129,6 @@ class TreeMultimap extends avl_tree_1.AVLTree {
99
129
  /**
100
130
  * Time Complexity: O(log n)
101
131
  * Space Complexity: O(1)
102
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
103
132
  */
104
133
  /**
105
134
  * Time Complexity: O(log n)
@@ -118,7 +147,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
118
147
  * was not successful.
119
148
  */
120
149
  add(keyOrNodeOrEntry, value, count = 1) {
121
- const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
150
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
122
151
  if (newNode === undefined)
123
152
  return false;
124
153
  const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
@@ -129,82 +158,11 @@ class TreeMultimap extends avl_tree_1.AVLTree {
129
158
  return true;
130
159
  }
131
160
  /**
132
- * Time Complexity: O(k log n)
133
- * Space Complexity: O(1)
134
- * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
135
- */
136
- /**
137
- * Time Complexity: O(k log n)
138
- * Space Complexity: O(1)
139
- *
140
- * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
141
- * structure.
142
- * @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
143
- * either keys, nodes, or entries.
144
- * @returns The method is returning an array of type `N | undefined`.
145
- */
146
- addMany(keysOrNodesOrEntries) {
147
- return super.addMany(keysOrNodesOrEntries);
148
- }
149
- /**
150
- * Time Complexity: O(n log n)
151
- * Space Complexity: O(n)
152
- * logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
153
- */
154
- /**
155
- * Time Complexity: O(n log n)
156
- * Space Complexity: O(n)
157
- *
158
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
159
- * tree using either a recursive or iterative approach.
160
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
161
- * type of iteration to use when building the balanced binary search tree. It can have two possible
162
- * values:
163
- * @returns a boolean value.
164
- */
165
- perfectlyBalance(iterationType = this.iterationType) {
166
- const sorted = this.dfs(node => node, 'in'), n = sorted.length;
167
- if (sorted.length < 1)
168
- return false;
169
- this.clear();
170
- if (iterationType === types_1.IterationType.RECURSIVE) {
171
- const buildBalanceBST = (l, r) => {
172
- if (l > r)
173
- return;
174
- const m = l + Math.floor((r - l) / 2);
175
- const midNode = sorted[m];
176
- this.add(midNode.key, midNode.value, midNode.count);
177
- buildBalanceBST(l, m - 1);
178
- buildBalanceBST(m + 1, r);
179
- };
180
- buildBalanceBST(0, n - 1);
181
- return true;
182
- }
183
- else {
184
- const stack = [[0, n - 1]];
185
- while (stack.length > 0) {
186
- const popped = stack.pop();
187
- if (popped) {
188
- const [l, r] = popped;
189
- if (l <= r) {
190
- const m = l + Math.floor((r - l) / 2);
191
- const midNode = sorted[m];
192
- this.add(midNode.key, midNode.value, midNode.count);
193
- stack.push([m + 1, r]);
194
- stack.push([l, m - 1]);
195
- }
196
- }
197
- }
198
- return true;
199
- }
200
- }
201
- /**
202
- * Time Complexity: O(k log n)
161
+ * Time Complexity: O(log n)
203
162
  * Space Complexity: O(1)
204
- * logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
205
163
  */
206
164
  /**
207
- * Time Complexity: O(k log n)
165
+ * Time Complexity: O(log n)
208
166
  * Space Complexity: O(1)
209
167
  *
210
168
  * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
@@ -220,7 +178,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
220
178
  * being deleted. If set to true, the count of the node will not be considered and the node will be
221
179
  * deleted regardless of its count. If set to false (default), the count of the node will be
222
180
  * decremented by 1 and
223
- * @returns an array of `BinaryTreeDeleteResult<N>`.
181
+ * @returns an array of `BinaryTreeDeleteResult<NODE>`.
224
182
  */
225
183
  delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
226
184
  var _a;
@@ -294,6 +252,57 @@ class TreeMultimap extends avl_tree_1.AVLTree {
294
252
  super.clear();
295
253
  this._count = 0;
296
254
  }
255
+ /**
256
+ * Time Complexity: O(n log n)
257
+ * Space Complexity: O(log n)
258
+ */
259
+ /**
260
+ * Time Complexity: O(n log n)
261
+ * Space Complexity: O(log n)
262
+ *
263
+ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
264
+ * tree using either a recursive or iterative approach.
265
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
266
+ * type of iteration to use when building the balanced binary search tree. It can have two possible
267
+ * values:
268
+ * @returns a boolean value.
269
+ */
270
+ perfectlyBalance(iterationType = this.iterationType) {
271
+ const sorted = this.dfs(node => node, 'in'), n = sorted.length;
272
+ if (sorted.length < 1)
273
+ return false;
274
+ this.clear();
275
+ if (iterationType === types_1.IterationType.RECURSIVE) {
276
+ const buildBalanceBST = (l, r) => {
277
+ if (l > r)
278
+ return;
279
+ const m = l + Math.floor((r - l) / 2);
280
+ const midNode = sorted[m];
281
+ this.add(midNode.key, midNode.value, midNode.count);
282
+ buildBalanceBST(l, m - 1);
283
+ buildBalanceBST(m + 1, r);
284
+ };
285
+ buildBalanceBST(0, n - 1);
286
+ return true;
287
+ }
288
+ else {
289
+ const stack = [[0, n - 1]];
290
+ while (stack.length > 0) {
291
+ const popped = stack.pop();
292
+ if (popped) {
293
+ const [l, r] = popped;
294
+ if (l <= r) {
295
+ const m = l + Math.floor((r - l) / 2);
296
+ const midNode = sorted[m];
297
+ this.add(midNode.key, midNode.value, midNode.count);
298
+ stack.push([m + 1, r]);
299
+ stack.push([l, m - 1]);
300
+ }
301
+ }
302
+ }
303
+ return true;
304
+ }
305
+ }
297
306
  /**
298
307
  * Time complexity: O(n)
299
308
  * Space complexity: O(n)
@@ -312,9 +321,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
312
321
  }
313
322
  /**
314
323
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
315
- * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
316
- * which the values will be swapped. It can be of type `K`, `N`, or `undefined`.
317
- * @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
324
+ * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
325
+ * which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
326
+ * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
318
327
  * node where the values from the source node will be swapped to.
319
328
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
320
329
  * if either `srcNode` or `destNode` is undefined.
@@ -340,6 +349,14 @@ class TreeMultimap extends avl_tree_1.AVLTree {
340
349
  }
341
350
  return undefined;
342
351
  }
352
+ /**
353
+ * The function replaces an old node with a new node and updates the count property of the new node.
354
+ * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
355
+ * needs to be replaced in a data structure.
356
+ * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
357
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
358
+ * superclass, after updating the `count` property of the `newNode` object.
359
+ */
343
360
  _replaceNode(oldNode, newNode) {
344
361
  newNode.count = oldNode.count + newNode.count;
345
362
  return super._replaceNode(oldNode, newNode);