1. 程式人生 > 實用技巧 >【演算法與資料結構】二叉樹刪除結點

【演算法與資料結構】二叉樹刪除結點

尚矽谷Java資料結構與java演算法課程第99p課後作業

視訊連結:https://www.bilibili.com/video/BV1E4411H73v?p=100

課後題要求:

按以上要求以及當前二叉樹來刪除二叉樹結點,程式碼如下,程式碼片段為70~80行以及172~207行,這裡只是多加了一個判斷。

BinaryTreeDemo.class
  1 package tree;
  2 
  3 public class BinaryTreeDemo {
  4     public static void main(String[] args){
  5         BinaryTree binaryTree = new
BinaryTree(); 6 PlayerNode root = new PlayerNode(1,"james"); 7 PlayerNode node2 = new PlayerNode(2, "wade"); 8 PlayerNode node3 = new PlayerNode(3, "kobe"); 9 PlayerNode node4 = new PlayerNode(4, "davais"); 10 PlayerNode node5 = new PlayerNode(5, "jodan");
11 12 root.setLeft(node2); 13 root.setRight(node3); 14 node3.setRight(node4); 15 node3.setLeft(node5); 16 binaryTree.setRoot(root); 17 //遍歷 18 System.out.println("前序遍歷");//1,2,3,5,4 19 binaryTree.preOrder(); 20 21 System.out.println("中序遍歷");//
2,1,5,3,4 22 binaryTree.infixOrder(); 23 24 System.out.println("後序遍歷");//2,5,4,3,1 25 binaryTree.postOrder(); 26 27 //查詢 28 System.out.println("前序查詢"); 29 PlayerNode resNode = binaryTree.preOrderSearch(5); 30 if (resNode != null){ 31 System.out.printf("找到了,資訊為 no = %d,name = %s\n",resNode.getNo(),resNode.getName()); 32 }else { 33 System.out.printf("沒有找到 no = %d的球員", 5); 34 } 35 36 System.out.println("中序查詢"); 37 PlayerNode resNode1 = binaryTree.infixOrderSearch(5); 38 if (resNode != null){ 39 System.out.printf("找到了,資訊為 no = %d,name = %s\n",resNode1.getNo(),resNode1.getName()); 40 }else { 41 System.out.printf("沒有找到 no = %d的球員", 5); 42 } 43 44 System.out.println("後序查詢"); 45 PlayerNode resNode2 = binaryTree.postOrderSearch(5); 46 if (resNode != null){ 47 System.out.printf("找到了,資訊為 no = %d,name = %s\n",resNode2.getNo(),resNode2.getName()); 48 }else { 49 System.out.printf("沒有找到 no = %d的球員", 5); 50 } 51 52 //刪除結點 53 System.out.println("刪除前,前序遍歷");//1 2 3 5 4 54 binaryTree.preOrder(); 55 binaryTree.delNode(3); 56 System.out.println("刪除後,前序遍歷");//1 2 5 4 57 binaryTree.preOrder(); 58 59 } 60 } 61 62 63 class BinaryTree{ 64 private PlayerNode root; 65 public void setRoot(PlayerNode root){ 66 this.root = root; 67 } 68 69 //刪除結點 70 public void delNode(int no){ 71 if (root != null){ 72 if (root.getNo() == no){ 73 root = null; 74 }else { 75 root.delNode(no); 76 } 77 }else { 78 System.out.println("樹空,不能刪除"); 79 } 80 } 81 82 public void preOrder(){ 83 if (this.root != null){ 84 root.preOrder(); 85 }else { 86 System.out.println("二叉樹為空,無法遍歷"); 87 } 88 } 89 90 public void infixOrder(){ 91 if (this.root != null){ 92 root.infixOrder(); 93 }else { 94 System.out.println("二叉樹為空,無法遍歷"); 95 } 96 } 97 public void postOrder(){ 98 if (this.root != null){ 99 root.postOrder(); 100 }else { 101 System.out.println("二叉樹為空,無法遍歷"); 102 } 103 } 104 105 public PlayerNode preOrderSearch(int no){ 106 if (root != null){ 107 return root.preOrdersearch(no); 108 }else { 109 return null; 110 } 111 } 112 113 public PlayerNode infixOrderSearch(int no){ 114 if (root != null){ 115 return root.infixOrderSearch(no); 116 }else { 117 return null; 118 } 119 } 120 121 public PlayerNode postOrderSearch(int no){ 122 if (root != null){ 123 return root.postOrderSearch(no); 124 }else { 125 return null; 126 } 127 } 128 } 129 130 131 class PlayerNode{ 132 private int no; 133 private String name; 134 private PlayerNode left; 135 private PlayerNode right; 136 public PlayerNode(int no, String name){ 137 this.no = no; 138 this.name = name; 139 } 140 public int getNo(){ 141 return no; 142 } 143 public void setNo(int no){ 144 this.no = no; 145 } 146 public String getName(){ 147 return name; 148 } 149 public void setName(String name){ 150 this.name = name; 151 } 152 public PlayerNode getLeft(){ 153 return left; 154 } 155 public void setLeft(PlayerNode left){ 156 this.left = left; 157 } 158 public PlayerNode getRight(){ 159 return right; 160 } 161 public void setRight(PlayerNode right){ 162 this.right = right; 163 } 164 @Override 165 public String toString(){ 166 return "PlayerNode [no=" + no + ",name=" + name + "]"; 167 } 168 169 //遞迴刪除結點 170 //1.如果刪除的節點是葉子節點,則刪除該節點 171 //2.如果刪除的節點是非葉子節點,則刪除該子樹 172 public void delNode(int no){ 173 if (this.left != null && this.left.no == no){ 174 PlayerNode tempNode = this.left.right; 175 if (this.left.left != null){ 176 this.setLeft(this.left.left); 177 this.left.setRight(tempNode); 178 return; 179 } 180 if (this.left.right != null){ 181 this.setLeft(this.left.right); 182 return; 183 } 184 this.left = null; 185 return; 186 } 187 if (this.right != null && this.right.no == no){ 188 PlayerNode tempNode1 = this.right.right; 189 if (this.right.left != null){ 190 this.setRight(this.right.left); 191 this.right.setRight(tempNode1); 192 return; 193 } 194 if (this.right.right != null){ 195 this.setRight(this.right.right); 196 return; 197 } 198 this.right = null; 199 return; 200 } 201 if (this.left != null){ 202 this.left.delNode(no); 203 } 204 if (this.right != null){ 205 this.right.delNode(no); 206 } 207 } 208 209 //遍歷 210 public void preOrder(){ 211 System.out.println(this);//父結點 212 if(this.left != null){ 213 this.left.preOrder(); 214 } 215 if(this.right != null){ 216 this.right.preOrder(); 217 } 218 } 219 220 public void infixOrder(){ 221 if (this.left != null){ 222 this.left.infixOrder(); 223 } 224 System.out.println(this); 225 if (this.right != null){ 226 this.right.infixOrder(); 227 } 228 } 229 230 public void postOrder(){ 231 if (this.left != null){ 232 this.left.postOrder(); 233 } 234 if (this.right != null){ 235 this.right.postOrder(); 236 } 237 System.out.println(this); 238 } 239 240 //查詢 241 public PlayerNode preOrdersearch(int no){ 242 System.out.println("進入前序查詢"); 243 if (this.no == no){ 244 return this; 245 } 246 PlayerNode resNode = null; 247 if (this.left != null){ 248 resNode = this.left.preOrdersearch(no); 249 } 250 if (resNode != null){ 251 return resNode; 252 } 253 if(this.right != null){ 254 resNode = this.right.preOrdersearch(no); 255 } 256 return resNode; 257 } 258 259 public PlayerNode infixOrderSearch(int no){ 260 PlayerNode resNode = null; 261 if (this.left != null){ 262 resNode = this.left.infixOrderSearch(no); 263 } 264 if (resNode != null){ 265 return resNode; 266 } 267 System.out.println("進入中序查詢"); 268 if (this.no == no){ 269 return this; 270 } 271 if (this.right != null){ 272 resNode = this.right.infixOrderSearch(no); 273 } 274 return resNode; 275 } 276 277 public PlayerNode postOrderSearch(int no){ 278 PlayerNode resNode = null; 279 if (this.left != null){ 280 resNode = this.left.postOrderSearch(no); 281 } 282 if (resNode != null){ 283 return resNode; 284 } 285 if (this.right != null){ 286 resNode = this.right.postOrderSearch(no); 287 } 288 if (resNode != null){ 289 return resNode; 290 } 291 System.out.println("進入後序查詢"); 292 if (this.no == no){ 293 return this; 294 } 295 return resNode; 296 } 297 }

執行結果:

 1 刪除前,前序遍歷
 2 PlayerNode [no=1,name=james]
 3 PlayerNode [no=2,name=wade]
 4 PlayerNode [no=3,name=kobe]
 5 PlayerNode [no=5,name=jodan]
 6 PlayerNode [no=4,name=davais]
 7 刪除後,前序遍歷
 8 PlayerNode [no=1,name=james]
 9 PlayerNode [no=2,name=wade]
10 PlayerNode [no=5,name=jodan]
11 PlayerNode [no=4,name=davais]