什么是二叉树?
二叉树树主要分为根节点(root),左子节点(left),右子节点(right)。每个节点有包含自己对应的信息(年龄,姓名,左右节点等等)
二叉树(Binary tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个结点最多只能有两棵子树,且有左右之分 。
二叉树是n个有限元素的集合,该集合或者为空、或者由一个称为根(root)的元素及两个不相交的、被分别称为左子树和右子树的二叉树组成,是有序树。当集合为空时,称该二叉树为空二叉树。在二叉树中,一个元素也称作一个结点 。
二叉树的几种类型:
满二叉树:如果一棵二叉树只有度为0的结点和度为2的结点,并且度为0的结点在同一层上,则这棵二叉树为满二叉树 。
2、完全二叉树:深度为k,有n个结点的二叉树当且仅当其每一个结点都与深度为k的满二叉树中编号从1到n的结点一一对应时,称为完全二叉树 。
完全二叉树的特点是叶子结点只可能出现在层序最大的两层上,并且某个结点的左分支下子孙的最大层序与右分支下子孙的最大层序相等或大1 。
对于二叉树相关的代码实现:
public static void main(String[] args) {
//创建二叉树
BinaryTree binaryTree = new BinaryTree();
HeroNode root1 = new HeroNode(1, "宋江");
HeroNode node1 = new HeroNode(2, "卢俊义");
HeroNode node2 = new HeroNode(3, "吴用");
HeroNode node3 = new HeroNode(4, "林冲");
HeroNode node4 = new HeroNode(5, "关胜");
root1.setLeft(node1);
root1.setRight(node2);
node2.setLeft(node4);
node2.setRight(node3);
binaryTree.setRoot(root1);
System.out.println("前序遍历");
binaryTree.preOrder();
binaryTree.delNode(5);
System.out.println("中序遍历");
binaryTree.infixOrder();
System.out.println("后序遍历");
binaryTree.postOrder();
System.out.println("==================================");
HeroNode resNode = binaryTree.preOrderSearch(3);
if (resNode == null) {
System.out.printf("没有找到no=%d的英雄", 5);
} else {//get方法用来输出,拿到的意思
System.out.printf("找到了,信息为no=%d,name=%s",resNode.getNo(),resNode.getName());
}
}
}
class BinaryTree{
private HeroNode root;
//输出
public void setRoot(HeroNode root) {
this.root = root;
}
//前序遍历
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){
boolean flag=false;
if (this.root!=null){
flag=true;
System.out.println(flag);
return this.root.preOrderSearch(no);
}else {
return null;
}
}
//中序遍历查找
public HeroNode infixOrderSearch(int no){
if (root!=null){
return root.infixOrderSearch(no);
}else {
return null;
}
}
//删除节点
public void delNode(int no){
if (root!=null){
//首先判断root节点是不是要删除的节点
if (root.getNo()==no){
root=null;
}else {
root.delOrder(no);
}
}else {
System.out.println("这是一个空树,不能删除");
}
}
}
//先创建HeroNode节点
class HeroNode{
private int no;
private String name;
private HeroNode left;//默认为null
private HeroNode right;//默认为null
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;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public void setRight(HeroNode right) {
this.right = right;
}
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
//前序遍历方法
public void preOrder(){
System.out.println(this);//先输出父节点
//递归向左边
if (this.left!=null){
this.left.preOrder();
}
if (this.right!=null){
this.right.preOrder();
}
}
//中序遍历
public void infixOrder(){
//递归向左子树中序遍历
if (this.left!=null){
this.left.infixOrder();
}
//输出父节点
System.out.println(this);
//递归向右子树中序遍历
if (this.right!=null){
this.right.infixOrder();
}
}
//后序遍历
public void postOrder(){
if (this.left!=null){
this.left.postOrder();
}
if (this.right!=null){
this.right.postOrder();
}
System.out.println(this);
}
/**
*
* @param no 查找no
* @return 如果找到就返回节点Node,如果没有找到就返回null
*/
//前序遍历查找
public HeroNode preOrderSearch(int no){
//比较当前节点
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;
}
//中序遍历查找方法
public HeroNode infixOrderSearch(int no){
//辅助值
HeroNode resNode=null;
if (this.left!=null){//说明找到左子树
resNode=this.left.infixOrderSearch(no);
}
if (resNode!=null){
return resNode;
}
if (this.no==no){
return this;
}
if (this.right!=null){
resNode=this.right.infixOrderSearch(no);
}
return resNode;
}
//后序遍历查找方法
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;
}
if (this.no==no){
return this;
}
return resNode;
}
//删除节点的方法,如果删除叶子节点,则直接删除.如果删除非叶子节点,则删除这个子树
public void delOrder(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.delOrder(no);
}
//如果左子树还不满足条件,则向右子树递归
if (this.right!=null){
this.right.delOrder(no);
}
}