red-black-tree-typed 1.53.6 → 1.54.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +52 -0
  2. package/dist/common/index.d.ts +12 -0
  3. package/dist/common/index.js +28 -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 -331
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
  10. package/dist/data-structures/binary-tree/avl-tree.js +131 -71
  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 +309 -208
  14. package/dist/data-structures/binary-tree/binary-tree.js +382 -300
  15. package/dist/data-structures/binary-tree/bst.d.ts +245 -127
  16. package/dist/data-structures/binary-tree/bst.js +366 -163
  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} +181 -108
  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 +26 -9
  35. package/dist/data-structures/heap/heap.js +37 -17
  36. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
  37. package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
  38. package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
  39. package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
  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 +111 -10
  57. package/dist/data-structures/trie/trie.js +123 -18
  58. package/dist/index.d.ts +2 -1
  59. package/dist/index.js +2 -1
  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-counter.js +2 -0
  64. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  65. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  66. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -4
  67. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
  68. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  69. package/dist/types/data-structures/binary-tree/index.js +2 -0
  70. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
  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/dist/types/utils/utils.d.ts +10 -6
  75. package/dist/utils/utils.js +4 -2
  76. package/package.json +2 -2
  77. package/src/common/index.ts +25 -0
  78. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  79. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  80. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +152 -373
  81. package/src/data-structures/binary-tree/avl-tree.ts +164 -106
  82. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  83. package/src/data-structures/binary-tree/binary-tree.ts +563 -447
  84. package/src/data-structures/binary-tree/bst.ts +433 -237
  85. package/src/data-structures/binary-tree/index.ts +3 -1
  86. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
  87. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  88. package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
  89. package/src/data-structures/graph/abstract-graph.ts +2 -2
  90. package/src/data-structures/graph/directed-graph.ts +3 -0
  91. package/src/data-structures/graph/map-graph.ts +3 -0
  92. package/src/data-structures/graph/undirected-graph.ts +3 -0
  93. package/src/data-structures/hash/hash-map.ts +37 -7
  94. package/src/data-structures/heap/heap.ts +72 -49
  95. package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
  96. package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
  97. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  98. package/src/data-structures/matrix/matrix.ts +3 -0
  99. package/src/data-structures/matrix/navigator.ts +3 -0
  100. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  101. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  102. package/src/data-structures/queue/deque.ts +72 -28
  103. package/src/data-structures/queue/queue.ts +50 -7
  104. package/src/data-structures/stack/stack.ts +39 -20
  105. package/src/data-structures/trie/trie.ts +123 -17
  106. package/src/index.ts +4 -3
  107. package/src/interfaces/binary-tree.ts +10 -21
  108. package/src/types/data-structures/base/base.ts +1 -1
  109. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  110. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  111. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  112. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -6
  113. package/src/types/data-structures/binary-tree/bst.ts +8 -7
  114. package/src/types/data-structures/binary-tree/index.ts +3 -1
  115. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  116. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  117. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
  118. package/src/types/utils/utils.ts +16 -10
  119. package/src/utils/utils.ts +4 -2
  120. package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
  121. package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
@@ -5,465 +5,223 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type {
9
- BinaryTreeDeleteResult,
10
- BSTNOptKeyOrNode,
11
- BTNRep,
12
- IterationType,
13
- OptNode,
14
- RBTNColor,
15
- TreeMultiMapNested,
16
- TreeMultiMapNodeNested,
17
- TreeMultiMapOptions
18
- } from '../../types';
8
+ import type { BTNOptKeyOrNull, BTNRep, OptNodeOrNull, TreeMultiMapOptions } from '../../types';
9
+ import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
19
10
  import { IBinaryTree } from '../../interfaces';
20
- import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
21
11
 
22
- export class TreeMultiMapNode<
23
- K = any,
24
- V = any,
25
- NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
26
- > extends RedBlackTreeNode<K, V, NODE> {
12
+ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
27
13
  /**
28
- * The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
29
- * @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
30
- * used to identify and locate the node within the tree.
31
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
32
- * associated with the key in the Red-Black Tree node. It is not required and can be omitted when
33
- * creating a new node.
34
- * @param [count=1] - The `count` parameter represents the number of occurrences of a particular key
35
- * in the Red-Black Tree. It is an optional parameter with a default value of 1.
36
- * @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
37
- * in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
14
+ * This TypeScript constructor initializes an object with a key of type K and an array of values of
15
+ * type V.
16
+ * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
17
+ * data being stored in the data structure. It helps in quickly accessing or retrieving the
18
+ * associated value in the data structure.
19
+ * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
20
+ * type `V`.
38
21
  */
39
- constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') {
40
- super(key, value, color);
41
- this.count = count;
22
+ constructor(key: K, value: V[]) {
23
+ super(key, value);
42
24
  }
43
25
 
44
- protected _count: number = 1;
26
+ override parent?: TreeMultiMapNode<K, V> = undefined;
45
27
 
46
- /**
47
- * The function returns the value of the private variable _count.
48
- * @returns The count property of the object, which is of type number.
49
- */
50
- get count(): number {
51
- return this._count;
52
- }
28
+ override _left?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined;
53
29
 
54
- /**
55
- * The above function sets the value of the count property.
56
- * @param {number} value - The value parameter is of type number, which means it can accept any
57
- * numeric value.
58
- */
59
- set count(value: number) {
60
- this._count = value;
30
+ override get left(): OptNodeOrNull<TreeMultiMapNode<K, V>> {
31
+ return this._left;
61
32
  }
62
- }
63
33
 
64
- export class TreeMultiMap<
65
- K = any,
66
- V = any,
67
- R = object,
68
- NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
69
- TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
70
- >
71
- extends RedBlackTree<K, V, R, NODE, TREE>
72
- implements IBinaryTree<K, V, R, NODE, TREE>
73
- {
74
- /**
75
- * The constructor function initializes a TreeMultiMap object with optional initial data.
76
- * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
77
- * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
78
- * TreeMultiMap with initial data.
79
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
80
- * behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
81
- * `compareValues`, which are functions used to compare keys and values respectively.
82
- */
83
- constructor(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE>> = [], options?: TreeMultiMapOptions<K, V, R>) {
84
- super([], options);
85
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
34
+ override set left(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) {
35
+ if (v) {
36
+ v.parent = this;
37
+ }
38
+ this._left = v;
86
39
  }
87
40
 
88
- protected _count = 0;
41
+ override _right?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined;
89
42
 
90
- // TODO the _count is not accurate after nodes count modified
91
- /**
92
- * The function calculates the sum of the count property of all nodes in a tree structure.
93
- * @returns the sum of the count property of all nodes in the tree.
94
- */
95
- get count(): number {
96
- return this._count;
43
+ override get right(): OptNodeOrNull<TreeMultiMapNode<K, V>> {
44
+ return this._right;
97
45
  }
98
46
 
99
- /**
100
- * Time Complexity: O(n)
101
- * Space Complexity: O(1)
102
- *
103
- * The function calculates the sum of the count property of all nodes in a tree using depth-first
104
- * search.
105
- * @returns the sum of the count property of all nodes in the tree.
106
- */
107
- getComputedCount(): number {
108
- let sum = 0;
109
- this.dfs(node => (sum += node.count));
110
- return sum;
47
+ override set right(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) {
48
+ if (v) {
49
+ v.parent = this;
50
+ }
51
+ this._right = v;
111
52
  }
53
+ }
112
54
 
55
+ /**
56
+ *
57
+ * @example
58
+ * // Find elements in a range
59
+ * const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]);
60
+ * console.log(tmm.search(new Range(5, 10))); // [5, 10, 7]
61
+ * console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7]
62
+ * console.log(tmm.search(new Range(15, 20))); // [15, 18]
63
+ */
64
+ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
65
+ extends RedBlackTree<K, V[], R, MK, MV[], MR>
66
+ implements IBinaryTree<K, V[], R, MK, MV, MR>
67
+ {
113
68
  /**
114
- * The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
115
- * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
116
- * which is a generic type representing the type of keys in the tree.
117
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
118
- * associated with the key in the node. It is of type `V`, which can be any data type.
119
- * @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
120
- * a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
121
- * @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
122
- * the tree. It is an optional parameter and is used to keep track of the number of values associated
123
- * with a key in the tree.
124
- * @returns A new instance of the TreeMultiMapNode class, casted as NODE.
125
- */
126
- override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE {
127
- return new TreeMultiMapNode(key, this._isMapMode ? undefined : value, count, color) as NODE;
69
+ * The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data
70
+ * and options.
71
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
72
+ * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
73
+ * TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
74
+ * the RedBlackTreeMulti
75
+ * @param [options] - The `options` parameter in the constructor is of type
76
+ * `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
77
+ * additional options for configuring the TreeMultiMap instance.
78
+ */
79
+ constructor(
80
+ keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], TreeMultiMapNode<K, V>> | R> = [],
81
+ options?: TreeMultiMapOptions<K, V[], R>
82
+ ) {
83
+ super([], { ...options, isMapMode: true });
84
+ if (keysNodesEntriesOrRaws) {
85
+ this.addMany(keysNodesEntriesOrRaws);
86
+ }
128
87
  }
129
88
 
130
89
  /**
131
- * The function creates a new instance of a TreeMultiMap with the specified options and returns it.
132
- * @param [options] - The `options` parameter is an optional object that contains additional
133
- * configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
134
- * R>`.
135
- * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
136
- * existing `iterationType` property. The returned value is casted as `TREE`.
137
- */
138
- override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
139
- return new TreeMultiMap<K, V, R, NODE, TREE>([], {
90
+ * Time Complexity: O(1)
91
+ * Space Complexity: O(1)
92
+ *
93
+ * The `createTree` function in TypeScript overrides the default implementation to create a new
94
+ * TreeMultiMap with specified options.
95
+ * @param [options] - The `options` parameter in the `createTree` method is of type
96
+ * `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration
97
+ * options when creating a new `TreeMultiMap` instance. It includes properties such as
98
+ * `iterationType`, `specifyComparable
99
+ * @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial
100
+ * data and the provided options merged with the existing properties of the current object.
101
+ */
102
+ override createTree(options?: TreeMultiMapOptions<K, V[], R>) {
103
+ return new TreeMultiMap<K, V, R, MK, MV, MR>([], {
140
104
  iterationType: this.iterationType,
141
- isMapMode: this._isMapMode,
142
- comparator: this._comparator,
105
+ specifyComparable: this._specifyComparable,
143
106
  toEntryFn: this._toEntryFn,
107
+ isReverse: this._isReverse,
144
108
  ...options
145
- }) as TREE;
146
- }
147
-
148
- /**
149
- * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
150
- * node based on the input.
151
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
152
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
153
- * @param {V} [value] - The `value` parameter is an optional value that represents the value
154
- * associated with the key in the node. It is used when creating a new node or updating the value of
155
- * an existing node.
156
- * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
157
- * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
158
- * @returns either a NODE object or undefined.
159
- */
160
- override keyValueNodeEntryRawToNodeAndValue(
161
- keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
162
- value?: V,
163
- count = 1
164
- ): [NODE | undefined, V | undefined] {
165
- if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
166
-
167
- if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
168
-
169
- if (this.isEntry(keyNodeEntryOrRaw)) {
170
- const [key, entryValue] = keyNodeEntryOrRaw;
171
- if (key === undefined || key === null) return [undefined, undefined];
172
- const finalValue = value ?? entryValue;
173
- if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
174
- }
175
-
176
- if (this._toEntryFn) {
177
- const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
178
- const finalValue = value ?? entryValue;
179
- if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
180
- }
181
-
182
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
183
-
184
- return [undefined, undefined];
109
+ });
185
110
  }
186
111
 
187
112
  /**
188
- * The function checks if the input is an instance of the TreeMultiMapNode class.
189
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
190
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
191
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
192
- * an instance of the `TreeMultiMapNode` class.
193
- */
194
- override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
195
- return keyNodeEntryOrRaw instanceof TreeMultiMapNode;
196
- }
197
-
198
- /**
199
- * Time Complexity: O(log n)
113
+ * Time Complexity: O(1)
200
114
  * Space Complexity: O(1)
201
115
  *
202
- * The function overrides the add method of a class and adds a new node to a data structure, updating
203
- * the count and returning a boolean indicating success.
204
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
205
- * `keyNodeEntryOrRaw` parameter can accept one of the following types:
206
- * @param {V} [value] - The `value` parameter represents the value associated with the key in the
207
- * data structure. It is an optional parameter, so it can be omitted if not needed.
208
- * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
209
- * be added to the data structure. By default, it is set to 1, meaning that if no value is provided
210
- * for `count`, the key-value pair will be added once.
211
- * @returns The method is returning a boolean value. It returns true if the addition of the new node
212
- * was successful, and false otherwise.
116
+ * The function `createNode` overrides the method to create a new `TreeMultiMapNode` with a specified
117
+ * key and an empty array of values.
118
+ * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
119
+ * that will be created in the TreeMultiMap data structure.
120
+ * @returns A new instance of `TreeMultiMapNode<K, V>` is being returned, with the specified key and
121
+ * an empty array as its value.
213
122
  */
214
- override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
215
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
216
- const orgCount = newNode?.count || 0;
217
- const isSuccessAdded = super.add(newNode, newValue);
218
-
219
- if (isSuccessAdded) {
220
- this._count += orgCount;
221
- return true;
222
- } else {
223
- return false;
224
- }
123
+ override createNode(key: K): TreeMultiMapNode<K, V> {
124
+ return new TreeMultiMapNode<K, V>(key, []);
225
125
  }
226
126
 
127
+ override add(node: BTNRep<K, V[], TreeMultiMapNode<K, V>>): boolean;
128
+
129
+ override add(key: K, value: V): boolean;
130
+
227
131
  /**
228
132
  * Time Complexity: O(log n)
229
- * Space Complexity: O(1)
133
+ * Space Complexity: O(log n)
230
134
  *
231
- * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
232
- * structure, handling cases where nodes have children and maintaining balance in the tree.
233
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
234
- * parameter in the `delete` method is used to specify the condition or key based on which a node
235
- * should be deleted from the binary tree. It can be a key, a node, or an entry.
236
- * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
237
- * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
238
- * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
239
- * `ignoreCount` is `false
240
- * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
241
- */
242
- override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount = false): BinaryTreeDeleteResult<NODE>[] {
243
- if (keyNodeEntryOrRaw === null) return [];
244
-
245
- const results: BinaryTreeDeleteResult<NODE>[] = [];
246
-
247
- let nodeToDelete: OptNode<NODE>;
248
- if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
249
- else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
250
-
251
- if (!nodeToDelete) {
252
- return results;
253
- }
254
-
255
- let originalColor = nodeToDelete.color;
256
- let replacementNode: NODE | undefined;
257
-
258
- if (!this.isRealNode(nodeToDelete.left)) {
259
- replacementNode = nodeToDelete.right;
260
- if (ignoreCount || nodeToDelete.count <= 1) {
261
- this._transplant(nodeToDelete, nodeToDelete.right);
262
- this._count -= nodeToDelete.count;
263
- } else {
264
- nodeToDelete.count--;
265
- this._count--;
266
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
267
- return results;
268
- }
269
- } else if (!this.isRealNode(nodeToDelete.right)) {
270
- replacementNode = nodeToDelete.left;
271
- if (ignoreCount || nodeToDelete.count <= 1) {
272
- this._transplant(nodeToDelete, nodeToDelete.left);
273
- this._count -= nodeToDelete.count;
274
- } else {
275
- nodeToDelete.count--;
276
- this._count--;
277
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
278
- return results;
135
+ * The function `add` in TypeScript overrides the superclass method to add key-value pairs to a
136
+ * TreeMultiMapNode, handling different input types and scenarios.
137
+ * @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
138
+ * parameter in the `override add` method can be either a `BTNRep` object containing a key, an array
139
+ * of values, and a `TreeMultiMapNode`, or just a key.
140
+ * @param {V} [value] - The `value` parameter in the `override add` method represents the value that
141
+ * you want to add to the TreeMultiMap. If the key is already present in the map, the new value will
142
+ * be added to the existing list of values associated with that key. If the key is not present,
143
+ * @returns The `add` method is returning a boolean value, which indicates whether the operation was
144
+ * successful or not.
145
+ */
146
+ override add(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value?: V): boolean {
147
+ if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
148
+
149
+ const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
150
+ if (key === undefined || key === null) return false;
151
+
152
+ const existingValues = this.get(key);
153
+ if (existingValues !== undefined && values !== undefined) {
154
+ for (const value of values) existingValues.push(value);
155
+ return true;
279
156
  }
280
- } else {
281
- const successor = this.getLeftMost(node => node, nodeToDelete.right);
282
- if (successor) {
283
- originalColor = successor.color;
284
- replacementNode = successor.right;
285
157
 
286
- if (successor.parent === nodeToDelete) {
287
- if (this.isRealNode(replacementNode)) {
288
- replacementNode.parent = successor;
289
- }
290
- } else {
291
- if (ignoreCount || nodeToDelete.count <= 1) {
292
- this._transplant(successor, successor.right);
293
- this._count -= nodeToDelete.count;
294
- } else {
295
- nodeToDelete.count--;
296
- this._count--;
297
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
298
- return results;
299
- }
300
- successor.right = nodeToDelete.right;
301
- if (this.isRealNode(successor.right)) {
302
- successor.right.parent = successor;
303
- }
158
+ const existingNode = this.getNode(key);
159
+ if (this.isRealNode(existingNode)) {
160
+ if (existingValues === undefined) {
161
+ super.add(key, values);
162
+ return true;
304
163
  }
305
- if (ignoreCount || nodeToDelete.count <= 1) {
306
- this._transplant(nodeToDelete, successor);
307
- this._count -= nodeToDelete.count;
164
+ if (values !== undefined) {
165
+ for (const value of values) existingValues.push(value);
166
+ return true;
308
167
  } else {
309
- nodeToDelete.count--;
310
- this._count--;
311
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
312
- return results;
313
- }
314
- successor.left = nodeToDelete.left;
315
- if (this.isRealNode(successor.left)) {
316
- successor.left.parent = successor;
168
+ return false;
317
169
  }
318
- successor.color = nodeToDelete.color;
170
+ } else {
171
+ return super.add(key, values);
319
172
  }
320
- }
321
- this._size--;
173
+ };
322
174
 
323
- // If the original color was black, fix the tree
324
- if (originalColor === 'BLACK') {
325
- this._deleteFixup(replacementNode);
175
+ if (this.isEntry(keyNodeOrEntry)) {
176
+ const [key, values] = keyNodeOrEntry;
177
+ return _commonAdd(key, value !== undefined ? [value] : values);
326
178
  }
327
179
 
328
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
329
-
330
- return results;
180
+ return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
331
181
  }
332
182
 
333
183
  /**
334
- * Time Complexity: O(1)
335
- * Space Complexity: O(1)
336
- *
337
- * The "clear" function overrides the parent class's "clear" function and also resets the count to
338
- * zero.
339
- */
340
- override clear() {
341
- super.clear();
342
- this._count = 0;
343
- }
344
-
345
- /**
346
- * Time Complexity: O(n log n)
184
+ * Time Complexity: O(log n)
347
185
  * Space Complexity: O(log n)
348
186
  *
349
- * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
350
- * tree using either a recursive or iterative approach.
351
- * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
352
- * specifies the type of iteration to use when building the balanced binary search tree. It has a
353
- * default value of `this.iterationType`, which means it will use the iteration type specified by the
354
- * `iterationType` property of the current object.
355
- * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
356
- * balancing operation is successful, and `false` if there are no nodes to balance.
357
- */
358
- override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
359
- const sorted = this.dfs(node => node, 'IN'),
360
- n = sorted.length;
361
- if (sorted.length < 1) return false;
362
-
363
- this.clear();
187
+ * The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
188
+ * and deletes the entire node if no values are left for that key.
189
+ * @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
190
+ * parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
191
+ * array of values, or just a key itself.
192
+ * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
193
+ * value that you want to remove from the multi-map data structure associated with a particular key.
194
+ * The function checks if the value exists in the array of values associated with the key, and if
195
+ * found, removes it from the array.
196
+ * @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
197
+ * successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
198
+ */
199
+ deleteValue(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value: V): boolean {
200
+ const values = this.get(keyNodeOrEntry);
201
+ if (Array.isArray(values)) {
202
+ const index = values.indexOf(value);
203
+ if (index === -1) return false;
204
+ values.splice(index, 1);
205
+
206
+ // If no values left, remove the entire node
207
+ if (values.length === 0) this.delete(keyNodeOrEntry);
364
208
 
365
- if (iterationType === 'RECURSIVE') {
366
- const buildBalanceBST = (l: number, r: number) => {
367
- if (l > r) return;
368
- const m = l + Math.floor((r - l) / 2);
369
- const midNode = sorted[m];
370
- if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
371
- else this.add(midNode.key, midNode.value, midNode.count);
372
- buildBalanceBST(l, m - 1);
373
- buildBalanceBST(m + 1, r);
374
- };
375
-
376
- buildBalanceBST(0, n - 1);
377
- return true;
378
- } else {
379
- const stack: [[number, number]] = [[0, n - 1]];
380
- while (stack.length > 0) {
381
- const popped = stack.pop();
382
- if (popped) {
383
- const [l, r] = popped;
384
- if (l <= r) {
385
- const m = l + Math.floor((r - l) / 2);
386
- const midNode = sorted[m];
387
- if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
388
- else this.add(midNode.key, midNode.value, midNode.count);
389
- stack.push([m + 1, r]);
390
- stack.push([l, m - 1]);
391
- }
392
- }
393
- }
394
209
  return true;
395
210
  }
211
+ return false;
396
212
  }
397
213
 
398
214
  /**
399
- * Time complexity: O(n)
400
- * Space complexity: O(n)
215
+ * Time Complexity: O(n)
216
+ * Space Complexity: O(n)
401
217
  *
402
- * The function overrides the clone method to create a deep copy of a tree object.
403
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
218
+ * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
219
+ * structure.
220
+ * @returns The `cloned` object is being returned.
404
221
  */
405
- override clone(): TREE {
222
+ override clone() {
406
223
  const cloned = this.createTree();
407
- this.bfs(node => cloned.add(node.key, undefined, node.count));
408
- if (this._isMapMode) cloned._store = this._store;
224
+ this._clone(cloned);
409
225
  return cloned;
410
226
  }
411
-
412
- /**
413
- * Time Complexity: O(1)
414
- * Space Complexity: O(1)
415
- *
416
- * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
417
- * in a binary search tree.
418
- * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
419
- * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
420
- * instance of the `BSTNOptKeyOrNode<K, NODE>` class.
421
- * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
422
- * node where the properties will be swapped with the source node.
423
- * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
424
- * If either `srcNode` or `destNode` is undefined, it returns undefined.
425
- */
426
- protected override _swapProperties(
427
- srcNode: R | BSTNOptKeyOrNode<K, NODE>,
428
- destNode: R | BSTNOptKeyOrNode<K, NODE>
429
- ): NODE | undefined {
430
- srcNode = this.ensureNode(srcNode);
431
- destNode = this.ensureNode(destNode);
432
- if (srcNode && destNode) {
433
- const { key, value, count, color } = destNode;
434
- const tempNode = this.createNode(key, value, color, count);
435
- if (tempNode) {
436
- tempNode.color = color;
437
-
438
- destNode.key = srcNode.key;
439
- if (!this._isMapMode) destNode.value = srcNode.value;
440
- destNode.count = srcNode.count;
441
- destNode.color = srcNode.color;
442
-
443
- srcNode.key = tempNode.key;
444
- if (!this._isMapMode) srcNode.value = tempNode.value;
445
- srcNode.count = tempNode.count;
446
- srcNode.color = tempNode.color;
447
- }
448
-
449
- return destNode;
450
- }
451
- return undefined;
452
- }
453
-
454
- /**
455
- * Time Complexity: O(1)
456
- * Space Complexity: O(1)
457
- *
458
- * The function replaces an old node with a new node and updates the count property of the new node.
459
- * @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
460
- * structure.
461
- * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
462
- * @returns The method is returning the result of calling the `_replaceNode` method from the
463
- * superclass, which is of type `NODE`.
464
- */
465
- protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
466
- newNode.count = oldNode.count + newNode.count;
467
- return super._replaceNode(oldNode, newNode);
468
- }
469
227
  }
@@ -947,7 +947,7 @@ export abstract class AbstractGraph<
947
947
  const filtered: [VertexKey, V | undefined][] = [];
948
948
  let index = 0;
949
949
  for (const [key, value] of this) {
950
- if (predicate.call(thisArg, value, key, index, this)) {
950
+ if (predicate.call(thisArg, key, value, index, this)) {
951
951
  filtered.push([key, value]);
952
952
  }
953
953
  index++;
@@ -972,7 +972,7 @@ export abstract class AbstractGraph<
972
972
  const mapped: T[] = [];
973
973
  let index = 0;
974
974
  for (const [key, value] of this) {
975
- mapped.push(callback.call(thisArg, value, key, index, this));
975
+ mapped.push(callback.call(thisArg, key, value, index, this));
976
976
  index++;
977
977
  }
978
978
  return mapped;