binary-tree-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/README.md +91 -7
- package/dist/cjs/index.cjs +22 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +22 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +22 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +22 -0
- 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/binary-tree-typed.js +22 -0
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +2 -2
- package/dist/umd/binary-tree-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
|
@@ -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 { AVLTreeCounterOptions, BinaryTreeDeleteResult,
|
|
8
|
+
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { AVLTree } from './avl-tree';
|
|
11
11
|
/**
|
|
@@ -186,7 +186,7 @@ export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K
|
|
|
186
186
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
187
187
|
* @returns A new AVLTreeCounter with mapped entries.
|
|
188
188
|
*/
|
|
189
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
189
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<AVLTreeCounterOptions<MK, MV, MR>>, thisArg?: unknown): AVLTreeCounter<MK, MV, MR>;
|
|
190
190
|
/**
|
|
191
191
|
* (Protected) Create an empty instance of the same concrete class.
|
|
192
192
|
* @remarks Time O(1), Space 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 { AVLTreeMultiMapOptions,
|
|
8
|
+
import type { AVLTreeMultiMapOptions, ElemOf, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
9
9
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -160,7 +160,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
160
160
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
161
161
|
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
|
|
162
162
|
*/
|
|
163
|
-
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<
|
|
163
|
+
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<AVLTreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
164
164
|
/**
|
|
165
165
|
* Create a new tree by mapping each [key, values] bucket.
|
|
166
166
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -172,7 +172,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
172
172
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
173
173
|
* @returns A new AVLTree when mapping to non-array values; see overloads.
|
|
174
174
|
*/
|
|
175
|
-
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<
|
|
175
|
+
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
|
|
176
176
|
/**
|
|
177
177
|
* (Protected) Create an empty instance of the same concrete class.
|
|
178
178
|
* @remarks Time O(1), Space O(1)
|
|
@@ -182,7 +182,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
182
182
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
183
183
|
* @returns An empty like-kind instance.
|
|
184
184
|
*/
|
|
185
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
185
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): this;
|
|
186
186
|
/**
|
|
187
187
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
188
188
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -193,5 +193,5 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
193
193
|
* @param [options] - Options merged with the current snapshot.
|
|
194
194
|
* @returns A like-kind AVLTree built from the iterable.
|
|
195
195
|
*/
|
|
196
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
196
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
197
197
|
}
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST } from './bst';
|
|
9
9
|
import type { AVLTreeOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
10
|
-
import { BSTOptions } from '../../types';
|
|
11
10
|
import { IBinaryTree } from '../../interfaces';
|
|
12
11
|
/**
|
|
13
12
|
* Represents a Node in an AVL (Adelson-Velsky and Landis) Tree.
|
|
@@ -128,8 +127,102 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
128
127
|
* 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.
|
|
129
128
|
*
|
|
130
129
|
* @example
|
|
130
|
+
* // basic AVLTree creation and add operation
|
|
131
|
+
* // Create a simple AVLTree with initial values
|
|
132
|
+
* const tree = new AVLTree([5, 2, 8, 1, 9]);
|
|
133
|
+
*
|
|
134
|
+
* tree.print();
|
|
135
|
+
* // _2___
|
|
136
|
+
* // / \
|
|
137
|
+
* // 1 _8_
|
|
138
|
+
* // / \
|
|
139
|
+
* // 5 9
|
|
140
|
+
*
|
|
141
|
+
* // Verify the tree maintains sorted order
|
|
142
|
+
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
143
|
+
*
|
|
144
|
+
* // Check size
|
|
145
|
+
* console.log(tree.size); // 5;
|
|
146
|
+
*
|
|
147
|
+
* // Add a new element
|
|
148
|
+
* tree.add(3);
|
|
149
|
+
* console.log(tree.size); // 6;
|
|
150
|
+
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
151
|
+
* @example
|
|
152
|
+
* // AVLTree has and get operations
|
|
153
|
+
* const tree = new AVLTree<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
154
|
+
*
|
|
155
|
+
* // Check if element exists
|
|
156
|
+
* console.log(tree.has(6)); // true;
|
|
157
|
+
* console.log(tree.has(99)); // false;
|
|
158
|
+
*
|
|
159
|
+
* // Get node by key
|
|
160
|
+
* const node = tree.getNode(6);
|
|
161
|
+
* console.log(node?.key); // 6;
|
|
162
|
+
*
|
|
163
|
+
* // Verify tree is balanced
|
|
164
|
+
* console.log(tree.isAVLBalanced()); // true;
|
|
165
|
+
* @example
|
|
166
|
+
* // AVLTree delete and balance verification
|
|
167
|
+
* const tree = new AVLTree([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
168
|
+
*
|
|
169
|
+
* // Delete an element
|
|
170
|
+
* tree.delete(10);
|
|
171
|
+
* console.log(tree.has(10)); // false;
|
|
172
|
+
*
|
|
173
|
+
* // Tree should remain balanced after deletion
|
|
174
|
+
* console.log(tree.isAVLBalanced()); // true;
|
|
175
|
+
*
|
|
176
|
+
* // Size decreased
|
|
177
|
+
* console.log(tree.size); // 15;
|
|
178
|
+
*
|
|
179
|
+
* // Remaining elements are still sorted
|
|
180
|
+
* const keys = [...tree.keys()];
|
|
181
|
+
* console.log(keys); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16];
|
|
182
|
+
* @example
|
|
183
|
+
* // AVLTree for university ranking system with strict balance
|
|
184
|
+
* interface University {
|
|
185
|
+
* name: string;
|
|
186
|
+
* rank: number;
|
|
187
|
+
* students: number;
|
|
188
|
+
* }
|
|
189
|
+
*
|
|
190
|
+
* // AVLTree provides highest search efficiency with strict balance
|
|
191
|
+
* // (every node's left/right subtrees differ by at most 1 in height)
|
|
192
|
+
* const universityTree = new AVLTree<number, University>([
|
|
193
|
+
* [1, { name: 'MIT', rank: 1, students: 1200 }],
|
|
194
|
+
* [5, { name: 'Stanford', rank: 5, students: 1800 }],
|
|
195
|
+
* [3, { name: 'Harvard', rank: 3, students: 2300 }],
|
|
196
|
+
* [2, { name: 'Caltech', rank: 2, students: 400 }],
|
|
197
|
+
* [4, { name: 'CMU', rank: 4, students: 1500 }]
|
|
198
|
+
* ]);
|
|
199
|
+
*
|
|
200
|
+
* // Quick lookup by rank
|
|
201
|
+
* const mit = universityTree.get(1);
|
|
202
|
+
* console.log(mit?.name); // 'MIT';
|
|
203
|
+
*
|
|
204
|
+
* const cmulevel = universityTree.getHeight(4);
|
|
205
|
+
* console.log(typeof cmulevel); // 'number';
|
|
206
|
+
*
|
|
207
|
+
* // Tree maintains strict balance during insertions and deletions
|
|
208
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
209
|
+
*
|
|
210
|
+
* // Add more universities
|
|
211
|
+
* universityTree.add(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
212
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
213
|
+
*
|
|
214
|
+
* // Delete and verify balance is maintained
|
|
215
|
+
* universityTree.delete(2);
|
|
216
|
+
* console.log(universityTree.has(2)); // false;
|
|
217
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
218
|
+
*
|
|
219
|
+
* // Get all remaining universities in rank order
|
|
220
|
+
* const remainingRanks = [...universityTree.keys()];
|
|
221
|
+
* console.log(remainingRanks); // [1, 3, 4, 5, 6];
|
|
222
|
+
* console.log(universityTree.size); // 5;
|
|
223
|
+
* @example
|
|
131
224
|
* // Find elements in a range
|
|
132
|
-
*
|
|
225
|
+
* // 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.
|
|
133
226
|
* type Datum = { timestamp: Date; temperature: number };
|
|
134
227
|
* // Fixed dataset of CPU temperature readings
|
|
135
228
|
* const cpuData: Datum[] = [
|
|
@@ -190,7 +283,7 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
190
283
|
* // { minute: 13, temperature: 60.2 },
|
|
191
284
|
* // { minute: 14, temperature: 59.8 },
|
|
192
285
|
* // { minute: 15, temperature: 58.6 }
|
|
193
|
-
* // ]
|
|
286
|
+
* // ];
|
|
194
287
|
*/
|
|
195
288
|
export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
196
289
|
/**
|
|
@@ -265,7 +358,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
265
358
|
* @param [options] - Options for the new tree.
|
|
266
359
|
* @returns A new, empty tree.
|
|
267
360
|
*/
|
|
268
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
361
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this;
|
|
269
362
|
/**
|
|
270
363
|
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
271
364
|
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
@@ -275,7 +368,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
275
368
|
* @param [options] - Options for the new tree.
|
|
276
369
|
* @returns A new AVLTree.
|
|
277
370
|
*/
|
|
278
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
371
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
279
372
|
/**
|
|
280
373
|
* (Protected) Swaps properties of two nodes, including height.
|
|
281
374
|
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
@@ -125,8 +125,86 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
125
125
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
126
126
|
*
|
|
127
127
|
* @example
|
|
128
|
+
* // basic BinaryTree creation and insertion
|
|
129
|
+
* // Create a BinaryTree with entries
|
|
130
|
+
* const entries: [number, string][] = [
|
|
131
|
+
* [6, 'six'],
|
|
132
|
+
* [1, 'one'],
|
|
133
|
+
* [2, 'two'],
|
|
134
|
+
* [7, 'seven'],
|
|
135
|
+
* [5, 'five'],
|
|
136
|
+
* [3, 'three'],
|
|
137
|
+
* [4, 'four'],
|
|
138
|
+
* [9, 'nine'],
|
|
139
|
+
* [8, 'eight']
|
|
140
|
+
* ];
|
|
141
|
+
*
|
|
142
|
+
* const tree = new BinaryTree(entries);
|
|
143
|
+
*
|
|
144
|
+
* // Verify size
|
|
145
|
+
* console.log(tree.size); // 9;
|
|
146
|
+
*
|
|
147
|
+
* // Add new element
|
|
148
|
+
* tree.add(10, 'ten');
|
|
149
|
+
* console.log(tree.size); // 10;
|
|
150
|
+
* @example
|
|
151
|
+
* // BinaryTree get and has operations
|
|
152
|
+
* const tree = new BinaryTree(
|
|
153
|
+
* [
|
|
154
|
+
* [5, 'five'],
|
|
155
|
+
* [3, 'three'],
|
|
156
|
+
* [7, 'seven'],
|
|
157
|
+
* [1, 'one'],
|
|
158
|
+
* [4, 'four'],
|
|
159
|
+
* [6, 'six'],
|
|
160
|
+
* [8, 'eight']
|
|
161
|
+
* ],
|
|
162
|
+
* { isMapMode: false }
|
|
163
|
+
* );
|
|
164
|
+
*
|
|
165
|
+
* // Check if key exists
|
|
166
|
+
* console.log(tree.has(5)); // true;
|
|
167
|
+
* console.log(tree.has(10)); // false;
|
|
168
|
+
*
|
|
169
|
+
* // Get value by key
|
|
170
|
+
* console.log(tree.get(3)); // 'three';
|
|
171
|
+
* console.log(tree.get(7)); // 'seven';
|
|
172
|
+
* console.log(tree.get(100)); // undefined;
|
|
173
|
+
*
|
|
174
|
+
* // Get node structure
|
|
175
|
+
* const node = tree.getNode(5);
|
|
176
|
+
* console.log(node?.key); // 5;
|
|
177
|
+
* console.log(node?.value); // 'five';
|
|
178
|
+
* @example
|
|
179
|
+
* // BinaryTree level-order traversal
|
|
180
|
+
* const tree = new BinaryTree([
|
|
181
|
+
* [1, 'one'],
|
|
182
|
+
* [2, 'two'],
|
|
183
|
+
* [3, 'three'],
|
|
184
|
+
* [4, 'four'],
|
|
185
|
+
* [5, 'five'],
|
|
186
|
+
* [6, 'six'],
|
|
187
|
+
* [7, 'seven']
|
|
188
|
+
* ]);
|
|
189
|
+
*
|
|
190
|
+
* // Binary tree maintains level-order insertion
|
|
191
|
+
* // Complete binary tree structure
|
|
192
|
+
* console.log(tree.size); // 7;
|
|
193
|
+
*
|
|
194
|
+
* // Verify all keys are present
|
|
195
|
+
* console.log(tree.has(1)); // true;
|
|
196
|
+
* console.log(tree.has(4)); // true;
|
|
197
|
+
* console.log(tree.has(7)); // true;
|
|
198
|
+
*
|
|
199
|
+
* // Iterate through tree
|
|
200
|
+
* const keys: number[] = [];
|
|
201
|
+
* for (const [key] of tree) {
|
|
202
|
+
* keys.push(key);
|
|
203
|
+
* }
|
|
204
|
+
* console.log(keys.length); // 7;
|
|
205
|
+
* @example
|
|
128
206
|
* // determine loan approval using a decision tree
|
|
129
|
-
*
|
|
207
|
+
* // Decision tree structure
|
|
130
208
|
* const loanDecisionTree = new BinaryTree<string>(
|
|
131
209
|
* ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
|
|
132
210
|
* { isDuplicate: true }
|
|
@@ -148,19 +226,19 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
148
226
|
* }
|
|
149
227
|
*
|
|
150
228
|
* // Test case 1: Stable income and good credit score
|
|
151
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
|
|
229
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved';
|
|
152
230
|
*
|
|
153
231
|
* // Test case 2: Stable income but poor credit score
|
|
154
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
|
|
232
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected';
|
|
155
233
|
*
|
|
156
234
|
* // Test case 3: No stable income
|
|
157
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
|
|
235
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected';
|
|
158
236
|
*
|
|
159
237
|
* // Test case 4: No stable income and poor credit score
|
|
160
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
|
|
238
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected';
|
|
161
239
|
* @example
|
|
162
240
|
* // evaluate the arithmetic expression represented by the binary tree
|
|
163
|
-
*
|
|
241
|
+
* const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
|
|
164
242
|
*
|
|
165
243
|
* function evaluate(node?: BinaryTreeNode<number | string> | null): number {
|
|
166
244
|
* if (!node) return 0;
|
|
@@ -185,7 +263,7 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
185
263
|
* }
|
|
186
264
|
* }
|
|
187
265
|
*
|
|
188
|
-
* console.log(evaluate(expressionTree.root)); // -27
|
|
266
|
+
* console.log(evaluate(expressionTree.root)); // -27;
|
|
189
267
|
*/
|
|
190
268
|
export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R> {
|
|
191
269
|
iterationType: IterationType;
|
|
@@ -360,6 +438,15 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
360
438
|
* @returns True if the addition was successful, false otherwise.
|
|
361
439
|
*/
|
|
362
440
|
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
441
|
+
/**
|
|
442
|
+
* Adds or updates a new node to the tree.
|
|
443
|
+
* @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).
|
|
444
|
+
*
|
|
445
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
446
|
+
* @param [value] - The value, if providing just a key.
|
|
447
|
+
* @returns True if the addition was successful, false otherwise.
|
|
448
|
+
*/
|
|
449
|
+
set(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
363
450
|
/**
|
|
364
451
|
* Adds multiple items to the tree.
|
|
365
452
|
* @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).
|
|
@@ -369,6 +456,15 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
369
456
|
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
370
457
|
*/
|
|
371
458
|
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
459
|
+
/**
|
|
460
|
+
* Adds or updates multiple items to the tree.
|
|
461
|
+
* @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).
|
|
462
|
+
*
|
|
463
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
464
|
+
* @param [values] - An optional parallel iterable of values.
|
|
465
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
466
|
+
*/
|
|
467
|
+
setMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
372
468
|
/**
|
|
373
469
|
* Merges another tree into this one by adding all its nodes.
|
|
374
470
|
* @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`).
|