递归实现树的遍历

递归实现树的遍历

树如下所示:

 * 		A 
 * 	   / \ 
 *    B   C 
 *   / \ 
 *  D   E 
 *     / \ 
 *    F   G

定义三个类:

  1. Node class——节点类
  2. BinaryTree class——树类
  3. 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));	
		}
	}
}

希望可以帮助到大家

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值