在二叉树中的递归函数调用

3
我有一张名为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; 
            }   
    }

1
请发布您的表结构,包括值和预期结果。 - Narendrasingh Sisodia
请检查这个递归函数是否正确。 - Vishnu jith
提交结果后,您将获得什么。 - Narendrasingh Sisodia
那么如果你没有得到返回值,你怎么能说它不起作用呢? - Narendrasingh Sisodia
实际上,我期望输出子元素的总数。 - Vishnu jith
1个回答

2
你应该将函数childCount作为类的方法进行调用,只需添加$this->即可。
return (1 + $this->childCount($id,'L') + $this->childCount($id,'R')); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接