- 构建二叉树节点
public class TNode<T> {
public T nodeValue;
public TNode<T> left,right;
public TNode(T item){
nodeValue=item;
left=right=null;
}
public TNode(T item,TNode<T> left,TNode<T> right){
nodeValue=item;
this.left=left;
this.right=right;
}
}
- 构建二叉树
TNode<Integer> p,q;
p=new TNode<Integer>(8);
q=new TNode<Integer>(4,null,p);
- 递规构建二叉树(前序加中序)
public TNode BT(T [] pre,T[] mid){
if(pre==null||pre.length==0)
return null;
TNode root=new TNode(pre[0]);
int Indexmid=0,i;
for( i=0;i<mid.length;i++){
if(mid[i]==root.nodeValue)
break;
}
Indexmid=i;
T[] preLeft= Arrays.copyOfRange(pre,1,1+Indexmid);
T[] prerigh=Arrays.copyOfRange(pre,1+Indexmid,pre.length);
T[] midLeft= Arrays.copyOfRange(mid,0,Indexmid);
T[] midrigh= Arrays.copyOfRange(mid,Indexmid+1,mid.length);
TNode leftChild=BT(preLeft,midLeft);
TNode rightChild=BT(prerigh,midrigh);
root.left=leftChild;
root.right=rightChild;
return root;
}
- 递规构建二叉树(后序加中序)
public TNode BT2(T[] succ,T[] mid){
if(succ==null||succ.length==0)
return null;
TNode root=new TNode(succ[succ.length-1]);
int Indexmid=0,i;
for( i=0;i<mid.length;i++){
if(mid[i]==root.nodeValue)
break;
}
Indexmid=i;
T[] succLeft= Arrays.copyOfRange(succ,0,Indexmid);
T[] succrigh=Arrays.copyOfRange(succ,Indexmid,succ.length-1);
T[] midLeft= Arrays.copyOfRange(mid,0,Indexmid);
T[] midrigh= Arrays.copyOfRange(mid,Indexmid+1,mid.length);
TNode leftChild=BT2(succLeft,midLeft);
TNode rightChild=BT2(succrigh,midrigh);
root.left=leftChild;
root.right=rightChild;
return root;
}
- 树的广度优先遍历
public void breadthFirst(TNode<T> root){
if(root==null)
return ;
TNode<T> p=root;
Queue<TNode<T>> que=new LinkedList<TNode<T>>();
que.add(p);
while(!que.isEmpty()){
p=que.poll();
p.visit();// System.out.println(nodeValue+"");
if(p.left!=null)
que.add(p.left);
if(p.right!=null)
que.add(p.right);
}
}
- 完整 代码:
package learn.java.com.java.datastru;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class TNode<T> {
public T nodeValue;
public TNode<T> left,right;
public TNode(T item){
nodeValue=item;
left=right=null;
}
public TNode(T item,TNode<T> left,TNode<T> right){
nodeValue=item;
this.left=left;
this.right=right;
}
public void visit(){
System.out.print(nodeValue+"");
}
public void breadthFirst(TNode<T> root){
if(root==null)
return ;
TNode<T> p=root;
Queue<TNode<T>> que=new LinkedList<TNode<T>>();
que.add(p);
while(!que.isEmpty()){
p=que.poll();
p.visit();// System.out.println(nodeValue+"");
if(p.left!=null)
que.add(p.left);
if(p.right!=null)
que.add(p.right);
}
}
public TNode BT(T [] pre,T[] mid){
if(pre==null||pre.length==0)
return null;
TNode root=new TNode(pre[0]);
int Indexmid=0,i;
for( i=0;i<mid.length;i++){
if(mid[i]==root.nodeValue)
break;
}
Indexmid=i;
T[] preLeft= Arrays.copyOfRange(pre,1,1+Indexmid);
T[] prerigh=Arrays.copyOfRange(pre,1+Indexmid,pre.length);
T[] midLeft= Arrays.copyOfRange(mid,0,Indexmid);
T[] midrigh= Arrays.copyOfRange(mid,Indexmid+1,mid.length);
TNode leftChild=BT(preLeft,midLeft);
TNode rightChild=BT(prerigh,midrigh);
root.left=leftChild;
root.right=rightChild;
return root;
}
public TNode BT2(T[] succ,T[] mid){
if(succ==null||succ.length==0)
return null;
TNode root=new TNode(succ[succ.length-1]);
int Indexmid=0,i;
for( i=0;i<mid.length;i++){
if(mid[i]==root.nodeValue)
break;
}
Indexmid=i;
T[] succLeft= Arrays.copyOfRange(succ,0,Indexmid);
T[] succrigh=Arrays.copyOfRange(succ,Indexmid,succ.length-1);
T[] midLeft= Arrays.copyOfRange(mid,0,Indexmid);
T[] midrigh= Arrays.copyOfRange(mid,Indexmid+1,mid.length);
TNode leftChild=BT2(succLeft,midLeft);
TNode rightChild=BT2(succrigh,midrigh);
root.left=leftChild;
root.right=rightChild;
return root;
}
public static void main(String[] args){
// TNode<Integer> p,q;
// p=new TNode<Integer>(8);
// q=new TNode<Integer>(4,null,p);
// q.breadthFirst(q);
TNode<Integer>p=new TNode<Integer>(0);
Integer []pre ={1,2,4,8,9,5,3,6,7};
Integer [] mid={8,4,9,2,5,1,6,3,7};
p=p.BT(pre,mid);
p.breadthFirst(p);
System.out.println("----------");
TNode<String>q=new TNode<String >("0");
String [] pre2 ={"a","b","f","g","c","d","e"};
String [] mid2={"f","b","g","a","d","c","e"};
q=q.BT(pre2,mid2);
q.breadthFirst(q);
System.out.println("-------");
String [] succ={"f","g","b","d","e","c","a"};
q=q.BT2(succ,mid2);
q.breadthFirst(q);
}
}
测试结果:
123456789----------
abcfgde-------
abcfgde