Java code recursive departmental structure tree

Source: Internet
Author: User

Organization of all departmental trees, and conditional Query Department tree:
    /*** Organizational Department tree *@return     */@Override PublicList<sxybranchvo>Findallbranchtree (String branchname) {SXYBRANCHVO Sxybranchvoparam=NewSXYBRANCHVO (); Sxybranchvoparam.setbranchcode (""); Sxybranchvoparam.setdeleteflag ("1"); //querying all root nodes rootList<sxybranchvo> sxybranchvolist =SXYBRANCHMAPPER.FINDPARENTBRANCHVO (Sxybranchvoparam); if(Stringutils.isnotblank (Branchname)) {//Query by department name, organize the eligible nodes into a treeSet<sxybranchvo> Branchvoset =NewHashset<>(); Set<SxyBranchVO> branchvosetsub =NewHashset<>(); //querying all child nodes that meet the criteriaList<sxybranchvo> Sxybranchvos =Sxybranchmapper.getbranchsbyname (branchname);  for(Sxybranchvo sxybranchvosub:sxybranchvos) {Sxybranchvosub.setdeleteflag ("1");                Branchvoset.add (sxybranchvosub); //recursive upward, placing the eligible node and all its parent nodes in the Set collectionBranchvosetsub =Buildtreeup (sxybranchvosub,branchvosetsub);  for(Sxybranchvo sbv:branchvosetsub) {branchvoset.add (SBV); }            }            //organization Tree, which organizes all eligible nodes into the treeList<sxybranchvo> sxybranchvolistnew =NewArraylist<>();  for(Sxybranchvo sxybranchvo:sxybranchvolist) {//Organizational tree, all departments under the root nodeSxybranchvo.setdeleteflag ("1");                Buildtree (Sxybranchvo,branchvoset); if(Sxybranchvo.getchildren ()! =NULL&& Sxybranchvo.getchildren (). Size () > 0) {sxybranchvolistnew.add (SXYBRANCHVO); }            }            returnsxybranchvolistnew; }Else{//querying all, organizing all nodes into a tree             for(Sxybranchvo sxybranchvo:sxybranchvolist) {//Organizational tree, all departments under the root nodeSxybranchvo.setdeleteflag ("1");            Buildtreeall (SXYBRANCHVO); }            returnsxybranchvolist; }    }    /*** Recursively queries all parent nodes (only one parent node per layer) *@paramSxybranchvosub *@paramBranchvosetsub *@return     */    PrivateSet<sxybranchvo> Buildtreeup (Sxybranchvo sxybranchvosub, set<sxybranchvo>branchvosetsub) {        //query all parent nodes recursivelySXYBRANCHVO SXYBRANCHVO =Sxybranchmapper.findparentbranchvobyparentcode (sxybranchvosub); if(SXYBRANCHVO! =NULL){//if not the root nodeBranchvosetsub.add (SXYBRANCHVO); Sxybranchvo.setdeleteflag ("1");        Buildtreeup (sxybranchvo,branchvosetsub); }        returnbranchvosetsub; }    /*** Recursive Query sub-node (recursive downward, organization-eligible tree) *@paramSXYBRANCHVO*/    Private voidBuildtree (SXYBRANCHVO sxybranchvo,set<sxybranchvo>Branchvoset) {List<SxyBranchVO> sxybranchvolistnew =NewArraylist<>(); //querying immediate child nodesList<sxybranchvo> sxybranchvolist =SXYBRANCHMAPPER.FINDPARENTBRANCHVO (SXYBRANCHVO);  for(Sxybranchvo sxybranch:sxybranchvolist) { for(Sxybranchvo sbr:branchvoset) {if(Sxybranch.getbranchcode (). Equals (Sbr.getbranchcode ())) {Sxybranchvolistnew.add (sxybranch);        }}} Sxybranchvo.setchildren (Sxybranchvolistnew);  for(Sxybranchvo sxybranchitem:sxybranchvolist) {Sxybranchitem.setdeleteflag ("1");        Buildtree (Sxybranchitem,branchvoset); }    }    /*** Recursive query child node (recursive down, organization of all trees) *@paramSXYBRANCHVO*/    Private voidBuildtreeall (Sxybranchvo sxybranchvo) {//querying immediate child nodesList<sxybranchvo> sxybranchvolist =SXYBRANCHMAPPER.FINDPARENTBRANCHVO (SXYBRANCHVO);        Sxybranchvo.setchildren (sxybranchvolist);  for(Sxybranchvo sxybranchitem:sxybranchvolist) {Sxybranchitem.setdeleteflag ("1");        Buildtreeall (Sxybranchitem); }    }

Comment out the code for reference:

/* Private Boolean Buildtreematch (SXYBRANCHVO sxybranchvo,list<sxybranchvo> sxybranchvos) {    Boolean Flag = false;    Query immediate child node    list<sxybranchvo> sxybranchvolist = SXYBRANCHMAPPER.FINDPARENTBRANCHVO (SXYBRANCHVO);    for (Sxybranchvo Sb:sxybranchvos) {for        (Sxybranchvo sbv:sxybranchvolist) {            if (Sb.getbranchcode (). Equals ( Sbv.getbranchcode ())) {                return true;            }        }    }    if (!flag) {        sxybranchvo.setchildren (sxybranchvolist);        for (Sxybranchvo sxybranchitem:sxybranchvolist) {            Sxybranchitem.setdeleteflag ("1");            Buildtreematch (Sxybranchitem,sxybranchvos);        }    }    return flag;} */

Java code recursive departmental structure tree

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.