The parent-child relationship using the recursive data into a tree structure of an object

Practical work, often encountered in the database table you want to have a parent-child relationship turn into Java objects, here to the administrative structure of the company, for example, using recursive algorithm to solve the problem with this scenario, there is something wrong, I also want to criticize correction.

data preparation

For simplicity, the database will engage, making simulated data directly in the code, wherein the TreeNode class has written on the back. as follows:

list.add ( new new TreeNode (0, null , "large group" )); 
list.add ( new new TreeNode (1, 0, "technology companies" )); 
list.add ( new new TreeNode (2, 0, "Culture Media company " )); 
list.add ( new new TreeNode (3, 0," Wuchang Office " )); 
list.add ( new new TreeNode (4, 0," Hankou Office " )); 
list.add ( new new TreeNode ( 5, 1, "R & D Center" )); 
List.add ( new new the TreeNode (. 6, an "administrative unit" )); 
List.add ( new new the TreeNode (. 7, 5, "technology" )); 
List.add ( new new TreeNode (8, 5, "Products " ));
List.add ( new new the TreeNode (. 9,. 5, "the operation and maintenance unit" )); 
List.add ( new new the TreeNode (10, 2, "writing portion" )); 
List.add ( new new the TreeNode (. 11, 2, "Administrative unit"));

Code

Not much to say, directly on the code is relatively simple.

TreeNode class representing data node:

@Getter
@Setter
public class TreeNode {

    private Integer id;
    private Integer parentId;
    private String name;
    private List<TreeNode> childNodes;

    public TreeNode(Integer id, Integer parentId, String name) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
        this.childNodes = new ArrayList<>();
    }
}

TreeNodeUtils class, the main achievement of logic in there:

public class TreeNodeUtils {

    private List<TreeNode> treeNodeList;

    public TreeNodeUtils(List<TreeNode> treeNodeList) {
        this.treeNodeList = treeNodeList;
    }

    /**
     * 生成以id为根节点的树
     *
     * @param id
     * @return
     */
    public TreeNode generateTree(int id) {
        TreeNode root = getById(id);
        List<TreeNode> childNodes = getChildrenById(id);
        if (childNodes != nullChildNodes.size && ()> 0 ) {
             for (the TreeNode Node: the childNodes) {
                 // child node as the root, the child node's child find 
                the TreeNode childRoot = generateTree (node.getId ()); 
                root.getChildNodes (). the Add (childRoot); 
            } 
        } 

        return the root; 
    } 

    / ** 
     * The find node id 
     * 
     * @param id 
     * @return 
     * / 
    Private the TreeNode getById ( int id) {
         for (the TreeNode node: treeNodeList) {
             IF (node.getId () == id) {
                return node;
            }
        }

        return null;
    }

    /**
     * 根据id查找所有的子节点
     *
     * @param id
     * @return
     */
    private List getChildrenById(int id) {
        List<TreeNode> childNodes = new ArrayList<>();
        for (TreeNode node : treeNodeList) {
            if (node.getParentId() != null && node.getParentId() == id) {
                childNodes.add(node);
            }
        }

        return childNodes;
    }
}

test

The above-prepared data as the data source test:

public class TreeNodeTest {

    public static void main(String[] args) {
        List<TreeNode> data = initData();
        TreeNodeUtils treeNodeUtils = new TreeNodeUtils(data);
        TreeNode root = treeNodeUtils.generateTree(0);
        System.out.println(JSON.toJSONString(root));
    }

    private static List<TreeNode> initData() {
        List<TreeNode> list = new ArrayList<>();
        list.add(new TreeNode(0, null, "大集团")); 
        List.add ( new new TreeNode (1, 0, "technology companies" )); 
        list.add ( new new TreeNode (2, 0, "the media company culture" )); 
        list.add ( new new TreeNode (3, 0 "Wuchang Office" )); 
        List.add ( new new the TreeNode (. 4, 0, "Hankou Office" )); 
        List.add ( new new the TreeNode (. 5,. 1, "R & D Center" )); 
        List.add ( new new the TreeNode (. 6,. 1, "administrative unit" )); 
        List.add ( new new the TreeNode (. 7,. 5, "technology" )); 
        List.add ( new new the TreeNode (. 8,. 5, "products" )); 
        list.add(newTreeNode (9, 5, "operation and maintenance unit" )); 
        List.add ( new new the TreeNode (10, 2, "writing portion" )); 
        List.add ( new new the TreeNode (. 11, 2, "Administration" ));
         return List; 
    } 
}

Results of the final look, the Json formatted as follows:

{
    "childNodes":[
        {
            "childNodes":[
                {
                    "childNodes":[
                        {
                            "childNodes":[],
                            "id":7,
                            "name":"技术部",
                            "parentId":5
                        },
                        {
                            "childNodes":[],
                            "id":8,
                            "name":"产品部",
                            "parentId":5
                        },
                        {
                            "childNodes":[],
                            "id":9,
                            "name":"运维部",
                            "parentId":5
                        }
                    ],
                    "id":5,
                    "name":"研发中心",
                    "parentId":1
                },
                {
                    "childNodes":[],
                    "id":6,
                    "name":"行政部",
                    "parentId":1 "name": "Technology Company",
            "the above mentioned id": 1
            ],
                }
            ,
            "parentId":0
        },
        {
            "childNodes":[
                {
                    "childNodes":[],
                    "id":10,
                    "name":"创作部",
                    "parentId":2
                },
                {
                    "childNodes":[],
                    "id":11,
                    "name":"行政部",
                    "parentId":2
                }
            ],
            "id":2,
            "name ":" Culture Media Company " 
        }," the parentId ": 0,
            
        {
            "the childNodes" : [],
             "ID":. 3 ,
             "name": "Wuchang office" ,
             "the parentId": 0 
        }, 
        {
             "the childNodes" : [],
             "ID":. 4 ,
             "name": "Hankou office " ,
             " the parentId ": 0 
        } 
    ],
     " the above mentioned id ": 0 ,
     " name ":" large group " 
}

 

Guess you like

Origin www.cnblogs.com/zhou-920644981/p/11329095.html