Java constructs a tree based on the root node and all sub-members. 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;
/**
* Tree structure Tool
* <P> Title: TreeUtil </p>
* @ Author zhangcd
* @ Date January 1, November 10, 2016
*/
Public class TreeUtil {
/**
* Based on the parent node, the child nodes are accumulated once.
* @ Author zhangcd
* @ Date 9:40:33 AM, January 1, November 10, 2016
* @ 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;
}
/**
* Get the depth
*
* @ 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;
}
/**
* Merged Columns
*
* @ 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;
}
}