data-structure-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 (94) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/README.md +355 -1672
  3. package/README_CN.md +509 -0
  4. package/SECURITY.md +962 -11
  5. package/SECURITY.zh-CN.md +966 -0
  6. package/SPECIFICATION.md +689 -30
  7. package/SPECIFICATION.zh-CN.md +715 -0
  8. package/SPONSOR.zh-CN.md +62 -0
  9. package/SPONSOR_POLISHED.md +62 -0
  10. package/benchmark/report.html +1 -1
  11. package/benchmark/report.json +215 -172
  12. package/dist/cjs/index.cjs +245 -72
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs-legacy/index.cjs +246 -72
  15. package/dist/cjs-legacy/index.cjs.map +1 -1
  16. package/dist/esm/index.mjs +245 -72
  17. package/dist/esm/index.mjs.map +1 -1
  18. package/dist/esm-legacy/index.mjs +246 -72
  19. package/dist/esm-legacy/index.mjs.map +1 -1
  20. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  21. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  22. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
  23. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  24. package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
  25. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
  26. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  27. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
  28. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  29. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  30. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  31. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  32. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  33. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  34. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  35. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  36. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  37. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  38. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  39. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  40. package/dist/umd/data-structure-typed.js +246 -72
  41. package/dist/umd/data-structure-typed.js.map +1 -1
  42. package/dist/umd/data-structure-typed.min.js +3 -3
  43. package/dist/umd/data-structure-typed.min.js.map +1 -1
  44. package/package.json +3 -2
  45. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  46. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  47. package/src/data-structures/binary-tree/avl-tree.ts +100 -7
  48. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  49. package/src/data-structures/binary-tree/bst.ts +431 -93
  50. package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
  51. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
  53. package/src/data-structures/graph/directed-graph.ts +126 -1
  54. package/src/data-structures/graph/undirected-graph.ts +160 -1
  55. package/src/data-structures/hash/hash-map.ts +110 -27
  56. package/src/data-structures/heap/heap.ts +107 -58
  57. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  58. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  59. package/src/data-structures/queue/deque.ts +95 -67
  60. package/src/data-structures/queue/queue.ts +90 -34
  61. package/src/data-structures/stack/stack.ts +58 -40
  62. package/src/data-structures/trie/trie.ts +109 -47
  63. package/src/interfaces/binary-tree.ts +2 -0
  64. package/src/types/data-structures/binary-tree/bst.ts +5 -5
  65. package/test/performance/benchmark-runner.ts +14 -11
  66. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +8 -8
  67. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +8 -8
  68. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -6
  69. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -5
  70. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +10 -10
  71. package/test/performance/reportor.ts +2 -1
  72. package/test/performance/single-suite-runner.ts +7 -4
  73. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
  74. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +117 -0
  75. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +166 -0
  76. package/test/unit/data-structures/binary-tree/bst.test.ts +771 -16
  77. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  78. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +90 -38
  79. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
  80. package/test/unit/data-structures/graph/directed-graph.test.ts +133 -0
  81. package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
  82. package/test/unit/data-structures/hash/hash-map.test.ts +149 -3
  83. package/test/unit/data-structures/heap/heap.test.ts +182 -47
  84. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +118 -14
  85. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +121 -0
  86. package/test/unit/data-structures/queue/deque.test.ts +98 -67
  87. package/test/unit/data-structures/queue/queue.test.ts +85 -51
  88. package/test/unit/data-structures/stack/stack.test.ts +142 -33
  89. package/test/unit/data-structures/trie/trie.test.ts +135 -39
  90. package/tsup.leetcode.config.js +99 -0
  91. package/typedoc.json +2 -1
  92. package/POSTS_zh-CN.md +0 -54
  93. package/README_zh-CN.md +0 -1208
  94. package/SPECIFICATION_zh-CN.md +0 -81
@@ -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 {};
@@ -7267,6 +7267,17 @@ var dataStructureTyped = (() => {
7267
7267
  }
7268
7268
  return false;
7269
7269
  }
7270
+ /**
7271
+ * Adds or updates a new node to the tree.
7272
+ * @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).
7273
+ *
7274
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
7275
+ * @param [value] - The value, if providing just a key.
7276
+ * @returns True if the addition was successful, false otherwise.
7277
+ */
7278
+ set(keyNodeOrEntry, value) {
7279
+ return this.add(keyNodeOrEntry, value);
7280
+ }
7270
7281
  /**
7271
7282
  * Adds multiple items to the tree.
7272
7283
  * @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).
@@ -7294,6 +7305,17 @@ var dataStructureTyped = (() => {
7294
7305
  }
7295
7306
  return inserted;
7296
7307
  }
7308
+ /**
7309
+ * Adds or updates multiple items to the tree.
7310
+ * @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).
7311
+ *
7312
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
7313
+ * @param [values] - An optional parallel iterable of values.
7314
+ * @returns An array of booleans indicating the success of each individual `add` operation.
7315
+ */
7316
+ setMany(keysNodesEntriesOrRaws, values) {
7317
+ return this.addMany(keysNodesEntriesOrRaws, values);
7318
+ }
7297
7319
  /**
7298
7320
  * Merges another tree into this one by adding all its nodes.
7299
7321
  * @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`).
@@ -8606,36 +8628,20 @@ var dataStructureTyped = (() => {
8606
8628
  constructor(keysNodesEntriesOrRaws = [], options) {
8607
8629
  super([], options);
8608
8630
  __publicField(this, "_root");
8609
- __publicField(this, "_isReverse", false);
8610
8631
  /**
8611
- * The default comparator function.
8612
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
8613
- */
8614
- __publicField(this, "_comparator", (a, b) => {
8615
- if (isComparable(a) && isComparable(b)) {
8616
- if (a > b) return 1;
8617
- if (a < b) return -1;
8618
- return 0;
8619
- }
8620
- if (this._specifyComparable) {
8621
- const va = this._specifyComparable(a);
8622
- const vb = this._specifyComparable(b);
8623
- if (va > vb) return 1;
8624
- if (va < vb) return -1;
8625
- return 0;
8626
- }
8627
- if (typeof a === "object" || typeof b === "object") {
8628
- throw TypeError(
8629
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
8630
- );
8631
- }
8632
- return 0;
8633
- });
8634
- __publicField(this, "_specifyComparable");
8632
+ * The comparator function used to determine the order of keys in the tree.
8633
+
8634
+ * @remarks Time O(1) Space O(1)
8635
+ */
8636
+ __publicField(this, "_comparator");
8635
8637
  if (options) {
8636
- const { specifyComparable, isReverse } = options;
8637
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
8638
- if (isReverse !== void 0) this._isReverse = isReverse;
8638
+ if ("comparator" in options && options.comparator !== void 0) {
8639
+ this._comparator = options.comparator;
8640
+ } else {
8641
+ this._comparator = this._createDefaultComparator();
8642
+ }
8643
+ } else {
8644
+ this._comparator = this._createDefaultComparator();
8639
8645
  }
8640
8646
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8641
8647
  }
@@ -8649,13 +8655,25 @@ var dataStructureTyped = (() => {
8649
8655
  return this._root;
8650
8656
  }
8651
8657
  /**
8652
- * Gets whether the tree's comparison logic is reversed.
8653
- * @remarks Time O(1)
8654
- *
8655
- * @returns True if the tree is reversed (e.g., a max-heap logic).
8658
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
8659
+ * @remarks Time O(1) Space O(1)
8660
+ * @returns The default comparator function.
8656
8661
  */
8657
- get isReverse() {
8658
- return this._isReverse;
8662
+ _createDefaultComparator() {
8663
+ return (a, b) => {
8664
+ debugger;
8665
+ if (isComparable(a) && isComparable(b)) {
8666
+ if (a > b) return 1;
8667
+ if (a < b) return -1;
8668
+ return 0;
8669
+ }
8670
+ if (typeof a === "object" || typeof b === "object") {
8671
+ throw TypeError(
8672
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
8673
+ );
8674
+ }
8675
+ return 0;
8676
+ };
8659
8677
  }
8660
8678
  /**
8661
8679
  * Gets the comparator function used by the tree.
@@ -8666,15 +8684,6 @@ var dataStructureTyped = (() => {
8666
8684
  get comparator() {
8667
8685
  return this._comparator;
8668
8686
  }
8669
- /**
8670
- * Gets the function used to extract a comparable value from a complex key.
8671
- * @remarks Time O(1)
8672
- *
8673
- * @returns The key-to-comparable conversion function.
8674
- */
8675
- get specifyComparable() {
8676
- return this._specifyComparable;
8677
- }
8678
8687
  /**
8679
8688
  * (Protected) Creates a new BST node.
8680
8689
  * @remarks Time O(1), Space O(1)
@@ -8716,7 +8725,7 @@ var dataStructureTyped = (() => {
8716
8725
  * @returns True if the key is valid, false otherwise.
8717
8726
  */
8718
8727
  isValidKey(key) {
8719
- return isComparable(key, this._specifyComparable !== void 0);
8728
+ return isComparable(key);
8720
8729
  }
8721
8730
  /**
8722
8731
  * Performs a Depth-First Search (DFS) traversal.
@@ -8806,8 +8815,8 @@ var dataStructureTyped = (() => {
8806
8815
  if (!this.isRealNode(cur.left)) return false;
8807
8816
  if (isRange) {
8808
8817
  const range = keyNodeEntryOrPredicate;
8809
- const leftS = this.isReverse ? range.high : range.low;
8810
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
8818
+ const leftS = range.low;
8819
+ const leftI = range.includeLow;
8811
8820
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
8812
8821
  }
8813
8822
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8821,8 +8830,8 @@ var dataStructureTyped = (() => {
8821
8830
  if (!this.isRealNode(cur.right)) return false;
8822
8831
  if (isRange) {
8823
8832
  const range = keyNodeEntryOrPredicate;
8824
- const rightS = this.isReverse ? range.low : range.high;
8825
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
8833
+ const rightS = range.high;
8834
+ const rightI = range.includeHigh;
8826
8835
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
8827
8836
  }
8828
8837
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8983,6 +8992,32 @@ var dataStructureTyped = (() => {
8983
8992
  else _iterate();
8984
8993
  return inserted;
8985
8994
  }
8995
+ /**
8996
+ * Returns the first node with a key greater than or equal to the given key.
8997
+ * This is equivalent to C++ std::lower_bound on a BST.
8998
+ * Supports RECURSIVE and ITERATIVE implementations.
8999
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
9000
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9001
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9002
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9003
+ * @returns The first node with key >= given key, or undefined if no such node exists.
9004
+ */
9005
+ lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9006
+ return this._bound(keyNodeEntryOrPredicate, true, iterationType);
9007
+ }
9008
+ /**
9009
+ * Returns the first node with a key strictly greater than the given key.
9010
+ * This is equivalent to C++ std::upper_bound on a BST.
9011
+ * Supports RECURSIVE and ITERATIVE implementations.
9012
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
9013
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9014
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9015
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9016
+ * @returns The first node with key > given key, or undefined if no such node exists.
9017
+ */
9018
+ upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9019
+ return this._bound(keyNodeEntryOrPredicate, false, iterationType);
9020
+ }
8986
9021
  /**
8987
9022
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
8988
9023
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -9117,31 +9152,171 @@ var dataStructureTyped = (() => {
9117
9152
  return out;
9118
9153
  }
9119
9154
  /**
9120
- * Deletes the first node found that satisfies the predicate.
9121
- * @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.
9155
+ * Deletes nodes that match a key, node, entry, predicate, or range.
9122
9156
  *
9123
- * @param predicate - A function to test each [key, value] pair.
9124
- * @returns True if a node was deleted, false otherwise.
9157
+ * @remarks
9158
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
9159
+ * Space Complexity: O(M) for storing matched nodes and result map.
9160
+ *
9161
+ * @template K - The key type.
9162
+ * @template V - The value type.
9163
+ *
9164
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
9165
+ * - A key (type K): searches for exact key match using the comparator.
9166
+ * - A BSTNode: searches for the matching node in the tree.
9167
+ * - An entry tuple: searches for the key-value pair.
9168
+ * - A NodePredicate function: tests each node and returns true for matches.
9169
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
9170
+ * - null or undefined: treated as no match, returns empty results.
9171
+ *
9172
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
9173
+ * If false (default), searches for and deletes all matching nodes.
9174
+ *
9175
+ * @param startNode - The node to start the search from. Can be:
9176
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
9177
+ * - null or undefined: defaults to the root, searching the entire tree.
9178
+ * - Default value: this._root (the tree's root).
9179
+ *
9180
+ * @param iterationType - Controls the internal traversal implementation:
9181
+ * - 'RECURSIVE': uses recursive function calls for traversal.
9182
+ * - 'ITERATIVE': uses explicit stack-based iteration.
9183
+ * - Default: this.iterationType (the tree's default iteration mode).
9184
+ *
9185
+ * @returns A Map<K, boolean> containing the deletion results:
9186
+ * - Key: the matched node's key.
9187
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
9188
+ * - If no nodes match the search criteria, the returned map is empty.
9189
+ */
9190
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9191
+ const toDelete = this.search(
9192
+ keyNodeEntryOrPredicate,
9193
+ onlyOne,
9194
+ (node) => node,
9195
+ startNode,
9196
+ iterationType
9197
+ );
9198
+ let results = [];
9199
+ for (const node of toDelete) {
9200
+ const deleteInfo = this.delete(node);
9201
+ results = results.concat(deleteInfo);
9202
+ }
9203
+ return results;
9204
+ }
9205
+ /**
9206
+ * (Protected) Core bound search implementation supporting all parameter types.
9207
+ * Unified logic for both lowerBound and upperBound.
9208
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
9209
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9210
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
9211
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9212
+ * @returns The first matching node, or undefined if no such node exists.
9125
9213
  */
9126
- deleteWhere(predicate) {
9127
- const stack = [];
9128
- let cur = this._root;
9129
- let index = 0;
9130
- while (stack.length > 0 || cur !== void 0) {
9131
- while (cur !== void 0 && cur !== null) {
9132
- stack.push(cur);
9133
- cur = cur.left;
9214
+ _bound(keyNodeEntryOrPredicate, isLower, iterationType) {
9215
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9216
+ return void 0;
9217
+ }
9218
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9219
+ return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
9220
+ }
9221
+ let targetKey;
9222
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9223
+ targetKey = keyNodeEntryOrPredicate.key;
9224
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9225
+ const key = keyNodeEntryOrPredicate[0];
9226
+ if (key === null || key === void 0) {
9227
+ return void 0;
9228
+ }
9229
+ targetKey = key;
9230
+ } else {
9231
+ targetKey = keyNodeEntryOrPredicate;
9232
+ }
9233
+ if (targetKey !== void 0) {
9234
+ return this._boundByKey(targetKey, isLower, iterationType);
9235
+ }
9236
+ return void 0;
9237
+ }
9238
+ /**
9239
+ * (Protected) Binary search for bound by key with pruning optimization.
9240
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9241
+ * For lowerBound: finds first node where key >= target.
9242
+ * For upperBound: finds first node where key > target.
9243
+ * @param key - The target key to search for.
9244
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
9245
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9246
+ * @returns The first node matching the bound condition, or undefined if none exists.
9247
+ */
9248
+ _boundByKey(key, isLower, iterationType) {
9249
+ var _a, _b;
9250
+ if (iterationType === "RECURSIVE") {
9251
+ const dfs = (cur) => {
9252
+ if (!this.isRealNode(cur)) return void 0;
9253
+ const cmp = this.comparator(cur.key, key);
9254
+ const condition = isLower ? cmp >= 0 : cmp > 0;
9255
+ if (condition) {
9256
+ const leftResult = dfs(cur.left);
9257
+ return leftResult != null ? leftResult : cur;
9258
+ } else {
9259
+ return dfs(cur.right);
9260
+ }
9261
+ };
9262
+ return dfs(this.root);
9263
+ } else {
9264
+ let current = this.root;
9265
+ let result = void 0;
9266
+ while (this.isRealNode(current)) {
9267
+ const cmp = this.comparator(current.key, key);
9268
+ const condition = isLower ? cmp >= 0 : cmp > 0;
9269
+ if (condition) {
9270
+ result = current;
9271
+ current = (_a = current.left) != null ? _a : void 0;
9272
+ } else {
9273
+ current = (_b = current.right) != null ? _b : void 0;
9274
+ }
9134
9275
  }
9135
- const node = stack.pop();
9136
- if (!node) break;
9137
- const key = node.key;
9138
- const val = node.value;
9139
- if (predicate(key, val, index++, this)) {
9140
- return this._deleteByKey(key);
9276
+ return result;
9277
+ }
9278
+ }
9279
+ /**
9280
+ * (Protected) In-order traversal search by predicate.
9281
+ * Falls back to linear in-order traversal when predicate-based search is required.
9282
+ * Returns the first node that satisfies the predicate function.
9283
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
9284
+ * Time Complexity: O(n) since it may visit every node.
9285
+ * @param predicate - The predicate function to test nodes.
9286
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9287
+ * @returns The first node satisfying predicate, or undefined if none found.
9288
+ */
9289
+ _boundByPredicate(predicate, iterationType) {
9290
+ if (iterationType === "RECURSIVE") {
9291
+ let result = void 0;
9292
+ const dfs = (cur) => {
9293
+ if (result || !this.isRealNode(cur)) return;
9294
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9295
+ if (!result && predicate(cur)) {
9296
+ result = cur;
9297
+ }
9298
+ if (!result && this.isRealNode(cur.right)) dfs(cur.right);
9299
+ };
9300
+ dfs(this.root);
9301
+ return result;
9302
+ } else {
9303
+ const stack = [];
9304
+ let current = this.root;
9305
+ while (stack.length > 0 || this.isRealNode(current)) {
9306
+ if (this.isRealNode(current)) {
9307
+ stack.push(current);
9308
+ current = current.left;
9309
+ } else {
9310
+ const node = stack.pop();
9311
+ if (!this.isRealNode(node)) break;
9312
+ if (predicate(node)) {
9313
+ return node;
9314
+ }
9315
+ current = node.right;
9316
+ }
9141
9317
  }
9142
- cur = node.right;
9318
+ return void 0;
9143
9319
  }
9144
- return false;
9145
9320
  }
9146
9321
  /**
9147
9322
  * (Protected) Creates a new, empty instance of the same BST constructor.
@@ -9178,8 +9353,7 @@ var dataStructureTyped = (() => {
9178
9353
  _snapshotOptions() {
9179
9354
  return {
9180
9355
  ...super._snapshotOptions(),
9181
- specifyComparable: this.specifyComparable,
9182
- isReverse: this.isReverse
9356
+ comparator: this._comparator
9183
9357
  };
9184
9358
  }
9185
9359
  /**
@@ -9207,14 +9381,14 @@ var dataStructureTyped = (() => {
9207
9381
  }
9208
9382
  /**
9209
9383
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
9210
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
9384
+ * @remarks Time O(1) Space O(1)
9211
9385
  *
9212
9386
  * @param a - The first key.
9213
9387
  * @param b - The second key.
9214
9388
  * @returns A number (1, -1, or 0) representing the comparison.
9215
9389
  */
9216
9390
  _compare(a, b) {
9217
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
9391
+ return this._comparator(a, b);
9218
9392
  }
9219
9393
  /**
9220
9394
  * (Private) Deletes a node by its key.