节点类:
package Tree;
public class BTNode<E> {
private E data;
private BTNode<E> left, right;
public BTNode(E initialData,BTNode<E> initialLeft,BTNode<E> initialRight){
left=initialLeft;
right=initialRight;
data=initialData;
}
public E getData()
{
return data;
}
public BTNode<E> getLeft()
{
return left;
}
public E getLeftmostData()
{
if(left==null)
return data;
else
return left.getLeftmostData();
}
public BTNode<E> 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<E> removeLeftmost()
{
if(left==null)
{
return right;
}
else
{
left=left.removeLeftmost();
return this;
}
}
public BTNode<E> removeRightmost()
{
if(right==null)
{
return left;
}
else
{
right=right.removeRightmost();
return this;
}
}
public void setData(E newData)
{
data=newData;
}
public void setLeft(BTNode<E> newLeft)
{
left=newLeft;
}
public void setRight(BTNode<E> newRight)
{
right=newRight;
}
public static <E> BTNode<E> treeCopy(BTNode<E> source)
{
BTNode<E> leftCopy,rightCopy;
if(source==null)
return null;
else{
leftCopy=treeCopy(source.left);
rightCopy=treeCopy(source.right);
return new BTNode<E>(source.data, leftCopy, rightCopy);
}
}
public static <E> int treesize(BTNode<E> 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<Integer> BuildTree(int[] treedata,int n){
if(treedata.length==0)
return null;
else{
if(n<treedata.length)
{
int l=n*2+1;
int r=n*2+2;
BTNode<Integer> TreeRoot=new BTNode<Integer>(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<Integer> treeroot;
int[] treedata=new int[data.length];
for(int i=0;i<treedata.length;i++)
{
treedata[i]=Integer.parseInt(data[i]);
System.out.println(treedata[i]);
}
treeroot=BuildTree(treedata,0);
treeroot.print(0);
System.out.println("后序遍历");
treeroot.postorderPrint();
System.out.println("前序遍历");
treeroot.preorderPrint();
System.out.println("中序遍历");
treeroot.inorderPrint();
}
}