二叉树

时间:2015-05-14 06:16:02

标签: php codeigniter recursion

我有一个表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 个答案:

答案 0 :(得分:2)

您应该将函数childCount称为类的方法,只需添加$this->

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