package dataStucture;
/**
* 树的节点类。这里是简单创建一个二叉树
* @author lxt
*
*/
public class BitTree {
int data;
/**
*左树指针
*/
BitTree lchild;
/**
* 右树指针
*/
BitTree rchild;
public BitTree(int data) {
super();
this.data = data;
}
/**
* 先序遍历,访问根,再先序遍历左子树,先序遍历右子树
* @param root
*/
public static void preOrder(BitTree root){
if(root==null) return ;
else {
System.out.printf("%7d",root.data);
preOrder(root.lchild);
preOrder(root.rchild);
}
}
/**
* 二叉树的中序遍历,先中序遍历左子树,再访问根,再中序遍历右子树
* @param root
*/
public static void inOrder(BitTree root){
if(root==null) return ;
else {
inOrder(root.lchild);
System.out.printf("%7d",root.data);
inOrder(root.rchild);
}
}
/**
* 后序遍历。
* 先后序遍历左子树,后序遍历右子树,最后访问根节点。
* @param root
*/
public static void postOrder(BitTree root){
if(root==null) return ;
else {
inOrder(root.lchild);
System.out.printf("%7d",root.data);
inOrder(root.rchild);
}
}
}
package dataStucture;
/**
* 树的节点类。这里是简单创建一个二叉树
* @author lxt
*
*/
public class TestBitTree {
public static void main(String[] args) {
BitTree root=new BitTree(1);
root.lchild=new BitTree(2);
root.rchild=new BitTree(3);
root.lchild.lchild=new BitTree(4);
root.lchild.rchild=new BitTree(5);
root.rchild.lchild=new BitTree(6);
root.rchild.rchild=new BitTree(7);
BitTree.preOrder(root);
System.out.println();
BitTree.inOrder(root);
System.out.println();
BitTree.postOrder(root);
}
}