對於給定的先序和中旬或者中序和後序重建樹
阿新 • • 發佈:2018-11-09
//根據先序遍歷結果和中序遍歷結果確定唯一的二叉樹,根據中序遍歷結果和後序遍歷結果確定唯一的二叉樹 import java.util.*; public class Rebuild_Tree { public static void main(String args[]){ int pre[]=new int[]{18,11,17,10}; int in[]=new int[]{18,11,17,10}; int post[]=new int[]{10,17,11,18}; //根據先序和中序構造樹,並分別打印出先序、中序、後序和層次遍歷的結果 Tree root1=f_pre_in(pre,in); System.out.println("先序:"); f_pre(root1); System.out.println(); System.out.println("中序:"); f_in(root1); System.out.println(); System.out.println("後序:"); f_post(root1); System.out.println(); System.out.println("層次遍歷:"); f_ceng(root1); System.out.println(); //根據中序和後序構造樹,並分別打印出先序、中序、後序和層次遍歷的結果 Tree root2=f_in_post(in,post); System.out.println("先序:"); f_pre(root2); System.out.println(); System.out.println("中序:"); f_in(root2); System.out.println(); System.out.println("後序:"); f_post(root2); System.out.println(); System.out.println("層次遍歷:"); f_ceng(root2); System.out.println(); } //根據先序和中序確定唯一的二叉樹 public static Tree f_pre_in(int pre[],int in[]){ if(pre.length==0 || in.length==0){ return null; } int pos=-1; for(int i=0;i<in.length;i++){ if(pre[0]==in[i]){ pos=i; break; } } if(pos==-1){ return null; } Tree root=new Tree(pre[0]); int new_pre_zuo[]=new int[pos]; for(int i=1;i<=pos;i++){ new_pre_zuo[i-1]=pre[i]; } int new_in_zuo[]=new int[pos]; for(int i=0;i<pos;i++){ new_in_zuo[i]=in[i]; } int new_pre_you[]=new int[in.length-1-pos]; for(int i=pos+1;i<in.length;i++){ new_pre_you[i-pos-1]=pre[i]; } int new_in_you[]=new int[in.length-1-pos]; for(int i=pos+1;i<=in.length-1;i++){ new_in_you[i-pos-1]=in[i]; } root.left=f_pre_in(new_pre_zuo,new_in_zuo); root.right=f_pre_in(new_pre_you,new_in_you); return root; } //根據中序和後序確定唯一的二叉樹 public static Tree f_in_post(int in[],int post[]){ if(in.length==0 || post.length==0) return null; int pos=-1; for(int i=0;i<in.length;i++){ if(in[i]==post[post.length-1]){ pos=i; break; } } if(pos==-1){ return null; } Tree root=new Tree(post[post.length-1]); int new_in_zuo[]=new int[pos]; for(int i=0;i<pos;i++){ new_in_zuo[i]=in[i]; } int new_post_zuo[]=new int[pos]; for(int i=0;i<pos;i++){ new_post_zuo[i]=post[i]; } int new_in_you[]=new int[in.length-1-pos]; for(int i=pos+1;i<in.length;i++){ new_in_you[i-pos-1]=in[i]; } int new_post_you[]=new int[in.length-1-pos]; for(int i=pos;i<in.length-1;i++){ new_post_you[i-pos]=post[i]; } root.left=f_in_post(new_in_zuo,new_post_zuo); root.right=f_in_post(new_in_you,new_post_you); return root; } //先序遍歷 public static void f_pre(Tree root){ if(root!=null){ System.out.print(root.value+" "); f_pre(root.left); f_pre(root.right); } } //中序遍歷 public static void f_in(Tree root){ if(root!=null){ f_in(root.left); System.out.print(root.value+" "); f_in(root.right); } } //後序遍歷 public static void f_post(Tree root){ if(root!=null){ f_post(root.left); f_post(root.right); System.out.print(root.value+" "); } } //層次遍歷 public static void f_ceng(Tree root){ if(root!=null){ Queue<Tree> queue=new LinkedList<Tree>(); queue.offer(root); while(queue.size()>0){ Tree temp1=queue.poll(); System.out.print(temp1.value+" "); if(temp1.left!=null){ queue.offer(temp1.left); } if(temp1.right!=null){ queue.offer(temp1.right); } } } } } class Tree{ Tree left; Tree right; int value; Tree(int value){ this.value=value; } }