Java constructs a tree based on the root node and all sub-members. javatree

Source: Internet
Author: User

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;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.