java实现树的遍历

首先来看看树节点的定义:

TNode.java:

package datastructure.tree;

/**
*树节点
* @author yunfeiyang
*/
public class TNode<E> {

private TNode<E> LChild = null, RChild = null;
private E data;

/**
* default node
*/
public TNode() {
data = null;
left(null);
right(null);
}

/**
* node with data
* @param data data of the node
*/
public TNode(E data) {
this.data = data;
left(null);
right(null);
}

/**
* set data of the node
* @param data data of the node
*/
public void data(E data) {
this.data = data;
}

/**
* get data of the node
* @return data of the node
*/
public E data() {
return data;
}

/**
*set the reference of left child
* @param left reference of left child
*/
public void left(TNode<E> left) {
LChild = left;
}

/**
* get reference of left child
* @return reference of left child
*/
public TNode<E> left() {
return LChild;
}

/**
*set the reference of right child
* @param reference of right child
*/
public void right(TNode<E> left) {
RChild = left;
}

/**
* get reference of right child
* @return reference of right child
*/
public TNode<E> right() {
return RChild;
}

@Override
public String toString() {
String s = "a node with value:" + data.toString();
return s;
}
}

其次,树的遍历方法写在一个类当中:

TreeOutput.java:


package datastructure.tree;
import java.util.ArrayList;

/**
*遍历并输出树节点的值
* @author yunfeiyang
* @version 1.0
*/
public class TreeOutput<E> {
/**
* 中序遍历树节点
* @param root 树的根节点
* @param separate 分隔符
*/
public void inorderOutput(TNode<E> root, String separate) {
if (root == null) {
return;
}
inorderOutput(root.left(), separate);
System.out.print(root.data() + separate);
inorderOutput(root.right(), separate);

}
/**
* 后续遍历树节点
* @param root 树的根节点
* @param separate 分隔符
*/
public void postorderOutput(TNode<E> root, String separate) {
if (root == null) {
return;
}
postorderOutput(root.left(), separate);
postorderOutput(root.right(), separate);
System.out.print(root.data() + separate);
}
/**
* 前序遍历树节点
* @param root 数的根节点
* @param separate 分隔符
*/
public void preorderOutput(TNode<E> root, String separate) {
if (root == null) {
return;
}
System.out.print(root.data() + separate);
preorderOutput(root.left(), separate);
preorderOutput(root.right(), separate);

}
/**
* 层次遍历树节点
* @param root 树的根节点
* @param separate 分隔符
*/
public void levelorderOutput(TNode<E> root, String separate) {
if (root == null) {
return;
}
ArrayList<TNode<E>> queue=new ArrayList<TNode<E>>();
queue.add(root);
while(!queue.isEmpty())
{
TNode<E> head=queue.remove(0);
System.out.print(head.data()+separate);
if(head.left()!=null)
{
queue.add(head.left());
}
if(head.right()!=null)
{
queue.add(head.right());
}
}
}


}

最后写测试方法来测试下:

Main.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package datastructure.tree.test;

import datastructure.tree.TNode;
import datastructure.tree.TreeOutput;

/**
*
* @author Administrator
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
TreeOutput<Integer> output = new TreeOutput<Integer>();
TNode<Integer> newNode = new TNode<Integer>(0);
TNode<Integer> root = main.initData(newNode);
System.out.print("中序遍历树节点:");
output.inorderOutput(root, ",");
System.out.println();
System.out.print("后序遍历树节点:");
output.postorderOutput(root, ",");
System.out.println();
System.out.print("前序遍历树节点:");
output.preorderOutput(root, ",");
System.out.println();
System.out.print("层次遍历树节点:");
output.levelorderOutput(root, ",");
System.out.println();
}

private TNode<Integer> initData(TNode<Integer> root) {
TNode<Integer> result=null;
if (root == null) {
result=root;
} else {
TNode<Integer> leftNode = new TNode<Integer>(10);
root.left(leftNode);
TNode<Integer> rightNode = new TNode<Integer>(20);
root.right(rightNode);
leftNode.left(new TNode<Integer>(30));
leftNode.right(new TNode<Integer>(40));
rightNode.left(new TNode<Integer>(50));
rightNode.right(new TNode<Integer>(60));
result= root;
}
return result;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值