BL602&BL604 TCP CLIENT
阿新 • • 發佈:2020-12-12
遍歷二叉樹(前序/中序/後續)
1.前序:父節點-->左子節點-->右子節點
2.中序:左子節點-->父節點-->右子節點
3.後續:左子節點-->右子節點-->父節點
注:關鍵看父節點的順序
遍歷思路:都是從根節點開始,並使用遞迴
前序:1.先返回當前節點
2.判斷左子節點是否為空,不為空則遞迴前序遍歷
3.判斷右子節點是否為空,不為空則遞迴前序遍歷
中序:1.判斷當前節點的左子節點是否為空,不為空則遞迴前序遍歷
2.返回當前節點
3.判斷右子節點是否為空,不為空則遞迴前序遍歷
後序:1.判斷當前節點的左子節點是否為空,不為空則遞迴前序遍歷
3.返回當前節點
圖示
前序遍歷
中序
後序
Java程式碼
//建立二叉樹 class BinaryTree { private Family root;//二叉樹從根節點出發 只需要根節點 public void setRoot(Family root) { this.root = root; } //前序遍歷 public void preOrder() { if (this.root != null) { System.out.println("前序遍歷=>"); this.root.preOrder(); } else { System.out.println("樹為空"); return; } } //中序遍歷 public void midOrder() { if (this.root != null) { System.out.println("中序遍歷=>"); this.root.midOrder(); } else { System.out.println("樹為空"); return; } } //後序遍歷 public void postOrder() { if (this.root != null) { System.out.println("後序遍歷=>"); this.root.postOrder(); } else { System.out.println("樹為空"); return; } } //前序查詢 public void preFind(int no) { Family resFamily = this.root.preFind(no); if (resFamily != null) { System.out.printf("【no=%d,name=%s】", resFamily.getNo(), resFamily.getName()); } else { System.out.println("查無此人"); } } //中序查詢 public void midFind(int no) { Family resFamily = this.root.midFind(no); if (resFamily != null) { System.out.printf("【no=%d,name=%s】", resFamily.getNo(), resFamily.getName()); } else { System.out.println("查無此人"); } } //後序查詢 public void postFind(int no) { Family resFamily = this.root.postFind(no); if (resFamily != null) { System.out.printf("【no=%d,name=%s】", resFamily.getNo(), resFamily.getName()); } else { System.out.println("查無此人"); } } //刪除一個節點,樹形不能進行自刪除,如果整棵樹只有root,並且root即為目標 則整棵樹置空 public void delFamilyByNo(int no) { if (this.root == null) { System.out.println("樹為空"); return; } if (this.root.getNo() == no) { this.root = null; return; } this.root.delFamilyByNo(no); } } class Family { private int no; private String name; private Family left;//左子樹 private Family right;//右子樹 //初始化左右子樹預設為null public Family(int no, String name) { this.no = no; this.name = name; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Family getLeft() { return left; } public void setLeft(Family left) { this.left = left; } public Family getRight() { return right; } public void setRight(Family right) { this.right = right; } //前序遍歷 public void preOrder() { //首先輸出根節點 System.out.println(this); //遞迴遍歷左子樹 if (this.left != null) { this.left.preOrder(); } //遞迴遍歷右子樹 if (this.right != null) { this.right.preOrder(); } } //中序遍歷 public void midOrder() { //先遞迴遍歷左子樹 if (this.left != null) { this.left.midOrder(); } //輸出根節點 System.out.println(this); //遞迴遍歷右子樹 if (this.right != null) { this.right.midOrder(); } } //後續遍歷 public void postOrder() { //遞迴遍歷左子樹 if (this.left != null) { this.left.postOrder(); } //遞迴遍歷右子樹 if (this.right != null) { this.right.postOrder(); } //輸出根節點 System.out.println(this); } //前序查詢 public Family preFind(int no) { if (this.no == no) { return this; } Family resFamily = null; if (this.left != null) { resFamily = this.left.preFind(no); } if (resFamily != null) { return resFamily; } if (this.right != null) { resFamily = this.right.preFind(no); } return resFamily; } //中序查詢 public Family midFind(int no) { Family resFamily = null; if (this.left != null) { resFamily = this.left.preFind(no); } if (resFamily != null) { return resFamily; } if (this.no == no) { return this; } if (this.right != null) { resFamily = this.right.preFind(no); } return resFamily; } //後序查詢 public Family postFind(int no) { Family resFamily = null; if (this.left != null) { resFamily = this.left.preFind(no); } if (resFamily != null) { return resFamily; } if (this.right != null) { resFamily = this.right.preFind(no); } if (this.no == no) { return this; } return resFamily; } //刪除 //樹形不能進行自刪除,只能刪除當前節點的子節點 public void delFamilyByNo(int no) { //判斷左子節點,如果滿足就把左子節點=null,並return結束遞迴 if (this.left != null && this.left.no == no) { this.left = null; return; } //判斷右子節點,如果滿足就把右子節點=null,並return結束遞迴 if (this.right != null && this.right.no == no) { this.right = null; return; } //遞迴左子節點刪除 if (this.left != null) { this.left.delFamilyByNo(no); } //遞迴右子節點刪除 if (this.right != null) { this.right.delFamilyByNo(no); } } @Override public String toString() { return "Family [no=" + this.no + ",name=" + this.name + "]"; } }