[英]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.