graph-typed 1.46.5 → 1.46.7

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.
@@ -441,11 +441,12 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
441
441
  [Symbol.iterator](node?: N | null | undefined): Generator<BTNKey, void, undefined>;
442
442
  /**
443
443
  * The `print` function is used to display a binary tree structure in a visually appealing way.
444
- * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
444
+ * @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
445
445
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
446
446
  * following types:
447
447
  */
448
448
  print(beginRoot?: BTNKey | N | null | undefined): void;
449
+ protected _displayAux(node: N | null | undefined): [string[], number, number, number];
449
450
  protected _defaultOneParamCallback: (node: N) => number;
450
451
  /**
451
452
  * Swap the data of two nodes in the binary tree.
@@ -1447,7 +1447,7 @@ class BinaryTree {
1447
1447
  }
1448
1448
  /**
1449
1449
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1450
- * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
1450
+ * @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
1451
1451
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
1452
1452
  * following types:
1453
1453
  */
@@ -1456,57 +1456,40 @@ class BinaryTree {
1456
1456
  if (!beginRoot)
1457
1457
  return;
1458
1458
  const display = (root) => {
1459
- const [lines, , ,] = _displayAux(root);
1459
+ const [lines, , ,] = this._displayAux(root);
1460
1460
  for (const line of lines) {
1461
1461
  console.log(line);
1462
1462
  }
1463
1463
  };
1464
- const _displayAux = (node) => {
1465
- if (!this.isRealNode(node)) {
1466
- return [[], 0, 0, 0];
1467
- }
1468
- if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
1469
- const line = `${node.key}`;
1470
- const width = line.length;
1471
- const height = 1;
1472
- const middle = Math.floor(width / 2);
1473
- return [[line], width, height, middle];
1474
- }
1475
- if (this.isRealNode(node) && !this.isRealNode(node.right)) {
1476
- const [lines, n, p, x] = _displayAux(node.left);
1477
- const s = `${node.key}`;
1478
- const u = s.length;
1479
- const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
1480
- const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
1481
- const shifted_lines = lines.map(line => line + ' '.repeat(u));
1482
- return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
1483
- }
1484
- if (this.isRealNode(node) && !this.isRealNode(node.left)) {
1485
- const [lines, n, p, u] = _displayAux(node.right);
1486
- const s = `${node.key}`;
1487
- const x = s.length;
1488
- const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
1489
- const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
1490
- const shifted_lines = lines.map(line => ' '.repeat(u) + line);
1491
- return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
1492
- }
1493
- const [left, n, p, x] = _displayAux(node.left);
1494
- const [right, m, q, y] = _displayAux(node.right);
1495
- const s = `${node.key}`;
1496
- const u = s.length;
1497
- const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
1498
- const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
1499
- if (p < q) {
1500
- left.push(...new Array(q - p).fill(' '.repeat(n)));
1501
- }
1502
- else if (q < p) {
1503
- right.push(...new Array(p - q).fill(' '.repeat(m)));
1504
- }
1505
- const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
1506
- return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
1507
- };
1508
1464
  display(beginRoot);
1509
1465
  }
1466
+ _displayAux(node) {
1467
+ if (!node) {
1468
+ return [['─'], 1, 0, 0];
1469
+ }
1470
+ const line = node.key.toString();
1471
+ const width = line.length;
1472
+ if (!node.left && !node.right) {
1473
+ return [[line], width, 1, Math.floor(width / 2)];
1474
+ }
1475
+ const [leftLines, leftWidth, leftHeight, leftMiddle] = node.left ? this._displayAux(node.left) : [[''], 0, 0, 0];
1476
+ const [rightLines, rightWidth, rightHeight, rightMiddle] = node.right ? this._displayAux(node.right) : [[''], 0, 0, 0];
1477
+ const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1))
1478
+ + '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1))
1479
+ + line
1480
+ + '_'.repeat(Math.max(0, rightMiddle))
1481
+ + ' '.repeat(Math.max(0, rightWidth - rightMiddle));
1482
+ const secondLine = (leftHeight > 0 ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1) : ' '.repeat(leftWidth))
1483
+ + ' '.repeat(width)
1484
+ + (rightHeight > 0 ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1) : ' '.repeat(rightWidth));
1485
+ const mergedLines = [firstLine, secondLine];
1486
+ for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
1487
+ const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
1488
+ const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
1489
+ mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
1490
+ }
1491
+ return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
1492
+ }
1510
1493
  /**
1511
1494
  * Swap the data of two nodes in the binary tree.
1512
1495
  * @param {N} srcNode - The source node to swap.
@@ -81,11 +81,16 @@ class RedBlackTree extends bst_1.BST {
81
81
  let x = this.root;
82
82
  while (x !== this.NIL) {
83
83
  y = x;
84
- if (x && node.key < x.key) {
85
- x = x.left;
86
- }
87
- else {
88
- x = x === null || x === void 0 ? void 0 : x.right;
84
+ if (x) {
85
+ if (node.key < x.key) {
86
+ x = x.left;
87
+ }
88
+ else if (node.key > x.key) {
89
+ x = x === null || x === void 0 ? void 0 : x.right;
90
+ }
91
+ else {
92
+ return;
93
+ }
89
94
  }
90
95
  }
91
96
  node.parent = y;
package/dist/index.d.ts CHANGED
@@ -7,4 +7,4 @@
7
7
  */
8
8
  export * from './data-structures/graph';
9
9
  export * from './types/data-structures/graph';
10
- export * from './types/helpers';
10
+ export * from './types/common';
package/dist/index.js CHANGED
@@ -37,4 +37,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
37
37
  // } from 'data-structure-typed';
38
38
  __exportStar(require("./data-structures/graph"), exports);
39
39
  __exportStar(require("./types/data-structures/graph"), exports);
40
- __exportStar(require("./types/helpers"), exports);
40
+ __exportStar(require("./types/common"), exports);
@@ -1,3 +1,3 @@
1
1
  export * from './data-structures';
2
- export * from './helpers';
2
+ export * from './common';
3
3
  export * from './utils';
@@ -15,5 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./data-structures"), exports);
18
- __exportStar(require("./helpers"), exports);
18
+ __exportStar(require("./common"), exports);
19
19
  __exportStar(require("./utils"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graph-typed",
3
- "version": "1.46.5",
3
+ "version": "1.46.7",
4
4
  "description": "Graph. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -136,6 +136,6 @@
136
136
  "typescript": "^4.9.5"
137
137
  },
138
138
  "dependencies": {
139
- "data-structure-typed": "^1.46.5"
139
+ "data-structure-typed": "^1.46.7"
140
140
  }
141
141
  }
@@ -1727,7 +1727,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1727
1727
 
1728
1728
  /**
1729
1729
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1730
- * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
1730
+ * @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
1731
1731
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
1732
1732
  * following types:
1733
1733
  */
@@ -1736,61 +1736,48 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1736
1736
  if (!beginRoot) return;
1737
1737
 
1738
1738
  const display = (root: N | null | undefined): void => {
1739
- const [lines, , ,] = _displayAux(root);
1739
+ const [lines, , ,] = this._displayAux(root);
1740
1740
  for (const line of lines) {
1741
1741
  console.log(line);
1742
1742
  }
1743
1743
  };
1744
1744
 
1745
- const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
1746
- if (!this.isRealNode(node)) {
1747
- return [[], 0, 0, 0];
1748
- }
1745
+ display(beginRoot);
1746
+ }
1749
1747
 
1750
- if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
1751
- const line = `${node.key}`;
1752
- const width = line.length;
1753
- const height = 1;
1754
- const middle = Math.floor(width / 2);
1755
- return [[line], width, height, middle];
1756
- }
1748
+ protected _displayAux(node: N | null | undefined): [string[], number, number, number] {
1749
+ if (!node) {
1750
+ return [['─'], 1, 0, 0];
1751
+ }
1757
1752
 
1758
- if (this.isRealNode(node) && !this.isRealNode(node.right)) {
1759
- const [lines, n, p, x] = _displayAux(node.left);
1760
- const s = `${node.key}`;
1761
- const u = s.length;
1762
- const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
1763
- const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
1764
- const shifted_lines = lines.map(line => line + ' '.repeat(u));
1765
- return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
1766
- }
1753
+ const line = node.key.toString();
1754
+ const width = line.length;
1767
1755
 
1768
- if (this.isRealNode(node) && !this.isRealNode(node.left)) {
1769
- const [lines, n, p, u] = _displayAux(node.right);
1770
- const s = `${node.key}`;
1771
- const x = s.length;
1772
- const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
1773
- const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
1774
- const shifted_lines = lines.map(line => ' '.repeat(u) + line);
1775
- return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
1776
- }
1756
+ if (!node.left && !node.right) {
1757
+ return [[line], width, 1, Math.floor(width / 2)];
1758
+ }
1777
1759
 
1778
- const [left, n, p, x] = _displayAux(node.left);
1779
- const [right, m, q, y] = _displayAux(node.right);
1780
- const s = `${node.key}`;
1781
- const u = s.length;
1782
- const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
1783
- const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
1784
- if (p < q) {
1785
- left.push(...new Array(q - p).fill(' '.repeat(n)));
1786
- } else if (q < p) {
1787
- right.push(...new Array(p - q).fill(' '.repeat(m)));
1788
- }
1789
- const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
1790
- return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
1791
- };
1760
+ const [leftLines, leftWidth, leftHeight, leftMiddle] = node.left ? this._displayAux(node.left) : [[''], 0, 0, 0];
1761
+ const [rightLines, rightWidth, rightHeight, rightMiddle] = node.right ? this._displayAux(node.right) : [[''], 0, 0, 0];
1792
1762
 
1793
- display(beginRoot);
1763
+ const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1))
1764
+ + '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1))
1765
+ + line
1766
+ + '_'.repeat(Math.max(0, rightMiddle))
1767
+ + ' '.repeat(Math.max(0, rightWidth - rightMiddle));
1768
+
1769
+ const secondLine = (leftHeight > 0 ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1) : ' '.repeat(leftWidth))
1770
+ + ' '.repeat(width)
1771
+ + (rightHeight > 0 ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1) : ' '.repeat(rightWidth));
1772
+
1773
+ const mergedLines = [firstLine, secondLine];
1774
+ for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
1775
+ const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
1776
+ const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
1777
+ mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
1778
+ }
1779
+
1780
+ return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
1794
1781
  }
1795
1782
 
1796
1783
  protected _defaultOneParamCallback = (node: N) => node.key;
@@ -103,11 +103,16 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
103
103
 
104
104
  while (x !== this.NIL) {
105
105
  y = x;
106
- if (x && node.key < x.key) {
107
- x = x.left;
108
- } else {
109
- x = x?.right;
106
+ if (x) {
107
+ if (node.key < x.key) {
108
+ x = x.left;
109
+ } else if (node.key > x.key) {
110
+ x = x?.right;
111
+ } else {
112
+ return;
113
+ }
110
114
  }
115
+
111
116
  }
112
117
 
113
118
  node.parent = y;
package/src/index.ts CHANGED
@@ -21,4 +21,4 @@
21
21
  // } from 'data-structure-typed';
22
22
  export * from './data-structures/graph';
23
23
  export * from './types/data-structures/graph';
24
- export * from './types/helpers';
24
+ export * from './types/common';
@@ -1,3 +1,3 @@
1
1
  export * from './data-structures';
2
- export * from './helpers';
2
+ export * from './common';
3
3
  export * from './utils';
File without changes
File without changes
File without changes