graph-typed 1.43.1 → 1.43.3
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.
- package/dist/data-structures/binary-tree/binary-tree.d.ts +8 -8
- package/dist/data-structures/binary-tree/binary-tree.js +62 -62
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -4
- package/dist/data-structures/binary-tree/rb-tree.js +1 -1
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +24 -24
- package/dist/data-structures/binary-tree/tree-multimap.js +50 -50
- package/dist/data-structures/graph/abstract-graph.d.ts +37 -37
- package/dist/data-structures/graph/abstract-graph.js +37 -37
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/data-structures/binary-tree/binary-tree.ts +85 -92
- package/src/data-structures/binary-tree/bst.ts +14 -22
- package/src/data-structures/binary-tree/rb-tree.ts +11 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +56 -58
- package/src/data-structures/graph/abstract-graph.ts +6 -22
- package/src/data-structures/graph/directed-graph.ts +3 -9
- package/src/data-structures/graph/map-graph.ts +6 -6
- package/src/data-structures/graph/undirected-graph.ts +1 -2
- package/src/data-structures/heap/heap.ts +1 -6
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -16
|
@@ -21,8 +21,7 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
21
21
|
|
|
22
22
|
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
25
|
-
{
|
|
24
|
+
implements IBinaryTree<V, N> {
|
|
26
25
|
/**
|
|
27
26
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
28
27
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -114,7 +113,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
114
113
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
115
114
|
* if either `srcNode` or `destNode` is undefined.
|
|
116
115
|
*/
|
|
117
|
-
protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N
|
|
116
|
+
protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
|
|
118
117
|
srcNode = this.ensureNotKey(srcNode);
|
|
119
118
|
destNode = this.ensureNotKey(destNode);
|
|
120
119
|
|
|
@@ -107,8 +107,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
107
107
|
* Represents a binary tree data structure.
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
|
-
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
-
implements IBinaryTree<V, N> {
|
|
110
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> implements IBinaryTree<V, N> {
|
|
112
111
|
iterationType: IterationType = IterationType.ITERATIVE;
|
|
113
112
|
|
|
114
113
|
/**
|
|
@@ -301,7 +300,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
301
300
|
): BiTreeDeleteResult<N>[] {
|
|
302
301
|
const deletedResult: BiTreeDeleteResult<N>[] = [];
|
|
303
302
|
if (!this.root) return deletedResult;
|
|
304
|
-
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
303
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
304
|
+
callback = (node => node) as C;
|
|
305
305
|
|
|
306
306
|
const curr = this.getNode(identifier, callback);
|
|
307
307
|
if (!curr) return deletedResult;
|
|
@@ -330,14 +330,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
330
330
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
331
331
|
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
332
332
|
if (parentOfLeftSubTreeMax) {
|
|
333
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
334
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
333
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
335
334
|
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
336
335
|
needBalanced = parentOfLeftSubTreeMax;
|
|
337
336
|
}
|
|
338
337
|
}
|
|
339
338
|
}
|
|
340
|
-
|
|
341
339
|
}
|
|
342
340
|
this._size = this.size - 1;
|
|
343
341
|
|
|
@@ -383,7 +381,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
383
381
|
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
384
382
|
*/
|
|
385
383
|
|
|
386
|
-
|
|
387
384
|
/**
|
|
388
385
|
* Time Complexity: O(n)
|
|
389
386
|
* Space Complexity: O(log n)
|
|
@@ -412,7 +409,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
412
409
|
|
|
413
410
|
return _getMaxHeight(beginRoot);
|
|
414
411
|
} else {
|
|
415
|
-
const stack: {
|
|
412
|
+
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
416
413
|
let maxHeight = 0;
|
|
417
414
|
|
|
418
415
|
while (stack.length > 0) {
|
|
@@ -539,7 +536,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
539
536
|
* Space Complexity: O(log n).
|
|
540
537
|
*/
|
|
541
538
|
|
|
542
|
-
|
|
543
539
|
/**
|
|
544
540
|
* Time Complexity: O(n)
|
|
545
541
|
* Space Complexity: O(log n).
|
|
@@ -572,7 +568,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
572
568
|
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
573
569
|
iterationType = this.iterationType
|
|
574
570
|
): N[] {
|
|
575
|
-
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
571
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
572
|
+
callback = (node => node) as C;
|
|
576
573
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
577
574
|
if (!beginRoot) return [];
|
|
578
575
|
|
|
@@ -660,7 +657,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
660
657
|
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
661
658
|
iterationType = this.iterationType
|
|
662
659
|
): boolean {
|
|
663
|
-
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
660
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
661
|
+
callback = (node => node) as C;
|
|
664
662
|
|
|
665
663
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
666
664
|
}
|
|
@@ -718,7 +716,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
718
716
|
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
719
717
|
iterationType = this.iterationType
|
|
720
718
|
): N | null | undefined {
|
|
721
|
-
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
719
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
720
|
+
callback = (node => node) as C;
|
|
722
721
|
|
|
723
722
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
724
723
|
}
|
|
@@ -836,7 +835,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
836
835
|
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
837
836
|
iterationType = this.iterationType
|
|
838
837
|
): V | undefined {
|
|
839
|
-
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
838
|
+
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
|
839
|
+
callback = (node => node) as C;
|
|
840
840
|
|
|
841
841
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
842
842
|
}
|
|
@@ -1165,7 +1165,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1165
1165
|
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
1166
1166
|
* @returns a boolean value.
|
|
1167
1167
|
*/
|
|
1168
|
-
isNodeOrNull(node: any): node is
|
|
1168
|
+
isNodeOrNull(node: any): node is N | null {
|
|
1169
1169
|
return this.isRealNode(node) || node === null;
|
|
1170
1170
|
}
|
|
1171
1171
|
|
|
@@ -1238,7 +1238,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1238
1238
|
iterationType: IterationType = IterationType.ITERATIVE,
|
|
1239
1239
|
includeNull = false
|
|
1240
1240
|
): ReturnType<C>[] {
|
|
1241
|
-
|
|
1242
1241
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
1243
1242
|
if (!beginRoot) return [];
|
|
1244
1243
|
const ans: ReturnType<C>[] = [];
|
|
@@ -1285,7 +1284,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1285
1284
|
_traverse(beginRoot);
|
|
1286
1285
|
} else {
|
|
1287
1286
|
// 0: visit, 1: print
|
|
1288
|
-
const stack: {
|
|
1287
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
1289
1288
|
|
|
1290
1289
|
while (stack.length > 0) {
|
|
1291
1290
|
const cur = stack.pop();
|
|
@@ -1454,7 +1453,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1454
1453
|
* Space complexity: O(n)
|
|
1455
1454
|
*/
|
|
1456
1455
|
|
|
1457
|
-
|
|
1458
1456
|
/**
|
|
1459
1457
|
* Time complexity: O(n)
|
|
1460
1458
|
* Space complexity: O(n)
|
|
@@ -1523,8 +1521,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1523
1521
|
return levelsNodes;
|
|
1524
1522
|
}
|
|
1525
1523
|
|
|
1526
|
-
getPredecessor(node: N): N
|
|
1527
|
-
|
|
1524
|
+
getPredecessor(node: N): N;
|
|
1528
1525
|
|
|
1529
1526
|
/**
|
|
1530
1527
|
* The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
|
|
@@ -1549,7 +1546,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1549
1546
|
}
|
|
1550
1547
|
}
|
|
1551
1548
|
|
|
1552
|
-
|
|
1553
1549
|
/**
|
|
1554
1550
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1555
1551
|
* @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
|
|
@@ -1681,7 +1677,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1681
1677
|
return ans;
|
|
1682
1678
|
}
|
|
1683
1679
|
|
|
1684
|
-
|
|
1685
1680
|
/**
|
|
1686
1681
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1687
1682
|
* either an iterative or recursive manner.
|
|
@@ -1691,7 +1686,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1691
1686
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1692
1687
|
* binary tree nodes in a specific order.
|
|
1693
1688
|
*/
|
|
1694
|
-
*
|
|
1689
|
+
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1695
1690
|
if (!node) {
|
|
1696
1691
|
return;
|
|
1697
1692
|
}
|
|
@@ -1722,6 +1717,74 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1722
1717
|
}
|
|
1723
1718
|
}
|
|
1724
1719
|
|
|
1720
|
+
/**
|
|
1721
|
+
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1722
|
+
* @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
|
|
1723
|
+
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1724
|
+
* following types:
|
|
1725
|
+
*/
|
|
1726
|
+
print(beginRoot: BTNKey | N | null | undefined = this.root): void {
|
|
1727
|
+
beginRoot = this.ensureNotKey(beginRoot);
|
|
1728
|
+
if (!beginRoot) return;
|
|
1729
|
+
|
|
1730
|
+
const display = (root: N | null | undefined): void => {
|
|
1731
|
+
const [lines, , ,] = _displayAux(root);
|
|
1732
|
+
for (const line of lines) {
|
|
1733
|
+
console.log(line);
|
|
1734
|
+
}
|
|
1735
|
+
};
|
|
1736
|
+
|
|
1737
|
+
const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
|
|
1738
|
+
if (!this.isRealNode(node)) {
|
|
1739
|
+
return [[], 0, 0, 0];
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
|
|
1743
|
+
const line = `${node.key}`;
|
|
1744
|
+
const width = line.length;
|
|
1745
|
+
const height = 1;
|
|
1746
|
+
const middle = Math.floor(width / 2);
|
|
1747
|
+
return [[line], width, height, middle];
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
if (this.isRealNode(node) && !this.isRealNode(node.right)) {
|
|
1751
|
+
const [lines, n, p, x] = _displayAux(node.left);
|
|
1752
|
+
const s = `${node.key}`;
|
|
1753
|
+
const u = s.length;
|
|
1754
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
1755
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
1756
|
+
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1757
|
+
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
if (this.isRealNode(node) && !this.isRealNode(node.left)) {
|
|
1761
|
+
const [lines, n, p, u] = _displayAux(node.right);
|
|
1762
|
+
const s = `${node.key}`;
|
|
1763
|
+
const x = s.length;
|
|
1764
|
+
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
1765
|
+
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
1766
|
+
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
1767
|
+
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
const [left, n, p, x] = _displayAux(node.left);
|
|
1771
|
+
const [right, m, q, y] = _displayAux(node.right);
|
|
1772
|
+
const s = `${node.key}`;
|
|
1773
|
+
const u = s.length;
|
|
1774
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
1775
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
1776
|
+
if (p < q) {
|
|
1777
|
+
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
1778
|
+
} else if (q < p) {
|
|
1779
|
+
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
1780
|
+
}
|
|
1781
|
+
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
1782
|
+
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
1783
|
+
};
|
|
1784
|
+
|
|
1785
|
+
display(beginRoot);
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1725
1788
|
protected _defaultOneParamCallback = (node: N) => node.key;
|
|
1726
1789
|
|
|
1727
1790
|
/**
|
|
@@ -1749,7 +1812,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1749
1812
|
return destNode;
|
|
1750
1813
|
}
|
|
1751
1814
|
return undefined;
|
|
1752
|
-
|
|
1753
1815
|
}
|
|
1754
1816
|
|
|
1755
1817
|
/**
|
|
@@ -1801,73 +1863,4 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1801
1863
|
}
|
|
1802
1864
|
this._root = v;
|
|
1803
1865
|
}
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
/**
|
|
1807
|
-
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1808
|
-
* @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
|
|
1809
|
-
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1810
|
-
* following types:
|
|
1811
|
-
*/
|
|
1812
|
-
print(beginRoot: BTNKey | N | null | undefined = this.root): void {
|
|
1813
|
-
beginRoot = this.ensureNotKey(beginRoot);
|
|
1814
|
-
if (!beginRoot) return;
|
|
1815
|
-
|
|
1816
|
-
const display = (root: N | null | undefined): void => {
|
|
1817
|
-
const [lines, , ,] = _displayAux(root);
|
|
1818
|
-
for (const line of lines) {
|
|
1819
|
-
console.log(line);
|
|
1820
|
-
}
|
|
1821
|
-
};
|
|
1822
|
-
|
|
1823
|
-
const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
|
|
1824
|
-
if (!this.isRealNode(node)) {
|
|
1825
|
-
return [[], 0, 0, 0];
|
|
1826
|
-
}
|
|
1827
|
-
|
|
1828
|
-
if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
|
|
1829
|
-
const line = `${node.key}`;
|
|
1830
|
-
const width = line.length;
|
|
1831
|
-
const height = 1;
|
|
1832
|
-
const middle = Math.floor(width / 2);
|
|
1833
|
-
return [[line], width, height, middle];
|
|
1834
|
-
}
|
|
1835
|
-
|
|
1836
|
-
if (this.isRealNode(node) && !this.isRealNode(node.right)) {
|
|
1837
|
-
const [lines, n, p, x] = _displayAux(node.left);
|
|
1838
|
-
const s = `${node.key}`;
|
|
1839
|
-
const u = s.length;
|
|
1840
|
-
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
1841
|
-
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
1842
|
-
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1843
|
-
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1844
|
-
}
|
|
1845
|
-
|
|
1846
|
-
if (this.isRealNode(node) && !this.isRealNode(node.left)) {
|
|
1847
|
-
const [lines, n, p, u] = _displayAux(node.right);
|
|
1848
|
-
const s = `${node.key}`;
|
|
1849
|
-
const x = s.length;
|
|
1850
|
-
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
1851
|
-
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
1852
|
-
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
1853
|
-
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
1854
|
-
}
|
|
1855
|
-
|
|
1856
|
-
const [left, n, p, x] = _displayAux(node.left);
|
|
1857
|
-
const [right, m, q, y] = _displayAux(node.right);
|
|
1858
|
-
const s = `${node.key}`;
|
|
1859
|
-
const u = s.length;
|
|
1860
|
-
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
1861
|
-
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
1862
|
-
if (p < q) {
|
|
1863
|
-
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
1864
|
-
} else if (q < p) {
|
|
1865
|
-
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
1866
|
-
}
|
|
1867
|
-
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
1868
|
-
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
1869
|
-
};
|
|
1870
|
-
|
|
1871
|
-
display(beginRoot);
|
|
1872
|
-
}
|
|
1873
1866
|
}
|
|
@@ -12,7 +12,7 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
12
12
|
import {Queue} from '../queue';
|
|
13
13
|
|
|
14
14
|
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
15
|
-
override parent?: N
|
|
15
|
+
override parent?: N;
|
|
16
16
|
|
|
17
17
|
constructor(key: BTNKey, value?: V) {
|
|
18
18
|
super(key, value);
|
|
@@ -21,7 +21,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
21
21
|
this._right = undefined;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
protected override _left?: N
|
|
24
|
+
protected override _left?: N;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Get the left child node.
|
|
@@ -41,8 +41,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
41
41
|
this._left = v;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
protected override _right?: N ;
|
|
44
|
+
protected override _right?: N;
|
|
46
45
|
|
|
47
46
|
/**
|
|
48
47
|
* Get the right child node.
|
|
@@ -63,10 +62,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
|
|
66
|
-
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
67
|
-
extends BinaryTree<V, N>
|
|
68
|
-
implements IBinaryTree<V, N> {
|
|
69
|
-
|
|
65
|
+
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
70
66
|
/**
|
|
71
67
|
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
72
68
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
@@ -83,7 +79,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
83
79
|
}
|
|
84
80
|
}
|
|
85
81
|
|
|
86
|
-
protected override _root?: N
|
|
82
|
+
protected override _root?: N;
|
|
87
83
|
|
|
88
84
|
/**
|
|
89
85
|
* Get the root node of the binary tree.
|
|
@@ -109,11 +105,10 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
109
105
|
* Space Complexity: O(1) - Constant space is used.
|
|
110
106
|
*/
|
|
111
107
|
|
|
112
|
-
|
|
113
108
|
/**
|
|
114
109
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
115
110
|
* Space Complexity: O(1) - Constant space is used.
|
|
116
|
-
*
|
|
111
|
+
*
|
|
117
112
|
* The `add` function adds a new node to a binary search tree based on the provided key and value.
|
|
118
113
|
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
119
114
|
* following types:
|
|
@@ -193,7 +188,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
193
188
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
194
189
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
195
190
|
*/
|
|
196
|
-
|
|
191
|
+
|
|
197
192
|
/**
|
|
198
193
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
199
194
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
@@ -228,12 +223,10 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
228
223
|
if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
|
|
229
224
|
return super.addMany(keysOrNodes, data).map(n => n ?? undefined);
|
|
230
225
|
}
|
|
231
|
-
|
|
226
|
+
|
|
232
227
|
const inserted: (N | undefined)[] = [];
|
|
233
|
-
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
234
|
-
|
|
235
|
-
);
|
|
236
|
-
|
|
228
|
+
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map((value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]);
|
|
229
|
+
|
|
237
230
|
let sorted = [];
|
|
238
231
|
|
|
239
232
|
function _isNodeOrUndefinedTuple(arr: [BTNKey | N, V][]): arr is [N, V][] {
|
|
@@ -244,7 +237,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
244
237
|
const _isBinaryTreeKeyOrNullTuple = (arr: [BTNKey | N, V][]): arr is [BTNKey, V][] => {
|
|
245
238
|
for (const [keyOrNode] of arr) if (this.isNodeKey(keyOrNode)) return true;
|
|
246
239
|
return false;
|
|
247
|
-
}
|
|
240
|
+
};
|
|
248
241
|
|
|
249
242
|
let sortedKeysOrNodes: (number | N | undefined)[] = [],
|
|
250
243
|
sortedData: (V | undefined)[] | undefined = [];
|
|
@@ -344,7 +337,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
344
337
|
const _dfs = (cur: N): N | undefined => {
|
|
345
338
|
if (cur.key === key) return cur;
|
|
346
339
|
if (!cur.left && !cur.right) return;
|
|
347
|
-
|
|
340
|
+
|
|
348
341
|
if (this._compare(cur.key, key) === CP.gt && cur.left) return _dfs(cur.left);
|
|
349
342
|
if (this._compare(cur.key, key) === CP.lt && cur.right) return _dfs(cur.right);
|
|
350
343
|
};
|
|
@@ -380,7 +373,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
380
373
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
381
374
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
382
375
|
*/
|
|
383
|
-
|
|
376
|
+
|
|
384
377
|
/**
|
|
385
378
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
386
379
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -580,7 +573,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
580
573
|
if (l <= r) {
|
|
581
574
|
const m = l + Math.floor((r - l) / 2);
|
|
582
575
|
const midNode = sorted[m];
|
|
583
|
-
debugger
|
|
576
|
+
debugger;
|
|
584
577
|
this.add(midNode.key, midNode.value);
|
|
585
578
|
stack.push([m + 1, r]);
|
|
586
579
|
stack.push([l, m - 1]);
|
|
@@ -672,5 +665,4 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
672
665
|
else if (compared < 0) return CP.lt;
|
|
673
666
|
else return CP.eq;
|
|
674
667
|
}
|
|
675
|
-
|
|
676
668
|
}
|
|
@@ -6,21 +6,14 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
IterationType,
|
|
14
|
-
RBTNColor,
|
|
15
|
-
RedBlackTreeNodeNested,
|
|
16
|
-
RBTreeOptions
|
|
17
|
-
} from '../../types';
|
|
18
|
-
import {BST, BSTNode} from "./bst";
|
|
19
|
-
import {IBinaryTree} from "../../interfaces";
|
|
20
|
-
import {BinaryTreeNode} from "./binary-tree";
|
|
9
|
+
import {BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNodeNested} from '../../types';
|
|
10
|
+
import {BST, BSTNode} from './bst';
|
|
11
|
+
import {IBinaryTree} from '../../interfaces';
|
|
12
|
+
import {BinaryTreeNode} from './binary-tree';
|
|
21
13
|
|
|
22
14
|
export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
23
15
|
color: RBTNColor;
|
|
16
|
+
|
|
24
17
|
constructor(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
|
25
18
|
super(key, value);
|
|
26
19
|
this.color = color;
|
|
@@ -36,8 +29,8 @@ export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBla
|
|
|
36
29
|
*/
|
|
37
30
|
export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>>
|
|
38
31
|
extends BST<V, N>
|
|
39
|
-
implements IBinaryTree<V, N>
|
|
40
|
-
|
|
32
|
+
implements IBinaryTree<V, N> {
|
|
33
|
+
NIL: N = new RedBlackTreeNode<V>(NaN) as unknown as N;
|
|
41
34
|
|
|
42
35
|
/**
|
|
43
36
|
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
|
@@ -61,8 +54,6 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
61
54
|
return this._size;
|
|
62
55
|
}
|
|
63
56
|
|
|
64
|
-
NIL: N = new RedBlackTreeNode<V>(NaN) as unknown as N;
|
|
65
|
-
|
|
66
57
|
/**
|
|
67
58
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
68
59
|
* Space Complexity: O(1)
|
|
@@ -83,7 +74,7 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
83
74
|
let node: N;
|
|
84
75
|
if (this.isNodeKey(keyOrNode)) {
|
|
85
76
|
node = this.createNode(keyOrNode, value, RBTNColor.RED);
|
|
86
|
-
} else if(keyOrNode instanceof RedBlackTreeNode) {
|
|
77
|
+
} else if (keyOrNode instanceof RedBlackTreeNode) {
|
|
87
78
|
node = keyOrNode;
|
|
88
79
|
} else if (keyOrNode === null) {
|
|
89
80
|
return;
|
|
@@ -226,21 +217,21 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
226
217
|
callback?: C,
|
|
227
218
|
beginRoot?: N | undefined,
|
|
228
219
|
iterationType?: IterationType
|
|
229
|
-
): N
|
|
220
|
+
): N | undefined;
|
|
230
221
|
|
|
231
222
|
getNode<C extends BTNCallback<N, N>>(
|
|
232
223
|
identifier: N | undefined,
|
|
233
224
|
callback?: C,
|
|
234
225
|
beginRoot?: N | undefined,
|
|
235
226
|
iterationType?: IterationType
|
|
236
|
-
): N
|
|
227
|
+
): N | undefined;
|
|
237
228
|
|
|
238
229
|
getNode<C extends BTNCallback<N>>(
|
|
239
230
|
identifier: ReturnType<C>,
|
|
240
231
|
callback: C,
|
|
241
232
|
beginRoot?: N | undefined,
|
|
242
233
|
iterationType?: IterationType
|
|
243
|
-
): N
|
|
234
|
+
): N | undefined;
|
|
244
235
|
|
|
245
236
|
/**
|
|
246
237
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|