简介
前序遍历、中序遍历、后序遍历、层序遍历 前序查找、中序查找、后序查找 获取二叉树结点数量 获取二叉树的深度 判断是否为满二叉树 获取二叉树最大宽度 判断是否为完全二叉树
代码
public class MyTree {
int data;//结点的值
MyTree leftTree;//左子树
MyTree rightTree;//右子树
public MyTree(int data) {
this.data = data;
leftTree=null;
rightTree=null;
}
//获取左子树
public MyTree getLeftTree() {
return leftTree;
}
//设置左子树
public void setLeftTree(MyTree leftTree) {
this.leftTree = leftTree;
}
//获取右子树
public MyTree getRightTree() {
return rightTree;
}
//设置右子树
public void setRightTree(MyTree rightTree) {
this.rightTree = rightTree;
}
//前序遍历 根-左-右
public void frontShow(){
if(this==null){
return;
}
System.out.println(this.data);
if(this.leftTree!=null){
this.leftTree.frontShow();
}
if(this.rightTree!=null){
this.rightTree.frontShow();
}
}
//中序遍历 左-根-右
public void middleShow(){
if(this==null){
return;
}
if(this.leftTree!=null){
this.leftTree.middleShow();
}
System.out.println(this.data);
if(this.rightTree!=null){
this.rightTree.middleShow();
}
}
//后序遍历 左-右-根
public void afterShow(){
if(this==null){
return;
}
if(this.leftTree!=null){
this.leftTree.afterShow();
}
if(this.rightTree!=null){
this.rightTree.afterShow();
}
System.out.println(this.data);
}
//层序遍历--用队列实现
public void layerShow(){
if(this==null){
return;
}
Queue<MyTree> queue=new LinkedList<>();
queue.add(this);
while (!queue.isEmpty()){
MyTree tree = queue.poll();
System.out.println(tree.data);
if(tree.leftTree!=null){
queue.add(tree.leftTree);
}
if(tree.rightTree!=null){
queue.add(tree.rightTree);
}
}
}
//前序查找
public MyTree frontSearch(int value){
MyTree target=null;
if(this==null){
return null;
}
//查找根
if(this.data==value){
return this;
}else {
//查找左子树
if(this.leftTree!=null){
target= this.leftTree.frontSearch(value);
}
if(target!=null){
return target;
}
//查找右子树
if(this.rightTree!=null){
target= this.rightTree.frontSearch(value);
}
}
return target;
}
//中序查找
public MyTree middleSearch(int value){
MyTree target=null;
if(this==null){
return null;
}
//查找左子树
if(this.leftTree!=null){
target=this.leftTree.middleSearch(value);
}
if(target!=null){
return target;
}
//查找根
if(this.data==value){
return this;
}
//查找右子树
if(this.rightTree!=null){
target=this.rightTree.middleSearch(value);
}
return target;
}
//后序查找
public MyTree afterSearch(int value){
MyTree target=null;
if(this==null){
return null;
}
//查找左子树
if(this.leftTree!=null){
target=this.leftTree.afterSearch(value);
}
if(target!=null){
return target;
}
//查找右子树
if(this.rightTree!=null){
target=this.rightTree.afterSearch(value);
}
//查找根
if(this.data==value){
return this;
}
return target;
}
//获取二叉树结点数量
public int nodeCount(){
int count=1;
if(this==null){
return 0;
}
if(this.leftTree!=null){
count+=this.leftTree.nodeCount();
}
if(this.rightTree!=null){
count+=this.rightTree.nodeCount();
}
return count;
}
//获取二叉树的深度
public int height(){
if(this==null){
return 0;
}
int left=0;
int right=0;
if(this.leftTree!=null){
left=this.leftTree.height();
}
if(this.rightTree!=null){
right=this.rightTree.height();
}
return Math.max(left,right)+1;
}
//判断是否为满二叉树
//2^h-1==n
//h:树的深度,n:树结点个数
public boolean isFull(){
if(this==null){
return true;
}
int h=this.height();
int n=this.nodeCount();
if((Math.pow(2,h)-1)==n){
return true;
}
return false;
}
//获取二叉树的宽度
public int width(){
int max=1;
if(this==null){
return 0;
}
Queue<MyTree> queue=new LinkedList<>();
queue.add(this);
while (!queue.isEmpty()){
MyTree tree = queue.poll();
if(tree.leftTree!=null){
queue.add(tree.leftTree);
}
if(tree.rightTree!=null){
queue.add(tree.rightTree);
}
max=Math.max(max,queue.size());
}
return max;
}
//判断是否为完全二叉树
public boolean isComplete(){
if(this == null){
return false;
}
Queue<MyTree> queue=new LinkedList<MyTree>();
queue.offer(this);
//为true的情况只有当左子树或右子树为空的情况
//当左子树为空时,不能有右子树
//当右子树为空时,下面的结点必须为叶子结点
boolean noChild=false;
while(!queue.isEmpty()){
MyTree cur=queue.poll();
if(cur.leftTree != null){
if(noChild){
return false;
}
queue.offer(cur.leftTree);
}
else{
noChild=true;
}
if(cur.rightTree != null){
if(noChild){
return false;
}
queue.offer(cur.rightTree);
}
else{
noChild=true;
}
}
return true;
}
}