Node实体:
package treeTest;
import java.io.Serializable;
import java.util.List;
/**
* ClassName: Node
* @Description: 节点类
* @author SHICC
* @date 2018年2月7日
*/
@SuppressWarnings("serial")
public class Node<T> implements Serializable {
protected Node<T> parentNode;
protected T nodeEntity;
protected List<Node<T>> childNodes;
public void setParentNode(Node<T> parentNode) {
this.parentNode = parentNode;
}
public Node<T> getParentNode() {
return parentNode;
}
public void setNodeEntity(T nodeEntity) {
this.nodeEntity = nodeEntity;
}
public T getNodeEntity() {
return nodeEntity;
}
public void setChildNodes(List<Node<T>> childNodes) {
this.childNodes = childNodes;
}
public List<Node<T>> getChildNodes() {
return childNodes;
}
}
TreeNode实现类:
package treeTest;
import java.util.ArrayList;
/**
* ClassName: TreeNode
* @Description: 树实现类
* @author SHICC
* @date 2018年2月7日
*/
@SuppressWarnings("serial")
public class TreeNode<T> extends Node<T>{
public TreeNode() {
}
public TreeNode(T nodeEntity) {
super();
super.nodeEntity = nodeEntity;
}
protected void addChildNode(Node<T> childNode) {
childNode.setParentNode(this);
if(childNodes == null) {
childNodes = new ArrayList<Node<T>>();
}
this.childNodes.add(childNode);
}
protected void removeChildNode(Node<T> childNode) {
if(childNodes != null) {
childNodes.remove(childNode);
}
}
public void display(Node<T> treeNode, int depth) {
StringBuilder sb = new StringBuilder("");
for(int i = 0; i < depth; i++) {
sb.append("-");
}
if(treeNode.parentNode == null) {
System.out.println(treeNode.getNodeEntity().toString());
}
for(Node<T> childNode : treeNode.getChildNodes()) {
System.out.println(new String(sb) + childNode.getNodeEntity().toString());
if(childNode.getChildNodes() != null) {
display(childNode, depth + 2);
}
}
}
}
LeafNode子叶实现类:
package treeTest;
/**
* ClassName: LeafNode
* @Description: 子叶实现类
* @author SHICC
* @date 2018年2月7日
*/
@SuppressWarnings("serial")
public class LeafNode<T> extends Node<T> {
public LeafNode() {
}
public LeafNode(T nodeEntity) {
super();
super.nodeEntity = nodeEntity;
}
public void display(Node<T> treeNode, int depth) {
StringBuilder sb = new StringBuilder("");
for(int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + treeNode.getNodeEntity().toString());
}
}
Company模型:
package treeTest;
public class Company {
private String name;
private String address;
private String tel;
public Company(String name, String address, String tel) {
this.name = name;
this.address = address;
this.tel = tel;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "Company [name=" + name + ", address=" + address + ", tel="
+ tel + "]";
}
}
测试类Test:
package treeTest;
public class Test {
public static void main(String[] args) {
Company company = new Company("久远银海成都总公司", "成都", "5175276");
Company company1 = new Company("久远银海上海分公司", "上海", "5175276");
Company company2 = new Company("久远银海重庆分公司", "重庆", "5175276");
Company company3 = new Company("久远银海西安分公司", "西安", "5175276");
Company company4 = new Company("久远银海重庆江北分公司", "重庆江北", "5175276");
Company company5 = new Company("久远银海重庆江北分公司槐树街办事处", "重庆江北", "5175276");
Company company6 = new Company("久远银海重庆江北分公司大丰路办事处", "重庆江北", "5175276");
TreeNode<Company> root = new TreeNode<Company>(company);
TreeNode<Company> c1 = new TreeNode<Company>(company1);
TreeNode<Company> c2 = new TreeNode<Company>(company2);
TreeNode<Company> c3 = new TreeNode<Company>(company3);
TreeNode<Company> c4 = new TreeNode<Company>(company4);
LeafNode<Company> l1 = new LeafNode<Company>(company5);
LeafNode<Company> l2 = new LeafNode<Company>(company6);
root.addChildNode(c1);
root.addChildNode(c2);
root.addChildNode(c3);
c2.addChildNode(c4);
c4.addChildNode(l1);
c4.addChildNode(l2);
root.display(root, 2);
TreeNode<String> root1 = new TreeNode<String>("中国");
TreeNode<String> r1 = new TreeNode<String>("四川");
TreeNode<String> r2 = new TreeNode<String>("广东");
TreeNode<String> r3 = new TreeNode<String>("成都");
TreeNode<String> r4 = new TreeNode<String>("泸州");
TreeNode<String> r5 = new TreeNode<String>("广州");
root1.addChildNode(r1);
root1.addChildNode(r2);
r1.addChildNode(r3);
r1.addChildNode(r4);
r2.addChildNode(r5);
root1.display(root1, 2);
}
}
结果: