gpt4 book ai didi

php - 二叉树,如何统计所有的左右子节点 php, mysql

转载 作者:行者123 更新时间:2023-11-30 21:43:34 24 4
gpt4 key购买 nike

Table name classes I show you database screenshot

我正在尝试这段代码,但输出为 0亲爱的 friend ,我必须计算二叉树的左 child 和右 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;

关于php - 二叉树,如何统计所有的左右子节点 php, mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50526436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com