tree-multimap-typed 2.2.2 → 2.2.4

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 (53) hide show
  1. package/dist/cjs/index.cjs +245 -72
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +246 -72
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +245 -72
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +246 -72
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  10. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
  17. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  18. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  19. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  20. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  21. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  22. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  23. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  24. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  25. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  26. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  27. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  28. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  29. package/dist/umd/tree-multimap-typed.js +246 -72
  30. package/dist/umd/tree-multimap-typed.js.map +1 -1
  31. package/dist/umd/tree-multimap-typed.min.js +3 -3
  32. package/dist/umd/tree-multimap-typed.min.js.map +1 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  35. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  36. package/src/data-structures/binary-tree/avl-tree.ts +100 -7
  37. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  38. package/src/data-structures/binary-tree/bst.ts +431 -93
  39. package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
  40. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
  42. package/src/data-structures/graph/directed-graph.ts +126 -1
  43. package/src/data-structures/graph/undirected-graph.ts +160 -1
  44. package/src/data-structures/hash/hash-map.ts +110 -27
  45. package/src/data-structures/heap/heap.ts +107 -58
  46. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  47. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  48. package/src/data-structures/queue/deque.ts +95 -67
  49. package/src/data-structures/queue/queue.ts +90 -34
  50. package/src/data-structures/stack/stack.ts +58 -40
  51. package/src/data-structures/trie/trie.ts +109 -47
  52. package/src/interfaces/binary-tree.ts +2 -0
  53. package/src/types/data-structures/binary-tree/bst.ts +5 -5
@@ -77,53 +77,99 @@ export declare class TrieNode {
77
77
  * 10. IP Routing: Used in certain types of IP routing algorithms.
78
78
  * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
79
79
  * @example
80
- * // Autocomplete: Prefix validation and checking
81
- * const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
80
+ * // basic Trie creation and add words
81
+ * // Create a simple Trie with initial words
82
+ * const trie = new Trie(['apple', 'app', 'apply']);
82
83
  *
83
- * // Get all completions for a prefix
84
- * const gmailCompletions = autocomplete.getWords('gmail');
85
- * console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
84
+ * // Verify size
85
+ * console.log(trie.size); // 3;
86
+ *
87
+ * // Check if words exist
88
+ * console.log(trie.has('apple')); // true;
89
+ * console.log(trie.has('app')); // true;
90
+ *
91
+ * // Add a new word
92
+ * trie.add('application');
93
+ * console.log(trie.size); // 4;
86
94
  * @example
87
- * // File System Path Operations
88
- * const fileSystem = new Trie<string>([
89
- * '/home/user/documents/file1.txt',
90
- * '/home/user/documents/file2.txt',
91
- * '/home/user/pictures/photo.jpg',
92
- * '/home/user/pictures/vacation/',
93
- * '/home/user/downloads'
94
- * ]);
95
+ * // Trie getWords and prefix search
96
+ * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
95
97
  *
96
- * // Find common directory prefix
97
- * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
98
+ * // Get all words with prefix 'app'
99
+ * const appWords = trie.getWords('app');
100
+ * console.log(appWords); // contains 'app';
101
+ * console.log(appWords); // contains 'apple';
102
+ * console.log(appWords); // contains 'apply';
103
+ * console.log(appWords); // contains 'application';
104
+ * expect(appWords).not.toContain('apricot');
105
+ * @example
106
+ * // Trie isPrefix and isAbsolutePrefix checks
107
+ * const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
98
108
  *
99
- * // List all files in a directory
100
- * const documentsFiles = fileSystem.getWords('/home/user/documents/');
101
- * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
109
+ * // Check if string is a prefix of any word
110
+ * console.log(trie.hasPrefix('tri')); // true;
111
+ * console.log(trie.hasPrefix('tr')); // true;
112
+ * console.log(trie.hasPrefix('xyz')); // false;
113
+ *
114
+ * // Check if string is an absolute prefix (not a complete word)
115
+ * console.log(trie.hasPurePrefix('tri')); // true;
116
+ * console.log(trie.hasPurePrefix('tree')); // false; // 'tree' is a complete word
117
+ *
118
+ * // Verify size
119
+ * console.log(trie.size); // 5;
102
120
  * @example
103
- * // Autocomplete: Basic word suggestions
104
- * // Create a trie for autocomplete
105
- * const autocomplete = new Trie<string>([
106
- * 'function',
107
- * 'functional',
108
- * 'functions',
109
- * 'class',
110
- * 'classes',
111
- * 'classical',
112
- * 'closure',
113
- * 'const',
114
- * 'constructor'
115
- * ]);
121
+ * // Trie delete and iteration
122
+ * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
123
+ *
124
+ * // Delete a word
125
+ * trie.delete('card');
126
+ * console.log(trie.has('card')); // false;
127
+ *
128
+ * // Word with same prefix still exists
129
+ * console.log(trie.has('care')); // true;
116
130
  *
117
- * // Test autocomplete with different prefixes
118
- * console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
119
- * console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
120
- * console.log(autocomplete.getWords('con')); // ['constructor', 'const']
131
+ * // Size decreased
132
+ * console.log(trie.size); // 5;
121
133
  *
122
- * // Test with non-matching prefix
123
- * console.log(autocomplete.getWords('xyz')); // []
134
+ * // Iterate through all words
135
+ * const allWords = [...trie];
136
+ * console.log(allWords.length); // 5;
137
+ * @example
138
+ * // Trie for autocomplete search index
139
+ * // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
140
+ * const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
141
+ *
142
+ * // User types 'j' - get all suggestions
143
+ * const jResults = searchIndex.getWords('j');
144
+ * console.log(jResults); // contains 'javascript';
145
+ * console.log(jResults); // contains 'java';
146
+ * console.log(jResults.length); // 2;
147
+ *
148
+ * // User types 'ja' - get more specific suggestions
149
+ * const jaResults = searchIndex.getWords('ja');
150
+ * console.log(jaResults); // contains 'javascript';
151
+ * console.log(jaResults); // contains 'java';
152
+ * console.log(jaResults.length); // 2;
153
+ *
154
+ * // User types 'jav' - even more specific
155
+ * const javResults = searchIndex.getWords('jav');
156
+ * console.log(javResults); // contains 'javascript';
157
+ * console.log(javResults); // contains 'java';
158
+ * console.log(javResults.length); // 2;
159
+ *
160
+ * // Check for common prefix
161
+ *
162
+ * console.log(searchIndex.hasCommonPrefix('ja')); // false; // Not all words start with 'ja'
163
+ *
164
+ * // Total words in index
165
+ * console.log(searchIndex.size); // 8;
166
+ *
167
+ * // Get height (depth of tree)
168
+ * const height = searchIndex.getHeight();
169
+ * console.log(typeof height); // 'number';
124
170
  * @example
125
171
  * // Dictionary: Case-insensitive word lookup
126
- * // Create a case-insensitive dictionary
172
+ * // Create a case-insensitive dictionary
127
173
  * const dictionary = new Trie<string>([], { caseSensitive: false });
128
174
  *
129
175
  * // Add words with mixed casing
@@ -132,14 +178,30 @@ export declare class TrieNode {
132
178
  * dictionary.add('JavaScript');
133
179
  *
134
180
  * // Test lookups with different casings
135
- * console.log(dictionary.has('hello')); // true
136
- * console.log(dictionary.has('HELLO')); // true
137
- * console.log(dictionary.has('Hello')); // true
138
- * console.log(dictionary.has('javascript')); // true
139
- * console.log(dictionary.has('JAVASCRIPT')); // true
181
+ * console.log(dictionary.has('hello')); // true;
182
+ * console.log(dictionary.has('HELLO')); // true;
183
+ * console.log(dictionary.has('Hello')); // true;
184
+ * console.log(dictionary.has('javascript')); // true;
185
+ * console.log(dictionary.has('JAVASCRIPT')); // true;
186
+ * @example
187
+ * // File System Path Operations
188
+ * const fileSystem = new Trie<string>([
189
+ * '/home/user/documents/file1.txt',
190
+ * '/home/user/documents/file2.txt',
191
+ * '/home/user/pictures/photo.jpg',
192
+ * '/home/user/pictures/vacation/',
193
+ * '/home/user/downloads'
194
+ * ]);
195
+ *
196
+ * // Find common directory prefix
197
+ * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/';
198
+ *
199
+ * // List all files in a directory
200
+ * const documentsFiles = fileSystem.getWords('/home/user/documents/');
201
+ * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt'];
140
202
  * @example
141
203
  * // IP Address Routing Table
142
- * // Add IP address prefixes and their corresponding routes
204
+ * // Add IP address prefixes and their corresponding routes
143
205
  * const routes = {
144
206
  * '192.168.1': 'LAN_SUBNET_1',
145
207
  * '192.168.2': 'LAN_SUBNET_2',
@@ -150,13 +212,13 @@ export declare class TrieNode {
150
212
  * const ipRoutingTable = new Trie<string>(Object.keys(routes));
151
213
  *
152
214
  * // Check IP address prefix matching
153
- * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
154
- * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
215
+ * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true;
216
+ * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true;
155
217
  *
156
218
  * // Validate IP address belongs to subnet
157
219
  * const ip = '192.168.1.100';
158
220
  * const subnet = ip.split('.').slice(0, 3).join('.');
159
- * console.log(ipRoutingTable.hasPrefix(subnet)); // true
221
+ * console.log(ipRoutingTable.hasPrefix(subnet)); // true;
160
222
  */
161
223
  export declare class Trie<R = any> extends IterableElementBase<string, R> {
162
224
  /**
@@ -17,6 +17,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
17
17
  createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
18
18
  createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R>;
19
19
  add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
20
+ set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
20
21
  addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
21
22
  delete(keyNodeEntryRawOrPredicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
22
23
  clear(): void;
@@ -1,12 +1,12 @@
1
1
  import type { BinaryTreeOptions } from './binary-tree';
2
- import { Comparable } from '../../utils';
3
- import { OptValue } from '../../common';
4
- export type BSTOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'> & {
5
- specifyComparable?: (key: K) => Comparable;
6
- isReverse?: boolean;
2
+ import type { Comparator, OptValue } from '../../common';
3
+ type BSTBaseOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'>;
4
+ export type BSTOptions<K, V, R> = BSTBaseOptions<K, V, R> & {
5
+ comparator?: Comparator<K>;
7
6
  };
8
7
  export type BSTNOptKey<K> = K | undefined;
9
8
  export type OptNode<NODE> = NODE | undefined;
10
9
  export type BSTNEntry<K, V> = [BSTNOptKey<K>, OptValue<V>];
11
10
  export type BSTNOptKeyOrNode<K, NODE> = BSTNOptKey<K> | NODE;
12
11
  export type BSTNRep<K, V, NODE> = BSTNEntry<K, V> | BSTNOptKeyOrNode<K, NODE>;
12
+ export {};
@@ -1479,6 +1479,17 @@ var treeMultimapTyped = (() => {
1479
1479
  }
1480
1480
  return false;
1481
1481
  }
1482
+ /**
1483
+ * Adds or updates a new node to the tree.
1484
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1485
+ *
1486
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
1487
+ * @param [value] - The value, if providing just a key.
1488
+ * @returns True if the addition was successful, false otherwise.
1489
+ */
1490
+ set(keyNodeOrEntry, value) {
1491
+ return this.add(keyNodeOrEntry, value);
1492
+ }
1482
1493
  /**
1483
1494
  * Adds multiple items to the tree.
1484
1495
  * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
@@ -1506,6 +1517,17 @@ var treeMultimapTyped = (() => {
1506
1517
  }
1507
1518
  return inserted;
1508
1519
  }
1520
+ /**
1521
+ * Adds or updates multiple items to the tree.
1522
+ * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
1523
+ *
1524
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1525
+ * @param [values] - An optional parallel iterable of values.
1526
+ * @returns An array of booleans indicating the success of each individual `add` operation.
1527
+ */
1528
+ setMany(keysNodesEntriesOrRaws, values) {
1529
+ return this.addMany(keysNodesEntriesOrRaws, values);
1530
+ }
1509
1531
  /**
1510
1532
  * Merges another tree into this one by adding all its nodes.
1511
1533
  * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
@@ -2818,36 +2840,20 @@ var treeMultimapTyped = (() => {
2818
2840
  constructor(keysNodesEntriesOrRaws = [], options) {
2819
2841
  super([], options);
2820
2842
  __publicField(this, "_root");
2821
- __publicField(this, "_isReverse", false);
2822
2843
  /**
2823
- * The default comparator function.
2824
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2825
- */
2826
- __publicField(this, "_comparator", (a, b) => {
2827
- if (isComparable(a) && isComparable(b)) {
2828
- if (a > b) return 1;
2829
- if (a < b) return -1;
2830
- return 0;
2831
- }
2832
- if (this._specifyComparable) {
2833
- const va = this._specifyComparable(a);
2834
- const vb = this._specifyComparable(b);
2835
- if (va > vb) return 1;
2836
- if (va < vb) return -1;
2837
- return 0;
2838
- }
2839
- if (typeof a === "object" || typeof b === "object") {
2840
- throw TypeError(
2841
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2842
- );
2843
- }
2844
- return 0;
2845
- });
2846
- __publicField(this, "_specifyComparable");
2844
+ * The comparator function used to determine the order of keys in the tree.
2845
+
2846
+ * @remarks Time O(1) Space O(1)
2847
+ */
2848
+ __publicField(this, "_comparator");
2847
2849
  if (options) {
2848
- const { specifyComparable, isReverse } = options;
2849
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2850
- if (isReverse !== void 0) this._isReverse = isReverse;
2850
+ if ("comparator" in options && options.comparator !== void 0) {
2851
+ this._comparator = options.comparator;
2852
+ } else {
2853
+ this._comparator = this._createDefaultComparator();
2854
+ }
2855
+ } else {
2856
+ this._comparator = this._createDefaultComparator();
2851
2857
  }
2852
2858
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2853
2859
  }
@@ -2861,13 +2867,25 @@ var treeMultimapTyped = (() => {
2861
2867
  return this._root;
2862
2868
  }
2863
2869
  /**
2864
- * Gets whether the tree's comparison logic is reversed.
2865
- * @remarks Time O(1)
2866
- *
2867
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2870
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2871
+ * @remarks Time O(1) Space O(1)
2872
+ * @returns The default comparator function.
2868
2873
  */
2869
- get isReverse() {
2870
- return this._isReverse;
2874
+ _createDefaultComparator() {
2875
+ return (a, b) => {
2876
+ debugger;
2877
+ if (isComparable(a) && isComparable(b)) {
2878
+ if (a > b) return 1;
2879
+ if (a < b) return -1;
2880
+ return 0;
2881
+ }
2882
+ if (typeof a === "object" || typeof b === "object") {
2883
+ throw TypeError(
2884
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2885
+ );
2886
+ }
2887
+ return 0;
2888
+ };
2871
2889
  }
2872
2890
  /**
2873
2891
  * Gets the comparator function used by the tree.
@@ -2878,15 +2896,6 @@ var treeMultimapTyped = (() => {
2878
2896
  get comparator() {
2879
2897
  return this._comparator;
2880
2898
  }
2881
- /**
2882
- * Gets the function used to extract a comparable value from a complex key.
2883
- * @remarks Time O(1)
2884
- *
2885
- * @returns The key-to-comparable conversion function.
2886
- */
2887
- get specifyComparable() {
2888
- return this._specifyComparable;
2889
- }
2890
2899
  /**
2891
2900
  * (Protected) Creates a new BST node.
2892
2901
  * @remarks Time O(1), Space O(1)
@@ -2928,7 +2937,7 @@ var treeMultimapTyped = (() => {
2928
2937
  * @returns True if the key is valid, false otherwise.
2929
2938
  */
2930
2939
  isValidKey(key) {
2931
- return isComparable(key, this._specifyComparable !== void 0);
2940
+ return isComparable(key);
2932
2941
  }
2933
2942
  /**
2934
2943
  * Performs a Depth-First Search (DFS) traversal.
@@ -3018,8 +3027,8 @@ var treeMultimapTyped = (() => {
3018
3027
  if (!this.isRealNode(cur.left)) return false;
3019
3028
  if (isRange) {
3020
3029
  const range = keyNodeEntryOrPredicate;
3021
- const leftS = this.isReverse ? range.high : range.low;
3022
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
3030
+ const leftS = range.low;
3031
+ const leftI = range.includeLow;
3023
3032
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
3024
3033
  }
3025
3034
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3033,8 +3042,8 @@ var treeMultimapTyped = (() => {
3033
3042
  if (!this.isRealNode(cur.right)) return false;
3034
3043
  if (isRange) {
3035
3044
  const range = keyNodeEntryOrPredicate;
3036
- const rightS = this.isReverse ? range.low : range.high;
3037
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
3045
+ const rightS = range.high;
3046
+ const rightI = range.includeHigh;
3038
3047
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
3039
3048
  }
3040
3049
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3195,6 +3204,32 @@ var treeMultimapTyped = (() => {
3195
3204
  else _iterate();
3196
3205
  return inserted;
3197
3206
  }
3207
+ /**
3208
+ * Returns the first node with a key greater than or equal to the given key.
3209
+ * This is equivalent to C++ std::lower_bound on a BST.
3210
+ * Supports RECURSIVE and ITERATIVE implementations.
3211
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
3212
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3213
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3214
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3215
+ * @returns The first node with key >= given key, or undefined if no such node exists.
3216
+ */
3217
+ lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3218
+ return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3219
+ }
3220
+ /**
3221
+ * Returns the first node with a key strictly greater than the given key.
3222
+ * This is equivalent to C++ std::upper_bound on a BST.
3223
+ * Supports RECURSIVE and ITERATIVE implementations.
3224
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
3225
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3226
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3227
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3228
+ * @returns The first node with key > given key, or undefined if no such node exists.
3229
+ */
3230
+ upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3231
+ return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3232
+ }
3198
3233
  /**
3199
3234
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
3200
3235
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -3329,31 +3364,171 @@ var treeMultimapTyped = (() => {
3329
3364
  return out;
3330
3365
  }
3331
3366
  /**
3332
- * Deletes the first node found that satisfies the predicate.
3333
- * @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
3367
+ * Deletes nodes that match a key, node, entry, predicate, or range.
3334
3368
  *
3335
- * @param predicate - A function to test each [key, value] pair.
3336
- * @returns True if a node was deleted, false otherwise.
3369
+ * @remarks
3370
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
3371
+ * Space Complexity: O(M) for storing matched nodes and result map.
3372
+ *
3373
+ * @template K - The key type.
3374
+ * @template V - The value type.
3375
+ *
3376
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
3377
+ * - A key (type K): searches for exact key match using the comparator.
3378
+ * - A BSTNode: searches for the matching node in the tree.
3379
+ * - An entry tuple: searches for the key-value pair.
3380
+ * - A NodePredicate function: tests each node and returns true for matches.
3381
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
3382
+ * - null or undefined: treated as no match, returns empty results.
3383
+ *
3384
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
3385
+ * If false (default), searches for and deletes all matching nodes.
3386
+ *
3387
+ * @param startNode - The node to start the search from. Can be:
3388
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
3389
+ * - null or undefined: defaults to the root, searching the entire tree.
3390
+ * - Default value: this._root (the tree's root).
3391
+ *
3392
+ * @param iterationType - Controls the internal traversal implementation:
3393
+ * - 'RECURSIVE': uses recursive function calls for traversal.
3394
+ * - 'ITERATIVE': uses explicit stack-based iteration.
3395
+ * - Default: this.iterationType (the tree's default iteration mode).
3396
+ *
3397
+ * @returns A Map<K, boolean> containing the deletion results:
3398
+ * - Key: the matched node's key.
3399
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
3400
+ * - If no nodes match the search criteria, the returned map is empty.
3401
+ */
3402
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
3403
+ const toDelete = this.search(
3404
+ keyNodeEntryOrPredicate,
3405
+ onlyOne,
3406
+ (node) => node,
3407
+ startNode,
3408
+ iterationType
3409
+ );
3410
+ let results = [];
3411
+ for (const node of toDelete) {
3412
+ const deleteInfo = this.delete(node);
3413
+ results = results.concat(deleteInfo);
3414
+ }
3415
+ return results;
3416
+ }
3417
+ /**
3418
+ * (Protected) Core bound search implementation supporting all parameter types.
3419
+ * Unified logic for both lowerBound and upperBound.
3420
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
3421
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3422
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
3423
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3424
+ * @returns The first matching node, or undefined if no such node exists.
3337
3425
  */
3338
- deleteWhere(predicate) {
3339
- const stack = [];
3340
- let cur = this._root;
3341
- let index = 0;
3342
- while (stack.length > 0 || cur !== void 0) {
3343
- while (cur !== void 0 && cur !== null) {
3344
- stack.push(cur);
3345
- cur = cur.left;
3426
+ _bound(keyNodeEntryOrPredicate, isLower, iterationType) {
3427
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3428
+ return void 0;
3429
+ }
3430
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3431
+ return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
3432
+ }
3433
+ let targetKey;
3434
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3435
+ targetKey = keyNodeEntryOrPredicate.key;
3436
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3437
+ const key = keyNodeEntryOrPredicate[0];
3438
+ if (key === null || key === void 0) {
3439
+ return void 0;
3346
3440
  }
3347
- const node = stack.pop();
3348
- if (!node) break;
3349
- const key = node.key;
3350
- const val = node.value;
3351
- if (predicate(key, val, index++, this)) {
3352
- return this._deleteByKey(key);
3441
+ targetKey = key;
3442
+ } else {
3443
+ targetKey = keyNodeEntryOrPredicate;
3444
+ }
3445
+ if (targetKey !== void 0) {
3446
+ return this._boundByKey(targetKey, isLower, iterationType);
3447
+ }
3448
+ return void 0;
3449
+ }
3450
+ /**
3451
+ * (Protected) Binary search for bound by key with pruning optimization.
3452
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3453
+ * For lowerBound: finds first node where key >= target.
3454
+ * For upperBound: finds first node where key > target.
3455
+ * @param key - The target key to search for.
3456
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
3457
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3458
+ * @returns The first node matching the bound condition, or undefined if none exists.
3459
+ */
3460
+ _boundByKey(key, isLower, iterationType) {
3461
+ var _a, _b;
3462
+ if (iterationType === "RECURSIVE") {
3463
+ const dfs = (cur) => {
3464
+ if (!this.isRealNode(cur)) return void 0;
3465
+ const cmp = this.comparator(cur.key, key);
3466
+ const condition = isLower ? cmp >= 0 : cmp > 0;
3467
+ if (condition) {
3468
+ const leftResult = dfs(cur.left);
3469
+ return leftResult != null ? leftResult : cur;
3470
+ } else {
3471
+ return dfs(cur.right);
3472
+ }
3473
+ };
3474
+ return dfs(this.root);
3475
+ } else {
3476
+ let current = this.root;
3477
+ let result = void 0;
3478
+ while (this.isRealNode(current)) {
3479
+ const cmp = this.comparator(current.key, key);
3480
+ const condition = isLower ? cmp >= 0 : cmp > 0;
3481
+ if (condition) {
3482
+ result = current;
3483
+ current = (_a = current.left) != null ? _a : void 0;
3484
+ } else {
3485
+ current = (_b = current.right) != null ? _b : void 0;
3486
+ }
3353
3487
  }
3354
- cur = node.right;
3488
+ return result;
3489
+ }
3490
+ }
3491
+ /**
3492
+ * (Protected) In-order traversal search by predicate.
3493
+ * Falls back to linear in-order traversal when predicate-based search is required.
3494
+ * Returns the first node that satisfies the predicate function.
3495
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
3496
+ * Time Complexity: O(n) since it may visit every node.
3497
+ * @param predicate - The predicate function to test nodes.
3498
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3499
+ * @returns The first node satisfying predicate, or undefined if none found.
3500
+ */
3501
+ _boundByPredicate(predicate, iterationType) {
3502
+ if (iterationType === "RECURSIVE") {
3503
+ let result = void 0;
3504
+ const dfs = (cur) => {
3505
+ if (result || !this.isRealNode(cur)) return;
3506
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3507
+ if (!result && predicate(cur)) {
3508
+ result = cur;
3509
+ }
3510
+ if (!result && this.isRealNode(cur.right)) dfs(cur.right);
3511
+ };
3512
+ dfs(this.root);
3513
+ return result;
3514
+ } else {
3515
+ const stack = [];
3516
+ let current = this.root;
3517
+ while (stack.length > 0 || this.isRealNode(current)) {
3518
+ if (this.isRealNode(current)) {
3519
+ stack.push(current);
3520
+ current = current.left;
3521
+ } else {
3522
+ const node = stack.pop();
3523
+ if (!this.isRealNode(node)) break;
3524
+ if (predicate(node)) {
3525
+ return node;
3526
+ }
3527
+ current = node.right;
3528
+ }
3529
+ }
3530
+ return void 0;
3355
3531
  }
3356
- return false;
3357
3532
  }
3358
3533
  /**
3359
3534
  * (Protected) Creates a new, empty instance of the same BST constructor.
@@ -3390,8 +3565,7 @@ var treeMultimapTyped = (() => {
3390
3565
  _snapshotOptions() {
3391
3566
  return {
3392
3567
  ...super._snapshotOptions(),
3393
- specifyComparable: this.specifyComparable,
3394
- isReverse: this.isReverse
3568
+ comparator: this._comparator
3395
3569
  };
3396
3570
  }
3397
3571
  /**
@@ -3419,14 +3593,14 @@ var treeMultimapTyped = (() => {
3419
3593
  }
3420
3594
  /**
3421
3595
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3422
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3596
+ * @remarks Time O(1) Space O(1)
3423
3597
  *
3424
3598
  * @param a - The first key.
3425
3599
  * @param b - The second key.
3426
3600
  * @returns A number (1, -1, or 0) representing the comparison.
3427
3601
  */
3428
3602
  _compare(a, b) {
3429
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3603
+ return this._comparator(a, b);
3430
3604
  }
3431
3605
  /**
3432
3606
  * (Private) Deletes a node by its key.