max-priority-queue-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.map +1 -1
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs.map +1 -1
- 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/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-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
|
@@ -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`).
|