二叉树的概述:
1)二叉树:任何一个节点的子节点数量不超过2
二叉树的子节点分为左节点和右节点。
2)满二叉树:所有叶子节点都在最后一层,且节点的总数是:2^n-1,n为树的高度。
3)完全二叉树:所有的叶子节点都在最后一层或者倒数第二层,且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续。
4)二叉树的存储结构:链式存储和顺序存储
链式存储代码分享:
package erchashu;
//链式存储的二叉树的节点
public class TreeNode {
//节点的权
int value;
TreeNode leftNode;
TreeNode rightNode;
public TreeNode(int value) {
this.value = value;
}
public void setLeftNode(TreeNode leftNode) {
this.leftNode = leftNode;
}
public void setRightNode(TreeNode rightNode) {
this.rightNode = rightNode;
}
//前序遍历
public void frontShow() {
//当前节点的内容
System.out.println(value);
//左节点
if (leftNode != null) {
leftNode.frontShow();
}
//右节点
if (rightNode != null) {
rightNode.frontShow();
}
}
//中序遍历
public void middleShow(){
if(leftNode!=null){
leftNode.middleShow();
}
//当前节点的value
System.out.println(value);
if(rightNode!=null){
rightNode.middleShow();
}
}
//后序遍历
public void afterShow(){
if(leftNode!=null){
leftNode.afterShow();
}
if(rightNode!=null){
rightNode.afterShow();
}
//当前节点的value
System.out.println(value);
}
//前序查找
public TreeNode frontSearch(int i){
TreeNode target=null;
if(this.value==i){
//对比当前节点的值
System.out.println("刚好找到这个Value:Node "+this.value);
return this;
}else{
//查找左儿子
if(leftNode!=null){
System.out.println("查看左子树一次");
//有可能可以查到,也可以查不到,查不到的话,target还是一个null;
target=leftNode.frontSearch(i);
}
//如果不为空,说明在左儿子中已经找到
if(target!=null){
System.out.println("查找左子树有结果");
System.out.println("这时的Node "+this.value);
return target;
}
//查找右儿子
if(rightNode!=null){
System.out.println("查看右子树一次");
target= rightNode.frontSearch(i);
}
}
return target;//如果二叉树中没有找到这个节点,target=null.
}
public void delete(int i){
TreeNode parent=this;
//判断左子树
if(parent.leftNode!=null && parent.leftNode.value==i){
parent.leftNode=null;
return;
}
//判断右子树
if(parent.rightNode!=null && parent.rightNode.value==i){
parent.rightNode=null;
return;
}
//递归检查并删除左儿子
parent=leftNode;
if(parent!=null){
parent.delete(i);
}
//递归检查并删除右儿子
parent=rightNode;
if(parent!=null){
parent.delete(i);
}
}
}
package erchashu;
//树
public class binaryTree {
//根节点
TreeNode root;
//设置根节点
public void setRoot(TreeNode root){
this.root=root;
}
//获取根节点
public TreeNode getRoot(){
return this.root;
}
//前序遍历
//binaryTree是外面的框框,真正要干事也不干,实际上干事的是root节点里面的方法。
public void frontShow(){
if(root!=null){
root.frontShow();
}
}
//中序遍历
public void middleShow(){
if(root!=null){
root.middleShow();
}
}
//后序遍历
public void afterShow(){
if(root!=null){
root.afterShow();
}
}
//前序查找
public TreeNode frontSearch(int i){
return root.frontSearch(i);
}
//删除子树
public void delete(int i){
if(root.value==i){
root=null;
} else{
root.delete(i);
}
}
}
package erchashu;
//二叉树的创建
public class TreeDemo1 {
public static void main(String[] args){
//创建一棵树
binaryTree btree=new binaryTree();
//创建一个根节点
TreeNode root=new TreeNode(1);
//把根节点放到树里面
btree.setRoot(root);
//创建根节点的左儿子和右儿子
TreeNode rootLeftNode=new TreeNode(2);
TreeNode rootRightNode=new TreeNode(3);
//设置根节点的左儿子和右儿子
root.setLeftNode(rootLeftNode);
root.setRightNode(rootRightNode);
//为第二层的左节点创建两个子节点
rootLeftNode.setLeftNode(new TreeNode(4));
rootLeftNode.setRightNode(new TreeNode(5));
//为第二层的右节点创建两个子节点
rootRightNode.setLeftNode(new TreeNode(6));
rootRightNode.setRightNode(new TreeNode(7));
//前序遍历
System.out.println("前序遍历:");
btree.frontShow();//
//中序遍历
System.out.println("中序遍历:");
btree.middleShow();
//后序遍历
System.out.println("后续遍历:");
btree.afterShow();
System.out.println("查找二叉树的节点:包括前序查找、中序查找、后序查找");
TreeNode temp=btree.frontSearch(8);
System.out.println(temp);
System.out.println("删除二叉树的子树");
//要删除一个节点:分为两种情况:
// 1)如果是根节点,就是让根节点等于null,
//2)如果不是根节点,就是让这个节点的父节点的右儿子或者左儿子等于null
btree.delete(1);
btree.frontShow();
}
}