SoFunction
Updated on 2025-03-04

Recursive query method for Java tree structure

Recursive query of Java tree structure

Ideas

First find all the root nodes, then loop through all the root nodes, find each child node, and finally form a tree structure

    /**
      * Construct tree structure data new
      * @param org
      * @return
      */
    public List<OrgEntity> builTree(OrgEntity org){
        List<OrgEntity> treeMenus =new ArrayList<>();
		// Query all data        List<OrgEntity> reList = (org);
		// Get root node data        List<OrgEntity> rootList = (reList);
        for(OrgEntity node : rootList){
		    // Add word point data recursively to root node data            node=buildChilTree(node,reList);
            (node);
        }
        return treeMenus;
    }
    /**
      * Get the root node new
      * @param list
      * @return
      */
    public List<OrgEntity> getRootNode(List<OrgEntity> list){
        List<OrgEntity> rootMenuLists =new ArrayList
		// traverse list to get root node data        for(OrgEntity menuNode : list) {
            boolean isExit = false;
            for(OrgEntity node : list){
                if(() != null && ().equals(())){
                    isExit = true;
                    break;
                }
            }
			// If the data does not have a higher-level node, it will be placed in the root node list, otherwise it will not be placed            if(!isExit){
                (menuNode);
            }
        }
        return rootMenuLists;
    }
    //Recursively establish a subtree structure new    public OrgEntity buildChilTree(OrgEntity pNode,List<OrgEntity> list){
        List<OrgEntity> chilMenus =new  ArrayList<>();
        for(OrgEntity menuNode : list) {
            if(().equals(())) {
                (buildChilTree(menuNode,list));
            }
        }
        (chilMenus);
        return pNode;
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.