Analytic hierarchy tree

1. Create a node object 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. Assemble the tree node relationship

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. reverse order traverse the hierarchy, the same level of the node acquired, for parsing service,

  Because the business needs, finish grinding his teeth, the LinkedList for benefits, goodwill doubled

4. The advantage of this embodiment is that wherein LinkedList is doubly linked list structure, it will be very efficient sequential access

  

Guess you like

Origin blog.csdn.net/yss1019/article/details/85283496