1. 程式人生 > 其它 >二叉樹-前序中序後序查詢

二叉樹-前序中序後序查詢

前序查詢:

  1.先判斷當前節點的no是否等於要查詢的

  2.若相等,則返回當前節點

  3.若不等,則判斷當前節點的左子節點是否為空,若不為空,則遞迴前序查詢

  4.若左遞迴前序查詢,找到節點,則返回,否繼續判斷。當前節點的右子節點是否為空,若不為空,則繼續向右遞迴前序查詢

中序查詢:

  1.判斷當前節點的左子節點是否為空,若不為空,則遞迴中序查詢

  2.若找到,則返回,若沒有找到,就和當前節點比較,若是,則返回當前節點,否則繼續進行右遞迴的中序查詢

  3.若右遞迴中序查詢找到就返回,否則返回null

後序查詢:

  1. 判斷當前節點的左子節點是否為空,若不為空,則繼續遞迴後序查詢

  2. 若找到,就返回,若沒找到,就判斷當前節點的右子節點是否為空,

  若不為空,則右遞迴進行後序查詢,若找到,就返回

  3.沒找到就和當前節點進行比較,若是返回,若不是返回null

  

  1 public class BinaryTreeDemo {
  2 
  3     public static int PRECOUNT = 0;
  4     public static int INFIXCOUNT = 0;
  5     public static int POSTCOUNT = 0;
  6     public static void main(String[] args) {
7 //先需要建立一棵二叉樹 8 BinaryTree binaryTree = new BinaryTree(); 9 EmpNode root = new EmpNode(1, "aa"); 10 EmpNode emp2 = new EmpNode(2, "bb"); 11 EmpNode emp3 = new EmpNode(3, "cc"); 12 EmpNode emp4 = new EmpNode(4, "dd"); 13 EmpNode emp5 = new
EmpNode(5, "ee"); 14 15 //手動建立二叉樹,後面學習遞迴的方式建立二叉樹 16 root.setLeft(emp2); 17 root.setRight(emp3); 18 emp3.setRight(emp4); 19 emp3.setLeft(emp5); 20 21 //測試 22 System.out.println("前序遍歷"); 23 binaryTree.setRoot(root); 24 binaryTree.preOrder(); 25 26 System.out.println("中序遍歷"); 27 binaryTree.setRoot(root); 28 binaryTree.infixOrder(); 29 30 31 System.out.println("後序遍歷"); 32 binaryTree.setRoot(root); 33 binaryTree.postOrder(); 34 35 //前序查詢 36 //前序查詢次數 37 System.out.println("前序查詢方式"); 38 EmpNode resNode = binaryTree.preOrderSearch(5); 39 if (resNode != null){ 40 System.out.printf("找到,資訊為no=%d name=%s 查詢次數為%s",resNode.getNo(),resNode.getName(),PRECOUNT); 41 42 }else { 43 System.out.printf("沒找到 no=%d",5); 44 } 45 46 System.out.println(); 47 48 //中序查詢 49 //中序查詢次數 50 System.out.println("中序查詢方式"); 51 EmpNode resNode1 = binaryTree.infixOrderSearch(5); 52 if (resNode1 != null){ 53 System.out.printf("找到,資訊為no=%d name=%s 查詢次數為%s",resNode1.getNo(),resNode1.getName(),INFIXCOUNT); 54 55 }else { 56 System.out.printf("沒找到 no=%d",5); 57 } 58 59 System.out.println(); 60 61 //後序查詢 62 //後序查詢次數 63 System.out.println("後序查詢方式"); 64 EmpNode resNode2 = binaryTree.postOrderSearch(5); 65 if (resNode1 != null){ 66 System.out.printf("找到,資訊為no=%d name=%s 查詢次數為%s",resNode2.getNo(),resNode2.getName(),POSTCOUNT); 67 68 }else { 69 System.out.printf("沒找到 no=%d",5); 70 } 71 72 73 74 75 } 76 77 } 78 79 //定義一個BinaryTree 80 class BinaryTree { 81 private EmpNode root; 82 83 public void setRoot(EmpNode root) { 84 this.root = root; 85 } 86 87 //前序遍歷 88 public void preOrder() { 89 if (this.root != null) { 90 this.root.preOrder(); 91 } else { 92 System.out.println("二叉樹為空,無法遍歷"); 93 } 94 } 95 96 //中序遍歷 97 public void infixOrder() { 98 if (this.root != null) { 99 this.root.infixOrder(); 100 } else { 101 System.out.println("二叉樹為空,無法遍歷"); 102 } 103 } 104 105 //後序遍歷 106 public void postOrder() { 107 if (this.root != null) { 108 this.root.postOrder(); 109 } else { 110 System.out.println("二叉樹為空,無法遍歷"); 111 } 112 } 113 114 //前序查詢 115 public EmpNode preOrderSearch(int no){ 116 if (root != null){ 117 return root.preOrderSearch(no); 118 }else { 119 return null; 120 } 121 } 122 123 //中序查詢 124 public EmpNode infixOrderSearch(int no){ 125 if (root != null){ 126 return root.infixOrderSearch(no); 127 }else { 128 return null; 129 } 130 } 131 //後序查詢 132 public EmpNode postOrderSearch(int no){ 133 if (root != null){ 134 return root.postOrderSearch(no); 135 }else { 136 return null; 137 } 138 } 139 } 140 141 //先建立HeroNode 142 class EmpNode { 143 private int no; 144 private String name; 145 private EmpNode left;//預設為null 146 private EmpNode right;//預設為null 147 148 public EmpNode(int no, String name) { 149 this.no = no; 150 this.name = name; 151 } 152 153 public int getNo() { 154 return no; 155 } 156 157 public void setNo(int no) { 158 this.no = no; 159 } 160 161 public String getName() { 162 return name; 163 } 164 165 public void setName(String name) { 166 this.name = name; 167 } 168 169 public EmpNode getLeft() { 170 return left; 171 } 172 173 public void setLeft(EmpNode left) { 174 this.left = left; 175 } 176 177 public EmpNode getRight() { 178 return right; 179 } 180 181 public void setRight(EmpNode right) { 182 this.right = right; 183 } 184 185 @Override 186 public String toString() { 187 return "EmpNode{" + 188 "no=" + no + 189 ", name='" + name + '\'' + 190 '}'; 191 } 192 //編寫前序遍歷方法 193 194 public void preOrder() { 195 System.out.println(this);//先輸出父節點 196 //遞歸向左子樹前序遍歷 197 if (this.left != null) { 198 this.left.preOrder(); 199 } 200 //遞歸向右子樹前序遍歷 201 if (this.right != null) { 202 this.right.preOrder(); 203 } 204 205 } 206 207 //編寫中序遍歷方法 208 public void infixOrder() { 209 //遞歸向左子樹中序遍歷 210 if (this.left != null) { 211 this.left.infixOrder();//2,1,3,4 212 } 213 //輸出父節點 214 System.out.println(this); 215 //遞歸向右子樹中序遍歷 216 if (this.right != null) { 217 this.right.infixOrder(); 218 } 219 } 220 221 //編寫後序遍歷方法 222 public void postOrder() { 223 if (this.left != null) { 224 this.left.postOrder(); 225 } 226 if (this.right != null) { 227 this.right.postOrder(); 228 } 229 System.out.println(this); 230 } 231 232 /** 233 * @param no 查詢no 234 * @return 若找到就返回該Node,若沒找到返回Null 235 */ 236 //前序遍歷查詢 237 public EmpNode preOrderSearch(int no) { 238 BinaryTreeDemo.PRECOUNT++; 239 //System.out.println("進入前序查詢"); 240 241 //比較當前節點是不是 242 if (this.no == no) { 243 return this; 244 } 245 //判斷當前節點的左子節點是否為空,若不為空,則遞迴前序查詢 246 //若左遞迴前序查詢,找到節點,則返回 247 248 EmpNode resNode = null; 249 if (this.left != null) { 250 resNode = this.left.preOrderSearch(no); 251 } 252 if (resNode != null) { 253 return resNode; 254 } 255 256 //左遞迴前序查詢,找到節點,則返回,否繼續判斷 257 //當前的結點的右子節點是否為空,若為空,則繼續向右遞迴前序查詢 258 if (this.right != null) { 259 resNode = this.right.preOrderSearch(no); 260 } 261 return resNode; 262 263 } 264 265 //中序遍歷查詢 266 public EmpNode infixOrderSearch(int no) { 267 268 //判斷當前節點的左子節點是否為空,若不為空,則遞迴中序查詢 269 EmpNode resNode = null; 270 if (this.left != null) { 271 resNode = this.left.infixOrderSearch(no); 272 } 273 if (resNode != null) { 274 return resNode; 275 } 276 BinaryTreeDemo.INFIXCOUNT++; 277 //若找到,則返回,若沒找到,就和當前節點比較,若是則返回 278 if (this.no == no) { 279 return this; 280 } 281 //否則繼續進行右遞迴的中序查詢 282 if (this.right != null) { 283 resNode = this.right.infixOrderSearch(no); 284 } 285 return resNode; 286 287 } 288 289 //後序遍歷查詢 290 public EmpNode postOrderSearch(int no) { 291 292 293 //判斷當前節點的左子節點是否為空,若不為空,則繼續遞迴後序查詢 294 EmpNode resNode = null; 295 if (this.left != null) { 296 resNode = this.left.postOrderSearch(no); 297 } 298 if (resNode != null) {//說明左子樹找到 299 return resNode; 300 } 301 //若左子樹沒找到,則向右子樹遞迴進行後序遍歷查詢 302 if (this.right != null) { 303 resNode = this.right.postOrderSearch(no); 304 } 305 if (resNode != null) { 306 return resNode; 307 } 308 //若左右子樹都沒找到,就比較當前節點是不是 309 BinaryTreeDemo.POSTCOUNT++; 310 if (this.no == no) { 311 return this; 312 } 313 return resNode; 314 } 315 316 }