您的位置:首页 > 编程语言 > Java开发

java实现二叉树的先序、中序、后序遍历

2013-09-26 13:56 573 查看
package com.lsw.tree;

/**
* 树节点
* @author lsww
* @E-mail lsww90@Gmail.com
* @createtime 2013-9-25 下午9:21:41
* @version 1.0
* @since JDK 1.7
*/
public class TreePoint implements java.io.Serializable{

private static final long serialVersionUID = -1309742686675129040L;

private String name;    //此处可按照需求,自定义树对象
private TreePoint leftChild;    //左子节点
private TreePoint rightChild;   //右子节点
private TreePoint parent;       //父节点
private boolean visit = false;  //是否遍历过

public TreePoint(String name) {
this.name = name;
}

public TreePoint(String name, TreePoint leftChild, TreePoint rightChild) {
this.name = name;
this.leftChild = leftChild;
this.rightChild = rightChild;
}

public TreePoint getLeftChild() {
if(leftChild != null && leftChild.isVisited()) {    //按照遍历需求,在有左子节点的情况下,且此子节点已遍历过,则返回null
return null;
}
return leftChild;
}

public void setLeftChild(TreePoint leftChild) {
this.leftChild = leftChild;
}

public TreePoint getRightChild() {
if(rightChild != null && rightChild.isVisited()) {  //按照遍历需求,在有右子节点的情况下,且此子节点已遍历过,则返回null
return null;
}
return rightChild;
}

public void setRightChild(TreePoint rightChild) {
this.rightChild = rightChild;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean isVisited() {
return visit;
}

public void setVisit(boolean visit) {
this.visit = visit;
}

public TreePoint getParent() {
return parent;
}

public void setParent(TreePoint parent) {
this.parent = parent;
}

@Override
public String toString() {
setVisit(true);     //已遍历
return name;
}
}

package com.lsw.tree;

import org.junit.Test;

/**
* 使用先序、中序、后序遍历二叉树
* @author lsww
* @E-mail lsww90@Gmail.com
* @createtime 2013-9-25 下午9:18:17
* @version 1.0
* @since JDK 1.7
*/
public class ForTreeTest {

private TreePoint root = null;      //根节点

/**
* 初始化二叉树
*/
@org.junit.Before
public void treeInit() {
TreePoint A = new TreePoint("-");
root = A;
TreePoint B = new TreePoint("+");
TreePoint C = new TreePoint("/");
A.setLeftChild(B);
A.setRightChild(C);
B.setParent(A);
C.setParent(A);
TreePoint D = new TreePoint("a");
TreePoint E = new TreePoint("*");
B.setLeftChild(D);
B.setRightChild(E);
D.setParent(B);
E.setParent(B);
TreePoint F = new TreePoint("e");
TreePoint G = new TreePoint("f");
C.setLeftChild(F);
C.setRightChild(G);
F.setParent(C);
G.setParent(C);
TreePoint H = new TreePoint("b");
TreePoint I = new TreePoint("-");
E.setLeftChild(H);
E.setRightChild(I);
H.setParent(E);
I.setParent(E);
TreePoint J = new TreePoint("c");
TreePoint K = new TreePoint("d");
I.setLeftChild(J);
I.setRightChild(K);
J.setParent(I);
K.setParent(I);
}

/**
* 检查根节点是否为空
* @param root
*          根节点
* @param forName
*          遍历名称
*/
public void check(TreePoint root, String forName) {
if(null == root) {
System.out.println("空树");
System.exit(0);
}
System.out.println(forName);
}

/**
* 先序遍历二叉树
*/
@org.junit.Test
public void testFirst() {
check(root, "先序遍历:");
TreePoint next = root;
while(next != null) {
if(!next.isVisited()) {     //保证先遍历得到根节点
System.out.print(next);
}
TreePoint flag = next.getLeftChild();   //保证先遍历左子节点后右子节点
if(flag == null) {
flag = next.getRightChild();
if(flag == null) {
next = next.getParent();
continue;
}
next = flag;
continue;
}
next = flag;
}
System.out.println("\n---------------------");
}

/**
* 中序遍历二叉树
*/
@org.junit.Test
public void testMiddon() {
check(root, "中序遍历:");
TreePoint next = root;
while(next != null) {
TreePoint flag = next.getLeftChild();   //保证先遍历左子节点后右子节点
if(flag == null) {
if(!next.isVisited()) {     //保证先得到左子节点、后根节点、再右子节点
System.out.print(next);
}
flag = next.getRightChild();
if(flag == null) {
next = next.getParent();
} else {
next = flag;
}
} else {
next = flag;
}
}
System.out.println("\n---------------------");
}

/**
* 后续遍历二叉树
*/
@Test
public void testLast() {
check(root, "后序遍历:");
TreePoint next = root;
while(next != null) {
TreePoint flag = next.getLeftChild();   //保证先遍历左子节点后右子节点
if(flag == null) {
flag = next.getRightChild();
if(flag == null) {
if(!next.isVisited()) {     //保证先得到左子节点、后右子节点、再根节点
System.out.print(next);
}
next = next.getParent();
} else {
next = flag;
}
} else {
next = flag;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐