簡體   English   中英

二叉樹,如何計算所有左右孩子php,mysql

[英]Binary Tree, how to Count All Left and Right Child php, mysql

表名類我向您展示數據庫截圖

我嘗試這段代碼,但輸出為0親愛的朋友,我必須數一棵二叉樹的Left Child和Right Child我的數據庫結構如下。

 function allcount($id)
{
    $sql = "SELECT parentsclass, childclassleft, childclassright FROM classes WHERE parentsclass = '".$id."'";
    $execsql = mysqli_query($conn,$sql); 
    $array = mysqli_fetch_array($execsql);  
    (array_count_values($array)); 
    $count = 0;  
    if(!empty($array['childclassleft']))
    {
        $count += allcount($array['childclassleft']) +1;
    }
    if(!empty($array['childclassright'])) 
    {
        $count += allcount($array['childclassright']) +1;
    }
    return $count; 
}

echo allcount();

我使用此代碼此代碼計數左右子節點。

$memid="95000";   // your id

function getTotalLeg($memid,$leg){ 
      global $conn;
     $sql="select child_class from tree_class where parent_id='".$memid."' and position='".$leg."'";
      $res=mysqli_query($conn,$sql);
      $row=mysqli_fetch_array($res);
      global $total;
       $total=$total+mysqli_num_rows($res);
         if($row['child_class']!=''){
           getTotalLeg ($row['child_class'],'L');
           getTotalLeg ($row['child_class'],'R');
          } 
          return $total;      
        }      

     $total=0; 
    $left=getTotalLeg($memid,"L");    
    echo $total."</br>"; 
     $total=0;
    $right=getTotalLeg($memid,"R");  
    echo $total; 

暫無
暫無

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

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