duannao1920 2014-03-31 18:53
浏览 79
已采纳

树结构:验证树的深度级别

I've build category tree using fancytree. Deepness of each node starting from root have to equal 4 so I may have:

  • category
    • subcategory 1
      • subcategory 2
        • subcategory 4
      • subcategory 5
        • subcategory 6

Script should allow to save to database only that kind of structure. I've tried to find some methods related to depth in Fancytree API but I couldn't find proper one.

I decided to write backend validation and I have something like that:

        private function validateTree($tree)
        {
            foreach($tree as $node)
            {
                $parentName = $node['title'];
                if(isset($node['children']) and is_array($node['children']))
                {
                    if($this->validateLevel($node['children']) < 4)
                    {
                        $this->errors[] = $parentName;
                    }
                }
                else
                {
                    $this->errors[] = $parentName;
                }
            }
        }

        private function validateLevel($nodes, &$depth = 2)
        {
            foreach($nodes as $node)
            {
                if(isset($node['children']) and !empty($node['children']) and is_array($node['children']))
                {
                    $this->validateLevel($node['children'], ++$depth);
                }
                else
                {
                    return $depth;
                }
            }

        }

This don't work as expected and probably not validating every childs of each root node. Any ideas?

  • 写回答

1条回答 默认 最新

  • doujingqu3030 2014-04-08 20:33
    关注

    Looks like no one can help me (apart from pointing out typo errors) so I figured out it by myself. It's little bit ugly solution but it works like I want:

    private function validateTree($tree)
        {
            foreach($tree as $node)
            {
                $parentName = $node['title'];
                if(isset($node['children']) and is_array($node['children']))
                {
                    if((bool)$node = $this->validateLevel($node['children']))
                    {
                        $this->errors[] = $node;
                    }
                }
                else
                {
                    $this->errors[] = $node;
                }
            }
        }
    
        private function validateLevel($nodes)
        {
            foreach($nodes as $node)
            {
                if(isset($node['children']) and (bool)$node['children'] and is_array($node['children']))
                {
                    foreach($node['children'] as $nodeLvl3)
                    {
                        if(isset($nodeLvl3['children']) and (bool)$nodeLvl3['children'] and is_array($nodeLvl3['children']))
                        {
                            foreach($nodeLvl3['children'] as $nodeLbl)
                            {
                                //everything is OK
                            }
                        }
                        else
                        {
                            $this->errors[] = 'Category'.$nodeLvl3['title'].' does not have lvl 4';
                        }
                    }
                }
                else
                {
                    $this->errors[] = 'Category'.$node['title'].' does not have lvl 3';
                }
            }
        } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算