java 根據 根節點及所有子成員 構造樹tree,javatree

來源:互聯網
上載者:User

java 根據 根節點及所有子成員 構造樹tree,javatree

 

//entity

package com.ompa.biz.entity;

import java.util.ArrayList;
import java.util.List;

public class TreeEntity {

private String id;
private String name;
private String fatherId;
private int level;
private boolean isleaf;
private List<TreeEntity> childList = new ArrayList<TreeEntity>();

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFatherId() {
return fatherId;
}
public void setFatherId(String fatherId) {
this.fatherId = fatherId;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public boolean isIsleaf() {
return isleaf;
}
public void setIsleaf(boolean isleaf) {
this.isleaf = isleaf;
}
public List<TreeEntity> getChildList() {
return childList;
}
public void setChildList(List<TreeEntity> childList) {
this.childList = childList;
}

}

package com.ompa.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.ompa.biz.entity.TreeEntity;

/**
* 樹結構工具類
* <p>Title:TreeUtil </p>
* @author zhangcd
* @date 2016年11月10日
*/
public class TreeUtil {

/**
* 根據父節點,將子節點一次累計起來
* @author zhangcd
* @date 2016年11月10日上午9:40:33
* @param root
* @param childList
* @return
*/
public TreeEntity getTreeList(TreeEntity root,List<TreeEntity> childList){
root.setLevel(1);
findChildren(root,childList);
return root;
}

private List<TreeEntity> findChildren(TreeEntity root, List<TreeEntity> allNodes) {
List<TreeEntity> children = new ArrayList<TreeEntity>();

for (TreeEntity comparedOne : allNodes) {
if (comparedOne.getFatherId().equals(root.getId())) {
root.getChildList().add(comparedOne);
comparedOne.setLevel(root.getLevel() + 1);
children.add(comparedOne);
}
}
List<TreeEntity> notChildren = (List<TreeEntity>) CollectionUtils.subtract(allNodes, children);

for (TreeEntity child : children) {
List<TreeEntity> tmpChildren = findChildren(child, notChildren);
if (tmpChildren == null || tmpChildren.size() < 1) {
child.setIsleaf(true);
} else {
child.setIsleaf(false);
}
// child.setChildren(tmpChildren);
}
return children;
}

/**
* 得到深度
*
* @author zhangcd
* @date 2016-6-13
* @param rowspan
* @param step
* @return
*/
private int getrowspan(int rowspan,TreeEntity step){
if(step.getChildList() != null && step.getChildList().size()>0){
for(TreeEntity steps:step.getChildList()){
if(rowspan < steps.getLevel()){
rowspan = steps.getLevel();
}
rowspan = getrowspan(rowspan,steps);
}
}
return rowspan;
}

/**
* 合并列數
*
* @author zhangcd
* @date 2016-6-12
* @param colspan
* @param step
* @return
*/
private int getcolspan(int colspan,TreeEntity step){
if(step.getChildList() != null && step.getChildList().size()>0){
for(TreeEntity steps:step.getChildList()){
colspan = getcolspan(colspan,steps);
}
}else{
colspan ++;
}
return colspan;
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.