java代码如下,其中先序中序后序遍历分别用递归的方式和迭代的方式实现:
public class Tree {
private Tree left;
private Tree right;
private Tree root;
private int data;
private int weight;
public Tree()
{
}
public Tree(Tree left,Tree right,int data)
{
this.left=left;
this.right=right;
this.data=data;
}
public Tree(Tree left,Tree right,Tree root,int data)
{
this(left,right,data);
this.root=root;
}
public Tree(Tree left,Tree right,int data,int weight)
{
this(left,right,data);
this.root=null;
this.weight=weight;
}
public Tree(Tree left,Tree right,Tree root,int data,int weight)
{
this(left,right,data);
this.root=root;
this.weight=weight;
}
public Tree(int data)
{
this(null,null,data);
}
public Tree(int data,int weight)
{
this(null,null,data);
this.weight=weight;
}
public ArrayList<Tree> setTreeArray(int[] a)
{
ArrayList<Tree> t=new ArrayList<Tree>();
for(int i:a) t.add(new Tree(i));
return t;
}
public void creByArray(int[] a)
{
List<Tree> t=new ArrayList<Tree>();
for(int i:a) t.add(new Tree(i));
for(int i=0;i<a.length/2;i++)
{
t.get(i).left=t.get(i*2+1);
t.get(i*2+1).root=t.get(i);
if(i*2+2<a.length)
{
t.get(i).right=t.get(i*2+2);
t.get(i*2+2).root=t.get(i);
}
}
this.left=t.get(0).left;
this.right=t.get(0).right;
this.data=t.get(0).data;
}
public void creByArray(int[] a,int[] b)
{
this.creByArray(a);
this.addWeight(b);
}
public boolean isEmpty()
{
return this.data==0 && this.left==null && this.right==null && this.root==null;
}
public Tree getLeft() {
return left;
}
public void setLeft(Tree left) {
this.left = left;
}
public Tree getRight() {
return right;
}
public void setRight(Tree right) {
this.right = right;
}
public Tree getRoot() {
return root;
}
public void setRoot(Tree root) {
this.root = root;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void setByInt(int i,int data)
{
switch(i)
{
case 0:setRoot(new Tree(data));break;
case 1:setLeft(new Tree(data));break;
default:setRight(new Tree(data));break;
}
}
public int getHeight(Tree t)
{
if(t.left==null && t.right==null) return 1;
else if(t.left==null) return getHeight(t.right)+1;
else if(t.right==null) return getHeight(t.left)+1;
if(getHeight(t.left)>=getHeight(t.right))
{
return getHeight(t.left)+1;
}
else return getHeight(t.right)+1;
}
public ArrayList<Tree> getPreorder(Tree t)
{
ArrayList<Tree> T=new ArrayList<Tree>();
T.add(t);
if(t.left!=null) T.addAll(getPreorder(t.left));
if(t.right!=null) T.addAll(getPreorder(t.right));
return T;
}
public ArrayList<Tree> getPostorder(Tree t)
{
ArrayList<Tree> T=new ArrayList<Tree>();
if(t.left!=null) T.addAll(getPostorder(t.left));
if(t.right!=null) T.addAll(getPostorder(t.right));
T.add(t);
return T;
}
public ArrayList<Tree> getInorder(Tree t)
{
ArrayList<Tree> T=new ArrayList<Tree>();
if(t.left!=null) T.addAll(getInorder(t.left));
T.add(t);
if(t.right!=null) T.addAll(getInorder(t.right));
return T;
}
public ArrayList<Tree> getLevelorder(Tree t)
{
ArrayList<Tree> T=new ArrayList<Tree>();
for(int i=0;i<t.getHeight(t);i++)
{
T.addAll(t.getLevel(t, i+1));
}
return T;
}
public ArrayList<Tree> getLevel(Tree t,int k)
{
ArrayList<Tree> T=new ArrayList<Tree>();
if(k==1) T.add(t);
if(t!=null && t.left!=null) T.addAll(getLevel(t.left,k-1));
if(t!=null && t.right!=null) T.addAll(getLevel(t.right,k-1));
return T;
}
public ArrayList<Tree> getPreorder2(Tree t)
{
int top=-1;
ArrayList<Tree> T=new ArrayList<Tree>();
Tree k[]=new Tree[100];
while(t!=null || top!=-1)
{
while(t!=null)
{
T.add(t);
k[++top]=t;
t=t.left;
}
if(top!=-1)
{
t=k[top--];
t=t.right;
}
}
return T;
}
public ArrayList<Tree> getPostorder2(Tree t)
{
int top=-1;
ArrayList<Tree> T=new ArrayList<Tree>();
Tree k[]=new Tree[100];
int flag[]=new int[100];
while(t!=null || top!=-1)
{
while(t!=null)
{
k[++top]=t;
flag[top]=1;
t=t.left;
}
while(top!=-1 && flag[top]==2)
{
T.add(k[top]);
k[top]=null;
flag[top]=0;
top--;
}
if(top!=-1)
{
flag[top]=2;
if(k[top]!=null)
t=k[top].right;
}
}
return T;
}
public ArrayList<Tree> getInorder2(Tree t)
{
int top=-1;
ArrayList<Tree> T=new ArrayList<Tree>();
Tree k[]=new Tree[100];
while(t!=null || top!=-1)
{
while(t!=null)
{
k[++top]=t;
t=t.left;
}
if(top!=-1)
{
t=k[top--];
T.add(t);
t=t.right;
}
}
return T;
}
public int getWide()
{
int wide=0;
for(int i=0;i<getHeight(this);i++)
{
if(this.getLevel(this,i+1).size()>=wide)
wide=this.getLevel(this,i+1).size();
}
return wide;
}
public boolean isComplete()
{
int i=0;
for(i=0;i<getHeight(this);i++)
{
if(this.getLevel(this,i+1).size()!=(int)Math.pow(2,i))
break;
}
if(i==getHeight(this)) return true;
else
{
ArrayList<Tree> T1=new ArrayList<Tree>();
ArrayList<Tree> T2=new ArrayList<Tree>();
T1=getLevel(this,i);
T2=getLevel(this,i+1);
for(int j=0;j<T2.size();j++)
{
if(T2.get(j).root!=T1.get(j/2)) return false;
}
}
return true;
}
public void addWeight(int weight[])
{
ArrayList<Tree> T=this.getLevelorder(this);
for(int i=0;i<weight.length;i++)
{
T.get(i).setWeight(weight[i]);
}
}
public ArrayList<Tree> arrangeByWeight(ArrayList<Tree> T)
{
Tree t=null;
for(int i=0;i<T.size()-1;i++)
{
for(int j=0;j<T.size()-1-i;j++)
{
if(T.get(j).weight>T.get(j+1).weight)
{
t=T.get(j);
T.set(j, T.get(j+1));
T.set(j+1, t);
}
}
}
return T;
}
}