mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 04:44:04 +00:00
fix: binary tree print bug fixed. test: integration test of rb-tree
This commit is contained in:
parent
5eaf1d6aab
commit
92552da31c
|
@ -1736,63 +1736,50 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
if (!beginRoot) return;
|
||||
|
||||
const display = (root: N | null | undefined): void => {
|
||||
const [lines, , ,] = _displayAux(root);
|
||||
const [lines, , ,] = this._displayAux(root);
|
||||
for (const line of lines) {
|
||||
console.log(line);
|
||||
}
|
||||
};
|
||||
|
||||
const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
|
||||
if (!this.isRealNode(node)) {
|
||||
return [[], 0, 0, 0];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
|
||||
const line = `${node.key}`;
|
||||
const width = line.length;
|
||||
const height = 1;
|
||||
const middle = Math.floor(width / 2);
|
||||
return [[line], width, height, middle];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.right)) {
|
||||
const [lines, n, p, x] = _displayAux(node.left);
|
||||
const s = `${node.key}`;
|
||||
const u = s.length;
|
||||
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
||||
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
||||
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
||||
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.left)) {
|
||||
const [lines, n, p, u] = _displayAux(node.right);
|
||||
const s = `${node.key}`;
|
||||
const x = s.length;
|
||||
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
||||
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
||||
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
||||
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
||||
}
|
||||
|
||||
const [left, n, p, x] = _displayAux(node.left);
|
||||
const [right, m, q, y] = _displayAux(node.right);
|
||||
const s = `${node.key}`;
|
||||
const u = s.length;
|
||||
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
||||
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
||||
if (p < q) {
|
||||
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
||||
} else if (q < p) {
|
||||
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
||||
}
|
||||
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
||||
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
||||
};
|
||||
|
||||
display(beginRoot);
|
||||
}
|
||||
|
||||
protected _displayAux(node: N | null | undefined): [string[], number, number, number] {
|
||||
if (!node) {
|
||||
return [['─'], 1, 0, 0];
|
||||
}
|
||||
|
||||
const line = node.key.toString();
|
||||
const width = line.length;
|
||||
|
||||
if (!node.left && !node.right) {
|
||||
return [[line], width, 1, Math.floor(width / 2)];
|
||||
}
|
||||
|
||||
const [leftLines, leftWidth, leftHeight, leftMiddle] = node.left ? this._displayAux(node.left) : [[''], 0, 0, 0];
|
||||
const [rightLines, rightWidth, rightHeight, rightMiddle] = node.right ? this._displayAux(node.right) : [[''], 0, 0, 0];
|
||||
|
||||
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));
|
||||
|
||||
const secondLine = (leftHeight > 0 ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1) : ' '.repeat(leftWidth))
|
||||
+ ' '.repeat(width)
|
||||
+ (rightHeight > 0 ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1) : ' '.repeat(rightWidth));
|
||||
|
||||
const mergedLines = [firstLine, secondLine];
|
||||
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
||||
const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
|
||||
const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
|
||||
mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
|
||||
}
|
||||
|
||||
return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
|
||||
}
|
||||
|
||||
protected _defaultOneParamCallback = (node: N) => node.key;
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,8 +3,12 @@
|
|||
<head>
|
||||
<meta charset='UTF-8'>
|
||||
<title>CDN Test</title>
|
||||
<!-- <script src="../../dist/umd/data-structure-typed.min.js"></script>-->
|
||||
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
||||
<script src="../../dist/umd/data-structure-typed.min.js"></script>
|
||||
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>-->
|
||||
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.42.2/dist/umd/data-structure-typed.min.js'></script>-->
|
||||
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.43.3/dist/umd/data-structure-typed.min.js'></script>-->
|
||||
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.44.0/dist/umd/data-structure-typed.min.js'></script>-->
|
||||
|
||||
<script src='https://unpkg.com/js-sdsl@4.4.2/dist/umd/js-sdsl.js'></script>
|
||||
</head>
|
||||
<body>
|
||||
|
@ -19,17 +23,20 @@
|
|||
|
||||
try {
|
||||
const queue = new Queue();
|
||||
for (let i = 0; i < 100000; i++) {
|
||||
const n = 100000;
|
||||
const startEn = performance.now();
|
||||
for (let i = 0; i < n; i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
console.log(`Queue ${n} enqueue `, performance.now() - startEn);
|
||||
let last = 0;
|
||||
const startTime = performance.now();
|
||||
|
||||
for (let i = 0; i < 100000; i++) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
last = queue.dequeue();
|
||||
}
|
||||
|
||||
console.log(performance.now() - startTime);
|
||||
console.log(`Queue ${n} dequeue `, performance.now() - startTime);
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
@ -77,21 +84,22 @@
|
|||
|
||||
try {
|
||||
const { OrderedMap } = sdsl;
|
||||
const { RedBlackTree } = dataStructureTyped;
|
||||
const cTree = new OrderedMap();
|
||||
const tree = new RedBlackTree();
|
||||
const { RedBlackTree, AVLTree} = dataStructureTyped;
|
||||
const cRBTree = new OrderedMap();
|
||||
const rbTree = new RedBlackTree();
|
||||
const tS = performance.now();
|
||||
for (let i = 1; i < 1000000; i++) {
|
||||
tree.add(i, i);
|
||||
const n = 100000;
|
||||
for (let i = 1; i < n; i++) {
|
||||
rbTree.add(i, i);
|
||||
}
|
||||
console.log(performance.now() - tS);
|
||||
console.log(tree.size);
|
||||
console.log(`RedBlackTree ${n} add`, performance.now() - tS);
|
||||
console.log(rbTree.size);
|
||||
const cS = performance.now();
|
||||
for (let i = 1; i < 1000000; i++) {
|
||||
cTree.setElement(i, i);
|
||||
for (let i = 1; i < 100000; i++) {
|
||||
cRBTree.setElement(i, i);
|
||||
}
|
||||
console.log(performance.now() - cS);
|
||||
console.log(cTree.size());
|
||||
console.log(`CRedBlackTree ${n} add`,performance.now() - cS);
|
||||
console.log(cRBTree.size());
|
||||
|
||||
// console.log(tree.isPerfectlyBalanced());
|
||||
// tree.print();
|
||||
|
@ -106,27 +114,27 @@
|
|||
const pq = new PriorityQueue({ comparator: (a, b) => b - a });
|
||||
|
||||
const tS = performance.now();
|
||||
|
||||
for (let i = 0; i < 1000000; i++) {
|
||||
const n = 1000000;
|
||||
for (let i = 0; i < n; i++) {
|
||||
pq.add(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 10000; i++) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
pq.pop();
|
||||
}
|
||||
console.log(performance.now() - tS);
|
||||
console.log(`PriorityQueue ${n} add`,performance.now() - tS);
|
||||
console.log(pq.size);
|
||||
const cS = performance.now();
|
||||
const cpq = new CPriorityQueue();
|
||||
|
||||
for (let i = 0; i < 1000000; i++) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
cpq.push(i);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 10000; i++) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
cpq.pop();
|
||||
}
|
||||
console.log(performance.now() - cS);
|
||||
console.log(`CPriorityQueue ${n} add`,performance.now() - cS);
|
||||
console.log(cpq.size());
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
|
Loading…
Reference in a new issue