Calling a recursive function in a binary tree

I have a ft_individual table that stores information about a binary tree consisting of the attributes Id, User_name, Active (to represent the user is active or not), Position (L left, R right) and Father_id ... I want to get the number of children at the left position of the specific user, followed by the number of children at the left position of the user.

i made a recursive function call, but it doesn't work. I am using PHP codeigniter framework ...... Help

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

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

      

inside the model.

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; 
            }   
    }

      

+3


source to share


1 answer


You must call the function childCount

as a class method, just add$this->



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

      

+2


source







All Articles