tree-multimap-typed 2.2.2 → 2.2.4
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 +245 -72
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +246 -72
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +245 -72
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +246 -72
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/tree-multimap-typed.js +246 -72
- package/dist/umd/tree-multimap-typed.js.map +1 -1
- package/dist/umd/tree-multimap-typed.min.js +3 -3
- package/dist/umd/tree-multimap-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +100 -7
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +431 -93
- package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tree-multimap-typed",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.4",
|
|
4
4
|
"description": "Tree Multimap",
|
|
5
5
|
"browser": "dist/umd/tree-multimap-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/tree-multimap-typed.min.js",
|
|
@@ -217,6 +217,6 @@
|
|
|
217
217
|
"typescript": "^4.9.5"
|
|
218
218
|
},
|
|
219
219
|
"dependencies": {
|
|
220
|
-
"data-structure-typed": "^2.2.
|
|
220
|
+
"data-structure-typed": "^2.2.4"
|
|
221
221
|
}
|
|
222
222
|
}
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
import type {
|
|
10
10
|
AVLTreeCounterOptions,
|
|
11
11
|
BinaryTreeDeleteResult,
|
|
12
|
-
BinaryTreeOptions,
|
|
13
12
|
BSTNOptKeyOrNode,
|
|
14
13
|
EntryCallback,
|
|
15
14
|
FamilyPosition,
|
|
@@ -404,7 +403,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
404
403
|
*/
|
|
405
404
|
override map<MK = K, MV = V, MR = any>(
|
|
406
405
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
407
|
-
options?: Partial<
|
|
406
|
+
options?: Partial<AVLTreeCounterOptions<MK, MV, MR>>,
|
|
408
407
|
thisArg?: unknown
|
|
409
408
|
): AVLTreeCounter<MK, MV, MR> {
|
|
410
409
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
10
|
AVLTreeMultiMapOptions,
|
|
11
|
-
AVLTreeOptions,
|
|
12
11
|
BTNOptKeyOrNull,
|
|
13
12
|
ElemOf,
|
|
14
13
|
EntryCallback,
|
|
@@ -354,7 +353,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
354
353
|
*/
|
|
355
354
|
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
356
355
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
357
|
-
options?: Partial<
|
|
356
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MVArr, MR>>,
|
|
358
357
|
thisArg?: unknown
|
|
359
358
|
): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
360
359
|
|
|
@@ -371,7 +370,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
371
370
|
*/
|
|
372
371
|
override map<MK = K, MV = V[], MR = any>(
|
|
373
372
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
374
|
-
options?: Partial<
|
|
373
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>,
|
|
375
374
|
thisArg?: unknown
|
|
376
375
|
): AVLTree<MK, MV, MR>;
|
|
377
376
|
|
|
@@ -388,7 +387,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
388
387
|
*/
|
|
389
388
|
override map<MK, MV, MR extends object>(
|
|
390
389
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
391
|
-
options?: Partial<
|
|
390
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>,
|
|
392
391
|
thisArg?: unknown
|
|
393
392
|
): AVLTree<MK, MV, MR> {
|
|
394
393
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -406,10 +405,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
406
405
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
407
406
|
* @returns An empty like-kind instance.
|
|
408
407
|
*/
|
|
409
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
408
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): this {
|
|
410
409
|
const Ctor = this.constructor as unknown as new (
|
|
411
410
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
412
|
-
opts?:
|
|
411
|
+
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
413
412
|
) => AVLTree<TK, TV, TR>;
|
|
414
413
|
return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
|
|
415
414
|
}
|
|
@@ -426,11 +425,11 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
426
425
|
*/
|
|
427
426
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
428
427
|
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
429
|
-
options?: Partial<
|
|
428
|
+
options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>
|
|
430
429
|
): AVLTree<TK, TV, TR> {
|
|
431
430
|
const Ctor = this.constructor as unknown as new (
|
|
432
431
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
433
|
-
opts?:
|
|
432
|
+
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
434
433
|
) => AVLTree<TK, TV, TR>;
|
|
435
434
|
return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
|
|
436
435
|
}
|
|
@@ -17,7 +17,6 @@ import type {
|
|
|
17
17
|
IterationType,
|
|
18
18
|
RBTNColor
|
|
19
19
|
} from '../../types';
|
|
20
|
-
import { BSTOptions } from '../../types';
|
|
21
20
|
import { IBinaryTree } from '../../interfaces';
|
|
22
21
|
|
|
23
22
|
/**
|
|
@@ -198,8 +197,102 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
198
197
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
199
198
|
*
|
|
200
199
|
* @example
|
|
200
|
+
* // basic AVLTree creation and add operation
|
|
201
|
+
* // Create a simple AVLTree with initial values
|
|
202
|
+
* const tree = new AVLTree([5, 2, 8, 1, 9]);
|
|
203
|
+
*
|
|
204
|
+
* tree.print();
|
|
205
|
+
* // _2___
|
|
206
|
+
* // / \
|
|
207
|
+
* // 1 _8_
|
|
208
|
+
* // / \
|
|
209
|
+
* // 5 9
|
|
210
|
+
*
|
|
211
|
+
* // Verify the tree maintains sorted order
|
|
212
|
+
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
213
|
+
*
|
|
214
|
+
* // Check size
|
|
215
|
+
* console.log(tree.size); // 5;
|
|
216
|
+
*
|
|
217
|
+
* // Add a new element
|
|
218
|
+
* tree.add(3);
|
|
219
|
+
* console.log(tree.size); // 6;
|
|
220
|
+
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
221
|
+
* @example
|
|
222
|
+
* // AVLTree has and get operations
|
|
223
|
+
* const tree = new AVLTree<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
224
|
+
*
|
|
225
|
+
* // Check if element exists
|
|
226
|
+
* console.log(tree.has(6)); // true;
|
|
227
|
+
* console.log(tree.has(99)); // false;
|
|
228
|
+
*
|
|
229
|
+
* // Get node by key
|
|
230
|
+
* const node = tree.getNode(6);
|
|
231
|
+
* console.log(node?.key); // 6;
|
|
232
|
+
*
|
|
233
|
+
* // Verify tree is balanced
|
|
234
|
+
* console.log(tree.isAVLBalanced()); // true;
|
|
235
|
+
* @example
|
|
236
|
+
* // AVLTree delete and balance verification
|
|
237
|
+
* const tree = new AVLTree([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
238
|
+
*
|
|
239
|
+
* // Delete an element
|
|
240
|
+
* tree.delete(10);
|
|
241
|
+
* console.log(tree.has(10)); // false;
|
|
242
|
+
*
|
|
243
|
+
* // Tree should remain balanced after deletion
|
|
244
|
+
* console.log(tree.isAVLBalanced()); // true;
|
|
245
|
+
*
|
|
246
|
+
* // Size decreased
|
|
247
|
+
* console.log(tree.size); // 15;
|
|
248
|
+
*
|
|
249
|
+
* // Remaining elements are still sorted
|
|
250
|
+
* const keys = [...tree.keys()];
|
|
251
|
+
* console.log(keys); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16];
|
|
252
|
+
* @example
|
|
253
|
+
* // AVLTree for university ranking system with strict balance
|
|
254
|
+
* interface University {
|
|
255
|
+
* name: string;
|
|
256
|
+
* rank: number;
|
|
257
|
+
* students: number;
|
|
258
|
+
* }
|
|
259
|
+
*
|
|
260
|
+
* // AVLTree provides highest search efficiency with strict balance
|
|
261
|
+
* // (every node's left/right subtrees differ by at most 1 in height)
|
|
262
|
+
* const universityTree = new AVLTree<number, University>([
|
|
263
|
+
* [1, { name: 'MIT', rank: 1, students: 1200 }],
|
|
264
|
+
* [5, { name: 'Stanford', rank: 5, students: 1800 }],
|
|
265
|
+
* [3, { name: 'Harvard', rank: 3, students: 2300 }],
|
|
266
|
+
* [2, { name: 'Caltech', rank: 2, students: 400 }],
|
|
267
|
+
* [4, { name: 'CMU', rank: 4, students: 1500 }]
|
|
268
|
+
* ]);
|
|
269
|
+
*
|
|
270
|
+
* // Quick lookup by rank
|
|
271
|
+
* const mit = universityTree.get(1);
|
|
272
|
+
* console.log(mit?.name); // 'MIT';
|
|
273
|
+
*
|
|
274
|
+
* const cmulevel = universityTree.getHeight(4);
|
|
275
|
+
* console.log(typeof cmulevel); // 'number';
|
|
276
|
+
*
|
|
277
|
+
* // Tree maintains strict balance during insertions and deletions
|
|
278
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
279
|
+
*
|
|
280
|
+
* // Add more universities
|
|
281
|
+
* universityTree.add(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
282
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
283
|
+
*
|
|
284
|
+
* // Delete and verify balance is maintained
|
|
285
|
+
* universityTree.delete(2);
|
|
286
|
+
* console.log(universityTree.has(2)); // false;
|
|
287
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
288
|
+
*
|
|
289
|
+
* // Get all remaining universities in rank order
|
|
290
|
+
* const remainingRanks = [...universityTree.keys()];
|
|
291
|
+
* console.log(remainingRanks); // [1, 3, 4, 5, 6];
|
|
292
|
+
* console.log(universityTree.size); // 5;
|
|
293
|
+
* @example
|
|
201
294
|
* // Find elements in a range
|
|
202
|
-
*
|
|
295
|
+
* // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
|
|
203
296
|
* type Datum = { timestamp: Date; temperature: number };
|
|
204
297
|
* // Fixed dataset of CPU temperature readings
|
|
205
298
|
* const cpuData: Datum[] = [
|
|
@@ -260,7 +353,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
260
353
|
* // { minute: 13, temperature: 60.2 },
|
|
261
354
|
* // { minute: 14, temperature: 59.8 },
|
|
262
355
|
* // { minute: 15, temperature: 58.6 }
|
|
263
|
-
* // ]
|
|
356
|
+
* // ];
|
|
264
357
|
*/
|
|
265
358
|
export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
266
359
|
/**
|
|
@@ -418,10 +511,10 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
418
511
|
* @param [options] - Options for the new tree.
|
|
419
512
|
* @returns A new, empty tree.
|
|
420
513
|
*/
|
|
421
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
514
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this {
|
|
422
515
|
const Ctor = this.constructor as unknown as new (
|
|
423
516
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
424
|
-
opts?:
|
|
517
|
+
opts?: AVLTreeOptions<TK, TV, TR>
|
|
425
518
|
) => this;
|
|
426
519
|
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
427
520
|
}
|
|
@@ -437,11 +530,11 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
437
530
|
*/
|
|
438
531
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
439
532
|
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
440
|
-
options?: Partial<
|
|
533
|
+
options?: Partial<AVLTreeOptions<TK, TV, TR>>
|
|
441
534
|
): AVLTree<TK, TV, TR> {
|
|
442
535
|
const Ctor = this.constructor as unknown as new (
|
|
443
536
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
444
|
-
opts?:
|
|
537
|
+
opts?: AVLTreeOptions<TK, TV, TR>
|
|
445
538
|
) => AVLTree<TK, TV, TR>;
|
|
446
539
|
return new Ctor(iter, { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) });
|
|
447
540
|
}
|
|
@@ -205,8 +205,86 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
205
205
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
206
206
|
*
|
|
207
207
|
* @example
|
|
208
|
+
* // basic BinaryTree creation and insertion
|
|
209
|
+
* // Create a BinaryTree with entries
|
|
210
|
+
* const entries: [number, string][] = [
|
|
211
|
+
* [6, 'six'],
|
|
212
|
+
* [1, 'one'],
|
|
213
|
+
* [2, 'two'],
|
|
214
|
+
* [7, 'seven'],
|
|
215
|
+
* [5, 'five'],
|
|
216
|
+
* [3, 'three'],
|
|
217
|
+
* [4, 'four'],
|
|
218
|
+
* [9, 'nine'],
|
|
219
|
+
* [8, 'eight']
|
|
220
|
+
* ];
|
|
221
|
+
*
|
|
222
|
+
* const tree = new BinaryTree(entries);
|
|
223
|
+
*
|
|
224
|
+
* // Verify size
|
|
225
|
+
* console.log(tree.size); // 9;
|
|
226
|
+
*
|
|
227
|
+
* // Add new element
|
|
228
|
+
* tree.add(10, 'ten');
|
|
229
|
+
* console.log(tree.size); // 10;
|
|
230
|
+
* @example
|
|
231
|
+
* // BinaryTree get and has operations
|
|
232
|
+
* const tree = new BinaryTree(
|
|
233
|
+
* [
|
|
234
|
+
* [5, 'five'],
|
|
235
|
+
* [3, 'three'],
|
|
236
|
+
* [7, 'seven'],
|
|
237
|
+
* [1, 'one'],
|
|
238
|
+
* [4, 'four'],
|
|
239
|
+
* [6, 'six'],
|
|
240
|
+
* [8, 'eight']
|
|
241
|
+
* ],
|
|
242
|
+
* { isMapMode: false }
|
|
243
|
+
* );
|
|
244
|
+
*
|
|
245
|
+
* // Check if key exists
|
|
246
|
+
* console.log(tree.has(5)); // true;
|
|
247
|
+
* console.log(tree.has(10)); // false;
|
|
248
|
+
*
|
|
249
|
+
* // Get value by key
|
|
250
|
+
* console.log(tree.get(3)); // 'three';
|
|
251
|
+
* console.log(tree.get(7)); // 'seven';
|
|
252
|
+
* console.log(tree.get(100)); // undefined;
|
|
253
|
+
*
|
|
254
|
+
* // Get node structure
|
|
255
|
+
* const node = tree.getNode(5);
|
|
256
|
+
* console.log(node?.key); // 5;
|
|
257
|
+
* console.log(node?.value); // 'five';
|
|
258
|
+
* @example
|
|
259
|
+
* // BinaryTree level-order traversal
|
|
260
|
+
* const tree = new BinaryTree([
|
|
261
|
+
* [1, 'one'],
|
|
262
|
+
* [2, 'two'],
|
|
263
|
+
* [3, 'three'],
|
|
264
|
+
* [4, 'four'],
|
|
265
|
+
* [5, 'five'],
|
|
266
|
+
* [6, 'six'],
|
|
267
|
+
* [7, 'seven']
|
|
268
|
+
* ]);
|
|
269
|
+
*
|
|
270
|
+
* // Binary tree maintains level-order insertion
|
|
271
|
+
* // Complete binary tree structure
|
|
272
|
+
* console.log(tree.size); // 7;
|
|
273
|
+
*
|
|
274
|
+
* // Verify all keys are present
|
|
275
|
+
* console.log(tree.has(1)); // true;
|
|
276
|
+
* console.log(tree.has(4)); // true;
|
|
277
|
+
* console.log(tree.has(7)); // true;
|
|
278
|
+
*
|
|
279
|
+
* // Iterate through tree
|
|
280
|
+
* const keys: number[] = [];
|
|
281
|
+
* for (const [key] of tree) {
|
|
282
|
+
* keys.push(key);
|
|
283
|
+
* }
|
|
284
|
+
* console.log(keys.length); // 7;
|
|
285
|
+
* @example
|
|
208
286
|
* // determine loan approval using a decision tree
|
|
209
|
-
*
|
|
287
|
+
* // Decision tree structure
|
|
210
288
|
* const loanDecisionTree = new BinaryTree<string>(
|
|
211
289
|
* ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
|
|
212
290
|
* { isDuplicate: true }
|
|
@@ -228,19 +306,19 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
228
306
|
* }
|
|
229
307
|
*
|
|
230
308
|
* // Test case 1: Stable income and good credit score
|
|
231
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
|
|
309
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved';
|
|
232
310
|
*
|
|
233
311
|
* // Test case 2: Stable income but poor credit score
|
|
234
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
|
|
312
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected';
|
|
235
313
|
*
|
|
236
314
|
* // Test case 3: No stable income
|
|
237
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
|
|
315
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected';
|
|
238
316
|
*
|
|
239
317
|
* // Test case 4: No stable income and poor credit score
|
|
240
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
|
|
318
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected';
|
|
241
319
|
* @example
|
|
242
320
|
* // evaluate the arithmetic expression represented by the binary tree
|
|
243
|
-
*
|
|
321
|
+
* const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
|
|
244
322
|
*
|
|
245
323
|
* function evaluate(node?: BinaryTreeNode<number | string> | null): number {
|
|
246
324
|
* if (!node) return 0;
|
|
@@ -265,7 +343,7 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
265
343
|
* }
|
|
266
344
|
* }
|
|
267
345
|
*
|
|
268
|
-
* console.log(evaluate(expressionTree.root)); // -27
|
|
346
|
+
* console.log(evaluate(expressionTree.root)); // -27;
|
|
269
347
|
*/
|
|
270
348
|
export class BinaryTree<K = any, V = any, R = any>
|
|
271
349
|
extends IterableEntryBase<K, V | undefined>
|
|
@@ -620,6 +698,21 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
620
698
|
return false; // Should not happen if tree is not full?
|
|
621
699
|
}
|
|
622
700
|
|
|
701
|
+
/**
|
|
702
|
+
* Adds or updates a new node to the tree.
|
|
703
|
+
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
|
|
704
|
+
*
|
|
705
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
706
|
+
* @param [value] - The value, if providing just a key.
|
|
707
|
+
* @returns True if the addition was successful, false otherwise.
|
|
708
|
+
*/
|
|
709
|
+
set(
|
|
710
|
+
keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
711
|
+
value?: V
|
|
712
|
+
): boolean {
|
|
713
|
+
return this.add(keyNodeOrEntry, value);
|
|
714
|
+
}
|
|
715
|
+
|
|
623
716
|
/**
|
|
624
717
|
* Adds multiple items to the tree.
|
|
625
718
|
* @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
|
|
@@ -657,6 +750,23 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
657
750
|
return inserted;
|
|
658
751
|
}
|
|
659
752
|
|
|
753
|
+
/**
|
|
754
|
+
* Adds or updates multiple items to the tree.
|
|
755
|
+
* @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
|
|
756
|
+
*
|
|
757
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
758
|
+
* @param [values] - An optional parallel iterable of values.
|
|
759
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
760
|
+
*/
|
|
761
|
+
setMany(
|
|
762
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
763
|
+
K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
764
|
+
>,
|
|
765
|
+
values?: Iterable<V | undefined>
|
|
766
|
+
): boolean[] {
|
|
767
|
+
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
768
|
+
}
|
|
769
|
+
|
|
660
770
|
/**
|
|
661
771
|
* Merges another tree into this one by adding all its nodes.
|
|
662
772
|
* @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
|