max-heap-typed 2.2.5 → 2.2.6
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 +0 -36
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +0 -36
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +0 -36
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +0 -36
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -126
- package/dist/umd/max-heap-typed.js +0 -33
- package/dist/umd/max-heap-typed.js.map +1 -1
- package/dist/umd/max-heap-typed.min.js +1 -1
- package/dist/umd/max-heap-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/index.ts +2 -4
- package/src/data-structures/base/iterable-entry-base.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +295 -89
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator,
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodePredicate, OptNode, RBTNColor } from '../../types';
|
|
9
9
|
import { BinaryTree } from './binary-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { Range } from '../../common';
|
|
@@ -257,7 +257,7 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
257
257
|
* return findFirstCommon(path1, path2);
|
|
258
258
|
* };
|
|
259
259
|
*
|
|
260
|
-
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
260
|
+
* function findFirstCommon(arr1: (number | undefined)[], arr2: (number | undefined)[]): number | undefined {
|
|
261
261
|
* for (const num of arr1) {
|
|
262
262
|
* if (arr2.indexOf(num) !== -1) {
|
|
263
263
|
* return num;
|
|
@@ -335,41 +335,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
335
335
|
* @returns True if the key is valid, false otherwise.
|
|
336
336
|
*/
|
|
337
337
|
isValidKey(key: any): key is K;
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
* @param [pattern='IN'] - The traversal order ('IN', 'PRE', 'POST').
|
|
345
|
-
* @param [onlyOne=false] - If true, stops after the first callback.
|
|
346
|
-
* @param [startNode=this._root] - The node to start from.
|
|
347
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
348
|
-
* @returns An array of callback results.
|
|
349
|
-
*/
|
|
350
|
-
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
351
|
-
/**
|
|
352
|
-
* Performs a Breadth-First Search (BFS) or Level-Order traversal.
|
|
353
|
-
* @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue.
|
|
354
|
-
*
|
|
355
|
-
* @template C - The type of the callback function.
|
|
356
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
357
|
-
* @param [startNode=this._root] - The node to start from.
|
|
358
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
359
|
-
* @returns An array of callback results.
|
|
360
|
-
*/
|
|
361
|
-
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
362
|
-
/**
|
|
363
|
-
* Returns a 2D array of nodes, grouped by level.
|
|
364
|
-
* @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
|
|
365
|
-
*
|
|
366
|
-
* @template C - The type of the callback function.
|
|
367
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
368
|
-
* @param [startNode=this._root] - The node to start from.
|
|
369
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
370
|
-
* @returns A 2D array of callback results.
|
|
371
|
-
*/
|
|
372
|
-
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[][];
|
|
338
|
+
dfs(): (K | undefined)[];
|
|
339
|
+
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
340
|
+
bfs(): (K | undefined)[];
|
|
341
|
+
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
342
|
+
listLevels(): (K | undefined)[][];
|
|
343
|
+
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[][];
|
|
373
344
|
/**
|
|
374
345
|
* Gets the first node matching a predicate.
|
|
375
346
|
* @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
|
|
@@ -380,33 +351,10 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
380
351
|
* @returns The first matching node, or undefined if not found.
|
|
381
352
|
*/
|
|
382
353
|
getNode(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
* Space O(log N) for the stack.
|
|
388
|
-
*
|
|
389
|
-
* @template C - The type of the callback function.
|
|
390
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, predicate, or range to search for.
|
|
391
|
-
* @param [onlyOne=false] - If true, stops after finding the first match.
|
|
392
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
393
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
394
|
-
* @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
|
|
395
|
-
* @returns An array of results from the callback function for each matching node.
|
|
396
|
-
*/
|
|
397
|
-
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
398
|
-
/**
|
|
399
|
-
* Performs an optimized search for nodes within a given key range.
|
|
400
|
-
* @remarks Time O(H + M), where H is tree height and M is the number of matches.
|
|
401
|
-
*
|
|
402
|
-
* @template C - The type of the callback function.
|
|
403
|
-
* @param range - A `Range` object or a `[low, high]` tuple.
|
|
404
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
405
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
406
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
407
|
-
* @returns An array of callback results.
|
|
408
|
-
*/
|
|
409
|
-
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
354
|
+
search(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean): (K | undefined)[];
|
|
355
|
+
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne: boolean, callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
356
|
+
rangeSearch(range: Range<K> | [K, K]): (K | undefined)[];
|
|
357
|
+
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
410
358
|
/**
|
|
411
359
|
* Adds a new node to the BST based on key comparison.
|
|
412
360
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -430,87 +378,59 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
430
378
|
*/
|
|
431
379
|
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
432
380
|
/**
|
|
433
|
-
* Returns the first
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
381
|
+
* Returns the first key with a value >= target.
|
|
382
|
+
* Equivalent to Java TreeMap.ceiling.
|
|
383
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
437
384
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
438
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
439
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
440
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
441
385
|
*/
|
|
442
|
-
|
|
386
|
+
ceiling(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
443
387
|
/**
|
|
444
|
-
* Returns the first node with a key
|
|
445
|
-
*
|
|
446
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
447
|
-
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
388
|
+
* Returns the first node with a key >= target and applies callback.
|
|
389
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
448
390
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
449
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
450
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
451
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
452
391
|
*/
|
|
453
|
-
|
|
392
|
+
ceiling<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
454
393
|
/**
|
|
455
|
-
* Returns the first
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
394
|
+
* Returns the first key with a value > target.
|
|
395
|
+
* Equivalent to Java TreeMap.higher.
|
|
396
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
459
397
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
460
|
-
*
|
|
461
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
462
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
463
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
464
398
|
*/
|
|
465
|
-
|
|
399
|
+
higher(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
466
400
|
/**
|
|
467
|
-
* Returns the first node with a key
|
|
468
|
-
*
|
|
469
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
470
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
401
|
+
* Returns the first node with a key > target and applies callback.
|
|
402
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
471
403
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
472
|
-
*
|
|
473
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
474
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
475
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
476
404
|
*/
|
|
477
|
-
|
|
405
|
+
higher<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
478
406
|
/**
|
|
479
|
-
* Returns the first
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
407
|
+
* Returns the first key with a value <= target.
|
|
408
|
+
* Equivalent to Java TreeMap.floor.
|
|
409
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
483
410
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
484
|
-
*
|
|
485
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
486
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
487
|
-
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
488
411
|
*/
|
|
489
|
-
|
|
412
|
+
floor(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
490
413
|
/**
|
|
491
|
-
* Returns the first node with a key
|
|
492
|
-
*
|
|
493
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
494
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
414
|
+
* Returns the first node with a key <= target and applies callback.
|
|
415
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
495
416
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
496
|
-
*
|
|
497
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
498
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
499
|
-
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
500
417
|
*/
|
|
501
|
-
|
|
418
|
+
floor<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
502
419
|
/**
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
*
|
|
511
|
-
*
|
|
420
|
+
* Returns the first key with a value < target.
|
|
421
|
+
* Equivalent to Java TreeMap.lower.
|
|
422
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
423
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
424
|
+
*/
|
|
425
|
+
lower(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
426
|
+
/**
|
|
427
|
+
* Returns the first node with a key < target and applies callback.
|
|
428
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
429
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
512
430
|
*/
|
|
513
|
-
|
|
431
|
+
lower<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
432
|
+
lesserOrGreaterTraverse(): (K | undefined)[];
|
|
433
|
+
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback: C, lesserOrGreater?: number, targetNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
514
434
|
/**
|
|
515
435
|
* Rebuilds the tree to be perfectly balanced.
|
|
516
436
|
* @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
@@ -932,37 +932,6 @@ var maxHeapTyped = (() => {
|
|
|
932
932
|
}
|
|
933
933
|
};
|
|
934
934
|
|
|
935
|
-
// src/utils/utils.ts
|
|
936
|
-
function isPrimitiveComparable(value) {
|
|
937
|
-
const valueType = typeof value;
|
|
938
|
-
if (valueType === "number") return true;
|
|
939
|
-
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
940
|
-
}
|
|
941
|
-
function tryObjectToPrimitive(obj) {
|
|
942
|
-
if (typeof obj.valueOf === "function") {
|
|
943
|
-
const valueOfResult = obj.valueOf();
|
|
944
|
-
if (valueOfResult !== obj) {
|
|
945
|
-
if (isPrimitiveComparable(valueOfResult)) return valueOfResult;
|
|
946
|
-
if (typeof valueOfResult === "object" && valueOfResult !== null) return tryObjectToPrimitive(valueOfResult);
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
if (typeof obj.toString === "function") {
|
|
950
|
-
const stringResult = obj.toString();
|
|
951
|
-
if (stringResult !== "[object Object]") return stringResult;
|
|
952
|
-
}
|
|
953
|
-
return null;
|
|
954
|
-
}
|
|
955
|
-
function isComparable(value, isForceObjectComparable = false) {
|
|
956
|
-
if (value === null || value === void 0) return false;
|
|
957
|
-
if (isPrimitiveComparable(value)) return true;
|
|
958
|
-
if (typeof value !== "object") return false;
|
|
959
|
-
if (value instanceof Date) return true;
|
|
960
|
-
if (isForceObjectComparable) return true;
|
|
961
|
-
const comparableValue = tryObjectToPrimitive(value);
|
|
962
|
-
if (comparableValue === null || comparableValue === void 0) return false;
|
|
963
|
-
return isPrimitiveComparable(comparableValue);
|
|
964
|
-
}
|
|
965
|
-
|
|
966
935
|
// src/common/index.ts
|
|
967
936
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
968
937
|
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
@@ -975,8 +944,6 @@ var maxHeapTyped = (() => {
|
|
|
975
944
|
this.high = high;
|
|
976
945
|
this.includeLow = includeLow;
|
|
977
946
|
this.includeHigh = includeHigh;
|
|
978
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
979
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
980
947
|
}
|
|
981
948
|
// Determine whether a key is within the range
|
|
982
949
|
isInRange(key, comparator) {
|