用Java實現簡單樹結構
阿新 • • 發佈:2018-12-25
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);
}
}
結果: