【发布时间】:2014-12-05 11:16:05
【问题描述】:
我遇到了一些情况,我必须计算二叉树的左子和右子我的数据库结构如下。
SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'"
其中 rid = 推荐 id 而 pid = 父 id,
例如,我需要计算给定父 ID 的所有叶子
如果 id 1 有左 2 和右 3 直接孩子,我需要知道左成员总数和右成员总数。
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ \
8 9 11
/ \
10 12
/ \
13 14
我需要计算所有 1 的孩子。 我正在使用这个功能,但它只计算最左边的,请修改它或解释你自己的
function leftcount($id) //Function to calculate leftcount
{
$sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
$execsql = mysql_query($sql);
$array = mysql_fetch_array($execsql);
//var_dump($array);
(array_count_values($array));
if(!empty($array['l_mem']))
{
$count += leftcount($array['l_mem']);
}
$totalcount = 1 + $count;
return $totalcount ;
}
$left = leftcount($id);
doing -1 because in function 1 + $count.
$left = $left-1;
如果您没有解决方案,请不要标记重复或任何其他内容
【问题讨论】:
标签: php mysql tree binary-tree