⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 visitdemo.java

📁 Java是最优秀的高级编程语言之一,二叉树是一种重要的数据结构.
💻 JAVA
字号:
import java.io.*;
public class VisitDemo
{
	public static void main(String[] args)
	{
		String pres=null,ins=null;
		BinaryTree t = new BinaryTree();
		
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		try
		{
			System.out.println("Enter the prefix:");
			pres = in.readLine();
			System.out.println("Enter the infix:");
			ins = in.readLine();
		}catch(IOException e)
		{
			System.out.println("Input Error!");System.exit(0);
		}
		
		t.root = buildBT(pres,ins);
		
		t.preorder();System.out.println();
		t.inorder();System.out.println();
		t.postorder();System.out.println();
		
	}
	public static BinaryNode buildBT(String preLine,String inLine)
	{
		
		int i=0;
		BinaryNode t;
		String pre=null;
		String in=null;
		
		if(preLine.length()==0)
		    return null;
		else
		{
	    t=new BinaryNode();
	    
        char a=preLine.charAt(0);
        t.data = a;
        
        while(inLine.charAt(i)!=a)
           i++;
           
        pre=preLine.substring(1,i+1);
        in=inLine.substring(0,i);
        t.left=buildBT(pre,in);
        
        pre=preLine.substring(i+1,preLine.length());
        in=inLine.substring(i+1,inLine.length());
        t.right=buildBT(pre,in); 
        return t;
        }  
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -