java根据层次遍历建立二叉树

节点类:

package Tree;

public class BTNode {
	private E data;
	private BTNode left, right;
	
	public BTNode(E initialData,BTNode initialLeft,BTNode initialRight){
		left=initialLeft;
		right=initialRight;
		data=initialData;
	}
	
	public E getData()
	{
		return data;
	}
	
	public BTNode getLeft()
	{
		return left;
	}
	
	public E getLeftmostData()
	{
		if(left==null)
			return data;
		else 
			return left.getLeftmostData();
	}
	
	public BTNode getRight()
	{
		return right;
	}
	
	public E getRightmostData()
	{
		if(right==null)
			return data;
		else 
			return right.getRightmostData();
	}
	
	//中序序遍历
	public void inorderPrint()
	{
		if(left!=null)
			left.inorderPrint();
		System.out.println(data);
		if(right!=null)
			right.inorderPrint();
	}
	
	//中序 遍历
	public void postorderPrint()
	{
		if(left!=null)
			left.postorderPrint();
		if(right!=null)
			right.postorderPrint();
		System.out.println(data);
	}
	
	//前序遍历
	public void preorderPrint()
	{
		System.out.println(data);
		if(left!=null)
			left.preorderPrint();
		if(right!=null)
			right.preorderPrint();
	}
	
	public boolean isLeaf()
	{
		return(left==null)&&(right==null);
	}
	
	public void print(int depth)
	{
		int i;
		for(i=1;i<=depth;i++)
			System.out.print("    ");
		System.out.println(data);
		
		//打印子树
		if(left!=null)
		{
			left.print(depth+1);
		}
		else if(right!=null)
		{
			for(i=0;i<=depth;i++)
				System.out.print("    ");
			System.out.println("--");
		}
		
		if(right!=null)
		{
			right.print(depth+1);
		}
		else if(left!=null)
		{
			for(i=0;i<=depth;i++)
				System.out.print("    ");
			System.out.println("--");
		}
	}
	
	public BTNode removeLeftmost()
	{
		if(left==null)
		{
			return right;
		}
		else 
		{
			left=left.removeLeftmost();
			return this;
		}
	}
	
	public BTNode removeRightmost()
	{
		if(right==null)
		{
			return left;
		}
		else 
		{
			right=right.removeRightmost();
			return this;
		}
	}
	
	public void setData(E newData)
	{
		data=newData;
	}
	public void setLeft(BTNode newLeft)
	{
		left=newLeft;
	}
	public void setRight(BTNode newRight)
	{
		right=newRight;
	}
	
	public static  BTNode treeCopy(BTNode source)
	{
		BTNode leftCopy,rightCopy;
		if(source==null)
			return null;
		else{
			leftCopy=treeCopy(source.left);
			rightCopy=treeCopy(source.right);
			
			return new BTNode(source.data, leftCopy, rightCopy);
		}
	}
	
	public static  int treesize(BTNode root) {
	
		if(root==null)
			return 0;
		else {
			return 1+treesize(root.left)+treesize(root.right);
		}

	}
}
层次遍历建立二叉树,这里使用的递归的方法,当然也可以建立一个队列来初始化这个二叉树!

package Tree;

import java.io.*;

public class Erchashu {
	
	public static BTNode BuildTree(int[] treedata,int n){
		if(treedata.length==0)
			return null;
		else{
			if(n TreeRoot=new BTNode(treedata[n], BuildTree(treedata, l), BuildTree(treedata, r));
				
				return TreeRoot;
			}	
			else 
				return null;
		}
	}
	
	
	public static void main(String args[]) throws Exception {
		
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String tempdata=br.readLine();
		String[] data=tempdata.split(" ");
		
		
		BTNode treeroot;
		int[] treedata=new int[data.length];
		
		for(int i=0;i




你可能感兴趣的:(java,二叉树,层次遍历,递归建立二叉树)