树形结构之三 帮助你测试

上面的两篇博客分析了树和结节各自的处理,下面给出一个查看树型结构的类,通过调用这个类可以看出相应的结点及子结点信息,显示方式经过简单的格式处理形成有层次和缩进的效果。   下面直接放代码:  

 

package com.power.tool.tree.util;

import com.ylsoft.power.tool.tree.node.Node;

public class TreePrinter<T> {
	/**
	 * *
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
	}

	static String seprator = " ";

	/**
	 * * 打印 *
	 * 
	 * @param topnode *
	 * @param num
	 */
	public void print(Node<T> topnode, int num) {
		if (topnode == null) {
			System.out.println("null");
		} else {
			System.out.println(seprator.substring(0, num) + topnode + "|"
					+ topnode.getGrade() + "|" + topnode.getSubLevels());
			if (topnode.getChildren() != null) {
				if (topnode.getChildren().size() > 0) {
					num += 5;
					System.out.println(seprator.substring(0, num - 5) + "{");
					for (Node<T> n : topnode.getChildren()) {
						print(n, num);
					}
					System.out.println(seprator.substring(0, num - 5) + "}");
				}
			} 
		}
	}
}
 

 

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值