2->3->5->4//binaryTree.delNode(5);//删除子叶binaryTree.delNode(3);//删除。11_1 韩顺平 数据结构与算法 树结构基础部分_二叉树( 三 )。" />

11_1 韩顺平 数据结构与算法 树结构基础部分_二叉树( 三 )


代码实现(删除部分)
Main://测试删除System.out.println("删除前,前序遍历");binaryTree.preOrder();//1->2->3->5->4//binaryTree.delNode(5);//删除子叶binaryTree.delNode(3);//删除子树System.out.println("删除后,前序遍历");//binaryTree.preOrder();//1->2->3->4binaryTree.preOrder();//1->2BinaryTree>delNode//删除节点public void delNode(int no){if (root!=null){//需要判断root是否是要删除的节点if (root.getNo()==no){root=null;}else {//递归删除root.delNode(no);}}else {System.out.println("空树,不能删除");}}HeroNode>delNode//递归删除节点//1.如果删除叶子节点,就直接删除//2.如果删除非叶子节点,就删除该子树public void delNode(int no) {//判断该节点的左右是否需要删除if (this.left != null && this.left.no == no) {this.left = null;return;}if (this.right != null && this.right.no == no) {this.right = null;return;}//上两步没完成任务,开始递归寻找if (this.left != null) {this.left.delNode(no);}if (this.right != null) {this.right.delNode(no);}}
代码实现【大汇总】——遍历+查询+删除(简单删除)
package DataStructures.Tree;public class BinaryTreeDemo {public static void main(String[] args) {//先创建一颗二叉树BinaryTree binaryTree = new BinaryTree();//创先需要的节点HeroNode root = new HeroNode(1, "宋江");HeroNode node2 = new HeroNode(2, "吴用");HeroNode node3 = new HeroNode(3, "卢俊义");HeroNode node4 = new HeroNode(4, "林冲");HeroNode node5 = new HeroNode(5, "关胜");//说明:我们先手动创建二叉树,后面使用递归创建二叉树root.setLeft(node2);root.setRight(node3);node3.setRight(node4);node3.setLeft(node5);binaryTree.setRoot(root);/*//测试:System.out.println("前序遍历");//1->2->3->5->4binaryTree.preOrder();//测试:System.out.println("中序遍历");//2->1->5->3->4binaryTree.infixOrder();//测试:System.out.println("后序遍历");//2->5->4->3->1binaryTree.postOrder();*//*//前序遍历查找System.out.println("前序遍历方式~~~~");HeroNode resNode_pre = binaryTree.preOrderSearch(5);if (resNode_pre != null) {System.out.printf("找到了,信息为no=%d name=%s\n", resNode_pre.getNo(), resNode_pre.getName());} else {System.out.printf("没有找到no = %d的英雄\n", 5);}//中序遍历查找——3次System.out.println("中序遍历方式!!!!");HeroNode resNode_infix = binaryTree.infixOrderSearch(5);if (resNode_infix != null) {System.out.printf("找到了,信息为no=%d name=%s\n", resNode_infix.getNo(), resNode_infix.getName());} else {System.out.printf("没有找到no = %d的英雄\n", 5);}//后序遍历查找——2次System.out.println("后序遍历方式····");HeroNode resNode_Post = binaryTree.postOrderSearch(5);if (resNode_Post != null) {System.out.printf("找到了,信息为no=%d name=%s\n", resNode_Post.getNo(), resNode_Post.getName());} else {System.out.printf("没有找到no = %d的英雄\n", 5);}*///测试删除System.out.println("删除前,前序遍历");binaryTree.preOrder();//1->2->3->5->4//binaryTree.delNode(5);//删除子叶binaryTree.delNode(3);//删除子树System.out.println("删除后,前序遍历");//binaryTree.preOrder();//1->2->3->4binaryTree.preOrder();//1->2}}//定义BinaryTree二叉树class BinaryTree {private HeroNode root;public void setRoot(HeroNode root) {this.root = root;}//删除节点public void delNode(int no){if (root!=null){//需要判断root是否是要删除的节点if (root.getNo()==no){root=null;}else {//递归删除root.delNode(no);}}else {System.out.println("空树,不能删除");}}//前序遍历public void preOrder() {if (this.root != null) {this.root.preOrder();} else {System.out.println("二叉树为空,无法遍历");}}//中序遍历public void infixOrder() {if (this.root != null) {this.root.infixOrder();} else {System.out.println("二叉树为空,无法遍历");}}//后序遍历public void postOrder() {if (this.root != null) {this.root.postOrder();} else {System.out.println("二叉树为空,无法遍历");}}//前序查找public HeroNode preOrderSearch(int no) {if (root != null) {return root.preOrderSearch(no);} else {return null;}}//中序查找public HeroNode infixOrderSearch(int no) {if (root != null) {return root.infixOrderSearch(no);} else {return null;}}//后序查找public HeroNode postOrderSearch(int no) {if (root != null) {return root.postOrderSearch(no);} else {return null;}}}//先创建HeroNode结点class HeroNode {private int no;private String name;private HeroNode left;private HeroNode right;public HeroNode(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 HeroNode getLeft() {return left;}public void setLeft(HeroNode left) {this.left = left;}public HeroNode getRight() {return right;}public void setRight(HeroNode right) {this.right = right;}@Overridepublic String toString() {return "HeroNode[no=" + no + ", name=" + name + "]";}//递归删除节点//1.如果删除叶子节点,就直接删除//2.如果删除非叶子节点,就删除该子树public void delNode(int no) {//判断该节点的左右是否需要删除if (this.left != null && this.left.no == no) {this.left = null;return;}if (this.right != null && this.right.no == no) {this.right = null;return;}//上两步没完成任务,开始递归寻找if (this.left != null) {this.left.delNode(no);}if (this.right != null) {this.right.delNode(no);}}// 1. 前序遍历public void preOrder() {//先输出父节点System.out.println(this);//向左子树前序遍历递归if (this.left != null) {this.left.preOrder();}//向右子树前序遍历递归if (this.right != null) {this.right.preOrder();}}// 2. 中序遍历public void infixOrder() {//向左子树中序遍历递归if (this.left != null) {this.left.infixOrder();}System.out.println(this);//向右子树中序遍历递归if (this.right != null) {this.right.infixOrder();}}// 3. 后序遍历public void postOrder() {//向左子树后序遍历递归iif (this.left != null) {this.left.postOrder();}if (this.right != null) {this.right.postOrder();}System.out.println(this);}// 1. 前序遍历查找/*** @param no 查找no* @return 如果找到就返回该Node,如果没有找到就返回null*/public HeroNode preOrderSearch(int no) {System.out.println("进入前序遍历查找~~~");//自己if (this.no == no) {return this;}//向左HeroNode resNode = null;if (this.left != null) {resNode = this.left.preOrderSearch(no);}if (resNode != null) {return resNode;}//向右if (this.right != null) {resNode = this.right.preOrderSearch(no);}return resNode;}// 2. 中序遍历查找public HeroNode infixOrderSearch(int no) {//向左HeroNode resNode = null;if (this.left != null) {resNode = this.left.infixOrderSearch(no);}if (resNode != null) {return resNode;}//自己System.out.println("进入中序查找");if (this.no == no) {return this;}//向右if (this.right != null) {resNode = this.right.infixOrderSearch(no);}return resNode;}// 3. 后序遍历查找public HeroNode postOrderSearch(int no) {HeroNode resNode = null;//向左if (this.left != null) {resNode = this.left.postOrderSearch(no);}if (resNode != null) {return resNode;}//向右if (this.right != null) {resNode = this.right.postOrderSearch(no);}if (resNode != null) {return resNode;}//自己System.out.println("后序遍历");if (this.no == no) {return this;}return resNode;}}