duannao1920 2014-03-31 10: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 12: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 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧