Java横向生长树测试

绘图
package draw;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Draw {

	private Map<String, PicNode> idMap = new HashMap<String, PicNode>();

	private PicNode[][] map = null;

	// 需要空开行数
	private int row = 0;

	private int maxX = -1;

	private int maxY = -1;

	public Draw() {
		List<PicNode> data = DrawData.getData();
		for (PicNode picNode : data) {
			idMap.put(picNode.getId(), picNode);
		}
		PicNode rootNode = data.get(0);

		read(rootNode);
		setIntoMap();
	}

	// 开始读取
	private void read(PicNode picNode) {
		if (!picNode.isExpand()) {
			return;
		}
		List<String> childId = picNode.getChildId();
		int size = childId.size();
		if (size == 0) {
			return;
		} else {
			for (String id : childId) {
				PicNode node = idMap.get(id);
				settingLocation(node);
				read(node);
			}
		}
	}

	// 设置坐标
	private void settingLocation(PicNode node) {
		PicNode parentNode = idMap.get(node.getParentId());
		List<String> childId = parentNode.getChildId();
		int size = childId.size();
		// 判断第几个子结点为当前结点
		int n = 0;
		for (String id : childId) {
			n++;
			if (id.equals(node.getId())) {
				break;
			}
		}

		// 如果当前结点子结点不止一个,则需要下移1行,每个结点仅会遍历一次
		if (size > 1 && n != 1) {
			row++;
		}

		// x.当前第几行; y父结点往后1个坐标,中间留多1个画线空间
		node.setX(row);
		node.setY(parentNode.getY() + 2);
		maxValue(node);
	}

	// 计算最大行列
	private void maxValue(PicNode node) {
		if (node.getX() > maxX) {
			maxX = node.getX();
		}
		if (node.getY() > maxY) {
			maxY = node.getY();
		}
	}

	// 绘图
	private void setIntoMap() {
		map = new PicNode[maxX + 1][maxY + 1];
		for (Entry<String, PicNode> entry : idMap.entrySet()) {
			PicNode value = entry.getValue();
			if (value.getX() != -1 && value.getY() != -1) {
				map[value.getX()][value.getY()] = value;
			}
		}
	}

	public static void main(String[] args) {
		Draw draw = new Draw();
		draw.print();
		System.exit(0);
	}

	// 绘图
	public void print() {
		for (int i = 0; i <= maxX; i++) {
			for (int j = 0; j <= maxY; j++) {
				PicNode picNode = map[i][j];
				if (picNode != null) {
					String id = picNode.getId().toString();
					if (id.length() == 1) {
						System.out.print("[0" + picNode.getId() + "]");
					} else {
						System.out.print("[" + picNode.getId() + "]");
					}
				} else {
					System.out.print("[  ]");
				}
			}
			System.out.println();
		}
	}
}
  
数据
package draw;

import java.util.ArrayList;
import java.util.List;

public class DrawData {

	/**
	 * [01][ ][02][ ][04][ ][07][ ][ ] <br>
	 * [ ][ ][ ][ ][ ][ ][08][ ][ ] <br>
	 * [ ][ ][ ][ ][05][ ][09][ ][11] <br>
	 * [ ][ ][ ][ ][ ][ ][ ][ ][12] <br>
	 * [ ][ ][ ][ ][ ][ ][ ][ ][13] <br>
	 * [ ][ ][ ][ ][06][ ][10][ ][ ] <br>
	 * [ ][ ][03][ ][ ][ ][ ][ ][ ] <br>
	 */
	public static List<PicNode> getData() {
		List<PicNode> list = new ArrayList<PicNode>();
		PicNode root = new PicNode("1", "-1", getChild("2", "3"), true);
		root.setX(0);
		root.setY(0);
		list.add(root);
		create("2", "1", getChild("4", "5", "6"), true, list);
		create("3", "1", getChild(""), true, list);
		create("4", "2", getChild("7", "8"), true, list);
		create("5", "2", getChild("9"), true, list);
		create("6", "2", getChild("10"), true, list);
		create("7", "4", getChild(""), true, list);
		create("8", "4", getChild(""), true, list);
		create("9", "5", getChild("11", "12", "13"), true, list);
		create("10", "6", getChild(""), true, list);
		create("11", "9", getChild(""), true, list);
		create("12", "9", getChild(""), true, list);
		create("13", "9", getChild(""), true, list);
		return list;
	}

	/**
	 * [01][ ][02][ ][05] <br>
	 * [ ][ ][ ][ ][06] <br>
	 * [ ][ ][ ][ ][07] <br>
	 * [ ][ ][03][ ][08] <br>
	 * [ ][ ][ ][ ][09] <br>
	 * [ ][ ][04][ ][ ] <br>
	 */
	public static List<PicNode> getData1() {
		List<PicNode> list = new ArrayList<PicNode>();
		PicNode root = new PicNode("1", "-1", getChild("2", "3", "4"), true);
		root.setX(0);
		root.setY(0);
		list.add(root);
		create("2", "1", getChild("5", "6", "7"), true, list);
		create("3", "1", getChild("8","9"), true, list);
		create("4", "1", getChild(""), true, list);
		create("5", "2", getChild(""), true, list);
		create("6", "2", getChild(""), true, list);
		create("7", "2", getChild(""), true, list);
		create("8", "3", getChild(""), true, list);
		create("9", "3", getChild(""), true, list);
		return list;
	}

	private static void create(String id, String pId, List<String> cList,
			boolean isExpand, List<PicNode> list) {
		list.add(new PicNode(id, pId, cList, isExpand));
	}

	private static List<String> getChild(String... strings) {
		List<String> list = new ArrayList<String>();
		if ("".equals(strings[0])) {
			return list;
		}
		for (String string : strings) {
			list.add(string);
		}
		return list;
	}
}
 
bean
package draw;

import java.util.ArrayList;
import java.util.List;

public class PicNode {

	// 当前结点id
	private String id = "-1";

	// 父结点id
	private String parentId = "-1";

	// 子结点id集
	private List<String> childId = new ArrayList<String>();

	// 当前结点是否展开,默认为展开
	private boolean isExpand = true;

	private int x = -1;

	private int y = -1;

	public PicNode(String id, String parentId, List<String> childId,
			boolean isExpand) {
		super();
		this.id = id;
		this.parentId = parentId;
		this.childId = childId;
		this.isExpand = isExpand;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public List<String> getChildId() {
		return childId;
	}

	public void setChildId(List<String> childId) {
		this.childId = childId;
	}

	public boolean isExpand() {
		return isExpand;
	}

	public void setExpand(boolean isExpand) {
		this.isExpand = isExpand;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
}
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值