樹的層級關係解析

1.創建節點對象  Node

package cn.com.citydo.supervise.service.util.tree;

import java.util.LinkedList;
import java.util.List;

public class Node {
	/**
     * 節點id
     */
    private String id;
	/**
     * 父節點id
     */
    private String parentId;
    /**
     * 節點名稱
     */
    private String nodeName;
 
    /**
     * 子節點
     */
    private List<Node> children;
    
    /**
     * 層級
     */
    private int level=0;
   
    public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public List<Node> getChildren() {
		return children;
	}

	public void setChildren(List<Node> children) {
		this.children = children;
	}



	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
 
	 public Node addChild(Node node) {
			if (children == null) {
				this.children = new LinkedList<Node>();
			}
			children.add(node);
			return this;
		}

	public Node() {
		super();
		// TODO Auto-generated constructor stub
	}
	 public Node(String id, String nodeName, String parentId) {
			super();
			this.id = id;
			this.nodeName = nodeName;
			this.parentId = parentId;
		}
}

2.組裝樹的節點關係

package cn.com.citydo.supervise.service.util.tree;

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

import org.apache.commons.lang.StringUtils;

public class TreeNode {
	private LinkedList<Node> list = new LinkedList<Node>();
	private Map<String, Node> nodeMap = new HashMap<String, Node>();
	
	 public LinkedList<Node> getList() {
		return list;
	}
	public void setList(LinkedList<Node> list) {
		this.list = list;
	}
	public Map<String, Node> getNodeMap() {
		return nodeMap;
	}
	public void setNodeMap(Map<String, Node> nodeMap) {
		this.nodeMap = nodeMap;
	}
	
	/**
    *組裝樹
    */
	 public TreeNode  addNode(Node node,String parentId){
		    nodeMap.put(node.getId(), node);

			if (StringUtils.isEmpty(parentId)) {
				list.add(node);
			} else {
				Node treenode = nodeMap.get(parentId);
				if (treenode == null) {
					
				} else {
					treenode.addChild(node);
				}
			}
			return this;
	 }
	
    //初始化層級
	 public void initLevel() {
		for (Node node : list) {
			initNodeLevel(node);
		}
	 }

    
     public void  initNodeLevel(Node node){
    	 
    	 String parentId = node.getParentId();
    	 if(StringUtils.isEmpty(parentId)){
    		 node.setLevel(1);
    	 }
    	 List<Node> childrenlist = node.getChildren();
    	 if(childrenlist!=null&&childrenlist.size()>0){
    		 for (Node nodechildren : childrenlist) {
    			 nodechildren.setLevel(node.getLevel()+1);
    			 List<Node> children = nodechildren.getChildren();
    			 if(children!=null&&children.size()>0){
    				 initNodeLevel(nodechildren);
    			 }
    		 }
    	 }
	 }
     
     //獲取最大層級
     public int maxLevel() {
 		int m0 = 0;
 		Iterator<Node> it = nodeMap.values().iterator();
 		while(it.hasNext()){
 			Node node = it.next();
 			int level = node.getLevel();
 			m0 = Math.max(m0, level);
 		}
 		return m0;
 	}
    
     //循環獲取當前節點層級的所有節點
 	public List<Node>  getListWithlevel(int level){
 		List<Node> list =new ArrayList<Node>();
 		Iterator<Node> it = nodeMap.values().iterator();
 		while(it.hasNext()){
 			Node node = it.next();
 			int nodelevel = node.getLevel();
 		    if(nodelevel==level){
 		    	list.add(node);
 		    }
 		}
		return list;
 	}
 	
 	/**
 	 * 循環獲取當前節點下的子節點
 	 * @param id
 	 * @return
 	 */
 	 public List<Node> getsonNode(String id){
 		Node node = nodeMap.get(id);
 		List<Node> children = node.getChildren();
 		if(children!=null&&children.size()>0){
 			return  children;
 		}else{
 			return null;
 		}
 		 
 	 } 
 	 
    public static void main(String[] args) {
    	   List<Node> nodeList = new ArrayList<Node>();
	       Node node1 = new Node("0", "蔬菜",null);
	       Node node2 = new Node("1", "蔬菜","0");
	       Node node3 = new Node("2", "蔬菜","0");
	       Node node4 = new Node("3", "蔬菜","2");
	      
	       nodeList.add(node1)   ;
	       nodeList.add(node2)   ;
	       nodeList.add(node3)   ;
	       nodeList.add(node4)   ;
	    TreeNode m = new TreeNode();   
	    for (Node node : nodeList) {
		   String parentId = node.getParentId();
		   m.addNode(node, parentId);
	   }  
	   m.initLevel();
	   int maxLevel = m.maxLevel(); 
	   for (int i = 0; i < maxLevel; i++) {
		
	   }
	}
}

3.倒序,順序遍歷層級,獲取相同層級的節點,進行業務解析,

  因爲業務需要,咬着牙寫完,對於LinkedList 的好處,好感倍增

4. 其中這種方式的好處在於LinkedList 是雙向鏈表結構,所以順序訪問會非常高效

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章