3)二叉树
1. 二叉树的概念
树有很多种,每个节点最多只有两个子节点的形式就是二叉树
二叉树的子节点分为左节点和右节点
如果该二叉树的所有叶子节点都在最后一层并且结点总数=2^n-1(n为层数)我们称为满二叉树
如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称之为完全二叉树
PS:连续是横向连续不是纵向连续(比如81->91左连续;71->61->15右连续)
2. 二叉树遍历说明
前序遍历:先输出父节点,再遍历左子树和右子树
中序遍历:先遍历左子树,再输出父节点,再遍历右子树
后续遍历:先遍历左子树,再遍历右子树,最后输出父节点
小结:看输出父节点的顺序,就确定是前序,中序还是后序
思路分析:
创建一颗二叉树前序遍历 先输出当前节点(初始的时候是root节点);如果左子节点不为空,则递归前序遍历;如果右子节点不为空,则递归前序遍历 中序遍历 如果当前节点的左子节点不为空,则递归中序遍历;输出当前节点如果当前节点的右子节点不为空,则递归中序遍历 后序遍历 如果当前节点的左子节点不为空,则递归后序遍历如果当前节点的右子节点不为空,则递归后序遍历输出当前节点
代码实现(遍历部分)
MainBinaryTree 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);BinaryTree>Search//前序遍历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("二叉树为空,无法遍历");}}HeroNode>Search// 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);}
3. 二叉树的检索(查询)
要求:
请编写前中后序查找方法分别用三种方法查找hero.no==5的节点分析三种查找方法,进行比较
思路:
前序查找
先判断当前节点的no是否等于要查找的相等:则返回当前节点不等,则判断左子节点是否为空,若不空则递归前序查找,一旦找到就返回;左边前序递归完未找到,再判断右节点是否为空,不为空就前序递归右边,一旦找到就返回;
中序查找
判断当前节点的左子节点是否为空,不为空则前序递归查找如果找到就返回,否则就和当前节点比较,如果是就返回当前节点,否则继续进行右递归的中序查找如果右递归中序查找找到就返回,否则返回null
后序查找
先判断当前节点的左子节点是否为空,不为空则递归后序遍历如果找到,就返回,如果没有找到,就判断当前节点的右子节点是否为空,如果不为空,则右递归进行后序查找,没会找到就返回最后和当前接待你进行比较,如果是就返回,不是就返回null
代码实现(检索部分)
Main//前序遍历查找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);}BinaryTree>preOrderSearch//前序查找public HeroNode preOrderSearch(int no) {if (root != null) {return root.preOrderSearch(no);} else {return null;}}BinaryTree>infixOrderSearch//中序查找public HeroNode infixOrderSearch(int no) {if (root != null) {return root.infixOrderSearch(no);} else {return null;}}BinaryTree>postOrderSearch//后序查找public HeroNode postOrderSearch(int no) {if (root != null) {return root.postOrderSearch(no);} else {return null;}}HeroNode>preOrderSearch// 1. 前序遍历查找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;}HeroNode>infixOrderSearch// 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;}HeroNode>postOrderSearch// 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;}
4. 二叉树的删除
要求:
如果删除的节点是叶子节点,则删除该节点如果删除的节点是非叶子节点,则删除改子树(后序再说树叶上提的问题)测试,删除掉5号叶子节点和3号子树
思路:
如果只有一个root节点,就等价将二叉树置空因为二叉树是单向的,所以我们是判断当前节点的子节点是否需要删除;而不能判断当前节点是否需要删除如果当前节点的左子节点不为空,并且左子节点就是要删除的节点,就将this.left = null;并且就返回(结束递归删除)如果当前节点的右子节点不为空,并且右子节点就是要删除的节点,就将this.right = null;并且就返回(结束递归删除)如果第2步和第3步都没有删除节点,那么我们就需要向左子树进行递归删除;如果第4步也没有删除节点,那么我们就需要向右子树进行递归删除;
代码实现(删除部分)
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;}}