avl-tree-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.
- package/dist/cjs/index.cjs +245 -72
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +246 -72
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +245 -72
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +246 -72
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/avl-tree-typed.js +246 -72
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +3 -3
- package/dist/umd/avl-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +100 -7
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +431 -93
- package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- 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
|
-
* //
|
|
81
|
-
*
|
|
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
|
-
* //
|
|
84
|
-
*
|
|
85
|
-
*
|
|
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
|
-
* //
|
|
88
|
-
*
|
|
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
|
-
* //
|
|
97
|
-
*
|
|
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
|
-
* //
|
|
100
|
-
*
|
|
101
|
-
* console.log(
|
|
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
|
-
* //
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
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
|
-
* //
|
|
118
|
-
* console.log(
|
|
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
|
-
* //
|
|
123
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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 {
|
|
3
|
-
|
|
4
|
-
export type BSTOptions<K, V, R> =
|
|
5
|
-
|
|
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 {};
|
|
@@ -1470,6 +1470,17 @@ var avlTreeTyped = (() => {
|
|
|
1470
1470
|
}
|
|
1471
1471
|
return false;
|
|
1472
1472
|
}
|
|
1473
|
+
/**
|
|
1474
|
+
* Adds or updates a new node to the tree.
|
|
1475
|
+
* @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).
|
|
1476
|
+
*
|
|
1477
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
1478
|
+
* @param [value] - The value, if providing just a key.
|
|
1479
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1480
|
+
*/
|
|
1481
|
+
set(keyNodeOrEntry, value) {
|
|
1482
|
+
return this.add(keyNodeOrEntry, value);
|
|
1483
|
+
}
|
|
1473
1484
|
/**
|
|
1474
1485
|
* Adds multiple items to the tree.
|
|
1475
1486
|
* @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).
|
|
@@ -1497,6 +1508,17 @@ var avlTreeTyped = (() => {
|
|
|
1497
1508
|
}
|
|
1498
1509
|
return inserted;
|
|
1499
1510
|
}
|
|
1511
|
+
/**
|
|
1512
|
+
* Adds or updates multiple items to the tree.
|
|
1513
|
+
* @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).
|
|
1514
|
+
*
|
|
1515
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1516
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1517
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1518
|
+
*/
|
|
1519
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1520
|
+
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1521
|
+
}
|
|
1500
1522
|
/**
|
|
1501
1523
|
* Merges another tree into this one by adding all its nodes.
|
|
1502
1524
|
* @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`).
|
|
@@ -2809,36 +2831,20 @@ var avlTreeTyped = (() => {
|
|
|
2809
2831
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2810
2832
|
super([], options);
|
|
2811
2833
|
__publicField(this, "_root");
|
|
2812
|
-
__publicField(this, "_isReverse", false);
|
|
2813
2834
|
/**
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
if (a > b) return 1;
|
|
2820
|
-
if (a < b) return -1;
|
|
2821
|
-
return 0;
|
|
2822
|
-
}
|
|
2823
|
-
if (this._specifyComparable) {
|
|
2824
|
-
const va = this._specifyComparable(a);
|
|
2825
|
-
const vb = this._specifyComparable(b);
|
|
2826
|
-
if (va > vb) return 1;
|
|
2827
|
-
if (va < vb) return -1;
|
|
2828
|
-
return 0;
|
|
2829
|
-
}
|
|
2830
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2831
|
-
throw TypeError(
|
|
2832
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2833
|
-
);
|
|
2834
|
-
}
|
|
2835
|
-
return 0;
|
|
2836
|
-
});
|
|
2837
|
-
__publicField(this, "_specifyComparable");
|
|
2835
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
2836
|
+
|
|
2837
|
+
* @remarks Time O(1) Space O(1)
|
|
2838
|
+
*/
|
|
2839
|
+
__publicField(this, "_comparator");
|
|
2838
2840
|
if (options) {
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2841
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
2842
|
+
this._comparator = options.comparator;
|
|
2843
|
+
} else {
|
|
2844
|
+
this._comparator = this._createDefaultComparator();
|
|
2845
|
+
}
|
|
2846
|
+
} else {
|
|
2847
|
+
this._comparator = this._createDefaultComparator();
|
|
2842
2848
|
}
|
|
2843
2849
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2844
2850
|
}
|
|
@@ -2852,13 +2858,25 @@ var avlTreeTyped = (() => {
|
|
|
2852
2858
|
return this._root;
|
|
2853
2859
|
}
|
|
2854
2860
|
/**
|
|
2855
|
-
*
|
|
2856
|
-
* @remarks Time O(1)
|
|
2857
|
-
*
|
|
2858
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
2861
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2862
|
+
* @remarks Time O(1) Space O(1)
|
|
2863
|
+
* @returns The default comparator function.
|
|
2859
2864
|
*/
|
|
2860
|
-
|
|
2861
|
-
return
|
|
2865
|
+
_createDefaultComparator() {
|
|
2866
|
+
return (a, b) => {
|
|
2867
|
+
debugger;
|
|
2868
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2869
|
+
if (a > b) return 1;
|
|
2870
|
+
if (a < b) return -1;
|
|
2871
|
+
return 0;
|
|
2872
|
+
}
|
|
2873
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2874
|
+
throw TypeError(
|
|
2875
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2876
|
+
);
|
|
2877
|
+
}
|
|
2878
|
+
return 0;
|
|
2879
|
+
};
|
|
2862
2880
|
}
|
|
2863
2881
|
/**
|
|
2864
2882
|
* Gets the comparator function used by the tree.
|
|
@@ -2869,15 +2887,6 @@ var avlTreeTyped = (() => {
|
|
|
2869
2887
|
get comparator() {
|
|
2870
2888
|
return this._comparator;
|
|
2871
2889
|
}
|
|
2872
|
-
/**
|
|
2873
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
2874
|
-
* @remarks Time O(1)
|
|
2875
|
-
*
|
|
2876
|
-
* @returns The key-to-comparable conversion function.
|
|
2877
|
-
*/
|
|
2878
|
-
get specifyComparable() {
|
|
2879
|
-
return this._specifyComparable;
|
|
2880
|
-
}
|
|
2881
2890
|
/**
|
|
2882
2891
|
* (Protected) Creates a new BST node.
|
|
2883
2892
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2919,7 +2928,7 @@ var avlTreeTyped = (() => {
|
|
|
2919
2928
|
* @returns True if the key is valid, false otherwise.
|
|
2920
2929
|
*/
|
|
2921
2930
|
isValidKey(key) {
|
|
2922
|
-
return isComparable(key
|
|
2931
|
+
return isComparable(key);
|
|
2923
2932
|
}
|
|
2924
2933
|
/**
|
|
2925
2934
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -3009,8 +3018,8 @@ var avlTreeTyped = (() => {
|
|
|
3009
3018
|
if (!this.isRealNode(cur.left)) return false;
|
|
3010
3019
|
if (isRange) {
|
|
3011
3020
|
const range = keyNodeEntryOrPredicate;
|
|
3012
|
-
const leftS =
|
|
3013
|
-
const leftI =
|
|
3021
|
+
const leftS = range.low;
|
|
3022
|
+
const leftI = range.includeLow;
|
|
3014
3023
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
3015
3024
|
}
|
|
3016
3025
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3024,8 +3033,8 @@ var avlTreeTyped = (() => {
|
|
|
3024
3033
|
if (!this.isRealNode(cur.right)) return false;
|
|
3025
3034
|
if (isRange) {
|
|
3026
3035
|
const range = keyNodeEntryOrPredicate;
|
|
3027
|
-
const rightS =
|
|
3028
|
-
const rightI =
|
|
3036
|
+
const rightS = range.high;
|
|
3037
|
+
const rightI = range.includeHigh;
|
|
3029
3038
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
3030
3039
|
}
|
|
3031
3040
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3186,6 +3195,32 @@ var avlTreeTyped = (() => {
|
|
|
3186
3195
|
else _iterate();
|
|
3187
3196
|
return inserted;
|
|
3188
3197
|
}
|
|
3198
|
+
/**
|
|
3199
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
3200
|
+
* This is equivalent to C++ std::lower_bound on a BST.
|
|
3201
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3202
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3203
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3204
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3205
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3206
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
3207
|
+
*/
|
|
3208
|
+
lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3209
|
+
return this._bound(keyNodeEntryOrPredicate, true, iterationType);
|
|
3210
|
+
}
|
|
3211
|
+
/**
|
|
3212
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
3213
|
+
* This is equivalent to C++ std::upper_bound on a BST.
|
|
3214
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3215
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3216
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3217
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3218
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3219
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
3220
|
+
*/
|
|
3221
|
+
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3222
|
+
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3223
|
+
}
|
|
3189
3224
|
/**
|
|
3190
3225
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3191
3226
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3320,31 +3355,171 @@ var avlTreeTyped = (() => {
|
|
|
3320
3355
|
return out;
|
|
3321
3356
|
}
|
|
3322
3357
|
/**
|
|
3323
|
-
* Deletes
|
|
3324
|
-
* @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.
|
|
3358
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
3325
3359
|
*
|
|
3326
|
-
* @
|
|
3327
|
-
*
|
|
3360
|
+
* @remarks
|
|
3361
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
3362
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
3363
|
+
*
|
|
3364
|
+
* @template K - The key type.
|
|
3365
|
+
* @template V - The value type.
|
|
3366
|
+
*
|
|
3367
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
3368
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
3369
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
3370
|
+
* - An entry tuple: searches for the key-value pair.
|
|
3371
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
3372
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
3373
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
3374
|
+
*
|
|
3375
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
3376
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
3377
|
+
*
|
|
3378
|
+
* @param startNode - The node to start the search from. Can be:
|
|
3379
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
3380
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
3381
|
+
* - Default value: this._root (the tree's root).
|
|
3382
|
+
*
|
|
3383
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
3384
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
3385
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
3386
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
3387
|
+
*
|
|
3388
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
3389
|
+
* - Key: the matched node's key.
|
|
3390
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
3391
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
3392
|
+
*/
|
|
3393
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3394
|
+
const toDelete = this.search(
|
|
3395
|
+
keyNodeEntryOrPredicate,
|
|
3396
|
+
onlyOne,
|
|
3397
|
+
(node) => node,
|
|
3398
|
+
startNode,
|
|
3399
|
+
iterationType
|
|
3400
|
+
);
|
|
3401
|
+
let results = [];
|
|
3402
|
+
for (const node of toDelete) {
|
|
3403
|
+
const deleteInfo = this.delete(node);
|
|
3404
|
+
results = results.concat(deleteInfo);
|
|
3405
|
+
}
|
|
3406
|
+
return results;
|
|
3407
|
+
}
|
|
3408
|
+
/**
|
|
3409
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3410
|
+
* Unified logic for both lowerBound and upperBound.
|
|
3411
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
3412
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3413
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3414
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3415
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
3328
3416
|
*/
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3417
|
+
_bound(keyNodeEntryOrPredicate, isLower, iterationType) {
|
|
3418
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3419
|
+
return void 0;
|
|
3420
|
+
}
|
|
3421
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3422
|
+
return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
3423
|
+
}
|
|
3424
|
+
let targetKey;
|
|
3425
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3426
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3427
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3428
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3429
|
+
if (key === null || key === void 0) {
|
|
3430
|
+
return void 0;
|
|
3431
|
+
}
|
|
3432
|
+
targetKey = key;
|
|
3433
|
+
} else {
|
|
3434
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3435
|
+
}
|
|
3436
|
+
if (targetKey !== void 0) {
|
|
3437
|
+
return this._boundByKey(targetKey, isLower, iterationType);
|
|
3438
|
+
}
|
|
3439
|
+
return void 0;
|
|
3440
|
+
}
|
|
3441
|
+
/**
|
|
3442
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
3443
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3444
|
+
* For lowerBound: finds first node where key >= target.
|
|
3445
|
+
* For upperBound: finds first node where key > target.
|
|
3446
|
+
* @param key - The target key to search for.
|
|
3447
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3448
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3449
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
3450
|
+
*/
|
|
3451
|
+
_boundByKey(key, isLower, iterationType) {
|
|
3452
|
+
var _a, _b;
|
|
3453
|
+
if (iterationType === "RECURSIVE") {
|
|
3454
|
+
const dfs = (cur) => {
|
|
3455
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3456
|
+
const cmp = this.comparator(cur.key, key);
|
|
3457
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3458
|
+
if (condition) {
|
|
3459
|
+
const leftResult = dfs(cur.left);
|
|
3460
|
+
return leftResult != null ? leftResult : cur;
|
|
3461
|
+
} else {
|
|
3462
|
+
return dfs(cur.right);
|
|
3463
|
+
}
|
|
3464
|
+
};
|
|
3465
|
+
return dfs(this.root);
|
|
3466
|
+
} else {
|
|
3467
|
+
let current = this.root;
|
|
3468
|
+
let result = void 0;
|
|
3469
|
+
while (this.isRealNode(current)) {
|
|
3470
|
+
const cmp = this.comparator(current.key, key);
|
|
3471
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3472
|
+
if (condition) {
|
|
3473
|
+
result = current;
|
|
3474
|
+
current = (_a = current.left) != null ? _a : void 0;
|
|
3475
|
+
} else {
|
|
3476
|
+
current = (_b = current.right) != null ? _b : void 0;
|
|
3477
|
+
}
|
|
3337
3478
|
}
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3479
|
+
return result;
|
|
3480
|
+
}
|
|
3481
|
+
}
|
|
3482
|
+
/**
|
|
3483
|
+
* (Protected) In-order traversal search by predicate.
|
|
3484
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3485
|
+
* Returns the first node that satisfies the predicate function.
|
|
3486
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
3487
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
3488
|
+
* @param predicate - The predicate function to test nodes.
|
|
3489
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3490
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
3491
|
+
*/
|
|
3492
|
+
_boundByPredicate(predicate, iterationType) {
|
|
3493
|
+
if (iterationType === "RECURSIVE") {
|
|
3494
|
+
let result = void 0;
|
|
3495
|
+
const dfs = (cur) => {
|
|
3496
|
+
if (result || !this.isRealNode(cur)) return;
|
|
3497
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3498
|
+
if (!result && predicate(cur)) {
|
|
3499
|
+
result = cur;
|
|
3500
|
+
}
|
|
3501
|
+
if (!result && this.isRealNode(cur.right)) dfs(cur.right);
|
|
3502
|
+
};
|
|
3503
|
+
dfs(this.root);
|
|
3504
|
+
return result;
|
|
3505
|
+
} else {
|
|
3506
|
+
const stack = [];
|
|
3507
|
+
let current = this.root;
|
|
3508
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3509
|
+
if (this.isRealNode(current)) {
|
|
3510
|
+
stack.push(current);
|
|
3511
|
+
current = current.left;
|
|
3512
|
+
} else {
|
|
3513
|
+
const node = stack.pop();
|
|
3514
|
+
if (!this.isRealNode(node)) break;
|
|
3515
|
+
if (predicate(node)) {
|
|
3516
|
+
return node;
|
|
3517
|
+
}
|
|
3518
|
+
current = node.right;
|
|
3519
|
+
}
|
|
3344
3520
|
}
|
|
3345
|
-
|
|
3521
|
+
return void 0;
|
|
3346
3522
|
}
|
|
3347
|
-
return false;
|
|
3348
3523
|
}
|
|
3349
3524
|
/**
|
|
3350
3525
|
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
@@ -3381,8 +3556,7 @@ var avlTreeTyped = (() => {
|
|
|
3381
3556
|
_snapshotOptions() {
|
|
3382
3557
|
return {
|
|
3383
3558
|
...super._snapshotOptions(),
|
|
3384
|
-
|
|
3385
|
-
isReverse: this.isReverse
|
|
3559
|
+
comparator: this._comparator
|
|
3386
3560
|
};
|
|
3387
3561
|
}
|
|
3388
3562
|
/**
|
|
@@ -3410,14 +3584,14 @@ var avlTreeTyped = (() => {
|
|
|
3410
3584
|
}
|
|
3411
3585
|
/**
|
|
3412
3586
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
3413
|
-
* @remarks Time O(1)
|
|
3587
|
+
* @remarks Time O(1) Space O(1)
|
|
3414
3588
|
*
|
|
3415
3589
|
* @param a - The first key.
|
|
3416
3590
|
* @param b - The second key.
|
|
3417
3591
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
3418
3592
|
*/
|
|
3419
3593
|
_compare(a, b) {
|
|
3420
|
-
return this.
|
|
3594
|
+
return this._comparator(a, b);
|
|
3421
3595
|
}
|
|
3422
3596
|
/**
|
|
3423
3597
|
* (Private) Deletes a node by its key.
|