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
@@ -1,416 +1,194 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
4
- const rb_tree_1 = require("./rb-tree");
5
- class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
6
- /**
7
- * The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
8
- * @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
9
- * used to identify and locate the node within the tree.
10
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
11
- * associated with the key in the Red-Black Tree node. It is not required and can be omitted when
12
- * creating a new node.
13
- * @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
14
- * in the Red-Black Tree. It is an optional parameter with a default value of 1.
15
- * @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
16
- * in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
17
- */
18
- constructor(key, value, count = 1, color = 'BLACK') {
19
- super(key, value, color);
20
- this._count = 1;
21
- this.count = count;
4
+ const red_black_tree_1 = require("./red-black-tree");
5
+ class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
6
+ /**
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) {
16
+ super(key, value);
17
+ this.parent = undefined;
18
+ this._left = undefined;
19
+ this._right = undefined;
20
+ }
21
+ get left() {
22
+ return this._left;
23
+ }
24
+ set left(v) {
25
+ if (v) {
26
+ v.parent = this;
27
+ }
28
+ this._left = v;
22
29
  }
23
- /**
24
- * The function returns the value of the private variable _count.
25
- * @returns The count property of the object, which is of type number.
26
- */
27
- get count() {
28
- return this._count;
30
+ get right() {
31
+ return this._right;
29
32
  }
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) {
36
- this._count = value;
33
+ set right(v) {
34
+ if (v) {
35
+ v.parent = this;
36
+ }
37
+ this._right = v;
37
38
  }
38
39
  }
39
40
  exports.TreeMultiMapNode = TreeMultiMapNode;
40
- class TreeMultiMap extends rb_tree_1.RedBlackTree {
41
- /**
42
- * The constructor function initializes a TreeMultiMap object with optional initial data.
43
- * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
44
- * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
45
- * TreeMultiMap with initial data.
46
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
47
- * behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
48
- * `compareValues`, which are functions used to compare keys and values respectively.
41
+ /**
42
+ *
43
+ * @example
44
+ * // Find elements in a range
45
+ * const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]);
46
+ * console.log(tmm.search(new Range(5, 10))); // [5, 10, 7]
47
+ * console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7]
48
+ * console.log(tmm.search(new Range(15, 20))); // [15, 18]
49
+ */
50
+ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
51
+ /**
52
+ * The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data
53
+ * and options.
54
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
55
+ * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
56
+ * TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
57
+ * the RedBlackTreeMulti
58
+ * @param [options] - The `options` parameter in the constructor is of type
59
+ * `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
60
+ * additional options for configuring the TreeMultiMap instance.
49
61
  */
50
62
  constructor(keysNodesEntriesOrRaws = [], options) {
51
- super([], options);
52
- this._count = 0;
53
- if (keysNodesEntriesOrRaws)
63
+ super([], Object.assign(Object.assign({}, options), { isMapMode: true }));
64
+ if (keysNodesEntriesOrRaws) {
54
65
  this.addMany(keysNodesEntriesOrRaws);
55
- }
56
- // TODO the _count is not accurate after nodes count modified
57
- /**
58
- * The function calculates the sum of the count property of all nodes in a tree structure.
59
- * @returns the sum of the count property of all nodes in the tree.
60
- */
61
- get count() {
62
- return this._count;
66
+ }
63
67
  }
64
68
  /**
65
- * Time Complexity: O(n)
69
+ * Time Complexity: O(1)
66
70
  * Space Complexity: O(1)
67
71
  *
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 TreeMultiMapNode with the specified key, value, color, 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 representing the type of keys in the tree.
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 {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
84
- * a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
85
- * @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
86
- * the tree. It is an optional parameter and is used to keep track of the number of values associated
87
- * with a key in the tree.
88
- * @returns A new instance of the TreeMultiMapNode class, casted as NODE.
89
- */
90
- createNode(key, value, color = 'BLACK', count) {
91
- return new TreeMultiMapNode(key, this._isMapMode ? undefined : value, count, color);
92
- }
93
- /**
94
- * The function creates a new instance of a TreeMultiMap with the specified options and returns it.
95
- * @param [options] - The `options` parameter is an optional object that contains additional
96
- * configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
97
- * R>`.
98
- * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
99
- * existing `iterationType` property. The returned value is casted as `TREE`.
72
+ * The `createTree` function in TypeScript overrides the default implementation to create a new
73
+ * TreeMultiMap with specified options.
74
+ * @param [options] - The `options` parameter in the `createTree` method is of type
75
+ * `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration
76
+ * options when creating a new `TreeMultiMap` instance. It includes properties such as
77
+ * `iterationType`, `specifyComparable
78
+ * @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial
79
+ * data and the provided options merged with the existing properties of the current object.
100
80
  */
101
81
  createTree(options) {
102
- return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn }, options));
82
+ return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
103
83
  }
104
84
  /**
105
- * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
106
- * node based on the input.
107
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
108
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
109
- * @param {V} [value] - The `value` parameter is an optional value that represents the value
110
- * associated with the key in the node. It is used when creating a new node or updating the value of
111
- * an existing node.
112
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
113
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
114
- * @returns either a NODE object or undefined.
115
- */
116
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
117
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
118
- return [undefined, undefined];
119
- if (this.isNode(keyNodeEntryOrRaw))
120
- return [keyNodeEntryOrRaw, value];
121
- if (this.isEntry(keyNodeEntryOrRaw)) {
122
- const [key, entryValue] = keyNodeEntryOrRaw;
123
- if (key === undefined || key === null)
124
- return [undefined, undefined];
125
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
126
- if (this.isKey(key))
127
- return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
128
- }
129
- if (this.isRaw(keyNodeEntryOrRaw)) {
130
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
131
- const finalValue = value !== null && value !== void 0 ? value : entryValue;
132
- if (this.isKey(key))
133
- return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
134
- }
135
- if (this.isKey(keyNodeEntryOrRaw))
136
- return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
137
- return [undefined, undefined];
138
- }
139
- /**
140
- * The function checks if the input is an instance of the TreeMultiMapNode class.
141
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
142
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
143
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
144
- * an instance of the `TreeMultiMapNode` class.
145
- */
146
- isNode(keyNodeEntryOrRaw) {
147
- return keyNodeEntryOrRaw instanceof TreeMultiMapNode;
148
- }
149
- /**
150
- * Time Complexity: O(log n)
85
+ * Time Complexity: O(1)
151
86
  * Space Complexity: O(1)
152
87
  *
153
- * The function overrides the add method of a class and adds a new node to a data structure, updating
154
- * the count and returning a boolean indicating success.
155
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
156
- * `keyNodeEntryOrRaw` parameter can accept one of the following types:
157
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
158
- * data structure. It is an optional parameter, so it can be omitted if not needed.
159
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
160
- * be added to the data structure. By default, it is set to 1, meaning that if no value is provided
161
- * for `count`, the key-value pair will be added once.
162
- * @returns The method is returning a boolean value. It returns true if the addition of the new node
163
- * was successful, and false otherwise.
88
+ * The function `createNode` overrides the method to create a new `TreeMultiMapNode` with a specified
89
+ * key and an empty array of values.
90
+ * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
91
+ * that will be created in the TreeMultiMap data structure.
92
+ * @returns A new instance of `TreeMultiMapNode<K, V>` is being returned, with the specified key and
93
+ * an empty array as its value.
164
94
  */
165
- add(keyNodeEntryOrRaw, value, count = 1) {
166
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
167
- const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
168
- const isSuccessAdded = super.add(newNode, newValue);
169
- if (isSuccessAdded) {
170
- this._count += orgCount;
171
- return true;
172
- }
173
- else {
174
- return false;
175
- }
95
+ createNode(key) {
96
+ return new TreeMultiMapNode(key, []);
176
97
  }
177
98
  /**
178
99
  * Time Complexity: O(log n)
179
- * Space Complexity: O(1)
100
+ * Space Complexity: O(log n)
180
101
  *
181
- * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
182
- * structure, handling cases where nodes have children and maintaining balance in the tree.
183
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
184
- * parameter in the `delete` method is used to specify the condition or key based on which a node
185
- * should be deleted from the binary tree. It can be a key, a node, or an entry.
186
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
187
- * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
188
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
189
- * `ignoreCount` is `false
190
- * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
191
- */
192
- delete(keyNodeEntryOrRaw, ignoreCount = false) {
193
- if (keyNodeEntryOrRaw === null)
194
- return [];
195
- const results = [];
196
- let nodeToDelete;
197
- if (this._isPredicate(keyNodeEntryOrRaw))
198
- nodeToDelete = this.getNode(keyNodeEntryOrRaw);
199
- else
200
- nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
201
- if (!nodeToDelete) {
202
- return results;
203
- }
204
- let originalColor = nodeToDelete.color;
205
- let replacementNode;
206
- if (!this.isRealNode(nodeToDelete.left)) {
207
- replacementNode = nodeToDelete.right;
208
- if (ignoreCount || nodeToDelete.count <= 1) {
209
- this._transplant(nodeToDelete, nodeToDelete.right);
210
- this._count -= nodeToDelete.count;
211
- }
212
- else {
213
- nodeToDelete.count--;
214
- this._count--;
215
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
216
- return results;
217
- }
218
- }
219
- else if (!this.isRealNode(nodeToDelete.right)) {
220
- replacementNode = nodeToDelete.left;
221
- if (ignoreCount || nodeToDelete.count <= 1) {
222
- this._transplant(nodeToDelete, nodeToDelete.left);
223
- this._count -= nodeToDelete.count;
224
- }
225
- else {
226
- nodeToDelete.count--;
227
- this._count--;
228
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
229
- return results;
102
+ * The function `add` in TypeScript overrides the superclass method to add key-value pairs to a
103
+ * TreeMultiMapNode, handling different input types and scenarios.
104
+ * @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
105
+ * parameter in the `override add` method can be either a `BTNRep` object containing a key, an array
106
+ * of values, and a `TreeMultiMapNode`, or just a key.
107
+ * @param {V} [value] - The `value` parameter in the `override add` method represents the value that
108
+ * you want to add to the TreeMultiMap. If the key is already present in the map, the new value will
109
+ * be added to the existing list of values associated with that key. If the key is not present,
110
+ * @returns The `add` method is returning a boolean value, which indicates whether the operation was
111
+ * successful or not.
112
+ */
113
+ add(keyNodeOrEntry, value) {
114
+ if (this.isRealNode(keyNodeOrEntry))
115
+ return super.add(keyNodeOrEntry);
116
+ const _commonAdd = (key, values) => {
117
+ if (key === undefined || key === null)
118
+ return false;
119
+ const existingValues = this.get(key);
120
+ if (existingValues !== undefined && values !== undefined) {
121
+ for (const value of values)
122
+ existingValues.push(value);
123
+ return true;
230
124
  }
231
- }
232
- else {
233
- const successor = this.getLeftMost(node => node, nodeToDelete.right);
234
- if (successor) {
235
- originalColor = successor.color;
236
- replacementNode = successor.right;
237
- if (successor.parent === nodeToDelete) {
238
- if (this.isRealNode(replacementNode)) {
239
- replacementNode.parent = successor;
240
- }
125
+ const existingNode = this.getNode(key);
126
+ if (this.isRealNode(existingNode)) {
127
+ if (existingValues === undefined) {
128
+ super.add(key, values);
129
+ return true;
241
130
  }
242
- else {
243
- if (ignoreCount || nodeToDelete.count <= 1) {
244
- this._transplant(successor, successor.right);
245
- this._count -= nodeToDelete.count;
246
- }
247
- else {
248
- nodeToDelete.count--;
249
- this._count--;
250
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
251
- return results;
252
- }
253
- successor.right = nodeToDelete.right;
254
- if (this.isRealNode(successor.right)) {
255
- successor.right.parent = successor;
256
- }
257
- }
258
- if (ignoreCount || nodeToDelete.count <= 1) {
259
- this._transplant(nodeToDelete, successor);
260
- this._count -= nodeToDelete.count;
131
+ if (values !== undefined) {
132
+ for (const value of values)
133
+ existingValues.push(value);
134
+ return true;
261
135
  }
262
136
  else {
263
- nodeToDelete.count--;
264
- this._count--;
265
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
266
- return results;
267
- }
268
- successor.left = nodeToDelete.left;
269
- if (this.isRealNode(successor.left)) {
270
- successor.left.parent = successor;
137
+ return false;
271
138
  }
272
- successor.color = nodeToDelete.color;
273
139
  }
140
+ else {
141
+ return super.add(key, values);
142
+ }
143
+ };
144
+ if (this.isEntry(keyNodeOrEntry)) {
145
+ const [key, values] = keyNodeOrEntry;
146
+ return _commonAdd(key, value !== undefined ? [value] : values);
274
147
  }
275
- this._size--;
276
- // If the original color was black, fix the tree
277
- if (originalColor === 'BLACK') {
278
- this._deleteFixup(replacementNode);
279
- }
280
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
281
- return results;
282
- }
283
- /**
284
- * Time Complexity: O(1)
285
- * Space Complexity: O(1)
286
- *
287
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
288
- * zero.
289
- */
290
- clear() {
291
- super.clear();
292
- this._count = 0;
148
+ return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
293
149
  }
294
150
  /**
295
- * Time Complexity: O(n log n)
151
+ * Time Complexity: O(log n)
296
152
  * Space Complexity: O(log n)
297
153
  *
298
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
299
- * tree using either a recursive or iterative approach.
300
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
301
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
302
- * default value of `this.iterationType`, which means it will use the iteration type specified by the
303
- * `iterationType` property of the current object.
304
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
305
- * balancing operation is successful, and `false` if there are no nodes to balance.
306
- */
307
- perfectlyBalance(iterationType = this.iterationType) {
308
- const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
309
- if (sorted.length < 1)
310
- return false;
311
- this.clear();
312
- if (iterationType === 'RECURSIVE') {
313
- const buildBalanceBST = (l, r) => {
314
- if (l > r)
315
- return;
316
- const m = l + Math.floor((r - l) / 2);
317
- const midNode = sorted[m];
318
- if (this._isMapMode)
319
- this.add(midNode.key, undefined, midNode.count);
320
- else
321
- this.add(midNode.key, midNode.value, midNode.count);
322
- buildBalanceBST(l, m - 1);
323
- buildBalanceBST(m + 1, r);
324
- };
325
- buildBalanceBST(0, n - 1);
326
- return true;
327
- }
328
- else {
329
- const stack = [[0, n - 1]];
330
- while (stack.length > 0) {
331
- const popped = stack.pop();
332
- if (popped) {
333
- const [l, r] = popped;
334
- if (l <= r) {
335
- const m = l + Math.floor((r - l) / 2);
336
- const midNode = sorted[m];
337
- if (this._isMapMode)
338
- this.add(midNode.key, undefined, midNode.count);
339
- else
340
- this.add(midNode.key, midNode.value, midNode.count);
341
- stack.push([m + 1, r]);
342
- stack.push([l, m - 1]);
343
- }
344
- }
345
- }
154
+ * The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
155
+ * and deletes the entire node if no values are left for that key.
156
+ * @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
157
+ * parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
158
+ * array of values, or just a key itself.
159
+ * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
160
+ * value that you want to remove from the multi-map data structure associated with a particular key.
161
+ * The function checks if the value exists in the array of values associated with the key, and if
162
+ * found, removes it from the array.
163
+ * @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
164
+ * successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
165
+ */
166
+ deleteValue(keyNodeOrEntry, value) {
167
+ const values = this.get(keyNodeOrEntry);
168
+ if (Array.isArray(values)) {
169
+ const index = values.indexOf(value);
170
+ if (index === -1)
171
+ return false;
172
+ values.splice(index, 1);
173
+ // If no values left, remove the entire node
174
+ if (values.length === 0)
175
+ this.delete(keyNodeOrEntry);
346
176
  return true;
347
177
  }
178
+ return false;
348
179
  }
349
180
  /**
350
- * Time complexity: O(n)
351
- * Space complexity: O(n)
181
+ * Time Complexity: O(n)
182
+ * Space Complexity: O(n)
352
183
  *
353
- * The function overrides the clone method to create a deep copy of a tree object.
354
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
184
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
185
+ * structure.
186
+ * @returns The `cloned` object is being returned.
355
187
  */
356
188
  clone() {
357
189
  const cloned = this.createTree();
358
- this.bfs(node => cloned.add(node.key, undefined, node.count));
359
- if (this._isMapMode)
360
- cloned._store = this._store;
190
+ this._clone(cloned);
361
191
  return cloned;
362
192
  }
363
- /**
364
- * Time Complexity: O(1)
365
- * Space Complexity: O(1)
366
- *
367
- * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
368
- * in a binary search tree.
369
- * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
370
- * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
371
- * instance of the `BSTNOptKeyOrNode<K, NODE>` class.
372
- * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
373
- * node where the properties will be swapped with the source node.
374
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
375
- * If either `srcNode` or `destNode` is undefined, it returns undefined.
376
- */
377
- _swapProperties(srcNode, destNode) {
378
- srcNode = this.ensureNode(srcNode);
379
- destNode = this.ensureNode(destNode);
380
- if (srcNode && destNode) {
381
- const { key, value, count, color } = destNode;
382
- const tempNode = this.createNode(key, value, color, count);
383
- if (tempNode) {
384
- tempNode.color = color;
385
- destNode.key = srcNode.key;
386
- if (!this._isMapMode)
387
- destNode.value = srcNode.value;
388
- destNode.count = srcNode.count;
389
- destNode.color = srcNode.color;
390
- srcNode.key = tempNode.key;
391
- if (!this._isMapMode)
392
- srcNode.value = tempNode.value;
393
- srcNode.count = tempNode.count;
394
- srcNode.color = tempNode.color;
395
- }
396
- return destNode;
397
- }
398
- return undefined;
399
- }
400
- /**
401
- * Time Complexity: O(1)
402
- * Space Complexity: O(1)
403
- *
404
- * The function replaces an old node with a new node and updates the count property of the new node.
405
- * @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
406
- * structure.
407
- * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
408
- * @returns The method is returning the result of calling the `_replaceNode` method from the
409
- * superclass, which is of type `NODE`.
410
- */
411
- _replaceNode(oldNode, newNode) {
412
- newNode.count = oldNode.count + newNode.count;
413
- return super._replaceNode(oldNode, newNode);
414
- }
415
193
  }
416
194
  exports.TreeMultiMap = TreeMultiMap;
@@ -813,7 +813,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
813
813
  const filtered = [];
814
814
  let index = 0;
815
815
  for (const [key, value] of this) {
816
- if (predicate.call(thisArg, value, key, index, this)) {
816
+ if (predicate.call(thisArg, key, value, index, this)) {
817
817
  filtered.push([key, value]);
818
818
  }
819
819
  index++;
@@ -837,7 +837,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
837
837
  const mapped = [];
838
838
  let index = 0;
839
839
  for (const [key, value] of this) {
840
- mapped.push(callback.call(thisArg, value, key, index, this));
840
+ mapped.push(callback.call(thisArg, key, value, index, this));
841
841
  index++;
842
842
  }
843
843
  return mapped;
@@ -34,6 +34,9 @@ export declare class DirectedEdge<E = any> extends AbstractEdge<E> {
34
34
  */
35
35
  constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E);
36
36
  }
37
+ /**
38
+ *
39
+ */
37
40
  export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V> = DirectedVertex<V>, EO extends DirectedEdge<E> = DirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
38
41
  /**
39
42
  * The constructor function initializes an instance of a class.
@@ -35,6 +35,9 @@ class DirectedEdge extends abstract_graph_1.AbstractEdge {
35
35
  }
36
36
  }
37
37
  exports.DirectedEdge = DirectedEdge;
38
+ /**
39
+ *
40
+ */
38
41
  class DirectedGraph extends abstract_graph_1.AbstractGraph {
39
42
  /**
40
43
  * The constructor function initializes an instance of a class.
@@ -30,6 +30,9 @@ export declare class MapEdge<E = any> extends DirectedEdge<E> {
30
30
  */
31
31
  constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E);
32
32
  }
33
+ /**
34
+ *
35
+ */
33
36
  export declare class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVertex<V>, EO extends MapEdge<E> = MapEdge<E>> extends DirectedGraph<V, E, VO, EO> {
34
37
  /**
35
38
  * The constructor function initializes the originCoord and bottomRight properties of a MapGraphCoordinate object.
@@ -38,6 +38,9 @@ class MapEdge extends directed_graph_1.DirectedEdge {
38
38
  }
39
39
  }
40
40
  exports.MapEdge = MapEdge;
41
+ /**
42
+ *
43
+ */
41
44
  class MapGraph extends directed_graph_1.DirectedGraph {
42
45
  /**
43
46
  * The constructor function initializes the originCoord and bottomRight properties of a MapGraphCoordinate object.
@@ -32,6 +32,9 @@ export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
32
32
  */
33
33
  constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E);
34
34
  }
35
+ /**
36
+ *
37
+ */
35
38
  export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVertex<V> = UndirectedVertex<V>, EO extends UndirectedEdge<E> = UndirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
36
39
  /**
37
40
  * The constructor initializes a new Map object to store edgeMap.