binary-tree-typed 2.3.0 → 2.4.1
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 +38 -19
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +40 -21
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +38 -19
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +40 -21
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/linear-base.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
- package/dist/umd/binary-tree-typed.js +40 -21
- 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/data-structures/base/linear-base.ts +2 -12
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +45 -21
- package/src/data-structures/binary-tree/bst.ts +85 -10
- package/src/data-structures/binary-tree/index.ts +3 -3
- package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
- package/src/data-structures/binary-tree/tree-map.ts +439 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
- package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
- package/src/data-structures/binary-tree/tree-set.ts +407 -0
- package/src/data-structures/queue/deque.ts +10 -0
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/index.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
- package/src/data-structures/binary-tree/tree-counter.ts +0 -575
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
- package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TreeSet (ordered set) — a restricted, native-like API backed by RedBlackTree.
|
|
3
|
+
*
|
|
4
|
+
* Design goals:
|
|
5
|
+
* - No node exposure (no node inputs/outputs)
|
|
6
|
+
* - Native Set-like surface + Java NavigableSet-like helpers
|
|
7
|
+
* - Strict default comparator (number/string/Date), otherwise require comparator
|
|
8
|
+
*/
|
|
9
|
+
import type { Comparator } from '../../types';
|
|
10
|
+
import type { TreeSetElementCallback, TreeSetOptions, TreeSetRangeOptions, TreeSetReduceCallback } from '../../types';
|
|
11
|
+
/**
|
|
12
|
+
* An ordered Set backed by a red-black tree.
|
|
13
|
+
*
|
|
14
|
+
* - Iteration order is ascending by key.
|
|
15
|
+
* - No node exposure: all APIs use keys only.
|
|
16
|
+
*/
|
|
17
|
+
export declare class TreeSet<K = any, R = K> implements Iterable<K> {
|
|
18
|
+
#private;
|
|
19
|
+
/**
|
|
20
|
+
* Create a TreeSet from an iterable of keys or raw elements.
|
|
21
|
+
*
|
|
22
|
+
* @param elements - Iterable of keys, or raw elements if `toElementFn` is provided.
|
|
23
|
+
* @param options - Configuration options including optional `toElementFn` to transform raw elements.
|
|
24
|
+
* @throws {TypeError} When using the default comparator and encountering unsupported key types,
|
|
25
|
+
* or invalid keys (e.g. `NaN`, invalid `Date`).
|
|
26
|
+
* @example
|
|
27
|
+
* // Standard usage with keys
|
|
28
|
+
* const set = new TreeSet([3, 1, 2]);
|
|
29
|
+
*
|
|
30
|
+
* // Using toElementFn to transform raw objects
|
|
31
|
+
* const users = [{ id: 3, name: 'Alice' }, { id: 1, name: 'Bob' }];
|
|
32
|
+
* const set = new TreeSet<number, User>(users, { toElementFn: u => u.id });
|
|
33
|
+
*/
|
|
34
|
+
constructor(elements?: Iterable<R> | Iterable<K>, options?: TreeSetOptions<K, R>);
|
|
35
|
+
/**
|
|
36
|
+
* Create the strict default comparator.
|
|
37
|
+
*
|
|
38
|
+
* Supports:
|
|
39
|
+
* - `number` (rejects `NaN`; treats `-0` and `0` as equal)
|
|
40
|
+
* - `string`
|
|
41
|
+
* - `Date` (orders by `getTime()`, rejects invalid dates)
|
|
42
|
+
*
|
|
43
|
+
* For other key types, a custom comparator must be provided.
|
|
44
|
+
*/
|
|
45
|
+
static createDefaultComparator<K>(): Comparator<K>;
|
|
46
|
+
/**
|
|
47
|
+
* Number of elements in the set.
|
|
48
|
+
*/
|
|
49
|
+
get size(): number;
|
|
50
|
+
/**
|
|
51
|
+
* Whether the set is empty.
|
|
52
|
+
*/
|
|
53
|
+
isEmpty(): boolean;
|
|
54
|
+
private _validateKey;
|
|
55
|
+
/**
|
|
56
|
+
* Add a key to the set (no-op if already present).
|
|
57
|
+
* @remarks Expected time O(log n)
|
|
58
|
+
*/
|
|
59
|
+
add(key: K): this;
|
|
60
|
+
/**
|
|
61
|
+
* Test whether a key exists.
|
|
62
|
+
* @remarks Expected time O(log n)
|
|
63
|
+
*/
|
|
64
|
+
has(key: K): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Delete a key.
|
|
67
|
+
* @returns `true` if the key existed; otherwise `false`.
|
|
68
|
+
* @remarks Expected time O(log n)
|
|
69
|
+
*/
|
|
70
|
+
delete(key: K): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Remove all keys.
|
|
73
|
+
*/
|
|
74
|
+
clear(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Iterate over keys in ascending order.
|
|
77
|
+
*/
|
|
78
|
+
keys(): IterableIterator<K>;
|
|
79
|
+
/**
|
|
80
|
+
* Iterate over values in ascending order.
|
|
81
|
+
*
|
|
82
|
+
* Note: for Set-like containers, `values()` is the same as `keys()`.
|
|
83
|
+
*/
|
|
84
|
+
values(): IterableIterator<K>;
|
|
85
|
+
/**
|
|
86
|
+
* Iterate over `[value, value]` pairs (native Set convention).
|
|
87
|
+
*
|
|
88
|
+
* Note: TreeSet stores only keys internally; `[k, k]` is created on-the-fly during iteration.
|
|
89
|
+
*/
|
|
90
|
+
entries(): IterableIterator<[K, K]>;
|
|
91
|
+
[Symbol.iterator](): IterableIterator<K>;
|
|
92
|
+
/**
|
|
93
|
+
* Visit each value in ascending order.
|
|
94
|
+
*
|
|
95
|
+
* Callback follows native Set convention: `(value, value2, set)`.
|
|
96
|
+
*/
|
|
97
|
+
forEach(cb: (value: K, value2: K, set: TreeSet<K>) => void, thisArg?: any): void;
|
|
98
|
+
/**
|
|
99
|
+
* Create a new TreeSet by mapping each value to a new key.
|
|
100
|
+
*
|
|
101
|
+
* This mirrors `RedBlackTree.map`: mapping produces a new ordered container.
|
|
102
|
+
* @remarks Time O(n log n) expected, Space O(n)
|
|
103
|
+
*/
|
|
104
|
+
map<MK>(callbackfn: TreeSetElementCallback<K, MK, TreeSet<K>>, options?: Omit<TreeSetOptions<MK>, 'toElementFn'> & {
|
|
105
|
+
comparator?: (a: MK, b: MK) => number;
|
|
106
|
+
}, thisArg?: unknown): TreeSet<MK>;
|
|
107
|
+
/**
|
|
108
|
+
* Create a new TreeSet containing only values that satisfy the predicate.
|
|
109
|
+
* @remarks Time O(n log n) expected, Space O(n)
|
|
110
|
+
*/
|
|
111
|
+
filter(callbackfn: TreeSetElementCallback<K, boolean, TreeSet<K>>, thisArg?: unknown): TreeSet<K>;
|
|
112
|
+
/**
|
|
113
|
+
* Reduce values into a single accumulator.
|
|
114
|
+
* @remarks Time O(n), Space O(1)
|
|
115
|
+
*/
|
|
116
|
+
reduce<A>(callbackfn: TreeSetReduceCallback<K, A, TreeSet<K>>, initialValue: A): A;
|
|
117
|
+
/**
|
|
118
|
+
* Test whether all values satisfy a predicate.
|
|
119
|
+
* @remarks Time O(n), Space O(1)
|
|
120
|
+
*/
|
|
121
|
+
every(callbackfn: TreeSetElementCallback<K, boolean, TreeSet<K>>, thisArg?: unknown): boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Test whether any value satisfies a predicate.
|
|
124
|
+
* @remarks Time O(n), Space O(1)
|
|
125
|
+
*/
|
|
126
|
+
some(callbackfn: TreeSetElementCallback<K, boolean, TreeSet<K>>, thisArg?: unknown): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Find the first value that satisfies a predicate.
|
|
129
|
+
* @remarks Time O(n), Space O(1)
|
|
130
|
+
*/
|
|
131
|
+
find(callbackfn: TreeSetElementCallback<K, boolean, TreeSet<K>>, thisArg?: unknown): K | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* Materialize the set into an array of keys.
|
|
134
|
+
* @remarks Time O(n), Space O(n)
|
|
135
|
+
*/
|
|
136
|
+
toArray(): K[];
|
|
137
|
+
/**
|
|
138
|
+
* Print a human-friendly representation.
|
|
139
|
+
* @remarks Time O(n), Space O(n)
|
|
140
|
+
*/
|
|
141
|
+
print(): void;
|
|
142
|
+
/**
|
|
143
|
+
* Smallest key in the set.
|
|
144
|
+
*/
|
|
145
|
+
first(): K | undefined;
|
|
146
|
+
/**
|
|
147
|
+
* Largest key in the set.
|
|
148
|
+
*/
|
|
149
|
+
last(): K | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* Remove and return the smallest key.
|
|
152
|
+
*/
|
|
153
|
+
pollFirst(): K | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Remove and return the largest key.
|
|
156
|
+
*/
|
|
157
|
+
pollLast(): K | undefined;
|
|
158
|
+
/**
|
|
159
|
+
* Smallest key that is >= the given key.
|
|
160
|
+
*/
|
|
161
|
+
ceiling(key: K): K | undefined;
|
|
162
|
+
/**
|
|
163
|
+
* Largest key that is <= the given key.
|
|
164
|
+
*/
|
|
165
|
+
floor(key: K): K | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* Smallest key that is > the given key.
|
|
168
|
+
*/
|
|
169
|
+
higher(key: K): K | undefined;
|
|
170
|
+
/**
|
|
171
|
+
* Largest key that is < the given key.
|
|
172
|
+
*/
|
|
173
|
+
lower(key: K): K | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* Return all keys in a given range.
|
|
176
|
+
*
|
|
177
|
+
* @param range `[low, high]`
|
|
178
|
+
* @param options Inclusive/exclusive bounds (defaults to inclusive).
|
|
179
|
+
*/
|
|
180
|
+
rangeSearch(range: [K, K], options?: TreeSetRangeOptions): K[];
|
|
181
|
+
}
|
|
@@ -11,7 +11,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
|
|
|
11
11
|
readonly isMapMode: boolean;
|
|
12
12
|
iterationType: IterationType;
|
|
13
13
|
readonly NIL: BinaryTreeNode<K, V>;
|
|
14
|
-
readonly store: Map<K, V
|
|
14
|
+
readonly store: Map<K, BinaryTreeNode<K, V>>;
|
|
15
15
|
readonly toEntryFn?: ToEntryFn<K, V, R>;
|
|
16
16
|
readonly isDuplicate: boolean;
|
|
17
17
|
createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
|
|
@@ -19,7 +19,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
|
|
|
19
19
|
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
20
20
|
set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
21
21
|
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
22
|
-
delete(keyNodeEntryRawOrPredicate:
|
|
22
|
+
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
23
23
|
clear(): void;
|
|
24
24
|
isEmpty(): boolean;
|
|
25
25
|
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;
|
|
@@ -2,8 +2,8 @@ export * from './binary-tree';
|
|
|
2
2
|
export * from './bst';
|
|
3
3
|
export * from './avl-tree';
|
|
4
4
|
export * from './segment-tree';
|
|
5
|
-
export * from './avl-tree-multi-map';
|
|
6
5
|
export * from './red-black-tree';
|
|
7
6
|
export * from './tree-multi-map';
|
|
8
|
-
export * from './tree-
|
|
9
|
-
export * from './
|
|
7
|
+
export * from './tree-set';
|
|
8
|
+
export * from './tree-map';
|
|
9
|
+
export * from './tree-multi-set';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Comparator } from '../../common';
|
|
2
|
+
export interface TreeMapOptions<K, V, R = [K, V]> {
|
|
3
|
+
comparator?: Comparator<K>;
|
|
4
|
+
/**
|
|
5
|
+
* Pass-through to the underlying RedBlackTree/BST `isMapMode` option.
|
|
6
|
+
*
|
|
7
|
+
* - `true` (default in core): store values in an internal key→value store.
|
|
8
|
+
* - `false`: store values on tree nodes (Node Mode).
|
|
9
|
+
*/
|
|
10
|
+
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Transform raw elements into `[key, value]` entries.
|
|
13
|
+
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<[K, V]>`.
|
|
14
|
+
*/
|
|
15
|
+
toEntryFn?: (rawElement: R) => [K, V];
|
|
16
|
+
}
|
|
17
|
+
export type TreeMapRangeOptions = {
|
|
18
|
+
lowInclusive?: boolean;
|
|
19
|
+
highInclusive?: boolean;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Callback used by TreeMap entry-wise utilities.
|
|
23
|
+
*
|
|
24
|
+
* `SELF` is intentionally generic to avoid type-layer circular imports.
|
|
25
|
+
* Implementations (e.g. `TreeMap<K, V>`) should bind `SELF` at use sites.
|
|
26
|
+
*/
|
|
27
|
+
export type TreeMapEntryCallback<K, V, R, SELF> = (value: V | undefined, key: K, index: number, map: SELF) => R;
|
|
28
|
+
/**
|
|
29
|
+
* Reducer callback used by TreeMap.
|
|
30
|
+
*
|
|
31
|
+
* `SELF` is intentionally generic to avoid type-layer circular imports.
|
|
32
|
+
*/
|
|
33
|
+
export type TreeMapReduceCallback<K, V, A, SELF> = (acc: A, value: V | undefined, key: K, index: number, map: SELF) => A;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Comparator } from '../../common';
|
|
2
|
+
export interface TreeMultiSetOptions<K, R = K> {
|
|
3
|
+
comparator?: Comparator<K>;
|
|
4
|
+
/**
|
|
5
|
+
* Pass-through to the underlying RedBlackTree/BST `isMapMode` option.
|
|
6
|
+
*
|
|
7
|
+
* - `true` (recommended): MapMode store uses key→node index for fast lookups.
|
|
8
|
+
* - `false`: Node Mode.
|
|
9
|
+
*/
|
|
10
|
+
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Transform raw elements into keys.
|
|
13
|
+
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
|
|
14
|
+
*/
|
|
15
|
+
toElementFn?: (rawElement: R) => K;
|
|
16
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Comparator } from '../../common';
|
|
2
|
+
export interface TreeSetOptions<K, R = K> {
|
|
3
|
+
comparator?: Comparator<K>;
|
|
4
|
+
/**
|
|
5
|
+
* Pass-through to the underlying RedBlackTree/BST `isMapMode` option.
|
|
6
|
+
*
|
|
7
|
+
* - `true` (default in core): store values in an internal key→value store.
|
|
8
|
+
* - `false`: store values on tree nodes (Node Mode).
|
|
9
|
+
*/
|
|
10
|
+
isMapMode?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Transform raw elements into keys.
|
|
13
|
+
* When provided, the constructor accepts `Iterable<R>` instead of `Iterable<K>`.
|
|
14
|
+
*/
|
|
15
|
+
toElementFn?: (rawElement: R) => K;
|
|
16
|
+
}
|
|
17
|
+
export type TreeSetRangeOptions = {
|
|
18
|
+
lowInclusive?: boolean;
|
|
19
|
+
highInclusive?: boolean;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Callback used by TreeSet element-wise utilities.
|
|
23
|
+
*
|
|
24
|
+
* `SELF` is intentionally generic to avoid type-layer circular imports.
|
|
25
|
+
* Implementations (e.g. `TreeSet<K>`) should bind `SELF` at use sites.
|
|
26
|
+
*/
|
|
27
|
+
export type TreeSetElementCallback<K, R, SELF> = (value: K, index: number, set: SELF) => R;
|
|
28
|
+
/**
|
|
29
|
+
* Reducer callback used by TreeSet.
|
|
30
|
+
*
|
|
31
|
+
* `SELF` is intentionally generic to avoid type-layer circular imports.
|
|
32
|
+
*/
|
|
33
|
+
export type TreeSetReduceCallback<K, A, SELF> = (acc: A, value: K, index: number, set: SELF) => A;
|
|
@@ -1205,6 +1205,9 @@ var binaryTreeTyped = (() => {
|
|
|
1205
1205
|
__publicField(this, "iterationType", "ITERATIVE");
|
|
1206
1206
|
__publicField(this, "_isMapMode", true);
|
|
1207
1207
|
__publicField(this, "_isDuplicate", false);
|
|
1208
|
+
// Map mode acceleration store:
|
|
1209
|
+
// - isMapMode=false: unused
|
|
1210
|
+
// - isMapMode=true: key -> node reference (O(1) has/getNode + fast get)
|
|
1208
1211
|
__publicField(this, "_store", /* @__PURE__ */ new Map());
|
|
1209
1212
|
__publicField(this, "_root");
|
|
1210
1213
|
__publicField(this, "_size", 0);
|
|
@@ -1300,7 +1303,7 @@ var binaryTreeTyped = (() => {
|
|
|
1300
1303
|
* @returns The newly created node.
|
|
1301
1304
|
*/
|
|
1302
1305
|
createNode(key, value) {
|
|
1303
|
-
return new BinaryTreeNode(key,
|
|
1306
|
+
return new BinaryTreeNode(key, value);
|
|
1304
1307
|
}
|
|
1305
1308
|
/**
|
|
1306
1309
|
* Creates a new, empty tree of the same type and configuration.
|
|
@@ -1447,11 +1450,11 @@ var binaryTreeTyped = (() => {
|
|
|
1447
1450
|
* @returns True if the addition was successful, false otherwise.
|
|
1448
1451
|
*/
|
|
1449
1452
|
set(keyNodeOrEntry, value) {
|
|
1450
|
-
const [newNode
|
|
1453
|
+
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1451
1454
|
if (newNode === void 0) return false;
|
|
1452
1455
|
if (!this._root) {
|
|
1453
1456
|
this._setRoot(newNode);
|
|
1454
|
-
if (this._isMapMode
|
|
1457
|
+
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1455
1458
|
this._size = 1;
|
|
1456
1459
|
return true;
|
|
1457
1460
|
}
|
|
@@ -1463,7 +1466,7 @@ var binaryTreeTyped = (() => {
|
|
|
1463
1466
|
if (!this._isDuplicate) {
|
|
1464
1467
|
if (newNode !== null && cur.key === newNode.key) {
|
|
1465
1468
|
this._replaceNode(cur, newNode);
|
|
1466
|
-
if (this._isMapMode) this.
|
|
1469
|
+
if (this._isMapMode && newNode !== null) this._store.set(cur.key, newNode);
|
|
1467
1470
|
return true;
|
|
1468
1471
|
}
|
|
1469
1472
|
}
|
|
@@ -1483,7 +1486,7 @@ var binaryTreeTyped = (() => {
|
|
|
1483
1486
|
} else if (potentialParent.right === void 0) {
|
|
1484
1487
|
potentialParent.right = newNode;
|
|
1485
1488
|
}
|
|
1486
|
-
if (this._isMapMode
|
|
1489
|
+
if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
|
|
1487
1490
|
this._size++;
|
|
1488
1491
|
return true;
|
|
1489
1492
|
}
|
|
@@ -1550,13 +1553,13 @@ var binaryTreeTyped = (() => {
|
|
|
1550
1553
|
* Deletes a node from the tree.
|
|
1551
1554
|
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
|
|
1552
1555
|
*
|
|
1553
|
-
* @param
|
|
1556
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
1554
1557
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
1555
1558
|
*/
|
|
1556
|
-
delete(
|
|
1559
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
1557
1560
|
const deletedResult = [];
|
|
1558
1561
|
if (!this._root) return deletedResult;
|
|
1559
|
-
const curr = this.getNode(
|
|
1562
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
1560
1563
|
if (!curr) return deletedResult;
|
|
1561
1564
|
const parent = curr == null ? void 0 : curr.parent;
|
|
1562
1565
|
let needBalanced;
|
|
@@ -1568,6 +1571,10 @@ var binaryTreeTyped = (() => {
|
|
|
1568
1571
|
if (leftSubTreeRightMost) {
|
|
1569
1572
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
1570
1573
|
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
1574
|
+
if (this._isMapMode) {
|
|
1575
|
+
this._store.set(curr.key, curr);
|
|
1576
|
+
this._store.set(leftSubTreeRightMost.key, leftSubTreeRightMost);
|
|
1577
|
+
}
|
|
1571
1578
|
if (parentOfLeftSubTreeMax) {
|
|
1572
1579
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
1573
1580
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -1651,6 +1658,13 @@ var binaryTreeTyped = (() => {
|
|
|
1651
1658
|
* @returns The first matching node, or undefined if not found.
|
|
1652
1659
|
*/
|
|
1653
1660
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1661
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
|
|
1662
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1663
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1664
|
+
if (key === null || key === void 0) return;
|
|
1665
|
+
return this._store.get(key);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1654
1668
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
1655
1669
|
}
|
|
1656
1670
|
/**
|
|
@@ -1663,15 +1677,22 @@ var binaryTreeTyped = (() => {
|
|
|
1663
1677
|
* @returns The associated value, or undefined.
|
|
1664
1678
|
*/
|
|
1665
1679
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1666
|
-
var _a;
|
|
1680
|
+
var _a, _b;
|
|
1667
1681
|
if (this._isMapMode) {
|
|
1668
1682
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1669
1683
|
if (key === null || key === void 0) return;
|
|
1670
|
-
return this._store.get(key);
|
|
1684
|
+
return (_a = this._store.get(key)) == null ? void 0 : _a.value;
|
|
1671
1685
|
}
|
|
1672
|
-
return (
|
|
1686
|
+
return (_b = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) == null ? void 0 : _b.value;
|
|
1673
1687
|
}
|
|
1674
1688
|
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1689
|
+
if (this._isMapMode && keyNodeEntryOrPredicate !== void 0 && keyNodeEntryOrPredicate !== null) {
|
|
1690
|
+
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
1691
|
+
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
1692
|
+
if (key === null || key === void 0) return false;
|
|
1693
|
+
return this._store.has(key);
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1675
1696
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
1676
1697
|
}
|
|
1677
1698
|
/**
|
|
@@ -1740,7 +1761,7 @@ var binaryTreeTyped = (() => {
|
|
|
1740
1761
|
}
|
|
1741
1762
|
return true;
|
|
1742
1763
|
};
|
|
1743
|
-
const isStandardBST = checkBST(
|
|
1764
|
+
const isStandardBST = checkBST();
|
|
1744
1765
|
const isInverseBST = checkBST(true);
|
|
1745
1766
|
return isStandardBST || isInverseBST;
|
|
1746
1767
|
}
|
|
@@ -2416,8 +2437,7 @@ var binaryTreeTyped = (() => {
|
|
|
2416
2437
|
}
|
|
2417
2438
|
current = stack.pop();
|
|
2418
2439
|
if (this.isRealNode(current)) {
|
|
2419
|
-
|
|
2420
|
-
else yield [current.key, current.value];
|
|
2440
|
+
yield [current.key, current.value];
|
|
2421
2441
|
current = current.right;
|
|
2422
2442
|
}
|
|
2423
2443
|
}
|
|
@@ -2425,8 +2445,7 @@ var binaryTreeTyped = (() => {
|
|
|
2425
2445
|
if (node.left && this.isRealNode(node)) {
|
|
2426
2446
|
yield* this[Symbol.iterator](node.left);
|
|
2427
2447
|
}
|
|
2428
|
-
|
|
2429
|
-
else yield [node.key, node.value];
|
|
2448
|
+
yield [node.key, node.value];
|
|
2430
2449
|
if (node.right && this.isRealNode(node)) {
|
|
2431
2450
|
yield* this[Symbol.iterator](node.right);
|
|
2432
2451
|
}
|
|
@@ -2504,8 +2523,7 @@ var binaryTreeTyped = (() => {
|
|
|
2504
2523
|
(node) => {
|
|
2505
2524
|
if (node === null) cloned.set(null);
|
|
2506
2525
|
else {
|
|
2507
|
-
|
|
2508
|
-
else cloned.set([node.key, node.value]);
|
|
2526
|
+
cloned.set([node.key, node.value]);
|
|
2509
2527
|
}
|
|
2510
2528
|
},
|
|
2511
2529
|
this._root,
|
|
@@ -2513,7 +2531,6 @@ var binaryTreeTyped = (() => {
|
|
|
2513
2531
|
true
|
|
2514
2532
|
// Include nulls
|
|
2515
2533
|
);
|
|
2516
|
-
if (this._isMapMode) cloned._store = this._store;
|
|
2517
2534
|
}
|
|
2518
2535
|
/**
|
|
2519
2536
|
* (Protected) Recursive helper for `toVisual`.
|
|
@@ -2676,8 +2693,10 @@ var binaryTreeTyped = (() => {
|
|
|
2676
2693
|
*/
|
|
2677
2694
|
_setValue(key, value) {
|
|
2678
2695
|
if (key === null || key === void 0) return false;
|
|
2679
|
-
|
|
2680
|
-
|
|
2696
|
+
const node = this._store.get(key);
|
|
2697
|
+
if (!node) return false;
|
|
2698
|
+
node.value = value;
|
|
2699
|
+
return true;
|
|
2681
2700
|
}
|
|
2682
2701
|
/**
|
|
2683
2702
|
* (Protected) Clears all nodes from the tree.
|