java对于树的构建

  1. package tree;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import org.apache.commons.collections.CollectionUtils;   
  7.   
  8.   
  9. public class TreeBuilder {   
  10.   
  11.     /**  
  12.      * 将集合建立成树结构  
  13.      *  
  14.      * @param dirs  
  15.      * @return  
  16.      */   
  17.     @SuppressWarnings("unchecked")   
  18.     private List<Node> buildListToTree(List<Node> dirs) {   
  19.         List<Node> roots = findRoots(dirs);   
  20.         List<Node> notRoots = (List<Node>) CollectionUtils.subtract(dirs, roots);   
  21.         for (Node root : roots) {   
  22.             root.setChildren(findChildren(root, notRoots));   
  23.         }   
  24.         return roots;   
  25.     }   
  26.   
  27.     /**  
  28.      * 找出集合中的根元素  
  29.      *  
  30.      * @param allDirs  
  31.      * @return  
  32.      */   
  33.     public List<Node> findRoots(List<Node> allNodes) {   
  34.         List<Node> results = new ArrayList<Node>();   
  35.         for (Node node : allNodes) {   
  36.             boolean isRoot = true;   
  37.             for (Node comparedOne : allNodes) {   
  38.                 if (node.getParentId() == comparedOne.getId()) {   
  39.                     isRoot = false;   
  40.                     break;   
  41.                 }   
  42.             }   
  43.             if (isRoot) {   
  44.                 node.setLevel(0);   
  45.                 results.add(node);   
  46.                 node.setRootId(node.getId());   
  47.             }   
  48.         }   
  49.         return results;   
  50.     }   
  51.   
  52.     /**  
  53.      * 递归找子目录  
  54.      *  
  55.      * @param root  
  56.      * @param allDirs  
  57.      * @return  
  58.      */   
  59.     @SuppressWarnings("unchecked")   
  60.     private List<Node> findChildren(Node root, List<Node> allNodes) {   
  61.         List<Node> children = new ArrayList<Node>();   
  62.   
  63.         for (Node comparedOne : allNodes) {   
  64.             if (comparedOne.getParentId() == root.getId()) {   
  65.                 comparedOne.setParent(root);   
  66.                 comparedOne.setLevel(root.getLevel() + 1);   
  67.                 children.add(comparedOne);   
  68.             }   
  69.         }   
  70.         List<Node> notChildren = (List<Node>) CollectionUtils.subtract(allNodes, children);   
  71.         for (Node child : children) {   
  72.             List<Node> tmpChildren = findChildren(child, notChildren);   
  73.             if (tmpChildren == null || tmpChildren.size() < 1) {   
  74.                 child.setLeaf(true);   
  75.             } else {   
  76.                 child.setLeaf(false);   
  77.             }   
  78.             child.setChildren(tmpChildren);   
  79.         }   
  80.         return children;   
  81.     }   
  82.       
  83.     public static void main(String[] args) {   
  84.         TreeBuilder tb = new TreeBuilder();   
  85.         List<Node> allNodes = new ArrayList<Node>();   
  86.         allNodes.add(new Node(1,0,"节点1"));   
  87.         allNodes.add(new Node(2,0,"节点2"));   
  88.         allNodes.add(new Node(3,0,"节点3"));   
  89.         allNodes.add(new Node(4,1,"节点4"));   
  90.         allNodes.add(new Node(5,1,"节点5"));   
  91.         allNodes.add(new Node(6,1,"节点6"));   
  92.         allNodes.add(new Node(7,4,"节点7"));   
  93.         allNodes.add(new Node(8,4,"节点8"));   
  94.         allNodes.add(new Node(9,5,"节点9"));   
  95.         allNodes.add(new Node(10,100,"节点10"));   
  96.         List<Node> roots = tb.buildListToTree(allNodes);   
  97.         for(Node n:roots){   
  98.             System.out.println(n);   
  99.         }   
  100.           
  101.     }   
  102. }   
  103.   
  104.   
  105.   
  106.   
  107.   
  108.   
  109. package tree;   
  110.   
  111. import java.util.List;   
  112.   
  113. public class Node implements java.io.Serializable {   
  114. private static final long serialVersionUID = -2721191232926604726L;   
  115.   
  116. private int id;   
  117.   
  118. private int parentId;   
  119.   
  120. private Node parent;   
  121.   
  122. private List<Node> children;   
  123.   
  124. private String name;   
  125.   
  126. private int level;   
  127.   
  128. private int sort;   
  129.   
  130. private int rootId;   
  131.   
  132. private String type;   
  133.   
  134. private boolean isLeaf;   
  135.   
  136. private String description;   
  137.   
  138.   
  139. public Node() {   
  140. super();   
  141. }   
  142.   
  143. public Node(int id, int parentId, String name) {   
  144. super();   
  145. this.id = id;   
  146. this.parentId = parentId;   
  147. this.name = name;   
  148. }   
  149.   
  150.   
  151. public String getDescription() {   
  152. return description;   
  153. }   
  154.   
  155. public void setDescription(String description) {   
  156. this.description = description;   
  157. }   
  158.   
  159. public int getId() {   
  160. return id;   
  161. }   
  162.   
  163. public void setId(int id) {   
  164. this.id = id;   
  165. }   
  166.   
  167. public Node getParent() {   
  168. return parent;   
  169. }   
  170.   
  171. public void setParent(Node parent) {   
  172. this.parent = parent;   
  173. }   
  174.   
  175. public int getParentId() {   
  176. return parentId;   
  177. }   
  178.   
  179. public void setParentId(int parentId) {   
  180. this.parentId = parentId;   
  181. }   
  182.   
  183. public String getName() {   
  184. return name;   
  185. }   
  186.   
  187. public void setName(String name) {   
  188. this.name = name;   
  189. }   
  190.   
  191. public int getLevel() {   
  192. return level;   
  193. }   
  194.   
  195. public void setLevel(int level) {   
  196. this.level = level;   
  197. }   
  198.   
  199. public String getType() {   
  200. return type;   
  201. }   
  202.   
  203. public List<Node> getChildren() {   
  204. return children;   
  205. }   
  206.   
  207. public void setChildren(List<Node> children) {   
  208. this.children = children;   
  209. }   
  210.   
  211. public void setType(String type) {   
  212. this.type = type;   
  213. }   
  214.   
  215. public boolean isLeaf() {   
  216. return isLeaf;   
  217. }   
  218.   
  219. public void setLeaf(boolean isLeaf) {   
  220. this.isLeaf = isLeaf;   
  221. }   
  222.   
  223. public int getSort() {   
  224. return sort;   
  225. }   
  226.   
  227. public void setSort(int sort) {   
  228. this.sort = sort;   
  229. }   
  230.   
  231.   
  232.   
  233. public int getRootId() {   
  234.         return rootId;   
  235.     }   
  236.   
  237. public void setRootId(int rootId) {   
  238.         this.rootId = rootId;   
  239.     }   
  240.   
  241.   
  242.   
  243.   
  244.   
  245.   
  246.       
  247.   
  248.     @Override   
  249.     public int hashCode() {   
  250.         final int prime = 31;   
  251.         int result = 1;   
  252.         result = prime * result + id;   
  253.         result = prime * result + parentId;   
  254.         return result;   
  255.     }   
  256.   
  257.     @Override   
  258.     public boolean equals(Object obj) {   
  259.         if (this == obj)   
  260.             return true;   
  261.         if (obj == null)   
  262.             return false;   
  263.         if (getClass() != obj.getClass())   
  264.             return false;   
  265.         Node other = (Node) obj;   
  266.         if (id != other.id)   
  267.             return false;   
  268.         if (parentId != other.parentId)   
  269.             return false;   
  270.         return true;   
  271.     }   
  272.   
  273.     @Override   
  274. public String toString() {   
  275. return "Node {id=" + id + ", parentId=" + parentId   
  276. ", children=" + children + ", name=" + name + "}";   
  277. }   
  278. }   
posted @ 2017-05-29 11:42  天涯海角路  阅读(377)  评论(0)    收藏  举报