binary-tree-typed 2.4.4 → 2.4.5
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 +152 -70
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +150 -68
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +152 -71
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +150 -69
- 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/binary-tree/binary-tree.d.ts +10 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +14 -0
- package/dist/types/data-structures/queue/deque.d.ts +41 -1
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/binary-tree-typed.js +149 -67
- 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 +3 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
- package/src/data-structures/binary-tree/binary-tree.ts +113 -42
- package/src/data-structures/binary-tree/bst.ts +11 -3
- package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
- package/src/data-structures/binary-tree/tree-map.ts +8 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-set.ts +5 -4
- package/src/data-structures/binary-tree/tree-set.ts +7 -6
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +4 -0
- package/src/data-structures/graph/undirected-graph.ts +95 -0
- package/src/data-structures/hash/hash-map.ts +13 -2
- package/src/data-structures/heap/heap.ts +4 -3
- package/src/data-structures/heap/max-heap.ts +2 -3
- package/src/data-structures/matrix/matrix.ts +9 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
- package/src/data-structures/queue/deque.ts +71 -3
- package/src/data-structures/trie/trie.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized error message templates.
|
|
3
|
+
* Keep using native Error/TypeError/RangeError — this only standardizes messages.
|
|
4
|
+
*/
|
|
5
|
+
export declare const ERR: {
|
|
6
|
+
readonly indexOutOfRange: (index: number, min: number, max: number, ctx?: string) => string;
|
|
7
|
+
readonly invalidIndex: (ctx?: string) => string;
|
|
8
|
+
readonly invalidArgument: (reason: string, ctx?: string) => string;
|
|
9
|
+
readonly comparatorRequired: (ctx?: string) => string;
|
|
10
|
+
readonly invalidKey: (reason: string, ctx?: string) => string;
|
|
11
|
+
readonly notAFunction: (name: string, ctx?: string) => string;
|
|
12
|
+
readonly invalidEntry: (ctx?: string) => string;
|
|
13
|
+
readonly invalidNaN: (ctx?: string) => string;
|
|
14
|
+
readonly invalidDate: (ctx?: string) => string;
|
|
15
|
+
readonly reduceEmpty: (ctx?: string) => string;
|
|
16
|
+
readonly callbackReturnType: (expected: string, got: string, ctx?: string) => string;
|
|
17
|
+
readonly invalidOperation: (reason: string, ctx?: string) => string;
|
|
18
|
+
readonly matrixDimensionMismatch: (op: string) => string;
|
|
19
|
+
readonly matrixSingular: () => string;
|
|
20
|
+
readonly matrixNotSquare: () => string;
|
|
21
|
+
readonly matrixNotRectangular: () => string;
|
|
22
|
+
readonly matrixRowMismatch: (expected: number, got: number) => string;
|
|
23
|
+
};
|
|
@@ -735,6 +735,16 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
735
735
|
* @returns Layout information for this subtree.
|
|
736
736
|
*/
|
|
737
737
|
protected _displayAux(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
738
|
+
protected static _buildNodeDisplay(line: string, width: number, left: NodeDisplayLayout, right: NodeDisplayLayout): NodeDisplayLayout;
|
|
739
|
+
/**
|
|
740
|
+
* Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
|
|
741
|
+
*/
|
|
742
|
+
protected _isDisplayLeaf(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): boolean;
|
|
743
|
+
protected _hasDisplayableChild(child: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): boolean;
|
|
744
|
+
/**
|
|
745
|
+
* Resolve a display leaf node to its layout.
|
|
746
|
+
*/
|
|
747
|
+
protected _resolveDisplayLeaf(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions, emptyDisplayLayout: NodeDisplayLayout): NodeDisplayLayout;
|
|
738
748
|
/**
|
|
739
749
|
* (Protected) Swaps the key/value properties of two nodes.
|
|
740
750
|
* @remarks Time O(1)
|
|
@@ -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, BTNRep, CRUD, EntryCallback, FamilyPosition, NodePredicate, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, FamilyPosition, NodePredicate, RBTNColor, IterationType, RedBlackTreeOptions } from '../../types';
|
|
9
9
|
import { BST } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
@@ -375,6 +375,12 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
375
375
|
* @param [thisArg] - See parameter type for details.
|
|
376
376
|
* @returns A new RedBlackTree with mapped entries.
|
|
377
377
|
*/
|
|
378
|
+
/**
|
|
379
|
+
* Red-Black trees are self-balancing — `perfectlyBalance` rebuilds via
|
|
380
|
+
* sorted bulk insert, which naturally produces a balanced RBT.
|
|
381
|
+
* @remarks Time O(N), Space O(N)
|
|
382
|
+
*/
|
|
383
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
378
384
|
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
379
385
|
/**
|
|
380
386
|
* (Internal) Create an empty instance of the same concrete tree type.
|
|
@@ -337,4 +337,48 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
337
337
|
* @remarks Time O(1), Space O(1)
|
|
338
338
|
*/
|
|
339
339
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
|
|
340
|
+
/**
|
|
341
|
+
* The edge connector string used in visual output.
|
|
342
|
+
* Override in subclasses (e.g., '--' for undirected, '->' for directed).
|
|
343
|
+
*/
|
|
344
|
+
protected get _edgeConnector(): string;
|
|
345
|
+
/**
|
|
346
|
+
* Generate a text-based visual representation of the graph.
|
|
347
|
+
*
|
|
348
|
+
* **Adjacency list format:**
|
|
349
|
+
* ```
|
|
350
|
+
* Graph (5 vertices, 6 edges):
|
|
351
|
+
* A -> B (1), C (2)
|
|
352
|
+
* B -> D (3)
|
|
353
|
+
* C -> (no outgoing edges)
|
|
354
|
+
* D -> A (1)
|
|
355
|
+
* E (isolated)
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* @param options - Optional display settings.
|
|
359
|
+
* @param options.showWeight - Whether to show edge weights (default: true).
|
|
360
|
+
* @returns The visual string.
|
|
361
|
+
*/
|
|
362
|
+
toVisual(options?: {
|
|
363
|
+
showWeight?: boolean;
|
|
364
|
+
}): string;
|
|
365
|
+
/**
|
|
366
|
+
* Generate DOT language representation for Graphviz.
|
|
367
|
+
*
|
|
368
|
+
* @param options - Optional display settings.
|
|
369
|
+
* @param options.name - Graph name (default: 'G').
|
|
370
|
+
* @param options.showWeight - Whether to label edges with weight (default: true).
|
|
371
|
+
* @returns DOT format string.
|
|
372
|
+
*/
|
|
373
|
+
toDot(options?: {
|
|
374
|
+
name?: string;
|
|
375
|
+
showWeight?: boolean;
|
|
376
|
+
}): string;
|
|
377
|
+
/**
|
|
378
|
+
* Print the graph to console.
|
|
379
|
+
* @param options - Display settings passed to `toVisual`.
|
|
380
|
+
*/
|
|
381
|
+
print(options?: {
|
|
382
|
+
showWeight?: boolean;
|
|
383
|
+
}): void;
|
|
340
384
|
}
|
|
@@ -157,6 +157,7 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
|
|
|
157
157
|
* @remarks Time O(1), Space O(1)
|
|
158
158
|
*/
|
|
159
159
|
constructor(options?: Partial<GraphOptions<V>>);
|
|
160
|
+
protected get _edgeConnector(): string;
|
|
160
161
|
protected _outEdgeMap: Map<VO, EO[]>;
|
|
161
162
|
get outEdgeMap(): Map<VO, EO[]>;
|
|
162
163
|
set outEdgeMap(v: Map<VO, EO[]>);
|
|
@@ -313,6 +313,20 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
|
|
|
313
313
|
bridges: EO[];
|
|
314
314
|
cutVertices: VO[];
|
|
315
315
|
};
|
|
316
|
+
/**
|
|
317
|
+
* Find biconnected components using edge-stack Tarjan variant.
|
|
318
|
+
* A biconnected component is a maximal biconnected subgraph.
|
|
319
|
+
* @returns Array of edge arrays, each representing a biconnected component.
|
|
320
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
321
|
+
*/
|
|
322
|
+
getBiconnectedComponents(): EO[][];
|
|
323
|
+
/**
|
|
324
|
+
* Detect whether the graph contains a cycle.
|
|
325
|
+
* Uses DFS with parent tracking.
|
|
326
|
+
* @returns `true` if a cycle exists, `false` otherwise.
|
|
327
|
+
* @remarks Time O(V + E), Space O(V)
|
|
328
|
+
*/
|
|
329
|
+
hasCycle(): boolean;
|
|
316
330
|
/**
|
|
317
331
|
* Get bridges discovered by `tarjan()`.
|
|
318
332
|
* @returns Array of edges that are bridges.
|
|
@@ -143,7 +143,10 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
143
143
|
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
144
144
|
* @returns New Deque instance.
|
|
145
145
|
*/
|
|
146
|
-
constructor(elements?: IterableWithSizeOrLength<E
|
|
146
|
+
constructor(elements?: IterableWithSizeOrLength<E>, options?: DequeOptions<E, R>);
|
|
147
|
+
constructor(elements: IterableWithSizeOrLength<R>, options: DequeOptions<E, R> & {
|
|
148
|
+
toElementFn: (rawElement: R) => E;
|
|
149
|
+
});
|
|
147
150
|
protected _bucketSize: number;
|
|
148
151
|
/**
|
|
149
152
|
* Get the current bucket size.
|
|
@@ -151,6 +154,26 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
151
154
|
* @returns Bucket capacity per bucket.
|
|
152
155
|
*/
|
|
153
156
|
get bucketSize(): number;
|
|
157
|
+
protected _autoCompactRatio: number;
|
|
158
|
+
/**
|
|
159
|
+
* Get the auto-compaction ratio.
|
|
160
|
+
* When `elements / (bucketCount * bucketSize)` drops below this ratio after
|
|
161
|
+
* enough shift/pop operations, the deque auto-compacts.
|
|
162
|
+
* @remarks Time O(1), Space O(1)
|
|
163
|
+
* @returns Current ratio threshold. 0 means auto-compact is disabled.
|
|
164
|
+
*/
|
|
165
|
+
get autoCompactRatio(): number;
|
|
166
|
+
/**
|
|
167
|
+
* Set the auto-compaction ratio.
|
|
168
|
+
* @remarks Time O(1), Space O(1)
|
|
169
|
+
* @param value - Ratio in [0,1]. 0 disables auto-compact.
|
|
170
|
+
*/
|
|
171
|
+
set autoCompactRatio(value: number);
|
|
172
|
+
/**
|
|
173
|
+
* Counter for shift/pop operations since last compaction check.
|
|
174
|
+
* Only checks ratio every `_bucketSize` operations to minimize overhead.
|
|
175
|
+
*/
|
|
176
|
+
protected _compactCounter: number;
|
|
154
177
|
protected _bucketFirst: number;
|
|
155
178
|
/**
|
|
156
179
|
* Get the index of the first bucket in use.
|
|
@@ -369,6 +392,23 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
|
|
|
369
392
|
* @remarks Time O(N), Space O(1)
|
|
370
393
|
* @returns void
|
|
371
394
|
*/
|
|
395
|
+
/**
|
|
396
|
+
* (Protected) Trigger auto-compaction if space utilization drops below threshold.
|
|
397
|
+
* Only checks every `_bucketSize` operations to minimize hot-path overhead.
|
|
398
|
+
* Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
|
|
399
|
+
*/
|
|
400
|
+
protected _autoCompact(): void;
|
|
401
|
+
/**
|
|
402
|
+
* Compact the deque by removing unused buckets.
|
|
403
|
+
* @remarks Time O(N), Space O(1)
|
|
404
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
405
|
+
*/
|
|
406
|
+
/**
|
|
407
|
+
* Compact the deque by removing unused buckets.
|
|
408
|
+
* @remarks Time O(N), Space O(1)
|
|
409
|
+
* @returns True if compaction was performed (bucket count reduced).
|
|
410
|
+
*/
|
|
411
|
+
compact(): boolean;
|
|
372
412
|
shrinkToFit(): void;
|
|
373
413
|
/**
|
|
374
414
|
* Deep clone this deque, preserving options.
|
|
@@ -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>;
|
|
@@ -26,6 +26,7 @@ var binaryTreeTyped = (() => {
|
|
|
26
26
|
BinaryTree: () => BinaryTree,
|
|
27
27
|
BinaryTreeNode: () => BinaryTreeNode,
|
|
28
28
|
DFSOperation: () => DFSOperation,
|
|
29
|
+
ERR: () => ERR,
|
|
29
30
|
Range: () => Range
|
|
30
31
|
});
|
|
31
32
|
|
|
@@ -80,6 +81,52 @@ var binaryTreeTyped = (() => {
|
|
|
80
81
|
return (...args) => trampoline(fn(...args));
|
|
81
82
|
}
|
|
82
83
|
|
|
84
|
+
// src/common/error.ts
|
|
85
|
+
var ERR = {
|
|
86
|
+
// Range / index
|
|
87
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
88
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
89
|
+
// Type / argument
|
|
90
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
91
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
92
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
93
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
94
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
95
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
96
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
97
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
98
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
99
|
+
// State / operation
|
|
100
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
101
|
+
// Matrix
|
|
102
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
103
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
104
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
105
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
106
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/common/index.ts
|
|
110
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
111
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
112
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
113
|
+
return DFSOperation2;
|
|
114
|
+
})(DFSOperation || {});
|
|
115
|
+
var Range = class {
|
|
116
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
117
|
+
this.low = low;
|
|
118
|
+
this.high = high;
|
|
119
|
+
this.includeLow = includeLow;
|
|
120
|
+
this.includeHigh = includeHigh;
|
|
121
|
+
}
|
|
122
|
+
// Determine whether a key is within the range
|
|
123
|
+
isInRange(key, comparator) {
|
|
124
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
125
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
126
|
+
return lowCheck && highCheck;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
83
130
|
// src/data-structures/base/iterable-element-base.ts
|
|
84
131
|
var IterableElementBase = class {
|
|
85
132
|
/**
|
|
@@ -102,7 +149,7 @@ var binaryTreeTyped = (() => {
|
|
|
102
149
|
if (options) {
|
|
103
150
|
const { toElementFn } = options;
|
|
104
151
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
105
|
-
else if (toElementFn) throw new TypeError("toElementFn
|
|
152
|
+
else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
|
|
106
153
|
}
|
|
107
154
|
}
|
|
108
155
|
/**
|
|
@@ -258,7 +305,7 @@ var binaryTreeTyped = (() => {
|
|
|
258
305
|
acc = initialValue;
|
|
259
306
|
} else {
|
|
260
307
|
const first = iter.next();
|
|
261
|
-
if (first.done) throw new TypeError(
|
|
308
|
+
if (first.done) throw new TypeError(ERR.reduceEmpty());
|
|
262
309
|
acc = first.value;
|
|
263
310
|
index = 1;
|
|
264
311
|
}
|
|
@@ -1036,27 +1083,6 @@ var binaryTreeTyped = (() => {
|
|
|
1036
1083
|
}
|
|
1037
1084
|
};
|
|
1038
1085
|
|
|
1039
|
-
// src/common/index.ts
|
|
1040
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
1041
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
1042
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
1043
|
-
return DFSOperation2;
|
|
1044
|
-
})(DFSOperation || {});
|
|
1045
|
-
var Range = class {
|
|
1046
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
1047
|
-
this.low = low;
|
|
1048
|
-
this.high = high;
|
|
1049
|
-
this.includeLow = includeLow;
|
|
1050
|
-
this.includeHigh = includeHigh;
|
|
1051
|
-
}
|
|
1052
|
-
// Determine whether a key is within the range
|
|
1053
|
-
isInRange(key, comparator) {
|
|
1054
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
1055
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
1056
|
-
return lowCheck && highCheck;
|
|
1057
|
-
}
|
|
1058
|
-
};
|
|
1059
|
-
|
|
1060
1086
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1061
1087
|
var BinaryTreeNode = class {
|
|
1062
1088
|
/**
|
|
@@ -1192,7 +1218,7 @@ var binaryTreeTyped = (() => {
|
|
|
1192
1218
|
return "MAL_NODE";
|
|
1193
1219
|
}
|
|
1194
1220
|
};
|
|
1195
|
-
var BinaryTree = class extends IterableEntryBase {
|
|
1221
|
+
var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
1196
1222
|
/**
|
|
1197
1223
|
* Creates an instance of BinaryTree.
|
|
1198
1224
|
* @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `set` operation). Space O(N) for storing the nodes.
|
|
@@ -1227,7 +1253,7 @@ var binaryTreeTyped = (() => {
|
|
|
1227
1253
|
if (isMapMode !== void 0) this._isMapMode = isMapMode;
|
|
1228
1254
|
if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
|
|
1229
1255
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
1230
|
-
else if (toEntryFn) throw TypeError("toEntryFn
|
|
1256
|
+
else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
|
|
1231
1257
|
}
|
|
1232
1258
|
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
1233
1259
|
}
|
|
@@ -1455,7 +1481,7 @@ var binaryTreeTyped = (() => {
|
|
|
1455
1481
|
if (!this._root) {
|
|
1456
1482
|
this._setRoot(newNode);
|
|
1457
1483
|
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1458
|
-
this._size = 1;
|
|
1484
|
+
if (newNode !== null) this._size = 1;
|
|
1459
1485
|
return true;
|
|
1460
1486
|
}
|
|
1461
1487
|
const queue = new Queue([this._root]);
|
|
@@ -1487,7 +1513,7 @@ var binaryTreeTyped = (() => {
|
|
|
1487
1513
|
potentialParent.right = newNode;
|
|
1488
1514
|
}
|
|
1489
1515
|
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1490
|
-
this._size++;
|
|
1516
|
+
if (newNode !== null) this._size++;
|
|
1491
1517
|
return true;
|
|
1492
1518
|
}
|
|
1493
1519
|
return false;
|
|
@@ -2056,7 +2082,7 @@ var binaryTreeTyped = (() => {
|
|
|
2056
2082
|
}
|
|
2057
2083
|
/**
|
|
2058
2084
|
* Finds all leaf nodes in the tree.
|
|
2059
|
-
* @remarks Time O(N), visits every node. Space O(H) for recursive
|
|
2085
|
+
* @remarks Time O(N), visits every node. Space O(H) for recursive or iterative stack.
|
|
2060
2086
|
*
|
|
2061
2087
|
* @template C - The type of the callback function.
|
|
2062
2088
|
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
|
|
@@ -2079,15 +2105,15 @@ var binaryTreeTyped = (() => {
|
|
|
2079
2105
|
};
|
|
2080
2106
|
dfs(startNode);
|
|
2081
2107
|
} else {
|
|
2082
|
-
const
|
|
2083
|
-
while (
|
|
2084
|
-
const cur =
|
|
2108
|
+
const stack = [startNode];
|
|
2109
|
+
while (stack.length > 0) {
|
|
2110
|
+
const cur = stack.pop();
|
|
2085
2111
|
if (this.isRealNode(cur)) {
|
|
2086
2112
|
if (this.isLeaf(cur)) {
|
|
2087
2113
|
leaves.push(callback(cur));
|
|
2088
2114
|
}
|
|
2089
|
-
if (this.isRealNode(cur.
|
|
2090
|
-
if (this.isRealNode(cur.
|
|
2115
|
+
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
2116
|
+
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
2091
2117
|
}
|
|
2092
2118
|
}
|
|
2093
2119
|
}
|
|
@@ -2543,42 +2569,98 @@ var binaryTreeTyped = (() => {
|
|
|
2543
2569
|
_displayAux(node, options) {
|
|
2544
2570
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
2545
2571
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
this._displayAux(node.right, options)
|
|
2559
|
-
);
|
|
2560
|
-
} else {
|
|
2561
|
-
const line = node === void 0 ? "U" : "N", width = line.length;
|
|
2562
|
-
return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
|
|
2563
|
-
}
|
|
2564
|
-
function _buildNodeDisplay(line, width, left, right) {
|
|
2565
|
-
const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
|
|
2566
|
-
const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
|
|
2567
|
-
const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
|
|
2568
|
-
const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
|
|
2569
|
-
const mergedLines = [firstLine, secondLine];
|
|
2570
|
-
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
|
2571
|
-
const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
|
|
2572
|
-
const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
|
|
2573
|
-
mergedLines.push(leftLine + " ".repeat(width) + rightLine);
|
|
2572
|
+
const newFrame = (n) => ({
|
|
2573
|
+
node: n,
|
|
2574
|
+
stage: 0,
|
|
2575
|
+
leftLayout: emptyDisplayLayout,
|
|
2576
|
+
rightLayout: emptyDisplayLayout
|
|
2577
|
+
});
|
|
2578
|
+
const stack = [newFrame(node)];
|
|
2579
|
+
let result = emptyDisplayLayout;
|
|
2580
|
+
const setChildResult = (layout) => {
|
|
2581
|
+
if (stack.length === 0) {
|
|
2582
|
+
result = layout;
|
|
2583
|
+
return;
|
|
2574
2584
|
}
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
];
|
|
2585
|
+
const parent = stack[stack.length - 1];
|
|
2586
|
+
if (parent.stage === 1) parent.leftLayout = layout;
|
|
2587
|
+
else parent.rightLayout = layout;
|
|
2588
|
+
};
|
|
2589
|
+
while (stack.length > 0) {
|
|
2590
|
+
const frame = stack[stack.length - 1];
|
|
2591
|
+
const cur = frame.node;
|
|
2592
|
+
if (frame.stage === 0) {
|
|
2593
|
+
if (this._isDisplayLeaf(cur, options)) {
|
|
2594
|
+
stack.pop();
|
|
2595
|
+
const layout = this._resolveDisplayLeaf(cur, options, emptyDisplayLayout);
|
|
2596
|
+
setChildResult(layout);
|
|
2597
|
+
continue;
|
|
2598
|
+
}
|
|
2599
|
+
frame.stage = 1;
|
|
2600
|
+
stack.push(newFrame(cur.left));
|
|
2601
|
+
} else if (frame.stage === 1) {
|
|
2602
|
+
frame.stage = 2;
|
|
2603
|
+
stack.push(newFrame(cur.right));
|
|
2604
|
+
} else {
|
|
2605
|
+
stack.pop();
|
|
2606
|
+
const line = this.isNIL(cur) ? "S" : String(cur.key);
|
|
2607
|
+
const layout = _BinaryTree._buildNodeDisplay(line, line.length, frame.leftLayout, frame.rightLayout);
|
|
2608
|
+
setChildResult(layout);
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
return result;
|
|
2612
|
+
}
|
|
2613
|
+
static _buildNodeDisplay(line, width, left, right) {
|
|
2614
|
+
const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
|
|
2615
|
+
const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
|
|
2616
|
+
const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
|
|
2617
|
+
const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
|
|
2618
|
+
const mergedLines = [firstLine, secondLine];
|
|
2619
|
+
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
|
2620
|
+
const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
|
|
2621
|
+
const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
|
|
2622
|
+
mergedLines.push(leftLine + " ".repeat(width) + rightLine);
|
|
2581
2623
|
}
|
|
2624
|
+
return [
|
|
2625
|
+
mergedLines,
|
|
2626
|
+
leftWidth + width + rightWidth,
|
|
2627
|
+
Math.max(leftHeight, rightHeight) + 2,
|
|
2628
|
+
leftWidth + Math.floor(width / 2)
|
|
2629
|
+
];
|
|
2630
|
+
}
|
|
2631
|
+
/**
|
|
2632
|
+
* Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
|
|
2633
|
+
*/
|
|
2634
|
+
_isDisplayLeaf(node, options) {
|
|
2635
|
+
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
2636
|
+
if (node === null && !isShowNull) return true;
|
|
2637
|
+
if (node === void 0 && !isShowUndefined) return true;
|
|
2638
|
+
if (this.isNIL(node) && !isShowRedBlackNIL) return true;
|
|
2639
|
+
if (node === null || node === void 0) return true;
|
|
2640
|
+
const hasDisplayableLeft = this._hasDisplayableChild(node.left, options);
|
|
2641
|
+
const hasDisplayableRight = this._hasDisplayableChild(node.right, options);
|
|
2642
|
+
return !hasDisplayableLeft && !hasDisplayableRight;
|
|
2643
|
+
}
|
|
2644
|
+
_hasDisplayableChild(child, options) {
|
|
2645
|
+
if (child === null) return !!options.isShowNull;
|
|
2646
|
+
if (child === void 0) return !!options.isShowUndefined;
|
|
2647
|
+
if (this.isNIL(child)) return !!options.isShowRedBlackNIL;
|
|
2648
|
+
return true;
|
|
2649
|
+
}
|
|
2650
|
+
/**
|
|
2651
|
+
* Resolve a display leaf node to its layout.
|
|
2652
|
+
*/
|
|
2653
|
+
_resolveDisplayLeaf(node, options, emptyDisplayLayout) {
|
|
2654
|
+
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
2655
|
+
if (node === null && !isShowNull) return emptyDisplayLayout;
|
|
2656
|
+
if (node === void 0 && !isShowUndefined) return emptyDisplayLayout;
|
|
2657
|
+
if (this.isNIL(node) && !isShowRedBlackNIL) return emptyDisplayLayout;
|
|
2658
|
+
if (node !== null && node !== void 0) {
|
|
2659
|
+
const line2 = this.isNIL(node) ? "S" : String(node.key);
|
|
2660
|
+
return _BinaryTree._buildNodeDisplay(line2, line2.length, emptyDisplayLayout, emptyDisplayLayout);
|
|
2661
|
+
}
|
|
2662
|
+
const line = node === void 0 ? "U" : "N";
|
|
2663
|
+
return _BinaryTree._buildNodeDisplay(line, line.length, [[""], 1, 0, 0], [[""], 1, 0, 0]);
|
|
2582
2664
|
}
|
|
2583
2665
|
/**
|
|
2584
2666
|
* (Protected) Swaps the key/value properties of two nodes.
|