首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

构建树对象打包信息

2012-11-07 
构建树对象封装信息package com.wangjin.domaintreepublic class NodeSource { private int id private

构建树对象封装信息

package com.wangjin.domaintree;

public class NodeSource {
 private int id;
 private int parentid;
 private String title;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public int getParentid() {
  return parentid;
 }
 public void setParentid(int parentid) {
  this.parentid = parentid;
 }
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public NodeSource(int id, int parentid, String title) {
  super();
  this.id = id;
  this.parentid = parentid;
  this.title = title;
 }
 public NodeSource() {
  super();
 }
 
 

}

 

package com.wangjin.domaintree;

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

public class TreeNode {
 // 节点携带的值
 private Map<String, Object> tag;

 // 儿子节点
 private List<TreeNode> nodes;

 /** 节点信息键值对,该属性不会为空指针,但可能没有键值对存在[就是map.size()==0] */
 public Map<String, Object> getTag() {
  if (tag == null)
   tag = new HashMap<String, Object>();
  return tag;
 }

 public void setTag(Map<String, Object> tag) {
  this.tag = tag;
 }

 /** 获得所有子节点,该属性不会为空指针,但可能没有集合元素存在 */
 public List<TreeNode> getNodes() {
  if (nodes == null)
   nodes = new ArrayList<TreeNode>();
  return nodes;
 }

 public void setNodes(List<TreeNode> nodes) {

  this.nodes = nodes;
 }

 public TreeNode(Map<String, Object> tag, List<TreeNode> nodes) {
  super();
  this.tag = tag;
  this.nodes = nodes;
 }

 public TreeNode() {
  super();
 }

 public TreeNode(Map<String, Object> tag) {
  super();
  this.tag = tag;
 }
}

package com.wangjin.domaintree;

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

public class CapsulationTree {
 public TreeNode getTree() {      
        //用一个集合模拟数据表的内容,也就是一下集合的内容完全可以查询自数据库 
        List<NodeSource> treeList = new ArrayList<NodeSource>(); 
        treeList.add(new NodeSource(0,-1,"root"));      
        treeList.add(new NodeSource(1,0,"node1")); 
        treeList.add(new NodeSource(2,1,"node2"));        
        treeList.add(new NodeSource(3,1,"node3"));      
        treeList.add(new NodeSource(4,0,"node4")); 
        treeList.add(new NodeSource(5,4,"node5"));
        treeList.add(new NodeSource(6,4, "node6"));      
        treeList.add(new NodeSource(7,5,"node7")); 
        treeList.add(new NodeSource(8,5,"node8"));
        treeList.add(new NodeSource(9,7, "node9"));      
        treeList.add(new NodeSource(10,7,"node10"));          
         
        //将这个从集合封装成TreeNode      
        //构造一个根节点 
        TreeNode rootNode = new TreeNode(); 
        rootNode.getTag().put("id", "0");       
        rootNode.getTag().put("title", "root");

 

         
        //将这个集合封装成Map 
        Map<String,TreeNode> treeNodeMap = new HashMap<String,TreeNode>(); 
        //下面的循环完成两个功能: 
        //      1.将所有对象封装成Map里面的键值对,且值是TreeNode类型 
        //      2.将没有父节点的TreeNode添加到根节点rootNode 
        for(NodeSource nodeSource:treeList){           
            TreeNode node = new TreeNode(); 
            node.getTag().put("id",((Integer)nodeSource.getId()).toString()); 
            node.getTag().put("title", nodeSource.getTitle()); 
            treeNodeMap.put(((Integer)nodeSource.getId()).toString(),node); 
            System.out.println("("+nodeSource.getId()+","+nodeSource.getParentid()+")");
            if (nodeSource.getParentid()==-1){//如果没有父节点则添加到根节点 
                rootNode.getNodes().add(node);
                System.out.println(nodeSource.getParentid()+"WANGJINROOT");
            } 
        } 
         
         
        //下面的代码建立节点之间的从属关系 
        for(int i=0;i<treeList.size();i++){ 
         NodeSource nodeSource = treeList.get(i); 
            if (treeNodeMap.containsKey(((Integer)nodeSource.getParentid()).toString())){//如果当前功能的父级功能编号在treeNodeMap中存在 
                treeNodeMap 
                        .get(((Integer)nodeSource.getParentid()).toString())//找出父节点 
                        .getNodes() //父节点的子节点集合 
                        .add(               //向子节点集合添加子节点 
                                treeNodeMap.get(((Integer)nodeSource.getId()).toString())//找出子节点 
                               
                        );
                System.out.println(((Integer)nodeSource.getParentid()).toString()+","+((Integer)nodeSource.getId()).toString());
            } 
        } 
        return treeNodeMap.get("0");
    } 
 public static void main(String[] args){
  CapsulationTree capsulationTree=new CapsulationTree();
  TreeNode rootNode=capsulationTree.getTree();
  System.out.print(rootNode.getTag().get("title")+"-");
  for(int i=0;i<rootNode.getNodes().size();i++){  
   TreeNode node1=rootNode.getNodes().get(i);
   System.out.print(node1.getTag().get("title")+"--");
   if(node1.getNodes().size()!=0){    
    for(int k=0;k<node1.getNodes().size();k++){
     TreeNode node2=node1.getNodes().get(k);
     System.out.print(node2.getTag().get("title")+"--");
     if(node2.getNodes().size()!=0){
      for(int k1=0;k1<node2.getNodes().size();k1++){
       TreeNode node3=node2.getNodes().get(k1);
       System.out.print(node3.getTag().get("title")+"--");
       if(node3.getNodes().size()!=0){
        for(int k2=0;k2<node3.getNodes().size();k2++){
         TreeNode node4=node3.getNodes().get(k2);
         System.out.print(node4.getTag().get("title")+"----");
        }
       }
      }
     }
    }
   }
  }
  
 }
}

 

热点排行