binary-tree-typed 2.4.4 → 2.5.0
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 +0 -84
- package/dist/cjs/index.cjs +965 -420
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +962 -417
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +965 -421
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +962 -418
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +313 -66
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/binary-tree-typed.js +959 -414
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +3 -3
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +425 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +343 -68
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +215 -44
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -77,32 +77,6 @@ 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
|
-
* // basic Trie creation and add words
|
|
81
|
-
* // Create a simple Trie with initial words
|
|
82
|
-
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
83
|
-
*
|
|
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;
|
|
94
|
-
* @example
|
|
95
|
-
* // Trie getWords and prefix search
|
|
96
|
-
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
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
80
|
* // Trie isPrefix and isAbsolutePrefix checks
|
|
107
81
|
* const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
|
|
108
82
|
*
|
|
@@ -118,23 +92,6 @@ export declare class TrieNode {
|
|
|
118
92
|
* // Verify size
|
|
119
93
|
* console.log(trie.size); // 5;
|
|
120
94
|
* @example
|
|
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;
|
|
130
|
-
*
|
|
131
|
-
* // Size decreased
|
|
132
|
-
* console.log(trie.size); // 5;
|
|
133
|
-
*
|
|
134
|
-
* // Iterate through all words
|
|
135
|
-
* const allWords = [...trie];
|
|
136
|
-
* console.log(allWords.length); // 5;
|
|
137
|
-
* @example
|
|
138
95
|
* // Trie for autocomplete search index
|
|
139
96
|
* // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
|
|
140
97
|
* const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
|
|
@@ -261,6 +218,32 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
261
218
|
* @remarks Time O(L), Space O(L)
|
|
262
219
|
* @param word - Word to insert.
|
|
263
220
|
* @returns True if the word was newly added.
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
* @example
|
|
233
|
+
* // basic Trie creation and add words
|
|
234
|
+
* // Create a simple Trie with initial words
|
|
235
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
236
|
+
*
|
|
237
|
+
* // Verify size
|
|
238
|
+
* console.log(trie.size); // 3;
|
|
239
|
+
*
|
|
240
|
+
* // Check if words exist
|
|
241
|
+
* console.log(trie.has('apple')); // true;
|
|
242
|
+
* console.log(trie.has('app')); // true;
|
|
243
|
+
*
|
|
244
|
+
* // Add a new word
|
|
245
|
+
* trie.add('application');
|
|
246
|
+
* console.log(trie.size); // 4;
|
|
264
247
|
*/
|
|
265
248
|
add(word: string): boolean;
|
|
266
249
|
/**
|
|
@@ -268,6 +251,21 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
268
251
|
* @remarks Time O(ΣL), Space O(ΣL)
|
|
269
252
|
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
270
253
|
* @returns Array of per-word 'added' flags.
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
* @example
|
|
263
|
+
* // Add multiple words
|
|
264
|
+
* const trie = new Trie();
|
|
265
|
+
* trie.addMany(['cat', 'car', 'card']);
|
|
266
|
+
* console.log(trie.has('cat')); // true;
|
|
267
|
+
* console.log(trie.has('car')); // true;
|
|
268
|
+
* console.log(trie.size); // 3;
|
|
271
269
|
*/
|
|
272
270
|
addMany(words: Iterable<string> | Iterable<R>): boolean[];
|
|
273
271
|
/**
|
|
@@ -275,18 +273,63 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
275
273
|
* @remarks Time O(L), Space O(1)
|
|
276
274
|
* @param word - Word to search for.
|
|
277
275
|
* @returns True if present.
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
* @example
|
|
288
|
+
* // Check if a word exists
|
|
289
|
+
* const dict = new Trie(['apple', 'app', 'application']);
|
|
290
|
+
*
|
|
291
|
+
* console.log(dict.has('app')); // true;
|
|
292
|
+
* console.log(dict.has('apple')); // true;
|
|
293
|
+
* console.log(dict.has('ap')); // false;
|
|
278
294
|
*/
|
|
279
295
|
has(word: string): boolean;
|
|
280
296
|
/**
|
|
281
297
|
* Check whether the trie is empty.
|
|
282
298
|
* @remarks Time O(1), Space O(1)
|
|
283
299
|
* @returns True if size is 0.
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
* @example
|
|
309
|
+
* // Check if empty
|
|
310
|
+
* const trie = new Trie();
|
|
311
|
+
* console.log(trie.isEmpty()); // true;
|
|
312
|
+
* trie.add('word');
|
|
313
|
+
* console.log(trie.isEmpty()); // false;
|
|
284
314
|
*/
|
|
285
315
|
isEmpty(): boolean;
|
|
286
316
|
/**
|
|
287
317
|
* Remove all words and reset to a fresh root.
|
|
288
318
|
* @remarks Time O(1), Space O(1)
|
|
289
319
|
* @returns void
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
* @example
|
|
329
|
+
* // Remove all words
|
|
330
|
+
* const trie = new Trie(['a', 'b', 'c']);
|
|
331
|
+
* trie.clear();
|
|
332
|
+
* console.log(trie.isEmpty()); // true;
|
|
290
333
|
*/
|
|
291
334
|
clear(): void;
|
|
292
335
|
/**
|
|
@@ -294,6 +337,34 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
294
337
|
* @remarks Time O(L), Space O(1)
|
|
295
338
|
* @param word - Word to delete.
|
|
296
339
|
* @returns True if a word was removed.
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
* @example
|
|
352
|
+
* // Trie delete and iteration
|
|
353
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
354
|
+
*
|
|
355
|
+
* // Delete a word
|
|
356
|
+
* trie.delete('card');
|
|
357
|
+
* console.log(trie.has('card')); // false;
|
|
358
|
+
*
|
|
359
|
+
* // Word with same prefix still exists
|
|
360
|
+
* console.log(trie.has('care')); // true;
|
|
361
|
+
*
|
|
362
|
+
* // Size decreased
|
|
363
|
+
* console.log(trie.size); // 5;
|
|
364
|
+
*
|
|
365
|
+
* // Iterate through all words
|
|
366
|
+
* const allWords = [...trie];
|
|
367
|
+
* console.log(allWords.length); // 5;
|
|
297
368
|
*/
|
|
298
369
|
delete(word: string): boolean;
|
|
299
370
|
/**
|
|
@@ -314,6 +385,24 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
314
385
|
* @remarks Time O(L), Space O(1)
|
|
315
386
|
* @param input - String to test as prefix.
|
|
316
387
|
* @returns True if input matches a path from root.
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
* @example
|
|
400
|
+
* // Check if a prefix exists
|
|
401
|
+
* const trie = new Trie(['hello', 'help', 'world']);
|
|
402
|
+
*
|
|
403
|
+
* console.log(trie.hasPrefix('hel')); // true;
|
|
404
|
+
* console.log(trie.hasPrefix('wor')); // true;
|
|
405
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
317
406
|
*/
|
|
318
407
|
hasPrefix(input: string): boolean;
|
|
319
408
|
/**
|
|
@@ -327,6 +416,22 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
327
416
|
* Return the longest common prefix among all words.
|
|
328
417
|
* @remarks Time O(H), Space O(1)
|
|
329
418
|
* @returns The longest common prefix string.
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
* @example
|
|
431
|
+
* // Find shared prefix
|
|
432
|
+
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
433
|
+
*
|
|
434
|
+
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
330
435
|
*/
|
|
331
436
|
getLongestCommonPrefix(): string;
|
|
332
437
|
/**
|
|
@@ -336,12 +441,48 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
336
441
|
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
337
442
|
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
338
443
|
* @returns Array of collected words (at most max).
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
* @example
|
|
456
|
+
* // Trie getWords and prefix search
|
|
457
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
458
|
+
*
|
|
459
|
+
* // Get all words with prefix 'app'
|
|
460
|
+
* const appWords = trie.getWords('app');
|
|
461
|
+
* console.log(appWords); // contains 'app';
|
|
462
|
+
* console.log(appWords); // contains 'apple';
|
|
463
|
+
* console.log(appWords); // contains 'apply';
|
|
464
|
+
* console.log(appWords); // contains 'application';
|
|
465
|
+
* expect(appWords).not.toContain('apricot');
|
|
339
466
|
*/
|
|
340
467
|
getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
|
|
341
468
|
/**
|
|
342
469
|
* Deep clone this trie by iterating and inserting all words.
|
|
343
470
|
* @remarks Time O(ΣL), Space O(ΣL)
|
|
344
471
|
* @returns A new trie with the same words and options.
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
* @example
|
|
481
|
+
* // Create independent copy
|
|
482
|
+
* const trie = new Trie(['hello', 'world']);
|
|
483
|
+
* const copy = trie.clone();
|
|
484
|
+
* copy.delete('hello');
|
|
485
|
+
* console.log(trie.has('hello')); // true;
|
|
345
486
|
*/
|
|
346
487
|
clone(): this;
|
|
347
488
|
/**
|
|
@@ -350,8 +491,37 @@ export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
350
491
|
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
351
492
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
352
493
|
* @returns A new trie containing words that satisfy the predicate.
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
* @example
|
|
503
|
+
* // Filter words
|
|
504
|
+
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
505
|
+
* const result = trie.filter(w => w.startsWith('ca'));
|
|
506
|
+
* console.log(result.size); // 3;
|
|
353
507
|
*/
|
|
354
508
|
filter(predicate: ElementCallback<string, R, boolean>, thisArg?: any): this;
|
|
509
|
+
/**
|
|
510
|
+
* Transform words
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
* @example
|
|
520
|
+
* // Transform words
|
|
521
|
+
* const trie = new Trie(['hello', 'world']);
|
|
522
|
+
* const upper = trie.map(w => w.toUpperCase());
|
|
523
|
+
* console.log(upper.has('HELLO')); // true;
|
|
524
|
+
*/
|
|
355
525
|
map<RM>(callback: ElementCallback<string, R, string>, options?: TrieOptions<RM>, thisArg?: any): Trie<RM>;
|
|
356
526
|
/**
|
|
357
527
|
* Map words into a new trie (possibly different record type).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { LinearBaseOptions } from '../base';
|
|
2
2
|
export type DequeOptions<E, R> = {
|
|
3
3
|
bucketSize?: number;
|
|
4
|
+
/**
|
|
5
|
+
* When the ratio of used buckets to total buckets falls below this threshold
|
|
6
|
+
* after a shift/pop, auto-compact is triggered. Set to 0 to disable.
|
|
7
|
+
* Default: 0.5 (compact when less than half the buckets are in use).
|
|
8
|
+
*/
|
|
9
|
+
autoCompactRatio?: number;
|
|
4
10
|
} & LinearBaseOptions<E, R>;
|