|
| 1 | +# 完全二叉树的一些公式 |
| 2 | + |
| 3 | +1.第n层的节点数最多为2<sup>n</sup>个节点 |
| 4 | + |
| 5 | +2.n层二叉树最多有2<sup>0</sup>+...+2<sup>n</sup>=2<sup>n+1</sup>-1个节点 |
| 6 | + |
| 7 | +3.第一个非叶子节点:length/2 |
| 8 | + |
| 9 | +4.一个节点的孩子节点:2n、2n+1 |
| 10 | + |
| 11 | +# 基本结构 |
| 12 | + |
| 13 | + |
| 14 | +插入,遍历,深度 |
| 15 | + |
| 16 | +```js |
| 17 | + function Node(data, left, right) { |
| 18 | + this.data = data; |
| 19 | + this.left = left; |
| 20 | + this.right = right; |
| 21 | + } |
| 22 | + |
| 23 | + Node.prototype = { |
| 24 | + show: function () { |
| 25 | + console.log(this.data); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + function Tree() { |
| 30 | + this.root = null; |
| 31 | + } |
| 32 | + |
| 33 | + Tree.prototype = { |
| 34 | + insert: function (data) { |
| 35 | + var node = new Node(data, null, null); |
| 36 | + if (!this.root) { |
| 37 | + this.root = node; |
| 38 | + return; |
| 39 | + } |
| 40 | + var current = this.root; |
| 41 | + var parent = null; |
| 42 | + while (current) { |
| 43 | + parent = current; |
| 44 | + if (data < parent.data) { |
| 45 | + current = current.left; |
| 46 | + if (!current) { |
| 47 | + parent.left = node; |
| 48 | + return; |
| 49 | + } |
| 50 | + } else { |
| 51 | + current = current.right; |
| 52 | + if (!current) { |
| 53 | + parent.right = node; |
| 54 | + return; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + }, |
| 60 | + preOrder: function (node) { |
| 61 | + if (node) { |
| 62 | + node.show(); |
| 63 | + this.preOrder(node.left); |
| 64 | + this.preOrder(node.right); |
| 65 | + } |
| 66 | + }, |
| 67 | + middleOrder: function (node) { |
| 68 | + if (node) { |
| 69 | + this.middleOrder(node.left); |
| 70 | + node.show(); |
| 71 | + this.middleOrder(node.right); |
| 72 | + } |
| 73 | + }, |
| 74 | + laterOrder: function (node) { |
| 75 | + if (node) { |
| 76 | + this.laterOrder(node.left); |
| 77 | + this.laterOrder(node.right); |
| 78 | + node.show(); |
| 79 | + } |
| 80 | + }, |
| 81 | + getMin: function () { |
| 82 | + var current = this.root; |
| 83 | + while(current){ |
| 84 | + if(!current.left){ |
| 85 | + return current; |
| 86 | + } |
| 87 | + current = current.left; |
| 88 | + } |
| 89 | + }, |
| 90 | + getMax: function () { |
| 91 | + var current = this.root; |
| 92 | + while(current){ |
| 93 | + if(!current.right){ |
| 94 | + return current; |
| 95 | + } |
| 96 | + current = current.right; |
| 97 | + } |
| 98 | + }, |
| 99 | + getDeep: function (node,deep) { |
| 100 | + deep = deep || 0; |
| 101 | + if(node == null){ |
| 102 | + return deep; |
| 103 | + } |
| 104 | + deep++; |
| 105 | + var dleft = this.getDeep(node.left,deep); |
| 106 | + var dright = this.getDeep(node.right,deep); |
| 107 | + return Math.max(dleft,dright); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +``` |
| 112 | + |
| 113 | +```js |
| 114 | + var t = new Tree(); |
| 115 | + t.insert(3); |
| 116 | + t.insert(8); |
| 117 | + t.insert(1); |
| 118 | + t.insert(2); |
| 119 | + t.insert(5); |
| 120 | + t.insert(7); |
| 121 | + t.insert(6); |
| 122 | + t.insert(0); |
| 123 | + console.log(t); |
| 124 | + // t.middleOrder(t.root); |
| 125 | + console.log(t.getMin(), t.getMax()); |
| 126 | + console.log(t.getDeep(t.root, 0)); |
| 127 | + console.log(t.getNode(5,t.root)); |
| 128 | + |
| 129 | + |
| 130 | +``` |
| 131 | + |
| 132 | + |
| 133 | +# 树查找 |
| 134 | + |
| 135 | +```js |
| 136 | + getNode: function (data, node) { |
| 137 | + if (node) { |
| 138 | + if (data === node.data) { |
| 139 | + return node; |
| 140 | + } else if (data < node.data) { |
| 141 | + return this.getNode(data,node.left); |
| 142 | + } else { |
| 143 | + return this.getNode(data,node.right); |
| 144 | + } |
| 145 | + } else { |
| 146 | + return null; |
| 147 | + } |
| 148 | + } |
| 149 | +``` |
| 150 | + |
| 151 | +利用二分的思想 |
| 152 | + |
| 153 | +# 二分查找 |
| 154 | + |
| 155 | +二分查找的条件是必须是有序的线性表。 |
| 156 | + |
| 157 | +和线性表的中点值进行比较,如果小就继续在小的序列中查找,如此递归直到找到相同的值。 |
| 158 | + |
| 159 | + |
| 160 | +```js |
| 161 | + function binarySearch(data, arr, start, end) { |
| 162 | + if (start > end) { |
| 163 | + return -1; |
| 164 | + } |
| 165 | + var mid = Math.floor((end + start) / 2); |
| 166 | + if (data == arr[mid]) { |
| 167 | + return mid; |
| 168 | + } else if (data < arr[mid]) { |
| 169 | + return binarySearch(data, arr, start, mid - 1); |
| 170 | + } else { |
| 171 | + return binarySearch(data, arr, mid + 1, end); |
| 172 | + } |
| 173 | + } |
| 174 | + var arr = [0, 1, 1, 1, 1, 1, 4, 6, 7, 8] |
| 175 | + console.log(binarySearch(1, arr, 0, arr.length-1)); |
| 176 | +``` |
| 177 | + |
0 commit comments