Java无限级分类递归法

递归

一种计算过程,如果其中每一步都要用到前一步或前几步的结果,称为递归的。用递归过程定义的函数,称为递归函数,例如连加、连乘及阶乘等。凡是递归的函数,都是可计算的,即能行的。

古典递归函数,是一种定义在自然数集合上的函数,它的未知值往往要通过有限次运算回归到已知值来求出,故称为“递归”。它是古典递归函数论的研究对象

近况

入手Java之后开始编写一些curd和业务的逻辑操作,近日碰到需要用到递归的方法,由于对java的一些强类型不是很熟悉,所以内事不决的时候就去度娘找了一遍,发现虽然有递归的一些demo但是都是一些很古老都不知道经过多少次复制粘贴的了。看上去也是很乱很乱,请教了某基佬之后提供了以下参考代码。

鉴于博客的百度权重和收录都不错,所以将代码贴出来方便以后有小白需要用到的,凭借着权重和收录质量,我相信这篇博文必定能名扬海外!!

TreeMain.java

package com.test.tree;

import java.util.ArrayList;
import java.util.List;

public class TreeMain {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode("1","广州","0");
        TreeNode treeNode2 = new TreeNode("2","深圳","0");

        TreeNode treeNode3 = new TreeNode("3","天河区",treeNode1);
        TreeNode treeNode4 = new TreeNode("4","越秀区",treeNode1);
        TreeNode treeNode5 = new TreeNode("5","黄埔区",treeNode1);
        TreeNode treeNode6 = new TreeNode("6","石牌",treeNode3);
        TreeNode treeNode7 = new TreeNode("7","百脑汇",treeNode6);


        TreeNode treeNode8 = new TreeNode("8","南山区",treeNode2);
        TreeNode treeNode9 = new TreeNode("9","宝安区",treeNode2);
        TreeNode treeNode10 = new TreeNode("10","科技园",treeNode8);

        List<TreeNode> list = new ArrayList<TreeNode>();

        list.add(treeNode1);
        list.add(treeNode2);
        list.add(treeNode3);
        list.add(treeNode4);
        list.add(treeNode5);
        list.add(treeNode6);
        list.add(treeNode7);
        list.add(treeNode8);
        list.add(treeNode9);
        list.add(treeNode10);

        List<TreeNode> trees = TreeBuilder.buildByRecursive(list);
        for (TreeNode treeNode : trees) {
            System.out.println(treeNode);
        }
    }
}

TreeNode.java

package com.test.tree;

import java.util.List;

public class TreeNode {
    private String id;
    private String parentId;
    private String name;
    private List<TreeNode> children;

    public TreeNode() {
    }

    public TreeNode(String id, String name, String parentId) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
    }

    public TreeNode(String id, String name, TreeNode parent) {
        this.id = id;
        this.parentId = parent.getId();
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "id=" + id +
                ", parentId='" + parentId + '\'' +
                ", name='" + name + '\'' +
                ", children=" + children +
                '}';
    }
}

TreeBuilder.java

package com.test.tree;

import java.util.ArrayList;
import java.util.List;

public class TreeBuilder {
    public static List<TreeNode> buildByRecursive(List<TreeNode> treeNodes) {
        List<TreeNode> trees = new ArrayList<TreeNode>();
        for (TreeNode treeNode : treeNodes) {
            if(0.equals(treeNode.getParentId())){
                trees.add(findChildren(treeNode,treeNodes));
            }
        }
        return trees;
    }

    public static TreeNode findChildren(TreeNode treeNode,List<TreeNode> treeNodes) {
        for (TreeNode it : treeNodes) {
            if(treeNode.getId().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<TreeNode>());
                }
                treeNode.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return treeNode;
    }
}
Comments: 1

「人生在世,留句话给我吧」

提交评论