red-black-tree-typed 2.2.2 → 2.2.3
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/README.md +92 -37
- package/dist/cjs/index.cjs +163 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +164 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +163 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +164 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- 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/umd/red-black-tree-typed.js +164 -0
- package/dist/umd/red-black-tree-typed.js.map +1 -1
- package/dist/umd/red-black-tree-typed.min.js +3 -3
- package/dist/umd/red-black-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +96 -2
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +322 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- 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
|
@@ -112,53 +112,99 @@ export class TrieNode {
|
|
|
112
112
|
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
113
113
|
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
114
114
|
* @example
|
|
115
|
-
* //
|
|
116
|
-
*
|
|
115
|
+
* // basic Trie creation and add words
|
|
116
|
+
* // Create a simple Trie with initial words
|
|
117
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
117
118
|
*
|
|
118
|
-
* //
|
|
119
|
-
*
|
|
120
|
-
*
|
|
119
|
+
* // Verify size
|
|
120
|
+
* console.log(trie.size); // 3;
|
|
121
|
+
*
|
|
122
|
+
* // Check if words exist
|
|
123
|
+
* console.log(trie.has('apple')); // true;
|
|
124
|
+
* console.log(trie.has('app')); // true;
|
|
125
|
+
*
|
|
126
|
+
* // Add a new word
|
|
127
|
+
* trie.add('application');
|
|
128
|
+
* console.log(trie.size); // 4;
|
|
121
129
|
* @example
|
|
122
|
-
* //
|
|
123
|
-
*
|
|
124
|
-
* '/home/user/documents/file1.txt',
|
|
125
|
-
* '/home/user/documents/file2.txt',
|
|
126
|
-
* '/home/user/pictures/photo.jpg',
|
|
127
|
-
* '/home/user/pictures/vacation/',
|
|
128
|
-
* '/home/user/downloads'
|
|
129
|
-
* ]);
|
|
130
|
+
* // Trie getWords and prefix search
|
|
131
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
130
132
|
*
|
|
131
|
-
* //
|
|
132
|
-
*
|
|
133
|
+
* // Get all words with prefix 'app'
|
|
134
|
+
* const appWords = trie.getWords('app');
|
|
135
|
+
* console.log(appWords); // contains 'app';
|
|
136
|
+
* console.log(appWords); // contains 'apple';
|
|
137
|
+
* console.log(appWords); // contains 'apply';
|
|
138
|
+
* console.log(appWords); // contains 'application';
|
|
139
|
+
* expect(appWords).not.toContain('apricot');
|
|
140
|
+
* @example
|
|
141
|
+
* // Trie isPrefix and isAbsolutePrefix checks
|
|
142
|
+
* const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
|
|
133
143
|
*
|
|
134
|
-
* //
|
|
135
|
-
*
|
|
136
|
-
* console.log(
|
|
144
|
+
* // Check if string is a prefix of any word
|
|
145
|
+
* console.log(trie.hasPrefix('tri')); // true;
|
|
146
|
+
* console.log(trie.hasPrefix('tr')); // true;
|
|
147
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
148
|
+
*
|
|
149
|
+
* // Check if string is an absolute prefix (not a complete word)
|
|
150
|
+
* console.log(trie.hasPurePrefix('tri')); // true;
|
|
151
|
+
* console.log(trie.hasPurePrefix('tree')); // false; // 'tree' is a complete word
|
|
152
|
+
*
|
|
153
|
+
* // Verify size
|
|
154
|
+
* console.log(trie.size); // 5;
|
|
137
155
|
* @example
|
|
138
|
-
* //
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* 'closure',
|
|
148
|
-
* 'const',
|
|
149
|
-
* 'constructor'
|
|
150
|
-
* ]);
|
|
156
|
+
* // Trie delete and iteration
|
|
157
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
158
|
+
*
|
|
159
|
+
* // Delete a word
|
|
160
|
+
* trie.delete('card');
|
|
161
|
+
* console.log(trie.has('card')); // false;
|
|
162
|
+
*
|
|
163
|
+
* // Word with same prefix still exists
|
|
164
|
+
* console.log(trie.has('care')); // true;
|
|
151
165
|
*
|
|
152
|
-
* //
|
|
153
|
-
* console.log(
|
|
154
|
-
* console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
|
|
155
|
-
* console.log(autocomplete.getWords('con')); // ['constructor', 'const']
|
|
166
|
+
* // Size decreased
|
|
167
|
+
* console.log(trie.size); // 5;
|
|
156
168
|
*
|
|
157
|
-
* //
|
|
158
|
-
*
|
|
169
|
+
* // Iterate through all words
|
|
170
|
+
* const allWords = [...trie];
|
|
171
|
+
* console.log(allWords.length); // 5;
|
|
172
|
+
* @example
|
|
173
|
+
* // Trie for autocomplete search index
|
|
174
|
+
* // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
|
|
175
|
+
* const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
|
|
176
|
+
*
|
|
177
|
+
* // User types 'j' - get all suggestions
|
|
178
|
+
* const jResults = searchIndex.getWords('j');
|
|
179
|
+
* console.log(jResults); // contains 'javascript';
|
|
180
|
+
* console.log(jResults); // contains 'java';
|
|
181
|
+
* console.log(jResults.length); // 2;
|
|
182
|
+
*
|
|
183
|
+
* // User types 'ja' - get more specific suggestions
|
|
184
|
+
* const jaResults = searchIndex.getWords('ja');
|
|
185
|
+
* console.log(jaResults); // contains 'javascript';
|
|
186
|
+
* console.log(jaResults); // contains 'java';
|
|
187
|
+
* console.log(jaResults.length); // 2;
|
|
188
|
+
*
|
|
189
|
+
* // User types 'jav' - even more specific
|
|
190
|
+
* const javResults = searchIndex.getWords('jav');
|
|
191
|
+
* console.log(javResults); // contains 'javascript';
|
|
192
|
+
* console.log(javResults); // contains 'java';
|
|
193
|
+
* console.log(javResults.length); // 2;
|
|
194
|
+
*
|
|
195
|
+
* // Check for common prefix
|
|
196
|
+
*
|
|
197
|
+
* console.log(searchIndex.hasCommonPrefix('ja')); // false; // Not all words start with 'ja'
|
|
198
|
+
*
|
|
199
|
+
* // Total words in index
|
|
200
|
+
* console.log(searchIndex.size); // 8;
|
|
201
|
+
*
|
|
202
|
+
* // Get height (depth of tree)
|
|
203
|
+
* const height = searchIndex.getHeight();
|
|
204
|
+
* console.log(typeof height); // 'number';
|
|
159
205
|
* @example
|
|
160
206
|
* // Dictionary: Case-insensitive word lookup
|
|
161
|
-
*
|
|
207
|
+
* // Create a case-insensitive dictionary
|
|
162
208
|
* const dictionary = new Trie<string>([], { caseSensitive: false });
|
|
163
209
|
*
|
|
164
210
|
* // Add words with mixed casing
|
|
@@ -167,14 +213,30 @@ export class TrieNode {
|
|
|
167
213
|
* dictionary.add('JavaScript');
|
|
168
214
|
*
|
|
169
215
|
* // Test lookups with different casings
|
|
170
|
-
* console.log(dictionary.has('hello')); // true
|
|
171
|
-
* console.log(dictionary.has('HELLO')); // true
|
|
172
|
-
* console.log(dictionary.has('Hello')); // true
|
|
173
|
-
* console.log(dictionary.has('javascript')); // true
|
|
174
|
-
* console.log(dictionary.has('JAVASCRIPT')); // true
|
|
216
|
+
* console.log(dictionary.has('hello')); // true;
|
|
217
|
+
* console.log(dictionary.has('HELLO')); // true;
|
|
218
|
+
* console.log(dictionary.has('Hello')); // true;
|
|
219
|
+
* console.log(dictionary.has('javascript')); // true;
|
|
220
|
+
* console.log(dictionary.has('JAVASCRIPT')); // true;
|
|
221
|
+
* @example
|
|
222
|
+
* // File System Path Operations
|
|
223
|
+
* const fileSystem = new Trie<string>([
|
|
224
|
+
* '/home/user/documents/file1.txt',
|
|
225
|
+
* '/home/user/documents/file2.txt',
|
|
226
|
+
* '/home/user/pictures/photo.jpg',
|
|
227
|
+
* '/home/user/pictures/vacation/',
|
|
228
|
+
* '/home/user/downloads'
|
|
229
|
+
* ]);
|
|
230
|
+
*
|
|
231
|
+
* // Find common directory prefix
|
|
232
|
+
* console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/';
|
|
233
|
+
*
|
|
234
|
+
* // List all files in a directory
|
|
235
|
+
* const documentsFiles = fileSystem.getWords('/home/user/documents/');
|
|
236
|
+
* console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt'];
|
|
175
237
|
* @example
|
|
176
238
|
* // IP Address Routing Table
|
|
177
|
-
*
|
|
239
|
+
* // Add IP address prefixes and their corresponding routes
|
|
178
240
|
* const routes = {
|
|
179
241
|
* '192.168.1': 'LAN_SUBNET_1',
|
|
180
242
|
* '192.168.2': 'LAN_SUBNET_2',
|
|
@@ -185,13 +247,13 @@ export class TrieNode {
|
|
|
185
247
|
* const ipRoutingTable = new Trie<string>(Object.keys(routes));
|
|
186
248
|
*
|
|
187
249
|
* // Check IP address prefix matching
|
|
188
|
-
* console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
|
|
189
|
-
* console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
|
|
250
|
+
* console.log(ipRoutingTable.hasPrefix('192.168.1')); // true;
|
|
251
|
+
* console.log(ipRoutingTable.hasPrefix('192.168.2')); // true;
|
|
190
252
|
*
|
|
191
253
|
* // Validate IP address belongs to subnet
|
|
192
254
|
* const ip = '192.168.1.100';
|
|
193
255
|
* const subnet = ip.split('.').slice(0, 3).join('.');
|
|
194
|
-
* console.log(ipRoutingTable.hasPrefix(subnet)); // true
|
|
256
|
+
* console.log(ipRoutingTable.hasPrefix(subnet)); // true;
|
|
195
257
|
*/
|
|
196
258
|
export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
197
259
|
/**
|
|
@@ -38,6 +38,8 @@ export interface IBinaryTree<K = any, V = any, R = any> {
|
|
|
38
38
|
|
|
39
39
|
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
40
40
|
|
|
41
|
+
set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
42
|
+
|
|
41
43
|
// Accept raw R as well (when toEntryFn is configured)
|
|
42
44
|
addMany(
|
|
43
45
|
keysNodesEntriesOrRaws: Iterable<
|