red-black-tree-typed 1.53.7 → 1.54.2

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 (119) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +52 -0
  3. package/dist/common/index.js +5 -0
  4. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  5. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  6. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -328
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
  10. package/dist/data-structures/binary-tree/avl-tree.js +130 -70
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +268 -202
  14. package/dist/data-structures/binary-tree/binary-tree.js +311 -263
  15. package/dist/data-structures/binary-tree/bst.d.ts +193 -139
  16. package/dist/data-structures/binary-tree/bst.js +248 -164
  17. package/dist/data-structures/binary-tree/index.d.ts +3 -1
  18. package/dist/data-structures/binary-tree/index.js +3 -1
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
  20. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +176 -107
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  22. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
  25. package/dist/data-structures/graph/abstract-graph.js +2 -2
  26. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  27. package/dist/data-structures/graph/directed-graph.js +3 -0
  28. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  29. package/dist/data-structures/graph/map-graph.js +3 -0
  30. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  31. package/dist/data-structures/graph/undirected-graph.js +3 -0
  32. package/dist/data-structures/hash/hash-map.d.ts +31 -1
  33. package/dist/data-structures/hash/hash-map.js +35 -5
  34. package/dist/data-structures/heap/heap.d.ts +20 -3
  35. package/dist/data-structures/heap/heap.js +31 -11
  36. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
  37. package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
  38. package/dist/data-structures/linked-list/singly-linked-list.d.ts +47 -11
  39. package/dist/data-structures/linked-list/singly-linked-list.js +73 -26
  40. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  41. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  42. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  43. package/dist/data-structures/matrix/matrix.js +3 -0
  44. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  45. package/dist/data-structures/matrix/navigator.js +3 -0
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  50. package/dist/data-structures/queue/deque.d.ts +37 -8
  51. package/dist/data-structures/queue/deque.js +73 -29
  52. package/dist/data-structures/queue/queue.d.ts +41 -1
  53. package/dist/data-structures/queue/queue.js +51 -9
  54. package/dist/data-structures/stack/stack.d.ts +27 -10
  55. package/dist/data-structures/stack/stack.js +39 -20
  56. package/dist/data-structures/trie/trie.d.ts +8 -7
  57. package/dist/data-structures/trie/trie.js +8 -7
  58. package/dist/index.d.ts +4 -4
  59. package/dist/index.js +4 -4
  60. package/dist/interfaces/binary-tree.d.ts +8 -8
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  64. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  65. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
  66. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
  67. package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
  68. package/dist/types/data-structures/binary-tree/index.js +3 -1
  69. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  70. package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
  71. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  72. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  73. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
  74. package/package.json +3 -3
  75. package/src/common/index.ts +7 -1
  76. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  77. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  78. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +151 -370
  79. package/src/data-structures/binary-tree/avl-tree.ts +162 -105
  80. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  81. package/src/data-structures/binary-tree/binary-tree.ts +488 -416
  82. package/src/data-structures/binary-tree/bst.ts +326 -251
  83. package/src/data-structures/binary-tree/index.ts +3 -1
  84. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +219 -145
  85. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  86. package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
  87. package/src/data-structures/graph/abstract-graph.ts +2 -2
  88. package/src/data-structures/graph/directed-graph.ts +3 -0
  89. package/src/data-structures/graph/map-graph.ts +3 -0
  90. package/src/data-structures/graph/undirected-graph.ts +3 -0
  91. package/src/data-structures/hash/hash-map.ts +37 -7
  92. package/src/data-structures/heap/heap.ts +33 -10
  93. package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
  94. package/src/data-structures/linked-list/singly-linked-list.ts +80 -27
  95. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  96. package/src/data-structures/matrix/matrix.ts +3 -0
  97. package/src/data-structures/matrix/navigator.ts +3 -0
  98. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  99. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  100. package/src/data-structures/queue/deque.ts +72 -28
  101. package/src/data-structures/queue/queue.ts +50 -7
  102. package/src/data-structures/stack/stack.ts +39 -20
  103. package/src/data-structures/trie/trie.ts +8 -7
  104. package/src/index.ts +4 -4
  105. package/src/interfaces/binary-tree.ts +10 -21
  106. package/src/types/data-structures/base/base.ts +1 -1
  107. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  108. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  109. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  110. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
  111. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  112. package/src/types/data-structures/binary-tree/index.ts +3 -1
  113. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  114. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  115. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
  116. package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -209
  117. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
  118. package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
  119. /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
@@ -4,381 +4,186 @@ exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0;
4
4
  const avl_tree_1 = require("./avl-tree");
5
5
  class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
6
6
  /**
7
- * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
8
- * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
9
- * of the binary tree node.
10
- * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
11
- * tree node. If no value is provided, it will be `undefined`.
12
- * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
13
- * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
14
- * parameter when creating a new instance of the `BinaryTreeNode` class.
15
- */
16
- constructor(key, value, count = 1) {
7
+ * This TypeScript constructor initializes an object with a key of type K and an array of values of
8
+ * type V.
9
+ * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
10
+ * data being stored in the data structure. It helps in quickly accessing or retrieving the
11
+ * associated value in the data structure.
12
+ * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
13
+ * type `V`.
14
+ */
15
+ constructor(key, value) {
17
16
  super(key, value);
18
- this._count = 1;
19
- this.count = count;
17
+ this.parent = undefined;
18
+ this._left = undefined;
19
+ this._right = undefined;
20
20
  }
21
- /**
22
- * The function returns the value of the protected variable _count.
23
- * @returns The count property of the object, which is of type number.
24
- */
25
- get count() {
26
- return this._count;
21
+ get left() {
22
+ return this._left;
27
23
  }
28
- /**
29
- * The above function sets the value of the count property.
30
- * @param {number} value - The value parameter is of type number, which means it can accept any
31
- * numeric value.
32
- */
33
- set count(value) {
34
- this._count = value;
24
+ set left(v) {
25
+ if (v) {
26
+ v.parent = this;
27
+ }
28
+ this._left = v;
29
+ }
30
+ get right() {
31
+ return this._right;
32
+ }
33
+ set right(v) {
34
+ if (v) {
35
+ v.parent = this;
36
+ }
37
+ this._right = v;
35
38
  }
36
39
  }
37
40
  exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
38
41
  /**
39
- * The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
42
+ *
40
43
  */
41
44
  class AVLTreeMultiMap extends avl_tree_1.AVLTree {
42
45
  /**
43
- * The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
44
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
45
- * iterable object that can contain either keys, nodes, entries, or raw elements.
46
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
47
- * behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
48
- * `compareValues` functions to define custom comparison logic for keys and values, respectively.
46
+ * The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
47
+ * and options.
48
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
49
+ * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
50
+ * AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
51
+ * the AVLTreeMulti
52
+ * @param [options] - The `options` parameter in the constructor is of type
53
+ * `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
54
+ * additional options for configuring the AVLTreeMultiMap instance.
49
55
  */
50
56
  constructor(keysNodesEntriesOrRaws = [], options) {
51
- super([], options);
52
- this._count = 0;
53
- if (keysNodesEntriesOrRaws)
57
+ super([], Object.assign(Object.assign({}, options), { isMapMode: true }));
58
+ if (keysNodesEntriesOrRaws) {
54
59
  this.addMany(keysNodesEntriesOrRaws);
60
+ }
55
61
  }
56
62
  /**
57
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
58
- * search.
59
- * @returns the sum of the count property of all nodes in the tree.
60
- */
61
- get count() {
62
- return this._count;
63
- }
64
- /**
65
- * Time Complexity: O(n)
63
+ * Time Complexity: O(1)
66
64
  * Space Complexity: O(1)
67
65
  *
68
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
69
- * search.
70
- * @returns the sum of the count property of all nodes in the tree.
71
- */
72
- getComputedCount() {
73
- let sum = 0;
74
- this.dfs(node => (sum += node.count));
75
- return sum;
76
- }
77
- /**
78
- * The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
79
- * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
80
- * which is a generic type that can be replaced with any specific type when using the function.
81
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
82
- * associated with the key in the node. It is of type `V`, which can be any data type.
83
- * @param {number} [count] - The `count` parameter represents the number of occurrences of a
84
- * key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
85
- * calling the `createNode` method. If provided, it specifies the initial count for the node.
86
- * @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
87
- */
88
- createNode(key, value, count) {
89
- return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count);
90
- }
91
- /**
92
- * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
93
- * @param [options] - The `options` parameter is an optional object that contains additional
94
- * configuration options for creating the AVLTreeMultiMap. It can have the following properties:
95
- * @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
96
- * object.
66
+ * The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
67
+ * specified options.
68
+ * @param [options] - The `options` parameter in the `createTree` function is of type
69
+ * `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
70
+ * `K`, `V[]`, and `R`. The function creates a new `AVL
71
+ * @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
72
+ * provided options.
97
73
  */
98
74
  createTree(options) {
99
- return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
75
+ return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
100
76
  }
101
77
  /**
102
- * The function checks if the input is an instance of AVLTreeMultiMapNode.
103
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
104
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
105
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
106
- * an instance of the `AVLTreeMultiMapNode` class.
107
- */
108
- isNode(keyNodeEntryOrRaw) {
109
- return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
110
- }
111
- /**
112
- * The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
113
- * a node object.
114
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
115
- * `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
116
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
117
- * `override` function. It represents the value associated with the key in the data structure. If no
118
- * value is provided, it will default to `undefined`.
119
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
120
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
121
- * @returns either a NODE object or undefined.
122
- */
123
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
124
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
125
- return [undefined, undefined];
126
- if (this.isNode(keyNodeEntryOrRaw))
127
- return [keyNodeEntryOrRaw, value];
128
- if (this.isEntry(keyNodeEntryOrRaw)) {
129
- const [key, entryValue] = keyNodeEntryOrRaw;
130
- if (key === undefined || key === null)
131
- return [undefined, undefined];
132
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
133
- return [this.createNode(key, finalValue, count), finalValue];
134
- }
135
- if (this.isRaw(keyNodeEntryOrRaw)) {
136
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
137
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
138
- if (this.isKey(key))
139
- return [this.createNode(key, finalValue, count), finalValue];
140
- }
141
- if (this.isKey(keyNodeEntryOrRaw))
142
- return [this.createNode(keyNodeEntryOrRaw, value, count), value];
143
- return [undefined, undefined];
144
- }
145
- /**
146
- * Time Complexity: O(log n)
78
+ * Time Complexity: O(1)
147
79
  * Space Complexity: O(1)
148
80
  *
149
- * The function overrides the add method of a TypeScript class to add a new node to a data structure
150
- * and update the count.
151
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
152
- * `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
153
- * can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node,
154
- * entry, or raw element
155
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
156
- * data structure. It is an optional parameter, so it can be omitted if not needed.
157
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
158
- * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
159
- * be added once. However, you can specify a different value for `count` if you want to add
160
- * @returns a boolean value.
81
+ * The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a
82
+ * specified key and an empty array of values.
83
+ * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
84
+ * that will be created in the AVLTreeMultiMap.
85
+ * @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an
86
+ * empty array.
161
87
  */
162
- add(keyNodeEntryOrRaw, value, count = 1) {
163
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
164
- if (newNode === undefined)
165
- return false;
166
- const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
167
- const inserted = super.add(newNode, newValue);
168
- if (inserted) {
169
- this._count += orgNodeCount;
170
- }
171
- return true;
88
+ createNode(key) {
89
+ return new AVLTreeMultiMapNode(key, []);
172
90
  }
173
91
  /**
174
92
  * Time Complexity: O(log n)
175
- * Space Complexity: O(1)
93
+ * Space Complexity: O(log n)
176
94
  *
177
- * The function overrides the delete method in a binary tree data structure, handling deletion of
178
- * nodes and maintaining balance in the tree.
179
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
180
- * parameter in the `delete` method is used to specify the condition for deleting a node from the
181
- * binary tree. It can be a key, node, or entry that determines which
182
- * node(s) should be deleted.
183
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
184
- * boolean flag that determines whether to ignore the count of the node being deleted. If
185
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
186
- * `ignoreCount` is set to
187
- * @returns The `delete` method overrides the default delete behavior in a binary tree data
188
- * structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The
189
- * method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
190
- * deleted node and whether balancing is needed in the tree.
191
- */
192
- delete(keyNodeEntryOrRaw, ignoreCount = false) {
193
- var _a;
194
- const deletedResult = [];
195
- if (!this.root)
196
- return deletedResult;
197
- const curr = (_a = this.getNode(keyNodeEntryOrRaw)) !== null && _a !== void 0 ? _a : undefined;
198
- if (!curr)
199
- return deletedResult;
200
- const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : undefined;
201
- let needBalanced = undefined, orgCurrent = curr;
202
- if (curr.count > 1 && !ignoreCount) {
203
- curr.count--;
204
- this._count--;
205
- }
206
- else {
207
- if (!curr.left) {
208
- if (!parent) {
209
- if (curr.right !== undefined)
210
- this._setRoot(curr.right);
95
+ * The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL
96
+ * tree multi-map.
97
+ * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
98
+ * parameter in the `override add` method can be either a key-value pair entry or just a key. If it
99
+ * is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and
100
+ * `values`
101
+ * @param {V} [value] - The `value` parameter in the `override add` method represents the value that
102
+ * you want to add to the AVLTreeMultiMap. It can be a single value or an array of values associated
103
+ * with a specific key.
104
+ * @returns The `override add` method is returning a boolean value, which indicates whether the
105
+ * addition operation was successful or not.
106
+ */
107
+ add(keyNodeOrEntry, value) {
108
+ if (this.isRealNode(keyNodeOrEntry))
109
+ return super.add(keyNodeOrEntry);
110
+ const _commonAdd = (key, values) => {
111
+ if (key === undefined || key === null)
112
+ return false;
113
+ const existingValues = this.get(key);
114
+ if (existingValues !== undefined && values !== undefined) {
115
+ for (const value of values)
116
+ existingValues.push(value);
117
+ return true;
118
+ }
119
+ const existingNode = this.getNode(key);
120
+ if (this.isRealNode(existingNode)) {
121
+ if (existingValues === undefined) {
122
+ super.add(key, values);
123
+ return true;
124
+ }
125
+ if (values !== undefined) {
126
+ for (const value of values)
127
+ existingValues.push(value);
128
+ return true;
211
129
  }
212
130
  else {
213
- const { familyPosition: fp } = curr;
214
- if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
215
- parent.left = curr.right;
216
- }
217
- else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
218
- parent.right = curr.right;
219
- }
220
- needBalanced = parent;
131
+ return false;
221
132
  }
222
133
  }
223
134
  else {
224
- const leftSubTreeRightMost = curr.left ? this.getRightMost(node => node, curr.left) : undefined;
225
- if (leftSubTreeRightMost) {
226
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
227
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
228
- if (parentOfLeftSubTreeMax) {
229
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
230
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
231
- }
232
- else {
233
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
234
- }
235
- needBalanced = parentOfLeftSubTreeMax;
236
- }
237
- }
135
+ return super.add(key, values);
238
136
  }
239
- this._size = this._size - 1;
240
- // TODO How to handle when the count of target node is lesser than current node's count
241
- if (orgCurrent)
242
- this._count -= orgCurrent.count;
243
- }
244
- deletedResult.push({ deleted: orgCurrent, needBalanced });
245
- if (needBalanced) {
246
- this._balancePath(needBalanced);
137
+ };
138
+ if (this.isEntry(keyNodeOrEntry)) {
139
+ const [key, values] = keyNodeOrEntry;
140
+ return _commonAdd(key, value !== undefined ? [value] : values);
247
141
  }
248
- return deletedResult;
249
- }
250
- /**
251
- * Time Complexity: O(1)
252
- * Space Complexity: O(1)
253
- *
254
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
255
- * zero.
256
- */
257
- clear() {
258
- super.clear();
259
- this._count = 0;
142
+ return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
260
143
  }
261
144
  /**
262
- * Time Complexity: O(n log n)
145
+ * Time Complexity: O(log n)
263
146
  * Space Complexity: O(log n)
264
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
265
- * tree using either a recursive or iterative approach.
266
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
267
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
268
- * default value of `this.iterationType`, which means it will use the iteration type currently set in
269
- * the object.
270
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
271
- * balancing operation is successful, and `false` if there are no nodes to balance.
272
- */
273
- perfectlyBalance(iterationType = this.iterationType) {
274
- const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
275
- if (sorted.length < 1)
276
- return false;
277
- this.clear();
278
- if (iterationType === 'RECURSIVE') {
279
- const buildBalanceBST = (l, r) => {
280
- if (l > r)
281
- return;
282
- const m = l + Math.floor((r - l) / 2);
283
- const midNode = sorted[m];
284
- if (this._isMapMode)
285
- this.add(midNode.key, undefined, midNode.count);
286
- else
287
- this.add(midNode.key, midNode.value, midNode.count);
288
- buildBalanceBST(l, m - 1);
289
- buildBalanceBST(m + 1, r);
290
- };
291
- buildBalanceBST(0, n - 1);
292
- return true;
293
- }
294
- else {
295
- const stack = [[0, n - 1]];
296
- while (stack.length > 0) {
297
- const popped = stack.pop();
298
- if (popped) {
299
- const [l, r] = popped;
300
- if (l <= r) {
301
- const m = l + Math.floor((r - l) / 2);
302
- const midNode = sorted[m];
303
- if (this._isMapMode)
304
- this.add(midNode.key, undefined, midNode.count);
305
- else
306
- this.add(midNode.key, midNode.value, midNode.count);
307
- stack.push([m + 1, r]);
308
- stack.push([l, m - 1]);
309
- }
310
- }
311
- }
147
+ *
148
+ * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
149
+ * structure and deletes the entire node if no values are left for that key.
150
+ * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
151
+ * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
152
+ * pair in the AVLTreeMultiMapNode, or just the key itself.
153
+ * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
154
+ * value that you want to delete from the multi-map data structure associated with a particular key.
155
+ * The function checks if the value exists in the array of values associated with the key, and if
156
+ * found, removes it from the array.
157
+ * @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
158
+ * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
159
+ * the value was not found in the array, it returns `false`.
160
+ */
161
+ deleteValue(keyNodeOrEntry, value) {
162
+ const values = this.get(keyNodeOrEntry);
163
+ if (Array.isArray(values)) {
164
+ const index = values.indexOf(value);
165
+ if (index === -1)
166
+ return false;
167
+ values.splice(index, 1);
168
+ // If no values left, remove the entire node
169
+ if (values.length === 0)
170
+ this.delete(keyNodeOrEntry);
312
171
  return true;
313
172
  }
173
+ return false;
314
174
  }
315
175
  /**
316
- * Time complexity: O(n)
317
- * Space complexity: O(n)
176
+ * Time Complexity: O(n)
177
+ * Space Complexity: O(n)
318
178
  *
319
- * The function overrides the clone method to create a deep copy of a tree object.
320
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
179
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
180
+ * structure.
181
+ * @returns A cloned tree object is being returned.
321
182
  */
322
183
  clone() {
323
184
  const cloned = this.createTree();
324
- if (this._isMapMode)
325
- this.bfs(node => cloned.add(node.key, undefined, node.count));
326
- else
327
- this.bfs(node => cloned.add(node.key, node.value, node.count));
328
- if (this._isMapMode)
329
- cloned._store = this._store;
185
+ this._clone(cloned);
330
186
  return cloned;
331
187
  }
332
- /**
333
- * Time Complexity: O(1)
334
- * Space Complexity: O(1)
335
- *
336
- * The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
337
- * in a binary search tree.
338
- * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
339
- * that will be swapped with the `destNode`.
340
- * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
341
- * node where the properties will be swapped with the source node.
342
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
343
- * If either `srcNode` or `destNode` is undefined, it returns `undefined`.
344
- */
345
- _swapProperties(srcNode, destNode) {
346
- srcNode = this.ensureNode(srcNode);
347
- destNode = this.ensureNode(destNode);
348
- if (srcNode && destNode) {
349
- const { key, value, count, height } = destNode;
350
- const tempNode = this.createNode(key, value, count);
351
- if (tempNode) {
352
- tempNode.height = height;
353
- destNode.key = srcNode.key;
354
- if (!this._isMapMode)
355
- destNode.value = srcNode.value;
356
- destNode.count = srcNode.count;
357
- destNode.height = srcNode.height;
358
- srcNode.key = tempNode.key;
359
- if (!this._isMapMode)
360
- srcNode.value = tempNode.value;
361
- srcNode.count = tempNode.count;
362
- srcNode.height = tempNode.height;
363
- }
364
- return destNode;
365
- }
366
- return undefined;
367
- }
368
- /**
369
- * Time Complexity: O(1)
370
- * Space Complexity: O(1)
371
- *
372
- * The function replaces an old node with a new node and updates the count property of the new node.
373
- * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
374
- * data structure. It is of type NODE.
375
- * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
376
- * @returns The method is returning the result of calling the `_replaceNode` method from the
377
- * superclass, which is of type `NODE`.
378
- */
379
- _replaceNode(oldNode, newNode) {
380
- newNode.count = oldNode.count + newNode.count;
381
- return super._replaceNode(oldNode, newNode);
382
- }
383
188
  }
384
189
  exports.AVLTreeMultiMap = AVLTreeMultiMap;