binary-tree-typed 2.4.3 → 2.4.5

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 (65) hide show
  1. package/dist/cjs/index.cjs +153 -71
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +151 -69
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +153 -72
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +151 -70
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +23 -0
  10. package/dist/types/common/index.d.ts +1 -0
  11. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +15 -5
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +7 -1
  15. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  16. package/dist/types/data-structures/graph/directed-graph.d.ts +3 -2
  17. package/dist/types/data-structures/graph/undirected-graph.d.ts +16 -2
  18. package/dist/types/data-structures/hash/hash-map.d.ts +2 -2
  19. package/dist/types/data-structures/heap/heap.d.ts +3 -7
  20. package/dist/types/data-structures/queue/deque.d.ts +41 -1
  21. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
  22. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  23. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  24. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  25. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  26. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  27. package/dist/types/types/data-structures/stack/stack.d.ts +1 -1
  28. package/dist/umd/binary-tree-typed.js +150 -68
  29. package/dist/umd/binary-tree-typed.js.map +1 -1
  30. package/dist/umd/binary-tree-typed.min.js +3 -3
  31. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  32. package/package.json +2 -2
  33. package/src/common/error.ts +60 -0
  34. package/src/common/index.ts +2 -0
  35. package/src/data-structures/base/iterable-element-base.ts +5 -4
  36. package/src/data-structures/binary-tree/binary-indexed-tree.ts +6 -5
  37. package/src/data-structures/binary-tree/binary-tree.ts +121 -49
  38. package/src/data-structures/binary-tree/bst.ts +12 -4
  39. package/src/data-structures/binary-tree/red-black-tree.ts +20 -0
  40. package/src/data-structures/binary-tree/tree-map.ts +8 -7
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +4 -4
  42. package/src/data-structures/binary-tree/tree-multi-set.ts +10 -9
  43. package/src/data-structures/binary-tree/tree-set.ts +7 -6
  44. package/src/data-structures/graph/abstract-graph.ts +124 -19
  45. package/src/data-structures/graph/directed-graph.ts +8 -4
  46. package/src/data-structures/graph/map-graph.ts +1 -1
  47. package/src/data-structures/graph/undirected-graph.ts +99 -4
  48. package/src/data-structures/hash/hash-map.ts +19 -6
  49. package/src/data-structures/heap/heap.ts +21 -17
  50. package/src/data-structures/heap/max-heap.ts +2 -3
  51. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  52. package/src/data-structures/linked-list/singly-linked-list.ts +15 -9
  53. package/src/data-structures/matrix/matrix.ts +9 -10
  54. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -3
  55. package/src/data-structures/queue/deque.ts +72 -4
  56. package/src/data-structures/stack/stack.ts +1 -1
  57. package/src/data-structures/trie/trie.ts +12 -6
  58. package/src/types/data-structures/binary-tree/avl-tree.ts +1 -1
  59. package/src/types/data-structures/binary-tree/red-black-tree.ts +1 -1
  60. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -1
  61. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -1
  62. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  63. package/src/types/data-structures/queue/deque.ts +7 -0
  64. package/src/types/data-structures/stack/stack.ts +1 -1
  65. package/src/utils/utils.ts +4 -2
@@ -26,6 +26,7 @@ var binaryTreeTyped = (() => {
26
26
  BinaryTree: () => BinaryTree,
27
27
  BinaryTreeNode: () => BinaryTreeNode,
28
28
  DFSOperation: () => DFSOperation,
29
+ ERR: () => ERR,
29
30
  Range: () => Range
30
31
  });
31
32
 
@@ -80,6 +81,52 @@ var binaryTreeTyped = (() => {
80
81
  return (...args) => trampoline(fn(...args));
81
82
  }
82
83
 
84
+ // src/common/error.ts
85
+ var ERR = {
86
+ // Range / index
87
+ indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
88
+ invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
89
+ // Type / argument
90
+ invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
91
+ comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
92
+ invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
93
+ notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
94
+ invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
95
+ invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
96
+ invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
97
+ reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
98
+ callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
99
+ // State / operation
100
+ invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
101
+ // Matrix
102
+ matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
103
+ matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
104
+ matrixNotSquare: () => "Matrix: Must be square for inversion.",
105
+ matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
106
+ matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
107
+ };
108
+
109
+ // src/common/index.ts
110
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
111
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
112
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
113
+ return DFSOperation2;
114
+ })(DFSOperation || {});
115
+ var Range = class {
116
+ constructor(low, high, includeLow = true, includeHigh = true) {
117
+ this.low = low;
118
+ this.high = high;
119
+ this.includeLow = includeLow;
120
+ this.includeHigh = includeHigh;
121
+ }
122
+ // Determine whether a key is within the range
123
+ isInRange(key, comparator) {
124
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
125
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
126
+ return lowCheck && highCheck;
127
+ }
128
+ };
129
+
83
130
  // src/data-structures/base/iterable-element-base.ts
84
131
  var IterableElementBase = class {
85
132
  /**
@@ -102,7 +149,7 @@ var binaryTreeTyped = (() => {
102
149
  if (options) {
103
150
  const { toElementFn } = options;
104
151
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
105
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
152
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
106
153
  }
107
154
  }
108
155
  /**
@@ -258,7 +305,7 @@ var binaryTreeTyped = (() => {
258
305
  acc = initialValue;
259
306
  } else {
260
307
  const first = iter.next();
261
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
308
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
262
309
  acc = first.value;
263
310
  index = 1;
264
311
  }
@@ -1036,27 +1083,6 @@ var binaryTreeTyped = (() => {
1036
1083
  }
1037
1084
  };
1038
1085
 
1039
- // src/common/index.ts
1040
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
1041
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
1042
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
1043
- return DFSOperation2;
1044
- })(DFSOperation || {});
1045
- var Range = class {
1046
- constructor(low, high, includeLow = true, includeHigh = true) {
1047
- this.low = low;
1048
- this.high = high;
1049
- this.includeLow = includeLow;
1050
- this.includeHigh = includeHigh;
1051
- }
1052
- // Determine whether a key is within the range
1053
- isInRange(key, comparator) {
1054
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1055
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1056
- return lowCheck && highCheck;
1057
- }
1058
- };
1059
-
1060
1086
  // src/data-structures/binary-tree/binary-tree.ts
1061
1087
  var BinaryTreeNode = class {
1062
1088
  /**
@@ -1192,7 +1218,7 @@ var binaryTreeTyped = (() => {
1192
1218
  return "MAL_NODE";
1193
1219
  }
1194
1220
  };
1195
- var BinaryTree = class extends IterableEntryBase {
1221
+ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1196
1222
  /**
1197
1223
  * Creates an instance of BinaryTree.
1198
1224
  * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `set` operation). Space O(N) for storing the nodes.
@@ -1220,14 +1246,14 @@ var binaryTreeTyped = (() => {
1220
1246
  * @param node - The node.
1221
1247
  * @returns The node's key or undefined.
1222
1248
  */
1223
- __publicField(this, "_DEFAULT_NODE_CALLBACK", (node) => node ? node.key : void 0);
1249
+ __publicField(this, "_DEFAULT_NODE_CALLBACK", (node) => node == null ? void 0 : node.key);
1224
1250
  if (options) {
1225
1251
  const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
1226
1252
  if (iterationType) this.iterationType = iterationType;
1227
1253
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1228
1254
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1229
1255
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1230
- else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1256
+ else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1231
1257
  }
1232
1258
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1233
1259
  }
@@ -1455,7 +1481,7 @@ var binaryTreeTyped = (() => {
1455
1481
  if (!this._root) {
1456
1482
  this._setRoot(newNode);
1457
1483
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1458
- this._size = 1;
1484
+ if (newNode !== null) this._size = 1;
1459
1485
  return true;
1460
1486
  }
1461
1487
  const queue = new Queue([this._root]);
@@ -1487,7 +1513,7 @@ var binaryTreeTyped = (() => {
1487
1513
  potentialParent.right = newNode;
1488
1514
  }
1489
1515
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1490
- this._size++;
1516
+ if (newNode !== null) this._size++;
1491
1517
  return true;
1492
1518
  }
1493
1519
  return false;
@@ -2056,7 +2082,7 @@ var binaryTreeTyped = (() => {
2056
2082
  }
2057
2083
  /**
2058
2084
  * Finds all leaf nodes in the tree.
2059
- * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
2085
+ * @remarks Time O(N), visits every node. Space O(H) for recursive or iterative stack.
2060
2086
  *
2061
2087
  * @template C - The type of the callback function.
2062
2088
  * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
@@ -2079,15 +2105,15 @@ var binaryTreeTyped = (() => {
2079
2105
  };
2080
2106
  dfs(startNode);
2081
2107
  } else {
2082
- const queue = new Queue([startNode]);
2083
- while (queue.length > 0) {
2084
- const cur = queue.shift();
2108
+ const stack = [startNode];
2109
+ while (stack.length > 0) {
2110
+ const cur = stack.pop();
2085
2111
  if (this.isRealNode(cur)) {
2086
2112
  if (this.isLeaf(cur)) {
2087
2113
  leaves.push(callback(cur));
2088
2114
  }
2089
- if (this.isRealNode(cur.left)) queue.push(cur.left);
2090
- if (this.isRealNode(cur.right)) queue.push(cur.right);
2115
+ if (this.isRealNode(cur.right)) stack.push(cur.right);
2116
+ if (this.isRealNode(cur.left)) stack.push(cur.left);
2091
2117
  }
2092
2118
  }
2093
2119
  }
@@ -2543,42 +2569,98 @@ var binaryTreeTyped = (() => {
2543
2569
  _displayAux(node, options) {
2544
2570
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2545
2571
  const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
2546
- if (node === null && !isShowNull) {
2547
- return emptyDisplayLayout;
2548
- } else if (node === void 0 && !isShowUndefined) {
2549
- return emptyDisplayLayout;
2550
- } else if (this.isNIL(node) && !isShowRedBlackNIL) {
2551
- return emptyDisplayLayout;
2552
- } else if (node !== null && node !== void 0) {
2553
- const key = node.key, line = this.isNIL(node) ? "S" : String(key), width = line.length;
2554
- return _buildNodeDisplay(
2555
- line,
2556
- width,
2557
- this._displayAux(node.left, options),
2558
- this._displayAux(node.right, options)
2559
- );
2560
- } else {
2561
- const line = node === void 0 ? "U" : "N", width = line.length;
2562
- return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
2563
- }
2564
- function _buildNodeDisplay(line, width, left, right) {
2565
- const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
2566
- const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
2567
- const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
2568
- const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
2569
- const mergedLines = [firstLine, secondLine];
2570
- for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
2571
- const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
2572
- const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
2573
- mergedLines.push(leftLine + " ".repeat(width) + rightLine);
2572
+ const newFrame = (n) => ({
2573
+ node: n,
2574
+ stage: 0,
2575
+ leftLayout: emptyDisplayLayout,
2576
+ rightLayout: emptyDisplayLayout
2577
+ });
2578
+ const stack = [newFrame(node)];
2579
+ let result = emptyDisplayLayout;
2580
+ const setChildResult = (layout) => {
2581
+ if (stack.length === 0) {
2582
+ result = layout;
2583
+ return;
2574
2584
  }
2575
- return [
2576
- mergedLines,
2577
- leftWidth + width + rightWidth,
2578
- Math.max(leftHeight, rightHeight) + 2,
2579
- leftWidth + Math.floor(width / 2)
2580
- ];
2585
+ const parent = stack[stack.length - 1];
2586
+ if (parent.stage === 1) parent.leftLayout = layout;
2587
+ else parent.rightLayout = layout;
2588
+ };
2589
+ while (stack.length > 0) {
2590
+ const frame = stack[stack.length - 1];
2591
+ const cur = frame.node;
2592
+ if (frame.stage === 0) {
2593
+ if (this._isDisplayLeaf(cur, options)) {
2594
+ stack.pop();
2595
+ const layout = this._resolveDisplayLeaf(cur, options, emptyDisplayLayout);
2596
+ setChildResult(layout);
2597
+ continue;
2598
+ }
2599
+ frame.stage = 1;
2600
+ stack.push(newFrame(cur.left));
2601
+ } else if (frame.stage === 1) {
2602
+ frame.stage = 2;
2603
+ stack.push(newFrame(cur.right));
2604
+ } else {
2605
+ stack.pop();
2606
+ const line = this.isNIL(cur) ? "S" : String(cur.key);
2607
+ const layout = _BinaryTree._buildNodeDisplay(line, line.length, frame.leftLayout, frame.rightLayout);
2608
+ setChildResult(layout);
2609
+ }
2610
+ }
2611
+ return result;
2612
+ }
2613
+ static _buildNodeDisplay(line, width, left, right) {
2614
+ const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
2615
+ const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
2616
+ const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
2617
+ const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
2618
+ const mergedLines = [firstLine, secondLine];
2619
+ for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
2620
+ const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
2621
+ const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
2622
+ mergedLines.push(leftLine + " ".repeat(width) + rightLine);
2581
2623
  }
2624
+ return [
2625
+ mergedLines,
2626
+ leftWidth + width + rightWidth,
2627
+ Math.max(leftHeight, rightHeight) + 2,
2628
+ leftWidth + Math.floor(width / 2)
2629
+ ];
2630
+ }
2631
+ /**
2632
+ * Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
2633
+ */
2634
+ _isDisplayLeaf(node, options) {
2635
+ const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2636
+ if (node === null && !isShowNull) return true;
2637
+ if (node === void 0 && !isShowUndefined) return true;
2638
+ if (this.isNIL(node) && !isShowRedBlackNIL) return true;
2639
+ if (node === null || node === void 0) return true;
2640
+ const hasDisplayableLeft = this._hasDisplayableChild(node.left, options);
2641
+ const hasDisplayableRight = this._hasDisplayableChild(node.right, options);
2642
+ return !hasDisplayableLeft && !hasDisplayableRight;
2643
+ }
2644
+ _hasDisplayableChild(child, options) {
2645
+ if (child === null) return !!options.isShowNull;
2646
+ if (child === void 0) return !!options.isShowUndefined;
2647
+ if (this.isNIL(child)) return !!options.isShowRedBlackNIL;
2648
+ return true;
2649
+ }
2650
+ /**
2651
+ * Resolve a display leaf node to its layout.
2652
+ */
2653
+ _resolveDisplayLeaf(node, options, emptyDisplayLayout) {
2654
+ const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2655
+ if (node === null && !isShowNull) return emptyDisplayLayout;
2656
+ if (node === void 0 && !isShowUndefined) return emptyDisplayLayout;
2657
+ if (this.isNIL(node) && !isShowRedBlackNIL) return emptyDisplayLayout;
2658
+ if (node !== null && node !== void 0) {
2659
+ const line2 = this.isNIL(node) ? "S" : String(node.key);
2660
+ return _BinaryTree._buildNodeDisplay(line2, line2.length, emptyDisplayLayout, emptyDisplayLayout);
2661
+ }
2662
+ const line = node === void 0 ? "U" : "N";
2663
+ return _BinaryTree._buildNodeDisplay(line, line.length, [[""], 1, 0, 0], [[""], 1, 0, 0]);
2582
2664
  }
2583
2665
  /**
2584
2666
  * (Protected) Swaps the key/value properties of two nodes.