废话不多说,上代码
package com.zhukovasky.bean;
import java.util.ArrayList;
import java.util.List;
public class Nodes {
private List<Nodes> subChildNodes;
private Nodes parentNode;
private Integer depth;
private String nodescontents;
public Nodes(String nodescontents,Integer depth,Nodes parentNode){
this.nodescontents=nodescontents;
this.depth=depth;
this.parentNode=parentNode;
this.subChildNodes=new ArrayList<Nodes>();
}
public void addNewSubNodes(String childnodescontents){
Nodes node=new Nodes(childnodescontents,this.depth+1,this);
this.subChildNodes.add(node);
}
public Nodes getChildNodesByString(String childnodescontents){
Nodes childnode=null;
for(Nodes iter:subChildNodes){
if(childnodescontents.equals(iter.getNodescontents())){
childnode=iter;
}
}
return childnode;
}
public Nodes getParentNode() {
return parentNode;
}
public void setParentNode(Nodes parentNode) {
this.parentNode = parentNode;
}
public Integer getDepth() {
return depth;
}
public void setDepth(Integer depth) {
this.depth = depth;
}
public String getNodescontents() {
return nodescontents;
}
public void setNodescontents(String nodescontents) {
this.nodescontents = nodescontents;
}
public List<Nodes> getChildNodeList(){
return this.subChildNodes;
}
public String toString(){
return "节点"+this.nodescontents+",深度"+this.depth;
}
public static void main(String[] args){
Nodes A=new Nodes("A",0,null);
A.addNewSubNodes("B");
A.addNewSubNodes("C");
A.addNewSubNodes("D");
System.out.println(A.getChildNodesByString("D"));
}
}
package com.zhukovasky.bean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class TreeNode {
private String root;
private Map<Nodes,Integer> maptree;
private Nodes rootNode;
public TreeNode(String root){
this.setRoot(root);
this.maptree=new HashMap<Nodes,Integer>();
this.rootNode=new Nodes(root,0,null);
this.maptree.put(rootNode, 0);
}
public void addNewNode(String childNode,Integer depth,String parentNode){
List<Nodes> list=getChildNodesByDepth(depth);
if(depth==1){
rootNode.addNewSubNodes(childNode);
this.maptree.put(rootNode, 0);
this.maptree.put(rootNode.getChildNodesByString(childNode), 1);
}
if(depth>1){
List<Nodes> listp=getChildNodesByDepth(depth-1);
if(list.isEmpty()){
for(Nodes iter:listp){
if(parentNode.equals(iter.getNodescontents())){
iter.addNewSubNodes(childNode);
this.maptree.put(iter, depth-1);
this.maptree.put(iter.getChildNodesByString(childNode),depth);
}
}
}else{
for(Nodes iter:listp){
if(parentNode.equals(iter.getNodescontents())){
iter.addNewSubNodes(childNode);
this.maptree.put(iter, depth-1);
this.maptree.put(iter.getChildNodesByString(childNode), depth);
}
}
}
}
}
public int getSize(){
return this.maptree.size();
}
public List<Nodes> getChildNodesByDepth(Integer depth){
List<Nodes> list=new ArrayList<Nodes>();
for(Entry<Nodes,Integer> entry:this.maptree.entrySet()){
if(entry.getValue().equals(depth)){
list.add(entry.getKey());
}
}
return list;
}
public String toString(){
return this.maptree.entrySet().toString();
}
public boolean isEndOf(String contents,Integer depth){
boolean flag=false;
for(Nodes iter:this.getChildNodesByDepth(depth)){
if(contents.equals(iter.getNodescontents())){
flag=true;
}
}
return flag;
}
public static void main(String[] args){
TreeNode t=new TreeNode("A");
t.addNewNode("A", 0, null);
t.addNewNode("B", 1, "A");
t.addNewNode("C", 1, "A");
t.addNewNode("E", 1, "A");
t.addNewNode("F", 1, "A");
t.addNewNode("G", 1, "A");
t.addNewNode("H", 1, "A");
t.addNewNode("M", 2, "B");
t.addNewNode("K", 2, "B");
t.addNewNode("L", 2, "C");
System.out.println(t.getChildNodesByDepth(1));
}
public String getRoot() {
return root;
}
public void setRoot(String root) {
this.root = root;
}
}
package com.zhukovasky.bean;
import java.util.HashMap;
import java.util.Map;
public class MapTree {
private Map<String,TreeNode> maps;
public MapTree(){
this.maps=new HashMap<String,TreeNode>();
}
public void initTreeRoot(String roots){
TreeNode tree=new TreeNode(roots);
this.maps.put(roots, tree);
}
public void addRootElementsByRoot(String root,String NodeValueChild,Integer depth,String ParentNode){
TreeNode tr=this.maps.get(root);
tr.addNewNode(NodeValueChild, depth, ParentNode);
this.maps.put(root, tr);
}
public boolean isEndOf(String root,String content ,Integer depth){
return this.maps.get(root).isEndOf(content, depth);
}
}