importjava.util.Stack;importjava.util.HashMap;public classBinTree {private chardate;privateBinTree lchild;privateBinTree rchild;public BinTree(charc) {
date=c;
}//先序遍历递归
public static voidpreOrder(BinTree t) {if (t == null) {return;
}
System.out.print(t.date);
preOrder(t.lchild);
preOrder(t.rchild);
}//中序遍历递归
public static voidInOrder(BinTree t) {if (t == null) {return;
}
InOrder(t.lchild);
System.out.print(t.date);
InOrder(t.rchild);
}//后序遍历递归
public static voidPostOrder(BinTree t) {if (t == null) {return;
}
PostOrder(t.lchild);
PostOrder(t.rchild);
System.out.print(t.date);
}//先序遍历非递归
public static voidpreOrder2(BinTree t) {
Stack s = new Stack();while (t != null || !s.empty()) {while (t != null) {
System.out.print(t.date);
s.push(t);
t=t.lchild;
}if (!s.empty()) {
t=s.pop();
t=t.rchild;
}
}
}//中序遍历非递归
public static voidInOrder2(BinTree t) {
Stack s = new Stack();while (t != null || !s.empty()) {while (t != null) {
s.push(t);
t=t.lchild;
}if (!s.empty()) {
t=s.pop();
System.out.print(t.date);
t=t.rchild;
}
}
}//后序遍历非递归
public static voidPostOrder2(BinTree t) {
Stack s = new Stack();
Stack s2 = new Stack();
Integer i= new Integer(1);while (t != null || !s.empty()) {while (t != null) {
s.push(t);
s2.push(new Integer(0));
t=t.lchild;
}while (!s.empty() &&s2.peek().equals(i)) {
s2.pop();
System.out.print(s.pop().date);
}if (!s.empty()) {
s2.pop();
s2.push(new Integer(1));
t=s.peek();
t=t.rchild;
}
}
}public static voidmain(String[] args) {
BinTree b1= new BinTree('a');
BinTree b2= new BinTree('b');
BinTree b3= new BinTree('c');
BinTree b4= new BinTree('d');
BinTree b5= new BinTree('e');/*** a
* / /
* b c
* / /
* d e*/b1.lchild=b2;
b1.rchild=b3;
b2.lchild=b4;
b2.rchild=b5;
BinTree.preOrder(b1);
System.out.println();
BinTree.preOrder2(b1);
System.out.println();
BinTree.InOrder(b1);
System.out.println();
BinTree.InOrder2(b1);
System.out.println();
BinTree.PostOrder(b1);
System.out.println();
BinTree.PostOrder2(b1);
}
}