【算法与数据结构】二叉树删除结点

尚硅谷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]

 

posted @ 2020-08-10 12:40  DJames23  阅读(319)  评论(0编辑  收藏  举报