递归实现树的遍历
树如下所示:
* A
* / \
* B C
* / \
* D E
* / \
* F G
定义三个类:
- Node class——节点类
- BinaryTree class——树类
- Demo01 class——测试类
Node class:
含有三个成员变量:data(数据)、left(左节点)、rigth(右节点)
含有两个构造函数,一个成员函数
public class Node {
// 数据
private Object data;
// 左右子节点
private Node left, right;
// 构造一个叶子节点
public Node(Object data) {
this(data, null, null);
}
// 构造一个有子节点的节点
public Node(Object data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
// 重写toString方法
public String toString() {
if (data == null) {
return null;
}
return getData().toString();
}
BinaryTree class:
含有三个成员变量:data(数据)、left(左节点)、rigth(右节点)
含有一个构造函数,五个成员函数
public class BinaryTree {
// 根节点
private Node root;
// 左右子节点
private Node left, right;
// 构造一个指定根节点的二叉树
public BinaryTree(Node root) {
super();
this.root = root;
}
// 重新定义toSting方法,返回树的全部信息
public String toStirng() {
return root.toString();
}
// 先序遍历ABDEFGC
public static Vector rootFirst(Node root) {
// 定义存储顺序的集合
Vector result = new Vector();
// 判断根节点是否为空
if (root == null) {
return result;
}
// 先添加根节点
result.add(root);
// 再递归添加左节点
result.addAll(rootFirst(root.getLeft()));
// 最后递归添加右节点
result.addAll(rootFirst(root.getRight()));
return result;
}
//中序遍历 DBFEGAC
public static Vector rootMiddle(Node root) {
// 定义存储顺序的集合
Vector result = new Vector();
// 判断根节点是否为空
if (root == null) {
return result;
}
// 先递归添加左节点
result.addAll(rootMiddle(root.getLeft()));
// 再添加根节点
result.add(root);
// 最后递归添加右节点
result.addAll(rootMiddle(root.getRight()));
return result;
}
//后序遍历 DFGEBCA
public static Vector rootLast(Node root) {
// 定义存储顺序的集合
Vector result = new Vector();
// 判断根节点是否为空
if (root == null) {
return result;
}
// 先递归添加左节点
result.addAll(rootLast(root.getLeft()));
// 再递归添加右节点
result.addAll(rootLast(root.getRight()));
// 最后添加根节点
result.add(root);
return result;
}
}
注意: 分别给 Node类 和 BinaryTree类 自动添加成员变量的 get() , set() 函数
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
Demo01 class:
public class Demo01 {
// 创建二叉树
public static BinaryTree create() {
Node a, b, c, d, e, f, g;
g = new Node("G");
f = new Node("F");
e = new Node("E", f, g);
d = new Node("D");
b = new Node("B", d, e);
c = new Node("C");
a = new Node("A", b, c);
//创建树
BinaryTree tree = new BinaryTree(a);
return tree;
}
public static void main(String[] args) {
BinaryTree tree = Demo01.create();
System.out.println("————————————先序————————————");
Vector v0 = tree.rootFirst(tree.getRoot());
for(int i=0;i<v0.size();i++) {
System.out.println(v0.get(i));
}
System.out.println("————————————中序————————————");
Vector v1 = tree.rootMiddle(tree.getRoot());
for(int i=0;i<v1.size();i++) {
System.out.println(v1.get(i));
}
System.out.println("————————————后序————————————");
Vector v2 = tree.rootLast(tree.getRoot());
for(int i=0;i<v2.size();i++) {
System.out.println(v2.get(i));
}
}
}
希望可以帮助到大家