1、由先根遍历序列和中根遍历序列建立二叉树:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class BiTreeNode{ //二叉树结点类
public Object data;
public BiTreeNode lchild,rchild;
public BiTreeNode(Object data) { //构造一棵左右孩子域为空的二叉树
this(data,null,null);
}
//构造一棵左右孩子域都不为空的二叉树
public BiTreeNode(Object data,BiTreeNode lchild,BiTreeNode rchild) {
this.data = data;
this.lchild = lchild;
this.rchild = rchild;
}
}
class BiTree{
BiTreeNode root;
//由先根遍历序列和中根遍历序列建立二叉树
public BiTree(String preOrder, String inOrder, int preIndex, int inIndex, int count) {
if(count>0) { //先根和中根非空
//取先根遍历序列中的第一个结点作为根结点
char r = preOrder.charAt(preIndex);
int i;
for(i=0;i<count;i++) { //寻找根节点在中根遍历序列中的位置
if(r == inOrder.charAt(i + inIndex));
break;
}
root = new BiTreeNode(r); //建立树的根节点
//建立左子树
root.lchild = new BiTree(preOrder,inOrder,preIndex+1,inIndex,i).root;
//建立右子树
root.rchild = new BiTree(preOrder,inOrder,preIndex+i+1,inIndex+i+1,count-i-1).root;
}
}
public void postRootTraverse() { //后根遍历的非递归做法
BiTreeNode T = root;
if(T!=null) {
Stack s = new Stack();
s.clear();
s.push(T);
Boolean vis;
BiTreeNode p = null;
while(!s.isEmpty()) {
while(s.peek()!=null)
s.push(((BiTreeNode)s.peek()).lchild);
s.pop();
while(!s.isEmpty()) {
T = (BiTreeNode)s.peek();
if(T.rchild == null || T.rchild == p) {
System.out.print(T.data+" ");
s.pop();
p = T;
vis = true;
}else {
s.push(T.rchild);
vis = false;
}
if(!vis) break;
}
}
}
}
}
public class Main {
public static void main(String[] args) {
String preOrder = "ABDEGCFH";
String inOrder = "DBGEAFHC";
BiTree T = new BiTree(preOrder,inOrder,0,0,preOrder.length());
System.out.print("(非递归)后根遍历序列:");
T.postRootTraverse();
System.out.println();
}
}
输出结果:
2、二叉树的遍历:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class BiTreeNode{ //二叉树结点类
public Object data;
public BiTreeNode lchild,rchild;
public BiTreeNode() { //构造一个空结点
this(null);
}
public BiTreeNode(Object data) { //构造一棵左右孩子域为空的二叉树
this(data,null,null);
}
//构造一棵左右孩子域都不为空的二叉树
public BiTreeNode(Object data,BiTreeNode lchild,BiTreeNode rchild) {
this.data = data;
this.lchild = lchild;
this.rchild = rchild;
}
}
class BiTree{
BiTreeNode root;
public BiTree(BiTreeNode root) { //构造一棵树
this.root = root;
}
private static int index = 0; //用于记录preStr的索引值
public void preRootTraverse(BiTreeNode T) { //先根遍历二叉树的递归做法
if(T!=null) {
System.out.print(T.data+" ");
preRootTraverse(T.lchild);
preRootTraverse(T.rchild);
}
}
public void preRootTraverse() { //先根遍历二叉树的非递归做法
BiTreeNode T = root;
if(T!=null) {
Stack s = new Stack();
s.clear();
s.push(T);
while(!s.isEmpty()) {
T = (BiTreeNode)s.pop();
System.out.print(T.data+" ");
while(T!=null) {
if(T.lchild!=null)
System.out.print(T.lchild.data+" ");
if(T.rchild!=null)
s.push(T.rchild);
T = T.lchild;
}
}
}
}
public void inRootTraverse(BiTreeNode T) { //中根遍历的递归做法
if(T!=null) {
inRootTraverse(T.lchild);
System.out.print(T.data+" ");
inRootTraverse(T.rchild);
}
}
public void inRootTraverse() { //中根遍历的非递归做法
BiTreeNode T = root;
if(T!=null) {
Stack s = new Stack();
s.clear();
s.push(T);
while(!s.isEmpty()) {
while(s.peek()!=null)
s.push(((BiTreeNode)s.peek()).lchild);
s.pop();
if(!s.isEmpty()) {
T = (BiTreeNode)s.pop();
System.out.print(T.data+" ");
s.push(T.rchild);
}
}
}
}
public void postRootTraverse(BiTreeNode T) { //后根遍历的递归做法
if(T!=null) {
postRootTraverse(T.lchild);
postRootTraverse(T.rchild);
System.out.print(T.data+" ");
}
}
public void postRootTraverse() { //后根遍历的非递归做法
BiTreeNode T = root;
if(T!=null) {
Stack s = new Stack();
s.clear();
s.push(T);
Boolean vis;
BiTreeNode p = null;
while(!s.isEmpty()) {
while(s.peek()!=null)
s.push(((BiTreeNode)s.peek()).lchild);
s.pop();
while(!s.isEmpty()) {
T = (BiTreeNode)s.peek();
if(T.rchild == null || T.rchild == p) {
System.out.print(T.data+" ");
s.pop();
p = T;
vis = true;
}else {
s.push(T.rchild);
vis = false;
}
if(!vis) break;
}
}
}
}
public void levelTraverse() { //层次遍历的非递归做法
BiTreeNode T = root;
if(T!=null) {
Queue<BiTreeNode> q = new LinkedList<>();
q.clear();
q.offer(T);
while(!q.isEmpty()) {
T = (BiTreeNode) q.poll();
System.out.print(T.data+" ");
if(T.lchild != null)
q.offer(T.lchild);
if(T.rchild != null)
q.offer(T.rchild);
}
}
}
}
public class Main {
public BiTree createBiTree() {
BiTreeNode d = new BiTreeNode('D');
BiTreeNode g = new BiTreeNode('G');
BiTreeNode h = new BiTreeNode('H');
BiTreeNode e = new BiTreeNode('E',g,null);
BiTreeNode b = new BiTreeNode('B',d,e);
BiTreeNode f = new BiTreeNode('F',null,h);
BiTreeNode c = new BiTreeNode('C',f,null);
BiTreeNode a = new BiTreeNode('A',b,c);
return new BiTree(a);
}
public static void main(String[] args) {
Main Tree = new Main();
BiTree T = Tree.createBiTree();
BiTreeNode root = T.root;
System.out.print("(非递归)先根遍历序列:");
T.preRootTraverse();
System.out.println();
System.out.print("(非递归)中根遍历序列:");
T.inRootTraverse();
System.out.println();
System.out.print("(非递归)后根遍历序列:");
T.postRootTraverse();
System.out.println();
System.out.print("层次遍历序列:");
T.levelTraverse();
System.out.println();
}
}
输出结果: