【问题标题】:recursive function call in binary tree二叉树中的递归函数调用
【发布时间】:2015-07-25 16:03:33
【问题描述】:

我有一个 ft_individual 表,它存储有关二叉树的信息,它包含属性 Id、User_name、Active(表示用户是否处于活动状态)、Position(L 代表左,R 代表右)和 Father_id... . 我想获取特定用户左侧位置的孩子数量,然后是用户左侧位置的孩子数量。

我进行了递归函数调用,但它不起作用。 我正在使用PHP codeigniter框架工作......帮助

$l_count=$this->tree_model->childCount($user_i,'L');

$r_count=$this->tree_model->childCount($user_i,'R');

内部模型。

public function childCount($user_id='',$position='')
    {     
            $this->db->select('id');
            $this->db->from('ft_individual');
            $this->db->where('father_id',$user_id);
            $this->db->where('position',$position);
            $this->db->where('active','yes');
            $result=$this->db->get();
            foreach ($result->result() as $row)
            {
               $id= $row->id;
            }  
            if($id!='')
            {   
                return (1+childCount($id,'L')+childCount($id,'R')); 
            }
           else
            {   
                return 1; 
            }   
    }

【问题讨论】:

  • 发布您的表结构以及值和预期结果
  • 请检查这个递归函数是否正确......
  • 你在结果中得到的结果发布你的结果
  • 如果你没有获得价值,你怎么能说它不起作用
  • 实际上我期望孩子的总数作为输出......

标签: php codeigniter recursion


【解决方案1】:

你应该调用函数childCount作为类的方法,只需添加$this->

return (1 + $this->childCount($id,'L') + $this->childCount($id,'R')); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    相关资源
    最近更新 更多