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.
Files changed (54) hide show
  1. package/README.md +91 -7
  2. package/dist/cjs/index.cjs +22 -0
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +22 -0
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +22 -0
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +22 -0
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  11. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  12. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
  16. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  19. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  20. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  21. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  22. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  23. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  24. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  25. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  26. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  27. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  28. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  29. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  30. package/dist/umd/binary-tree-typed.js +22 -0
  31. package/dist/umd/binary-tree-typed.js.map +1 -1
  32. package/dist/umd/binary-tree-typed.min.js +2 -2
  33. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  34. package/package.json +2 -2
  35. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  36. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  37. package/src/data-structures/binary-tree/avl-tree.ts +100 -7
  38. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  39. package/src/data-structures/binary-tree/bst.ts +431 -93
  40. package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
  41. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  42. package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
  43. package/src/data-structures/graph/directed-graph.ts +126 -1
  44. package/src/data-structures/graph/undirected-graph.ts +160 -1
  45. package/src/data-structures/hash/hash-map.ts +110 -27
  46. package/src/data-structures/heap/heap.ts +107 -58
  47. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  48. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  49. package/src/data-structures/queue/deque.ts +95 -67
  50. package/src/data-structures/queue/queue.ts +90 -34
  51. package/src/data-structures/stack/stack.ts +58 -40
  52. package/src/data-structures/trie/trie.ts +109 -47
  53. package/src/interfaces/binary-tree.ts +2 -0
  54. package/src/types/data-structures/binary-tree/bst.ts +5 -5
package/README.md CHANGED
@@ -34,9 +34,93 @@ yarn add binary-tree-typed
34
34
 
35
35
  [//]: # (No deletion!!! Start of Example Replace Section)
36
36
 
37
+ ### basic BinaryTree creation and insertion
38
+ ```typescript
39
+ // Create a BinaryTree with entries
40
+ const entries: [number, string][] = [
41
+ [6, 'six'],
42
+ [1, 'one'],
43
+ [2, 'two'],
44
+ [7, 'seven'],
45
+ [5, 'five'],
46
+ [3, 'three'],
47
+ [4, 'four'],
48
+ [9, 'nine'],
49
+ [8, 'eight']
50
+ ];
51
+
52
+ const tree = new BinaryTree(entries);
53
+
54
+ // Verify size
55
+ console.log(tree.size); // 9;
56
+
57
+ // Add new element
58
+ tree.add(10, 'ten');
59
+ console.log(tree.size); // 10;
60
+ ```
61
+
62
+ ### BinaryTree get and has operations
63
+ ```typescript
64
+ const tree = new BinaryTree(
65
+ [
66
+ [5, 'five'],
67
+ [3, 'three'],
68
+ [7, 'seven'],
69
+ [1, 'one'],
70
+ [4, 'four'],
71
+ [6, 'six'],
72
+ [8, 'eight']
73
+ ],
74
+ { isMapMode: false }
75
+ );
76
+
77
+ // Check if key exists
78
+ console.log(tree.has(5)); // true;
79
+ console.log(tree.has(10)); // false;
80
+
81
+ // Get value by key
82
+ console.log(tree.get(3)); // 'three';
83
+ console.log(tree.get(7)); // 'seven';
84
+ console.log(tree.get(100)); // undefined;
85
+
86
+ // Get node structure
87
+ const node = tree.getNode(5);
88
+ console.log(node?.key); // 5;
89
+ console.log(node?.value); // 'five';
90
+ ```
91
+
92
+ ### BinaryTree level-order traversal
93
+ ```typescript
94
+ const tree = new BinaryTree([
95
+ [1, 'one'],
96
+ [2, 'two'],
97
+ [3, 'three'],
98
+ [4, 'four'],
99
+ [5, 'five'],
100
+ [6, 'six'],
101
+ [7, 'seven']
102
+ ]);
103
+
104
+ // Binary tree maintains level-order insertion
105
+ // Complete binary tree structure
106
+ console.log(tree.size); // 7;
107
+
108
+ // Verify all keys are present
109
+ console.log(tree.has(1)); // true;
110
+ console.log(tree.has(4)); // true;
111
+ console.log(tree.has(7)); // true;
112
+
113
+ // Iterate through tree
114
+ const keys: number[] = [];
115
+ for (const [key] of tree) {
116
+ keys.push(key);
117
+ }
118
+ console.log(keys.length); // 7;
119
+ ```
120
+
37
121
  ### determine loan approval using a decision tree
38
122
  ```typescript
39
- // Decision tree structure
123
+ // Decision tree structure
40
124
  const loanDecisionTree = new BinaryTree<string>(
41
125
  ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
42
126
  { isDuplicate: true }
@@ -58,21 +142,21 @@ yarn add binary-tree-typed
58
142
  }
59
143
 
60
144
  // Test case 1: Stable income and good credit score
61
- console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
145
+ console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved';
62
146
 
63
147
  // Test case 2: Stable income but poor credit score
64
- console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
148
+ console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected';
65
149
 
66
150
  // Test case 3: No stable income
67
- console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
151
+ console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected';
68
152
 
69
153
  // Test case 4: No stable income and poor credit score
70
- console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
154
+ console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected';
71
155
  ```
72
156
 
73
157
  ### evaluate the arithmetic expression represented by the binary tree
74
158
  ```typescript
75
- const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
159
+ const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
76
160
 
77
161
  function evaluate(node?: BinaryTreeNode<number | string> | null): number {
78
162
  if (!node) return 0;
@@ -97,7 +181,7 @@ yarn add binary-tree-typed
97
181
  }
98
182
  }
99
183
 
100
- console.log(evaluate(expressionTree.root)); // -27
184
+ console.log(evaluate(expressionTree.root)); // -27;
101
185
  ```
102
186
 
103
187
  [//]: # (No deletion!!! End of Example Replace Section)
@@ -1463,6 +1463,17 @@ var BinaryTree = class extends IterableEntryBase {
1463
1463
  }
1464
1464
  return false;
1465
1465
  }
1466
+ /**
1467
+ * Adds or updates a new node to the tree.
1468
+ * @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).
1469
+ *
1470
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
1471
+ * @param [value] - The value, if providing just a key.
1472
+ * @returns True if the addition was successful, false otherwise.
1473
+ */
1474
+ set(keyNodeOrEntry, value) {
1475
+ return this.add(keyNodeOrEntry, value);
1476
+ }
1466
1477
  /**
1467
1478
  * Adds multiple items to the tree.
1468
1479
  * @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).
@@ -1490,6 +1501,17 @@ var BinaryTree = class extends IterableEntryBase {
1490
1501
  }
1491
1502
  return inserted;
1492
1503
  }
1504
+ /**
1505
+ * Adds or updates multiple items to the tree.
1506
+ * @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).
1507
+ *
1508
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1509
+ * @param [values] - An optional parallel iterable of values.
1510
+ * @returns An array of booleans indicating the success of each individual `add` operation.
1511
+ */
1512
+ setMany(keysNodesEntriesOrRaws, values) {
1513
+ return this.addMany(keysNodesEntriesOrRaws, values);
1514
+ }
1493
1515
  /**
1494
1516
  * Merges another tree into this one by adding all its nodes.
1495
1517
  * @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`).