欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java SE: A simple example of Tree

程序员文章站 2022-04-01 16:17:08
...

1. TreeOriginalNode.java

package edu.xmu.tree;

public class TreeOriginalNode
{
	private String attr1;
	private String attr2;
	private String attr3;

	private int total;

	public String getAttr1()
	{
		return attr1;
	}

	public void setAttr1(String attr1)
	{
		this.attr1 = attr1;
	}

	public String getAttr2()
	{
		return attr2;
	}

	public void setAttr2(String attr2)
	{
		this.attr2 = attr2;
	}

	public String getAttr3()
	{
		return attr3;
	}

	public void setAttr3(String attr3)
	{
		this.attr3 = attr3;
	}

	public int getTotal()
	{
		return total;
	}

	public void setTotal(int total)
	{
		this.total = total;
	}

	@Override
	public String toString()
	{
		return "TreeOriginalNode [attr1=" + attr1 + ", attr2=" + attr2
				+ ", attr3=" + attr3 + ", total=" + total + "]";
	}

}

 

2. TreeNode.java

package edu.xmu.tree;

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

public class TreeNode
{
	private String attr1;

	private int total;

	private List<TreeNode> nodeList = new ArrayList<TreeNode>();

	public TreeNode()
	{
	}

	public TreeNode(String attr1, int total)
	{
		this.attr1 = attr1;
		this.total = total;
	}

	public String getAttr1()
	{
		return attr1;
	}

	public void setAttr1(String attr1)
	{
		this.attr1 = attr1;
	}

	public void setTotal(int total)
	{
		this.total = total;
	}

	public int getTotal()
	{
		return this.total;
	}

	public List<TreeNode> getNodeList()
	{
		return nodeList;
	}

	public void setNodeList(List<TreeNode> nodeList)
	{
		this.nodeList = nodeList;
	}

	@Override
	public String toString()
	{
		return "TreeNode [attr1=" + attr1 + ", total=" + total + ", nodeList="
				+ nodeList + "]";
	}

}

 

3. TreeRoot.java

package edu.xmu.tree;

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

public class TreeRoot
{
	private List<TreeNode> levelOneList = new ArrayList<TreeNode>();

	public void addNode(TreeOriginalNode node)
	{
		if (levelOneList.isEmpty())
		{
			levelOneList.add(new TreeNode(node.getAttr1(), node.getTotal()));
			System.out.println(levelOneList.get(0));
		} else
		{
			for (Iterator<TreeNode> levelOneIter = levelOneList.iterator(); levelOneIter
					.hasNext();)
			{
				TreeNode levelOneTempNode = levelOneIter.next();
				if (levelOneTempNode.getAttr1().equals(node.getAttr1()))
				{
					int levelOneTotal = levelOneTempNode.getTotal();
					levelOneTotal += node.getTotal();
					levelOneTempNode.setTotal(levelOneTotal);

					List<TreeNode> levelTwoList = levelOneTempNode
							.getNodeList();
					if (levelTwoList.isEmpty())
					{
						System.out.println("Add node" + node
								+ " to levelTwoList");
						levelTwoList.add(new TreeNode(node.getAttr2(), node
								.getTotal()));
						System.out.println(levelTwoList.get(0));
					} else
					{
						for (Iterator<TreeNode> levelTwoIter = levelTwoList
								.iterator(); levelTwoIter.hasNext();)
						{
							TreeNode levelTwoTempNode = levelTwoIter.next();

							if (levelTwoTempNode.getAttr1().equals(
									node.getAttr2()))
							{
								int levelTwoTotal = levelTwoTempNode.getTotal();
								levelTwoTotal += node.getTotal();
								levelTwoTempNode.setTotal(levelTwoTotal);

								List<TreeNode> levelThreeList = levelTwoTempNode
										.getNodeList();

								levelThreeList.add(new TreeNode(
										node.getAttr3(), node.getTotal()));
							} else
							{
								levelTwoList.add(new TreeNode(node.getAttr2(),
										node.getTotal()));
								break;
							}
						}
					}
				} else
				{
					levelOneList.add(new TreeNode(node.getAttr1(), node
							.getTotal()));
					break;
				}
			}
		}
	}

	public List<TreeNode> getNodeList()
	{
		return levelOneList;
	}

	public void setNodeList(List<TreeNode> nodeList)
	{
		this.levelOneList = nodeList;
	}

	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();

		for (Iterator<TreeNode> iter = levelOneList.iterator(); iter.hasNext();)
		{
			TreeNode node = iter.next();
			sb.append(node.toString());
			sb.append("\n");
		}
		return sb.toString();
	}

}

 

4. TreeRootTest.java

package edu.xmu.tree;

import org.junit.Test;

public class TreeRootTest
{
	@Test
	public void test()
	{
		TreeRoot root = new TreeRoot();

		TreeOriginalNode node1 = new TreeOriginalNode();
		node1.setAttr1("aaa");
		node1.setAttr2("AAA");
		node1.setAttr3("111");

		TreeOriginalNode node2 = new TreeOriginalNode();
		node2.setAttr1("aaa");
		node2.setAttr2("AAA");
		node2.setAttr3("222");

		TreeOriginalNode node3 = new TreeOriginalNode();
		node3.setAttr1("aaa");
		node3.setAttr2("BBB");
		node3.setAttr3("111");

		TreeOriginalNode node4 = new TreeOriginalNode();
		node4.setAttr1("bbb");
		node4.setAttr2("AAA");
		node4.setAttr3("111");

		root.addNode(node1);
		root.addNode(node2);
		root.addNode(node3);
		root.addNode(node4);
		
		System.out.println(root);
	}
}