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
@@ -59,6 +59,49 @@ function makeTrampoline(fn) {
59
59
  }
60
60
  __name(makeTrampoline, "makeTrampoline");
61
61
 
62
+ // src/common/error.ts
63
+ var ERR = {
64
+ // Range / index
65
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
66
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
67
+ // Type / argument
68
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
69
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
70
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
71
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
72
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
73
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
74
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
75
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
76
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
77
+ // State / operation
78
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
79
+ // Matrix
80
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
81
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
82
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
83
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
84
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
85
+ };
86
+
87
+ // src/common/index.ts
88
+ var _Range = class _Range {
89
+ constructor(low, high, includeLow = true, includeHigh = true) {
90
+ this.low = low;
91
+ this.high = high;
92
+ this.includeLow = includeLow;
93
+ this.includeHigh = includeHigh;
94
+ }
95
+ // Determine whether a key is within the range
96
+ isInRange(key, comparator) {
97
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
98
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
99
+ return lowCheck && highCheck;
100
+ }
101
+ };
102
+ __name(_Range, "Range");
103
+ var Range = _Range;
104
+
62
105
  // src/data-structures/base/iterable-element-base.ts
63
106
  var _IterableElementBase = class _IterableElementBase {
64
107
  /**
@@ -81,7 +124,7 @@ var _IterableElementBase = class _IterableElementBase {
81
124
  if (options) {
82
125
  const { toElementFn } = options;
83
126
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
84
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
127
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
85
128
  }
86
129
  }
87
130
  /**
@@ -237,7 +280,7 @@ var _IterableElementBase = class _IterableElementBase {
237
280
  acc = initialValue;
238
281
  } else {
239
282
  const first = iter.next();
240
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
283
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
241
284
  acc = first.value;
242
285
  index = 1;
243
286
  }
@@ -1023,24 +1066,6 @@ var _IterableEntryBase = class _IterableEntryBase {
1023
1066
  __name(_IterableEntryBase, "IterableEntryBase");
1024
1067
  var IterableEntryBase = _IterableEntryBase;
1025
1068
 
1026
- // src/common/index.ts
1027
- var _Range = class _Range {
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
- // Determine whether a key is within the range
1035
- isInRange(key, comparator) {
1036
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1037
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1038
- return lowCheck && highCheck;
1039
- }
1040
- };
1041
- __name(_Range, "Range");
1042
- var Range = _Range;
1043
-
1044
1069
  // src/data-structures/binary-tree/binary-tree.ts
1045
1070
  var _BinaryTreeNode = class _BinaryTreeNode {
1046
1071
  /**
@@ -1206,14 +1231,14 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1206
1231
  * @param node - The node.
1207
1232
  * @returns The node's key or undefined.
1208
1233
  */
1209
- __publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK"));
1234
+ __publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node == null ? void 0 : node.key, "_DEFAULT_NODE_CALLBACK"));
1210
1235
  if (options) {
1211
1236
  const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
1212
1237
  if (iterationType) this.iterationType = iterationType;
1213
1238
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1214
1239
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1215
1240
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1216
- else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1241
+ else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1217
1242
  }
1218
1243
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1219
1244
  }
@@ -1441,7 +1466,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1441
1466
  if (!this._root) {
1442
1467
  this._setRoot(newNode);
1443
1468
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1444
- this._size = 1;
1469
+ if (newNode !== null) this._size = 1;
1445
1470
  return true;
1446
1471
  }
1447
1472
  const queue = new Queue([this._root]);
@@ -1473,7 +1498,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1473
1498
  potentialParent.right = newNode;
1474
1499
  }
1475
1500
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1476
- this._size++;
1501
+ if (newNode !== null) this._size++;
1477
1502
  return true;
1478
1503
  }
1479
1504
  return false;
@@ -2042,7 +2067,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2042
2067
  }
2043
2068
  /**
2044
2069
  * Finds all leaf nodes in the tree.
2045
- * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
2070
+ * @remarks Time O(N), visits every node. Space O(H) for recursive or iterative stack.
2046
2071
  *
2047
2072
  * @template C - The type of the callback function.
2048
2073
  * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
@@ -2065,15 +2090,15 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2065
2090
  }, "dfs");
2066
2091
  dfs(startNode);
2067
2092
  } else {
2068
- const queue = new Queue([startNode]);
2069
- while (queue.length > 0) {
2070
- const cur = queue.shift();
2093
+ const stack = [startNode];
2094
+ while (stack.length > 0) {
2095
+ const cur = stack.pop();
2071
2096
  if (this.isRealNode(cur)) {
2072
2097
  if (this.isLeaf(cur)) {
2073
2098
  leaves.push(callback(cur));
2074
2099
  }
2075
- if (this.isRealNode(cur.left)) queue.push(cur.left);
2076
- if (this.isRealNode(cur.right)) queue.push(cur.right);
2100
+ if (this.isRealNode(cur.right)) stack.push(cur.right);
2101
+ if (this.isRealNode(cur.left)) stack.push(cur.left);
2077
2102
  }
2078
2103
  }
2079
2104
  }
@@ -2529,42 +2554,98 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2529
2554
  _displayAux(node, options) {
2530
2555
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2531
2556
  const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
2532
- if (node === null && !isShowNull) {
2533
- return emptyDisplayLayout;
2534
- } else if (node === void 0 && !isShowUndefined) {
2535
- return emptyDisplayLayout;
2536
- } else if (this.isNIL(node) && !isShowRedBlackNIL) {
2537
- return emptyDisplayLayout;
2538
- } else if (node !== null && node !== void 0) {
2539
- const key = node.key, line = this.isNIL(node) ? "S" : String(key), width = line.length;
2540
- return _buildNodeDisplay(
2541
- line,
2542
- width,
2543
- this._displayAux(node.left, options),
2544
- this._displayAux(node.right, options)
2545
- );
2546
- } else {
2547
- const line = node === void 0 ? "U" : "N", width = line.length;
2548
- return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
2549
- }
2550
- function _buildNodeDisplay(line, width, left, right) {
2551
- const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
2552
- const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
2553
- 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));
2554
- const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
2555
- const mergedLines = [firstLine, secondLine];
2556
- for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
2557
- const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
2558
- const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
2559
- mergedLines.push(leftLine + " ".repeat(width) + rightLine);
2557
+ const newFrame = /* @__PURE__ */ __name((n) => ({
2558
+ node: n,
2559
+ stage: 0,
2560
+ leftLayout: emptyDisplayLayout,
2561
+ rightLayout: emptyDisplayLayout
2562
+ }), "newFrame");
2563
+ const stack = [newFrame(node)];
2564
+ let result = emptyDisplayLayout;
2565
+ const setChildResult = /* @__PURE__ */ __name((layout) => {
2566
+ if (stack.length === 0) {
2567
+ result = layout;
2568
+ return;
2560
2569
  }
2561
- return [
2562
- mergedLines,
2563
- leftWidth + width + rightWidth,
2564
- Math.max(leftHeight, rightHeight) + 2,
2565
- leftWidth + Math.floor(width / 2)
2566
- ];
2570
+ const parent = stack[stack.length - 1];
2571
+ if (parent.stage === 1) parent.leftLayout = layout;
2572
+ else parent.rightLayout = layout;
2573
+ }, "setChildResult");
2574
+ while (stack.length > 0) {
2575
+ const frame = stack[stack.length - 1];
2576
+ const cur = frame.node;
2577
+ if (frame.stage === 0) {
2578
+ if (this._isDisplayLeaf(cur, options)) {
2579
+ stack.pop();
2580
+ const layout = this._resolveDisplayLeaf(cur, options, emptyDisplayLayout);
2581
+ setChildResult(layout);
2582
+ continue;
2583
+ }
2584
+ frame.stage = 1;
2585
+ stack.push(newFrame(cur.left));
2586
+ } else if (frame.stage === 1) {
2587
+ frame.stage = 2;
2588
+ stack.push(newFrame(cur.right));
2589
+ } else {
2590
+ stack.pop();
2591
+ const line = this.isNIL(cur) ? "S" : String(cur.key);
2592
+ const layout = _BinaryTree._buildNodeDisplay(line, line.length, frame.leftLayout, frame.rightLayout);
2593
+ setChildResult(layout);
2594
+ }
2595
+ }
2596
+ return result;
2597
+ }
2598
+ static _buildNodeDisplay(line, width, left, right) {
2599
+ const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
2600
+ const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
2601
+ 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));
2602
+ const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
2603
+ const mergedLines = [firstLine, secondLine];
2604
+ for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
2605
+ const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
2606
+ const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
2607
+ mergedLines.push(leftLine + " ".repeat(width) + rightLine);
2608
+ }
2609
+ return [
2610
+ mergedLines,
2611
+ leftWidth + width + rightWidth,
2612
+ Math.max(leftHeight, rightHeight) + 2,
2613
+ leftWidth + Math.floor(width / 2)
2614
+ ];
2615
+ }
2616
+ /**
2617
+ * Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
2618
+ */
2619
+ _isDisplayLeaf(node, options) {
2620
+ const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2621
+ if (node === null && !isShowNull) return true;
2622
+ if (node === void 0 && !isShowUndefined) return true;
2623
+ if (this.isNIL(node) && !isShowRedBlackNIL) return true;
2624
+ if (node === null || node === void 0) return true;
2625
+ const hasDisplayableLeft = this._hasDisplayableChild(node.left, options);
2626
+ const hasDisplayableRight = this._hasDisplayableChild(node.right, options);
2627
+ return !hasDisplayableLeft && !hasDisplayableRight;
2628
+ }
2629
+ _hasDisplayableChild(child, options) {
2630
+ if (child === null) return !!options.isShowNull;
2631
+ if (child === void 0) return !!options.isShowUndefined;
2632
+ if (this.isNIL(child)) return !!options.isShowRedBlackNIL;
2633
+ return true;
2634
+ }
2635
+ /**
2636
+ * Resolve a display leaf node to its layout.
2637
+ */
2638
+ _resolveDisplayLeaf(node, options, emptyDisplayLayout) {
2639
+ const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2640
+ if (node === null && !isShowNull) return emptyDisplayLayout;
2641
+ if (node === void 0 && !isShowUndefined) return emptyDisplayLayout;
2642
+ if (this.isNIL(node) && !isShowRedBlackNIL) return emptyDisplayLayout;
2643
+ if (node !== null && node !== void 0) {
2644
+ const line2 = this.isNIL(node) ? "S" : String(node.key);
2645
+ return _BinaryTree._buildNodeDisplay(line2, line2.length, emptyDisplayLayout, emptyDisplayLayout);
2567
2646
  }
2647
+ const line = node === void 0 ? "U" : "N";
2648
+ return _BinaryTree._buildNodeDisplay(line, line.length, [[""], 1, 0, 0], [[""], 1, 0, 0]);
2568
2649
  }
2569
2650
  /**
2570
2651
  * (Protected) Swaps the key/value properties of two nodes.
@@ -3570,9 +3651,15 @@ var _BST = class _BST extends BinaryTree {
3570
3651
  if (a < b) return -1;
3571
3652
  return 0;
3572
3653
  }
3654
+ if (a instanceof Date && b instanceof Date) {
3655
+ const ta = a.getTime();
3656
+ const tb = b.getTime();
3657
+ if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
3658
+ return ta > tb ? 1 : ta < tb ? -1 : 0;
3659
+ }
3573
3660
  if (typeof a === "object" || typeof b === "object") {
3574
- throw TypeError(
3575
- `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
3661
+ throw new TypeError(
3662
+ ERR.comparatorRequired("BST")
3576
3663
  );
3577
3664
  }
3578
3665
  return 0;