From 92552da31c5f451aa8305120d512250a03ef9754 Mon Sep 17 00:00:00 2001 From: Revone Date: Mon, 20 Nov 2023 17:30:55 +0800 Subject: [PATCH] fix: binary tree print bug fixed. test: integration test of rb-tree --- .../binary-tree/binary-tree.ts | 85 ++++++++----------- test/integration/index.html | 54 +++++++----- 2 files changed, 67 insertions(+), 72 deletions(-) diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 44ece16..7a9ed9e 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -1736,63 +1736,50 @@ export class BinaryTree = 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; /** diff --git a/test/integration/index.html b/test/integration/index.html index e9b2d7f..77a0bf0 100644 --- a/test/integration/index.html +++ b/test/integration/index.html @@ -3,8 +3,12 @@ CDN Test - - + + + + + + @@ -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);