max-priority-queue-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 +63 -0
- package/dist/cjs/index.cjs +403 -98
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +402 -97
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +403 -99
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +402 -98
- 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/max-priority-queue-typed.js +400 -95
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-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
|
@@ -125,84 +125,6 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
125
125
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
126
126
|
*
|
|
127
127
|
* @example
|
|
128
|
-
* // basic BinaryTree creation and insertion
|
|
129
|
-
* // Create a BinaryTree with entries
|
|
130
|
-
* const entries: [number, string][] = [
|
|
131
|
-
* [6, 'six'],
|
|
132
|
-
* [1, 'one'],
|
|
133
|
-
* [2, 'two'],
|
|
134
|
-
* [7, 'seven'],
|
|
135
|
-
* [5, 'five'],
|
|
136
|
-
* [3, 'three'],
|
|
137
|
-
* [4, 'four'],
|
|
138
|
-
* [9, 'nine'],
|
|
139
|
-
* [8, 'eight']
|
|
140
|
-
* ];
|
|
141
|
-
*
|
|
142
|
-
* const tree = new BinaryTree(entries);
|
|
143
|
-
*
|
|
144
|
-
* // Verify size
|
|
145
|
-
* console.log(tree.size); // 9;
|
|
146
|
-
*
|
|
147
|
-
* // Add new element
|
|
148
|
-
* tree.set(10, 'ten');
|
|
149
|
-
* console.log(tree.size); // 10;
|
|
150
|
-
* @example
|
|
151
|
-
* // BinaryTree get and has operations
|
|
152
|
-
* const tree = new BinaryTree(
|
|
153
|
-
* [
|
|
154
|
-
* [5, 'five'],
|
|
155
|
-
* [3, 'three'],
|
|
156
|
-
* [7, 'seven'],
|
|
157
|
-
* [1, 'one'],
|
|
158
|
-
* [4, 'four'],
|
|
159
|
-
* [6, 'six'],
|
|
160
|
-
* [8, 'eight']
|
|
161
|
-
* ],
|
|
162
|
-
* { isMapMode: false }
|
|
163
|
-
* );
|
|
164
|
-
*
|
|
165
|
-
* // Check if key exists
|
|
166
|
-
* console.log(tree.has(5)); // true;
|
|
167
|
-
* console.log(tree.has(10)); // false;
|
|
168
|
-
*
|
|
169
|
-
* // Get value by key
|
|
170
|
-
* console.log(tree.get(3)); // 'three';
|
|
171
|
-
* console.log(tree.get(7)); // 'seven';
|
|
172
|
-
* console.log(tree.get(100)); // undefined;
|
|
173
|
-
*
|
|
174
|
-
* // Get node structure
|
|
175
|
-
* const node = tree.getNode(5);
|
|
176
|
-
* console.log(node?.key); // 5;
|
|
177
|
-
* console.log(node?.value); // 'five';
|
|
178
|
-
* @example
|
|
179
|
-
* // BinaryTree level-order traversal
|
|
180
|
-
* const tree = new BinaryTree([
|
|
181
|
-
* [1, 'one'],
|
|
182
|
-
* [2, 'two'],
|
|
183
|
-
* [3, 'three'],
|
|
184
|
-
* [4, 'four'],
|
|
185
|
-
* [5, 'five'],
|
|
186
|
-
* [6, 'six'],
|
|
187
|
-
* [7, 'seven']
|
|
188
|
-
* ]);
|
|
189
|
-
*
|
|
190
|
-
* // Binary tree maintains level-order insertion
|
|
191
|
-
* // Complete binary tree structure
|
|
192
|
-
* console.log(tree.size); // 7;
|
|
193
|
-
*
|
|
194
|
-
* // Verify all keys are present
|
|
195
|
-
* console.log(tree.has(1)); // true;
|
|
196
|
-
* console.log(tree.has(4)); // true;
|
|
197
|
-
* console.log(tree.has(7)); // true;
|
|
198
|
-
*
|
|
199
|
-
* // Iterate through tree
|
|
200
|
-
* const keys: number[] = [];
|
|
201
|
-
* for (const [key] of tree) {
|
|
202
|
-
* keys.push(key);
|
|
203
|
-
* }
|
|
204
|
-
* console.log(keys.length); // 7;
|
|
205
|
-
* @example
|
|
206
128
|
* // determine loan approval using a decision tree
|
|
207
129
|
* // Decision tree structure
|
|
208
130
|
* const loanDecisionTree = new BinaryTree<string>(
|
|
@@ -435,6 +357,20 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
435
357
|
*
|
|
436
358
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
437
359
|
* @returns True if the addition was successful, false otherwise.
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
* @example
|
|
367
|
+
* // Add a single node
|
|
368
|
+
* const tree = new BinaryTree<number>();
|
|
369
|
+
* tree.add(1);
|
|
370
|
+
* tree.add(2);
|
|
371
|
+
* tree.add(3);
|
|
372
|
+
* console.log(tree.size); // 3;
|
|
373
|
+
* console.log(tree.has(1)); // true;
|
|
438
374
|
*/
|
|
439
375
|
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
|
|
440
376
|
/**
|
|
@@ -444,6 +380,40 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
444
380
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
445
381
|
* @param [value] - The value, if providing just a key.
|
|
446
382
|
* @returns True if the addition was successful, false otherwise.
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
* @example
|
|
395
|
+
* // basic BinaryTree creation and insertion
|
|
396
|
+
* // Create a BinaryTree with entries
|
|
397
|
+
* const entries: [number, string][] = [
|
|
398
|
+
* [6, 'six'],
|
|
399
|
+
* [1, 'one'],
|
|
400
|
+
* [2, 'two'],
|
|
401
|
+
* [7, 'seven'],
|
|
402
|
+
* [5, 'five'],
|
|
403
|
+
* [3, 'three'],
|
|
404
|
+
* [4, 'four'],
|
|
405
|
+
* [9, 'nine'],
|
|
406
|
+
* [8, 'eight']
|
|
407
|
+
* ];
|
|
408
|
+
*
|
|
409
|
+
* const tree = new BinaryTree(entries);
|
|
410
|
+
*
|
|
411
|
+
* // Verify size
|
|
412
|
+
* console.log(tree.size); // 9;
|
|
413
|
+
*
|
|
414
|
+
* // Add new element
|
|
415
|
+
* tree.set(10, 'ten');
|
|
416
|
+
* console.log(tree.size); // 10;
|
|
447
417
|
*/
|
|
448
418
|
set(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
449
419
|
/**
|
|
@@ -452,6 +422,20 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
452
422
|
*
|
|
453
423
|
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
454
424
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
* @example
|
|
435
|
+
* // Bulk add
|
|
436
|
+
* const tree = new BinaryTree<number>();
|
|
437
|
+
* tree.addMany([1, 2, 3, 4, 5]);
|
|
438
|
+
* console.log(tree.size); // 5;
|
|
455
439
|
*/
|
|
456
440
|
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>): boolean[];
|
|
457
441
|
/**
|
|
@@ -461,6 +445,13 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
461
445
|
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
462
446
|
* @param [values] - An optional parallel iterable of values.
|
|
463
447
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
* @example
|
|
451
|
+
* // Set multiple entries
|
|
452
|
+
* const tree = new BinaryTree<number, string>();
|
|
453
|
+
* tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
454
|
+
* console.log(tree.size); // 3;
|
|
464
455
|
*/
|
|
465
456
|
setMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
466
457
|
/**
|
|
@@ -468,6 +459,21 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
468
459
|
* @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
|
|
469
460
|
*
|
|
470
461
|
* @param anotherTree - The tree to merge.
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
* @example
|
|
472
|
+
* // Combine trees
|
|
473
|
+
* const t1 = new BinaryTree<number>([1, 2]);
|
|
474
|
+
* const t2 = new BinaryTree<number>([3, 4]);
|
|
475
|
+
* t1.merge(t2);
|
|
476
|
+
* console.log(t1.size); // 4;
|
|
471
477
|
*/
|
|
472
478
|
merge(anotherTree: BinaryTree<K, V, R>): void;
|
|
473
479
|
/**
|
|
@@ -484,8 +490,35 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
484
490
|
*
|
|
485
491
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
486
492
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
* @example
|
|
505
|
+
* // Remove a node
|
|
506
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
507
|
+
* tree.delete(3);
|
|
508
|
+
* console.log(tree.has(3)); // false;
|
|
509
|
+
* console.log(tree.size); // 4;
|
|
487
510
|
*/
|
|
488
511
|
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
512
|
+
/**
|
|
513
|
+
* Search by predicate
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
* @example
|
|
517
|
+
* // Search by predicate
|
|
518
|
+
* const tree = new BinaryTree<number>([5, 3, 7, 1, 9]);
|
|
519
|
+
* const found = tree.search(n => n!.key > 5, true);
|
|
520
|
+
* console.log(found.length); // >= 1;
|
|
521
|
+
*/
|
|
489
522
|
search(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean): (K | undefined)[];
|
|
490
523
|
search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne: boolean, callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
491
524
|
/**
|
|
@@ -497,6 +530,19 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
497
530
|
* @param [startNode=this._root] - The node to start the search from.
|
|
498
531
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
499
532
|
* @returns An array of matching nodes.
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
* @example
|
|
542
|
+
* // Get nodes by condition
|
|
543
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
544
|
+
* const nodes = tree.getNodes(node => node.key > 3);
|
|
545
|
+
* console.log(nodes.length); // 2;
|
|
500
546
|
*/
|
|
501
547
|
getNodes(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V>[];
|
|
502
548
|
/**
|
|
@@ -507,6 +553,19 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
507
553
|
* @param [startNode=this._root] - The node to start the search from.
|
|
508
554
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
509
555
|
* @returns The first matching node, or undefined if not found.
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
* @example
|
|
566
|
+
* // Get node by key
|
|
567
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
568
|
+
* console.log(tree.getNode(2)?.value); // 'child';
|
|
510
569
|
*/
|
|
511
570
|
getNode(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined;
|
|
512
571
|
/**
|
|
@@ -517,6 +576,22 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
517
576
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
518
577
|
* @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
|
|
519
578
|
* @returns The associated value, or undefined.
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
* @example
|
|
591
|
+
* // Retrieve value by key
|
|
592
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
593
|
+
* console.log(tree.get(2)); // 'left';
|
|
594
|
+
* console.log(tree.get(99)); // undefined;
|
|
520
595
|
*/
|
|
521
596
|
get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;
|
|
522
597
|
/**
|
|
@@ -527,11 +602,64 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
527
602
|
* @param [startNode] - The node to start the search from.
|
|
528
603
|
* @param [iterationType] - The traversal method.
|
|
529
604
|
* @returns True if a matching node exists, false otherwise.
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
* @example
|
|
617
|
+
* // BinaryTree get and has operations
|
|
618
|
+
* const tree = new BinaryTree(
|
|
619
|
+
* [
|
|
620
|
+
* [5, 'five'],
|
|
621
|
+
* [3, 'three'],
|
|
622
|
+
* [7, 'seven'],
|
|
623
|
+
* [1, 'one'],
|
|
624
|
+
* [4, 'four'],
|
|
625
|
+
* [6, 'six'],
|
|
626
|
+
* [8, 'eight']
|
|
627
|
+
* ],
|
|
628
|
+
* { isMapMode: false }
|
|
629
|
+
* );
|
|
630
|
+
*
|
|
631
|
+
* // Check if key exists
|
|
632
|
+
* console.log(tree.has(5)); // true;
|
|
633
|
+
* console.log(tree.has(10)); // false;
|
|
634
|
+
*
|
|
635
|
+
* // Get value by key
|
|
636
|
+
* console.log(tree.get(3)); // 'three';
|
|
637
|
+
* console.log(tree.get(7)); // 'seven';
|
|
638
|
+
* console.log(tree.get(100)); // undefined;
|
|
639
|
+
*
|
|
640
|
+
* // Get node structure
|
|
641
|
+
* const node = tree.getNode(5);
|
|
642
|
+
* console.log(node?.key); // 5;
|
|
643
|
+
* console.log(node?.value); // 'five';
|
|
530
644
|
*/
|
|
531
645
|
has(keyNodeEntryOrPredicate?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
|
|
532
646
|
/**
|
|
533
647
|
* Clears the tree of all nodes and values.
|
|
534
648
|
* @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
* @example
|
|
659
|
+
* // Remove all nodes
|
|
660
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
661
|
+
* tree.clear();
|
|
662
|
+
* console.log(tree.isEmpty()); // true;
|
|
535
663
|
*/
|
|
536
664
|
clear(): void;
|
|
537
665
|
/**
|
|
@@ -539,6 +667,18 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
539
667
|
* @remarks Time O(1), Space O(1)
|
|
540
668
|
*
|
|
541
669
|
* @returns True if the tree has no nodes, false otherwise.
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
* @example
|
|
680
|
+
* // Check empty
|
|
681
|
+
* console.log(new BinaryTree().isEmpty()); // true;
|
|
542
682
|
*/
|
|
543
683
|
isEmpty(): boolean;
|
|
544
684
|
/**
|
|
@@ -556,6 +696,20 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
556
696
|
* @param [startNode=this._root] - The node to start checking from.
|
|
557
697
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
558
698
|
* @returns True if it's a valid BST, false otherwise.
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
* @example
|
|
709
|
+
* // Check BST property
|
|
710
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
711
|
+
* // BinaryTree doesn't guarantee BST order
|
|
712
|
+
* console.log(typeof tree.isBST()); // 'boolean';
|
|
559
713
|
*/
|
|
560
714
|
isBST(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
|
|
561
715
|
/**
|
|
@@ -565,6 +719,22 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
565
719
|
* @param dist - The node to find the depth of.
|
|
566
720
|
* @param [startNode=this._root] - The node to measure depth from (defaults to root).
|
|
567
721
|
* @returns The depth (0 if `dist` is `startNode`).
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
* @example
|
|
734
|
+
* // Get depth of a node
|
|
735
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
736
|
+
* const node = tree.getNode(4);
|
|
737
|
+
* console.log(tree.getDepth(node!)); // 2;
|
|
568
738
|
*/
|
|
569
739
|
getDepth(dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): number;
|
|
570
740
|
/**
|
|
@@ -574,6 +744,21 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
574
744
|
* @param [startNode=this._root] - The node to start measuring from.
|
|
575
745
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
576
746
|
* @returns The height ( -1 for an empty tree, 0 for a single-node tree).
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
* @example
|
|
759
|
+
* // Get tree height
|
|
760
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
761
|
+
* console.log(tree.getHeight()); // 2;
|
|
577
762
|
*/
|
|
578
763
|
getHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number;
|
|
579
764
|
/**
|
|
@@ -607,17 +792,127 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
607
792
|
* @returns The successor node, or null/undefined if none exists.
|
|
608
793
|
*/
|
|
609
794
|
getSuccessor(x?: K | BinaryTreeNode<K, V> | null): BinaryTreeNode<K, V> | null | undefined;
|
|
795
|
+
/**
|
|
796
|
+
* Depth-first search traversal
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
* @example
|
|
809
|
+
* // Depth-first search traversal
|
|
810
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
811
|
+
* const inOrder = tree.dfs(node => node.key, 'IN');
|
|
812
|
+
* console.log(inOrder); // [4, 2, 5, 1, 3];
|
|
813
|
+
*/
|
|
610
814
|
dfs(): (K | undefined)[];
|
|
611
815
|
dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
612
816
|
dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[];
|
|
817
|
+
/**
|
|
818
|
+
* BinaryTree level-order traversal
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
* @example
|
|
831
|
+
* // BinaryTree level-order traversal
|
|
832
|
+
* const tree = new BinaryTree([
|
|
833
|
+
* [1, 'one'],
|
|
834
|
+
* [2, 'two'],
|
|
835
|
+
* [3, 'three'],
|
|
836
|
+
* [4, 'four'],
|
|
837
|
+
* [5, 'five'],
|
|
838
|
+
* [6, 'six'],
|
|
839
|
+
* [7, 'seven']
|
|
840
|
+
* ]);
|
|
841
|
+
*
|
|
842
|
+
* // Binary tree maintains level-order insertion
|
|
843
|
+
* // Complete binary tree structure
|
|
844
|
+
* console.log(tree.size); // 7;
|
|
845
|
+
*
|
|
846
|
+
* // Verify all keys are present
|
|
847
|
+
* console.log(tree.has(1)); // true;
|
|
848
|
+
* console.log(tree.has(4)); // true;
|
|
849
|
+
* console.log(tree.has(7)); // true;
|
|
850
|
+
*
|
|
851
|
+
* // Iterate through tree
|
|
852
|
+
* const keys: number[] = [];
|
|
853
|
+
* for (const [key] of tree) {
|
|
854
|
+
* keys.push(key);
|
|
855
|
+
* }
|
|
856
|
+
* console.log(keys.length); // 7;
|
|
857
|
+
*/
|
|
613
858
|
bfs(): (K | undefined)[];
|
|
614
859
|
bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
615
860
|
bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
861
|
+
/**
|
|
862
|
+
* Get leaf nodes
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
* @example
|
|
873
|
+
* // Get leaf nodes
|
|
874
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
875
|
+
* const leafKeys = tree.leaves(node => node.key);
|
|
876
|
+
* console.log(leafKeys.length); // > 0;
|
|
877
|
+
*/
|
|
616
878
|
leaves(): (K | undefined)[];
|
|
617
879
|
leaves<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
880
|
+
/**
|
|
881
|
+
* Level-order grouping
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
* @example
|
|
891
|
+
* // Level-order grouping
|
|
892
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
893
|
+
* const levels = tree.listLevels(node => node.key);
|
|
894
|
+
* console.log(levels[0]); // [1];
|
|
895
|
+
* console.log(levels[1].sort()); // [2, 3];
|
|
896
|
+
*/
|
|
618
897
|
listLevels(): (K | undefined)[][];
|
|
619
898
|
listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
|
|
620
899
|
listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
900
|
+
/**
|
|
901
|
+
* Morris traversal (O(1) space)
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
* @example
|
|
911
|
+
* // Morris traversal (O(1) space)
|
|
912
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
913
|
+
* const result = tree.morris(node => node.key, 'IN');
|
|
914
|
+
* console.log(result.length); // 3;
|
|
915
|
+
*/
|
|
621
916
|
morris(): (K | undefined)[];
|
|
622
917
|
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[];
|
|
623
918
|
/**
|
|
@@ -625,6 +920,21 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
625
920
|
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
|
|
626
921
|
*
|
|
627
922
|
* @returns A new, cloned instance of the tree.
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
* @example
|
|
933
|
+
* // Deep copy
|
|
934
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
935
|
+
* const copy = tree.clone();
|
|
936
|
+
* copy.delete(1);
|
|
937
|
+
* console.log(tree.has(1)); // true;
|
|
628
938
|
*/
|
|
629
939
|
clone(): this;
|
|
630
940
|
/**
|
|
@@ -634,6 +944,20 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
634
944
|
* @param predicate - A function to test each [key, value] pair.
|
|
635
945
|
* @param [thisArg] - `this` context for the predicate.
|
|
636
946
|
* @returns A new, filtered tree.
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
* @example
|
|
957
|
+
* // Filter nodes by condition
|
|
958
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
959
|
+
* const result = tree.filter((_, key) => key > 2);
|
|
960
|
+
* console.log(result.size); // 2;
|
|
637
961
|
*/
|
|
638
962
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
|
|
639
963
|
/**
|
|
@@ -647,6 +971,20 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
647
971
|
* @param [options] - Options for the new tree.
|
|
648
972
|
* @param [thisArg] - `this` context for the callback.
|
|
649
973
|
* @returns A new, mapped tree.
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
* @example
|
|
984
|
+
* // Transform to new tree
|
|
985
|
+
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
986
|
+
* const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
987
|
+
* console.log([...mapped.values()]); // contains 11;
|
|
650
988
|
*/
|
|
651
989
|
map<MK = K, MV = V, MR = any>(cb: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): BinaryTree<MK, MV, MR>;
|
|
652
990
|
/**
|
|
@@ -664,6 +1002,19 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
664
1002
|
*
|
|
665
1003
|
* @param [options] - Options to control the output.
|
|
666
1004
|
* @param [startNode=this._root] - The node to start printing from.
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
* @example
|
|
1015
|
+
* // Display tree
|
|
1016
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
1017
|
+
* expect(() => tree.print()).not.toThrow();
|
|
667
1018
|
*/
|
|
668
1019
|
print(options?: BinaryTreePrintOptions, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
|
|
669
1020
|
protected _dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRight?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldProcessRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean): ReturnType<C>[];
|
|
@@ -735,6 +1086,16 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
735
1086
|
* @returns Layout information for this subtree.
|
|
736
1087
|
*/
|
|
737
1088
|
protected _displayAux(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
1089
|
+
protected static _buildNodeDisplay(line: string, width: number, left: NodeDisplayLayout, right: NodeDisplayLayout): NodeDisplayLayout;
|
|
1090
|
+
/**
|
|
1091
|
+
* Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
|
|
1092
|
+
*/
|
|
1093
|
+
protected _isDisplayLeaf(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): boolean;
|
|
1094
|
+
protected _hasDisplayableChild(child: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): boolean;
|
|
1095
|
+
/**
|
|
1096
|
+
* Resolve a display leaf node to its layout.
|
|
1097
|
+
*/
|
|
1098
|
+
protected _resolveDisplayLeaf(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions, emptyDisplayLayout: NodeDisplayLayout): NodeDisplayLayout;
|
|
738
1099
|
/**
|
|
739
1100
|
* (Protected) Swaps the key/value properties of two nodes.
|
|
740
1101
|
* @remarks Time O(1)
|