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
@@ -61,6 +61,49 @@ function makeTrampoline(fn) {
61
61
  }
62
62
  __name(makeTrampoline, "makeTrampoline");
63
63
 
64
+ // src/common/error.ts
65
+ var ERR = {
66
+ // Range / index
67
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
68
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
69
+ // Type / argument
70
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
71
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
72
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
73
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
74
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
75
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
76
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
77
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
78
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
79
+ // State / operation
80
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
81
+ // Matrix
82
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
83
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
84
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
85
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
86
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
87
+ };
88
+
89
+ // src/common/index.ts
90
+ var _Range = class _Range {
91
+ constructor(low, high, includeLow = true, includeHigh = true) {
92
+ this.low = low;
93
+ this.high = high;
94
+ this.includeLow = includeLow;
95
+ this.includeHigh = includeHigh;
96
+ }
97
+ // Determine whether a key is within the range
98
+ isInRange(key, comparator) {
99
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
100
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
101
+ return lowCheck && highCheck;
102
+ }
103
+ };
104
+ __name(_Range, "Range");
105
+ var Range = _Range;
106
+
64
107
  // src/data-structures/base/iterable-element-base.ts
65
108
  var _IterableElementBase = class _IterableElementBase {
66
109
  /**
@@ -83,7 +126,7 @@ var _IterableElementBase = class _IterableElementBase {
83
126
  if (options) {
84
127
  const { toElementFn } = options;
85
128
  if (typeof toElementFn === "function") this._toElementFn = toElementFn;
86
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
129
+ else if (toElementFn) throw new TypeError(ERR.notAFunction("toElementFn"));
87
130
  }
88
131
  }
89
132
  /**
@@ -239,7 +282,7 @@ var _IterableElementBase = class _IterableElementBase {
239
282
  acc = initialValue;
240
283
  } else {
241
284
  const first = iter.next();
242
- if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
285
+ if (first.done) throw new TypeError(ERR.reduceEmpty());
243
286
  acc = first.value;
244
287
  index = 1;
245
288
  }
@@ -1025,24 +1068,6 @@ var _IterableEntryBase = class _IterableEntryBase {
1025
1068
  __name(_IterableEntryBase, "IterableEntryBase");
1026
1069
  var IterableEntryBase = _IterableEntryBase;
1027
1070
 
1028
- // src/common/index.ts
1029
- var _Range = class _Range {
1030
- constructor(low, high, includeLow = true, includeHigh = true) {
1031
- this.low = low;
1032
- this.high = high;
1033
- this.includeLow = includeLow;
1034
- this.includeHigh = includeHigh;
1035
- }
1036
- // Determine whether a key is within the range
1037
- isInRange(key, comparator) {
1038
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1039
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1040
- return lowCheck && highCheck;
1041
- }
1042
- };
1043
- __name(_Range, "Range");
1044
- var Range = _Range;
1045
-
1046
1071
  // src/data-structures/binary-tree/binary-tree.ts
1047
1072
  var _BinaryTreeNode = class _BinaryTreeNode {
1048
1073
  /**
@@ -1208,14 +1233,14 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1208
1233
  * @param node - The node.
1209
1234
  * @returns The node's key or undefined.
1210
1235
  */
1211
- __publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node ? node.key : void 0, "_DEFAULT_NODE_CALLBACK"));
1236
+ __publicField(this, "_DEFAULT_NODE_CALLBACK", /* @__PURE__ */ __name((node) => node == null ? void 0 : node.key, "_DEFAULT_NODE_CALLBACK"));
1212
1237
  if (options) {
1213
1238
  const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
1214
1239
  if (iterationType) this.iterationType = iterationType;
1215
1240
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1216
1241
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1217
1242
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1218
- else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1243
+ else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1219
1244
  }
1220
1245
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1221
1246
  }
@@ -1443,7 +1468,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1443
1468
  if (!this._root) {
1444
1469
  this._setRoot(newNode);
1445
1470
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1446
- this._size = 1;
1471
+ if (newNode !== null) this._size = 1;
1447
1472
  return true;
1448
1473
  }
1449
1474
  const queue = new Queue([this._root]);
@@ -1475,7 +1500,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1475
1500
  potentialParent.right = newNode;
1476
1501
  }
1477
1502
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1478
- this._size++;
1503
+ if (newNode !== null) this._size++;
1479
1504
  return true;
1480
1505
  }
1481
1506
  return false;
@@ -2044,7 +2069,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2044
2069
  }
2045
2070
  /**
2046
2071
  * Finds all leaf nodes in the tree.
2047
- * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
2072
+ * @remarks Time O(N), visits every node. Space O(H) for recursive or iterative stack.
2048
2073
  *
2049
2074
  * @template C - The type of the callback function.
2050
2075
  * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
@@ -2067,15 +2092,15 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2067
2092
  }, "dfs");
2068
2093
  dfs(startNode);
2069
2094
  } else {
2070
- const queue = new Queue([startNode]);
2071
- while (queue.length > 0) {
2072
- const cur = queue.shift();
2095
+ const stack = [startNode];
2096
+ while (stack.length > 0) {
2097
+ const cur = stack.pop();
2073
2098
  if (this.isRealNode(cur)) {
2074
2099
  if (this.isLeaf(cur)) {
2075
2100
  leaves.push(callback(cur));
2076
2101
  }
2077
- if (this.isRealNode(cur.left)) queue.push(cur.left);
2078
- if (this.isRealNode(cur.right)) queue.push(cur.right);
2102
+ if (this.isRealNode(cur.right)) stack.push(cur.right);
2103
+ if (this.isRealNode(cur.left)) stack.push(cur.left);
2079
2104
  }
2080
2105
  }
2081
2106
  }
@@ -2531,42 +2556,98 @@ var _BinaryTree = class _BinaryTree 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;
2562
2571
  }
2563
- return [
2564
- mergedLines,
2565
- leftWidth + width + rightWidth,
2566
- Math.max(leftHeight, rightHeight) + 2,
2567
- leftWidth + Math.floor(width / 2)
2568
- ];
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);
2596
+ }
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);
2569
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 _BST 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;