树形结构之三 帮助你测试

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

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) + "}");
				}
			} 
		}
	}
}
 

效果图:

猜你喜欢

转载自gege-s.iteye.com/blog/1463283
今日推荐