簡體   English   中英

二叉樹中的遞歸函數調用

[英]recursive function call in binary tree

我有一個表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; 
            }   
    }

您應該將函數childCount作為類的方法調用,只需添加$this->

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM