GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — javascript-data-structure/day12-tree-functions.js at master · a90100/javascript-data-structure Expand file tree/ Copy pathday12-tree-functions.jsMore file actions 130 lines (116 loc) · 2.68 KB / Copy pathday12-tree-functions.jsFile metadata and controls130 lines (116 loc) · 2.68 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 class TreeNode { constructor(data, left = null, right = null) { this.data = data this.left = left this.right = right } } class Tree { constructor() { this.root = null } // 使用遞迴去遍歷樹節點 collect(current = this.root, nodes = []) { if (current === null) { return nodes } nodes.push(current.data) this.collect(current.left, nodes) this.collect(current.right, nodes) return nodes } sum(node = this.root) { if (node === null) { return 0 } return node.data + this.sum(node.left) + this.sum(node.right) } contains(value) { return this._contains(this.root, value) } _contains(node, value) { if (node === null) { return false } else if (node.data === value) { return true } return this._contains(node.left, value) || this._contains(node.right, value) } size(node = this.root) { if (node === null) { return 0 } return 1 + this.size(node.left) + this.size(node.right) } leaves(node = this.root) { if (node === null) { return 0 } if (node.left === null && node.right === null) { return 1 } return this.leaves(node.left) + this.leaves(node.right) } min(node = this.root) { if (node === null) { return tree.max() } let leftMin = this.min(node.left) let rightMin = this.min(node.right) if (node.data < leftMin && node.data < rightMin) { return node.data } else if (leftMin < rightMin) { return leftMin } else { return rightMin } } max(node = this.root) { if (node === null) { return 0 } let leftMax = this.max(node.left) let rightMax = this.max(node.right) if (node.data > leftMax && node.data > rightMax) { return node.data } else if (leftMax > rightMax) { return leftMax } else { return rightMax } } height(node = this.root) { if (node === null) { return 0 } let leftHeight = this.height(node.left) let rightHeight = this.height(node.right) if (leftHeight > rightHeight) { return 1 + leftHeight } else { return 1 + rightHeight } } } let n1 = new TreeNode(50); let n2 = new TreeNode(43); let n3 = new TreeNode(67); let n4 = new TreeNode(81); let n5 = new TreeNode(75); let tree = new Tree(); tree.root = n1 n1.left = n2 n1.right = n3 n3.right = n4 n4.left = n5 console.log("collect: " + tree.collect()) console.log("sum: " + tree.sum()) console.log("contains: " + tree.contains(2)) console.log("size: " + tree.size()) console.log("leaves: " + tree.leaves()) console.log("min: " + tree.min()) console.log("max: " + tree.max()) console.log("height: " + tree.height()) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |