如何使用递归实现无限极分类

c# 

        /// <summary>
        /// 递归创建小程序类目树
        /// </summary>
        public List<MiniCategorysTree> BuildMenuTree(MiniCategorysTree node)
        {
            //条件:item.parent_category_id == node.CategoryId
            List<MiniCategorysTree> childrenCategorys = MiniCateGoryList.Where(item => item.parent_category_id == node.CategoryId)
                .Select(item => new MiniCategorysTree()
                {
                    Name = item.category_name,
                    CategoryId = item.category_id,
                    HasChild = item.has_child,
                    NeedLicense = item?.need_license,
                    NeedOutDoorPic = item?.need_out_door_pic,
                    NeedSpecialLicense = item?.need_special_license,
                })
                .ToList();

            node.ChildrenCategorys = childrenCategorys;
            for (int i = 0; i < childrenCategorys.Count; i++)
            {
                //递归调用创建子节点
                if (childrenCategorys[i].HasChild == true)
                    childrenCategorys[i].ChildrenCategorys = BuildMenuTree(childrenCategorys[i]);
            }

            return childrenCategorys;
        }

在需要用到的地方调用:

MiniCategorysTree nodeRoot = new MiniCategorysTree()
                {
                    Name = "根节点",
                    CategoryId = "0",
                    HasChild = true,
                    NeedLicense = false,
                    NeedOutDoorPic = false,
                    NeedSpecialLicense = false,
                };
                
List<MiniCategorysTree> miniCategorysTree = AppService.BuildMenuTree(nodeRoot);

miniCategorysTree的 结果:

参考:https://www.cnblogs.com/xwei/p/6166320.html 

 

php

class foo{

    static $array;

    public function __construct()
    {
        self::$array = [
            ['id' => 1,'parent_id'=>0],
            ['id' => 2,'parent_id'=>1],
            ['id' => 3,'parent_id'=>1],
            ['id' => 4,'parent_id'=>1],
            ['id' => 5,'parent_id'=>1],
            ['id' => 6,'parent_id'=>0],
            ['id' => 7,'parent_id'=>5],
        ];
    }

    public static function buildTree($node){

        foreach(self::$array as $key => $item){
            if($node['nodeId'] == $item['parent_id']){
                array_push($node['childrenNodes'], [
                    'nodeId' => $item['id'],
                    'parent_id' =>$item['parent_id'],
                    'hasNode' => false,
                    'childrenNodes' => [],

                ]);

                unset(self::$array[$key]);
            }
        }

        if(count($node['childrenNodes']))  $node['hasNode'] = true;


        foreach($node['childrenNodes'] as $key => &$cNode){
            $cNode = self::buildTree($cNode);
        }

        return $node;
    }

    public function getTree()
    {
        $rootNode = [
            'nodeId' => 0,
            'parent_id' =>'-1',
            'hasNode' => false,
            'childrenNodes' => [],
        ];
        
        return  self::buildTree($rootNode);
    }

}

$foo = new foo();
print_r(json_encode($foo->getTree()));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值