avl-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 +155 -68
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +153 -66
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +155 -68
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +153 -66
  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/avl-tree-typed.js +152 -65
  29. package/dist/umd/avl-tree-typed.js.map +1 -1
  30. package/dist/umd/avl-tree-typed.min.js +3 -3
  31. package/dist/umd/avl-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
@@ -57,6 +57,50 @@ function makeTrampoline(fn) {
57
57
  }
58
58
  __name(makeTrampoline, "makeTrampoline");
59
59
 
60
+ // src/common/error.ts
61
+ var ERR = {
62
+ // Range / index
63
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
64
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
65
+ // Type / argument
66
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
67
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
68
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
69
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
70
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
71
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
72
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
73
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
74
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
75
+ // State / operation
76
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
77
+ // Matrix
78
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
79
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
80
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
81
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
82
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
83
+ };
84
+
85
+ // src/common/index.ts
86
+ var Range = class {
87
+ constructor(low, high, includeLow = true, includeHigh = true) {
88
+ this.low = low;
89
+ this.high = high;
90
+ this.includeLow = includeLow;
91
+ this.includeHigh = includeHigh;
92
+ }
93
+ static {
94
+ __name(this, "Range");
95
+ }
96
+ // Determine whether a key is within the range
97
+ isInRange(key, comparator) {
98
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
99
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
100
+ return lowCheck && highCheck;
101
+ }
102
+ };
103
+
60
104
  // src/data-structures/base/iterable-element-base.ts
61
105
  var IterableElementBase = class {
62
106
  static {
@@ -75,7 +119,7 @@ var IterableElementBase = class {
75
119
  if (options) {
76
120
  const { toElementFn } = options;
77
121
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
78
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
122
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
79
123
  }
80
124
  }
81
125
  /**
@@ -238,7 +282,7 @@ var IterableElementBase = class {
238
282
  acc = initialValue;
239
283
  } else {
240
284
  const first = iter.next();
241
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
285
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
242
286
  acc = first.value;
243
287
  index = 1;
244
288
  }
@@ -1023,25 +1067,6 @@ var IterableEntryBase = class {
1023
1067
  }
1024
1068
  };
1025
1069
 
1026
- // src/common/index.ts
1027
- var Range = class {
1028
- constructor(low, high, includeLow = true, includeHigh = true) {
1029
- this.low = low;
1030
- this.high = high;
1031
- this.includeLow = includeLow;
1032
- this.includeHigh = includeHigh;
1033
- }
1034
- static {
1035
- __name(this, "Range");
1036
- }
1037
- // Determine whether a key is within the range
1038
- isInRange(key, comparator) {
1039
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1040
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1041
- return lowCheck && highCheck;
1042
- }
1043
- };
1044
-
1045
1070
  // src/data-structures/binary-tree/binary-tree.ts
1046
1071
  var BinaryTreeNode = class {
1047
1072
  static {
@@ -1180,7 +1205,7 @@ var BinaryTreeNode = class {
1180
1205
  return "MAL_NODE";
1181
1206
  }
1182
1207
  };
1183
- var BinaryTree = class extends IterableEntryBase {
1208
+ var BinaryTree = class _BinaryTree extends IterableEntryBase {
1184
1209
  static {
1185
1210
  __name(this, "BinaryTree");
1186
1211
  }
@@ -1200,7 +1225,7 @@ var BinaryTree = class extends IterableEntryBase {
1200
1225
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1201
1226
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1202
1227
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1203
- else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1228
+ else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1204
1229
  }
1205
1230
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1206
1231
  }
@@ -1438,7 +1463,7 @@ var BinaryTree = class extends IterableEntryBase {
1438
1463
  if (!this._root) {
1439
1464
  this._setRoot(newNode);
1440
1465
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1441
- this._size = 1;
1466
+ if (newNode !== null) this._size = 1;
1442
1467
  return true;
1443
1468
  }
1444
1469
  const queue = new Queue([this._root]);
@@ -1470,7 +1495,7 @@ var BinaryTree = class extends IterableEntryBase {
1470
1495
  potentialParent.right = newNode;
1471
1496
  }
1472
1497
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1473
- this._size++;
1498
+ if (newNode !== null) this._size++;
1474
1499
  return true;
1475
1500
  }
1476
1501
  return false;
@@ -2038,7 +2063,7 @@ var BinaryTree = class extends IterableEntryBase {
2038
2063
  }
2039
2064
  /**
2040
2065
  * Finds all leaf nodes in the tree.
2041
- * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
2066
+ * @remarks Time O(N), visits every node. Space O(H) for recursive or iterative stack.
2042
2067
  *
2043
2068
  * @template C - The type of the callback function.
2044
2069
  * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
@@ -2061,15 +2086,15 @@ var BinaryTree = class extends IterableEntryBase {
2061
2086
  }, "dfs");
2062
2087
  dfs(startNode);
2063
2088
  } else {
2064
- const queue = new Queue([startNode]);
2065
- while (queue.length > 0) {
2066
- const cur = queue.shift();
2089
+ const stack = [startNode];
2090
+ while (stack.length > 0) {
2091
+ const cur = stack.pop();
2067
2092
  if (this.isRealNode(cur)) {
2068
2093
  if (this.isLeaf(cur)) {
2069
2094
  leaves.push(callback(cur));
2070
2095
  }
2071
- if (this.isRealNode(cur.left)) queue.push(cur.left);
2072
- if (this.isRealNode(cur.right)) queue.push(cur.right);
2096
+ if (this.isRealNode(cur.right)) stack.push(cur.right);
2097
+ if (this.isRealNode(cur.left)) stack.push(cur.left);
2073
2098
  }
2074
2099
  }
2075
2100
  }
@@ -2438,7 +2463,7 @@ var BinaryTree = class extends IterableEntryBase {
2438
2463
  * @param node - The node.
2439
2464
  * @returns The node's key or undefined.
2440
2465
  */
2441
- _DEFAULT_NODE_CALLBACK = /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK");
2466
+ _DEFAULT_NODE_CALLBACK = /* @__PURE__ */ __name((node) => node?.key, "_DEFAULT_NODE_CALLBACK");
2442
2467
  /**
2443
2468
  * (Protected) Snapshots the current tree's configuration options.
2444
2469
  * @remarks Time O(1)
@@ -2531,42 +2556,98 @@ var BinaryTree = class extends IterableEntryBase {
2531
2556
  _displayAux(node, options) {
2532
2557
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2533
2558
  const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
2534
- if (node === null && !isShowNull) {
2535
- return emptyDisplayLayout;
2536
- } else if (node === void 0 && !isShowUndefined) {
2537
- return emptyDisplayLayout;
2538
- } else if (this.isNIL(node) && !isShowRedBlackNIL) {
2539
- return emptyDisplayLayout;
2540
- } else if (node !== null && node !== void 0) {
2541
- const key = node.key, line = this.isNIL(node) ? "S" : String(key), width = line.length;
2542
- return _buildNodeDisplay(
2543
- line,
2544
- width,
2545
- this._displayAux(node.left, options),
2546
- this._displayAux(node.right, options)
2547
- );
2548
- } else {
2549
- const line = node === void 0 ? "U" : "N", width = line.length;
2550
- return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
2551
- }
2552
- function _buildNodeDisplay(line, width, left, right) {
2553
- const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
2554
- const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
2555
- 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));
2556
- const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
2557
- const mergedLines = [firstLine, secondLine];
2558
- for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
2559
- const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
2560
- const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
2561
- mergedLines.push(leftLine + " ".repeat(width) + rightLine);
2559
+ const newFrame = /* @__PURE__ */ __name((n) => ({
2560
+ node: n,
2561
+ stage: 0,
2562
+ leftLayout: emptyDisplayLayout,
2563
+ rightLayout: emptyDisplayLayout
2564
+ }), "newFrame");
2565
+ const stack = [newFrame(node)];
2566
+ let result = emptyDisplayLayout;
2567
+ const setChildResult = /* @__PURE__ */ __name((layout) => {
2568
+ if (stack.length === 0) {
2569
+ result = layout;
2570
+ return;
2571
+ }
2572
+ const parent = stack[stack.length - 1];
2573
+ if (parent.stage === 1) parent.leftLayout = layout;
2574
+ else parent.rightLayout = layout;
2575
+ }, "setChildResult");
2576
+ while (stack.length > 0) {
2577
+ const frame = stack[stack.length - 1];
2578
+ const cur = frame.node;
2579
+ if (frame.stage === 0) {
2580
+ if (this._isDisplayLeaf(cur, options)) {
2581
+ stack.pop();
2582
+ const layout = this._resolveDisplayLeaf(cur, options, emptyDisplayLayout);
2583
+ setChildResult(layout);
2584
+ continue;
2585
+ }
2586
+ frame.stage = 1;
2587
+ stack.push(newFrame(cur.left));
2588
+ } else if (frame.stage === 1) {
2589
+ frame.stage = 2;
2590
+ stack.push(newFrame(cur.right));
2591
+ } else {
2592
+ stack.pop();
2593
+ const line = this.isNIL(cur) ? "S" : String(cur.key);
2594
+ const layout = _BinaryTree._buildNodeDisplay(line, line.length, frame.leftLayout, frame.rightLayout);
2595
+ setChildResult(layout);
2562
2596
  }
2563
- return [
2564
- mergedLines,
2565
- leftWidth + width + rightWidth,
2566
- Math.max(leftHeight, rightHeight) + 2,
2567
- leftWidth + Math.floor(width / 2)
2568
- ];
2569
2597
  }
2598
+ return result;
2599
+ }
2600
+ static _buildNodeDisplay(line, width, left, right) {
2601
+ const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
2602
+ const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
2603
+ 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));
2604
+ const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
2605
+ const mergedLines = [firstLine, secondLine];
2606
+ for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
2607
+ const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
2608
+ const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
2609
+ mergedLines.push(leftLine + " ".repeat(width) + rightLine);
2610
+ }
2611
+ return [
2612
+ mergedLines,
2613
+ leftWidth + width + rightWidth,
2614
+ Math.max(leftHeight, rightHeight) + 2,
2615
+ leftWidth + Math.floor(width / 2)
2616
+ ];
2617
+ }
2618
+ /**
2619
+ * Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
2620
+ */
2621
+ _isDisplayLeaf(node, options) {
2622
+ const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2623
+ if (node === null && !isShowNull) return true;
2624
+ if (node === void 0 && !isShowUndefined) return true;
2625
+ if (this.isNIL(node) && !isShowRedBlackNIL) return true;
2626
+ if (node === null || node === void 0) return true;
2627
+ const hasDisplayableLeft = this._hasDisplayableChild(node.left, options);
2628
+ const hasDisplayableRight = this._hasDisplayableChild(node.right, options);
2629
+ return !hasDisplayableLeft && !hasDisplayableRight;
2630
+ }
2631
+ _hasDisplayableChild(child, options) {
2632
+ if (child === null) return !!options.isShowNull;
2633
+ if (child === void 0) return !!options.isShowUndefined;
2634
+ if (this.isNIL(child)) return !!options.isShowRedBlackNIL;
2635
+ return true;
2636
+ }
2637
+ /**
2638
+ * Resolve a display leaf node to its layout.
2639
+ */
2640
+ _resolveDisplayLeaf(node, options, emptyDisplayLayout) {
2641
+ const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2642
+ if (node === null && !isShowNull) return emptyDisplayLayout;
2643
+ if (node === void 0 && !isShowUndefined) return emptyDisplayLayout;
2644
+ if (this.isNIL(node) && !isShowRedBlackNIL) return emptyDisplayLayout;
2645
+ if (node !== null && node !== void 0) {
2646
+ const line2 = this.isNIL(node) ? "S" : String(node.key);
2647
+ return _BinaryTree._buildNodeDisplay(line2, line2.length, emptyDisplayLayout, emptyDisplayLayout);
2648
+ }
2649
+ const line = node === void 0 ? "U" : "N";
2650
+ return _BinaryTree._buildNodeDisplay(line, line.length, [[""], 1, 0, 0], [[""], 1, 0, 0]);
2570
2651
  }
2571
2652
  /**
2572
2653
  * (Protected) Swaps the key/value properties of two nodes.
@@ -3572,9 +3653,15 @@ var BST = class extends BinaryTree {
3572
3653
  if (a < b) return -1;
3573
3654
  return 0;
3574
3655
  }
3656
+ if (a instanceof Date && b instanceof Date) {
3657
+ const ta = a.getTime();
3658
+ const tb = b.getTime();
3659
+ if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
3660
+ return ta > tb ? 1 : ta < tb ? -1 : 0;
3661
+ }
3575
3662
  if (typeof a === "object" || typeof b === "object") {
3576
- throw TypeError(
3577
- `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
3663
+ throw new TypeError(
3664
+ ERR.comparatorRequired("BST")
3578
3665
  );
3579
3666
  }
3580
3667
  return 0;