您的位置:首页 > 编程语言 > Java开发

java中二叉树的创建以及几个常用的函数

2015-07-31 17:34 381 查看
二叉树是我们在编程过程中常用的数据模型,但是java本身并没有提供简单的二叉树类以供调用,今天我们就自己写了一个BinaryTreeFactory类,用来创建一棵二叉树,并且提供先序遍历,中序遍历,后序遍历,层次遍历这几种不同的遍历方式。

废话不多说,上代码

首先是二叉树的节点类,你可以将它写成内部类的形式

/**
*
* @author yyd
*二叉树的节点类
*/
public class BTNode {
private BTNode left;
private BTNode right;
private int data;
public BTNode(int data){
this.left = null;
this.right = null;
this.data = data;
}
public BTNode getLeft() {
return left;
}
public void setLeft(BTNode left) {
this.left = left;
}
public BTNode getRight() {
return right;
}
public void setRight(BTNode right) {
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}

}


接下来是BinaryTreeFactory类,即是一个工厂类,提供多种创建方法以及遍历方法

/**
*
* @author pc
*工厂类
*用来产生二叉树,返回的是所生成二叉树的根节点
*并且提供先序,中序,后序,层次遍历四种遍历方式
*提供查找某节点以及该节点高度的方法
*/
public class BinaryTreeFactory {

/**
* 根据一个数组来创建二叉树
* @param a[0..n] n+1个数据组成的数组,其中a[0]=-1不参与树的创建
* a[1]~a
代表数的节点,a[2*i]是a[i]的左子结点,a[2*i+1]是右子节点
* 若a[t]=-1则代表该节点为空节点
*/
public static BTNode buildTreeByArray(int[] a,int n){
BTNode[] p=new BTNode[n+1];
p[0]=null;
for(int i=1;i<=n;i++){
if(a[i]==-1){
p[i]=null;
}
else{
p[i]=new BTNode(a[i]);
}
}
for(int i=1;i<=n/2;i++){
if(a[i]!=-1){
if((2*i<=n)&&a[2*i]!=-1){
p[i].setLeft(p[2*i]);
}
if((2*i+1<=n)&&(a[2*i+1]!=-1)){
p[i].setRight(p[2*i+1]);
}
}
}
return p[1];
}

/**
* 根据一个先序遍历数组和一个中序遍历数组建立一棵二叉树
* 数组从1开始,pre[0]=-1与in[0]=-1不参与树的构建
*/
public static BTNode buildByPreInOrder(int[] pre,int[] in,int prelow,int prehigh,int inlow,int inhigh){
if((prehigh-prelow>=0)&&(inhigh-inlow>=0)){
BTNode n=new BTNode(pre[prelow]);
int k=inlow;
for(int i=inlow;i<=inhigh;i++){
if(pre[prelow]==in[i]){
k=i;
break;
}
}
n.setLeft(buildByPreInOrder(pre,in,prelow+1,prelow+k-1,inlow,k-1));
n.setRight(buildByPreInOrder(pre, in, prelow+k, prehigh, k+1, inhigh));
return n;
}
else
return null;
}

/**
* 根据一个中序遍历数组和一个后序遍历数组建立一棵二叉树
* 数组从1开始,post[0]=-1与in[0]=-1不参与树的构建
*/
public static BTNode buildByInPostOrder(int[] in,int[] post,int inlow,int inhigh,int postlow,int posthigh){
if((posthigh-postlow>=0)&&(inhigh-inlow>=0)){
BTNode n=new BTNode(post[posthigh]);
int k=inlow;
for(int i=inlow;i<=inhigh;i++){
if(post[posthigh]==in[i]){
k=i;
break;
}
}
int leftLen=k-inlow;
int rightLen=inhigh-k;
n.setLeft(buildByInPostOrder(in,post,inlow,inlow+leftLen-1,postlow,postlow+leftLen-1));
n.setRight(buildByInPostOrder(in,post, k+1, inhigh, postlow+leftLen, posthigh-1));
return n;
}
else
return null;
}

/**
* 前序遍历
* @param node
*/
public static void preOrder(BTNode node){
if(node != null){
System.out.print(node.getData()+" ");
preOrder(node.getLeft());
preOrder(node.getRight());
}
}

/**
* 中序遍历
* @param node
*/
public static void inOrder(BTNode node){
if(node != null){
inOrder(node.getLeft());
System.out.print(node.getData()+" ");
inOrder(node.getRight());
}
}

/**
* 后序遍历
* @param node
*/
public static void postOrder(BTNode node){
if(node != null){
postOrder(node.getLeft());
postOrder(node.getRight());
System.out.print(node.getData()+" ");
}
}

/**
* 层次遍历
* @param node
*/
public static void levelOrder(BTNode node){
BTNode[] queue=new BTNode[255];
int tail=0;
int head=0;//队头指针与队尾指针
BTNode p=node;
if(p!=null){
queue[tail++]=node;//将节点加入队列中
while(head!=tail){//即队不空
BTNode temp=queue[head++];
System.out.print(temp.getData()+" ");
if(temp.getLeft()!=null){
queue[tail++]=temp.getLeft();
}
if(temp.getRight()!=null){
queue[tail++]=temp.getRight();
}
}
}
}

/**
* 从以node为根的树中找出值为x的节点,若找到就返回该节点,否则返回null
* @param x
* @param node
*/
public static BTNode find(int x,BTNode node){
if(node!=null){
if(node.getData()==x){
return node;
}
else{
BTNode t=find(x,node.getLeft());
if(t!=null)
return t;
else
return find(x,node.getRight());
}
}
else{
return null;
}
}

/**
* 给出一个节点判断它在树中的高度
* @param node
* @return
*/
public static int getDepth(BTNode node){
if(node==null)
return 0;
else{
int lh=getDepth(node.getLeft());
int rh=getDepth(node.getRight());
return lh>=rh?lh+1:rh+1;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: