diff --git a/codes/typescript/module/PrintUtil.ts b/codes/typescript/module/PrintUtil.ts index c2e3f3c75..9174be159 100644 --- a/codes/typescript/module/PrintUtil.ts +++ b/codes/typescript/module/PrintUtil.ts @@ -1,11 +1,26 @@ /* * File: PrintUtil.ts - * Created Time: 2022-12-10 + * Created Time: 2022-12-13 * Author: Justin (xiefahit@gmail.com) */ import ListNode from './ListNode'; +import { TreeNode } from './TreeNode'; +class Trunk { + prev: Trunk | null; + str: string; + + constructor(prev: Trunk | null, str: string) { + this.prev = prev; + this.str = str; + } +} + +/** + * Print a linked list + * @param head + */ function printLinkedList(head: ListNode | null): void { const list: string[] = []; while (head !== null) { @@ -15,4 +30,67 @@ function printLinkedList(head: ListNode | null): void { console.log(list.join(' -> ')); } -export { printLinkedList }; +/** + * The interface of the tree printer + * This tree printer is borrowed from TECHIE DELIGHT + * https://www.techiedelight.com/c-program-print-binary-tree/ + * @param root + */ +function printTree(root: TreeNode | null) { + printTreeHelper(root, null, false); +} + +/** + * Print a binary tree + * @param root + * @param prev + * @param isLeft + */ +function printTreeHelper(root: TreeNode | null, prev: Trunk | null, isLeft: boolean) { + if (root === null) { + return; + } + + let prev_str = ' '; + const trunk = new Trunk(prev, prev_str); + + printTreeHelper(root.right, trunk, true); + + if (prev === null) { + trunk.str = '———'; + } else if (isLeft) { + trunk.str = '/———'; + prev_str = ' |'; + } else { + trunk.str = '\\———'; + prev.str = prev_str; + } + + showTrunks(trunk); + console.log(' ' + root.val); + + if (prev) { + prev.str = prev_str; + } + trunk.str = ' |'; + + printTreeHelper(root.left, trunk, false); +} + +/** + * Helper function to print branches of the binary tree + * @param p + */ +function showTrunks(p: Trunk | null) { + if (p === null) { + return; + } + + showTrunks(p.prev); + process.stdout.write(p.str); + // ts-node to execute, we need to install type definitions for node + // solve: npm i --save-dev @types/node + // restart the vscode +} + +export { printLinkedList, printTree }; diff --git a/codes/typescript/module/TreeNode.ts b/codes/typescript/module/TreeNode.ts new file mode 100644 index 000000000..0bc783aa9 --- /dev/null +++ b/codes/typescript/module/TreeNode.ts @@ -0,0 +1,51 @@ +/* + * File: TreeNode.ts + * Created Time: 2022-12-13 + * Author: Justin (xiefahit@gmail.com) + */ + +/** + * Definition for a binary tree node. + */ +class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = val === undefined ? 0 : val; // 结点值 + this.left = left === undefined ? null : left; // 左子结点指针 + this.right = right === undefined ? null : right; // 右子结点指针 + } +} + +/** + * Generate a binary tree with an array + * @param arr + * @return + */ +function arrToTree(arr: (number | null)[]): TreeNode | null { + if (arr.length === 0) { + return null; + } + + const root = new TreeNode(arr[0] as number); + const queue = [root]; + let i = 1; + while (queue.length) { + let node = queue.shift() as TreeNode; + if (arr[i] !== null) { + node.left = new TreeNode(arr[i] as number); + queue.push(node.left); + } + i++; + if (arr[i] !== null) { + node.right = new TreeNode(arr[i] as number); + queue.push(node.right); + } + i++; + } + return root; +} + +export { TreeNode, arrToTree };