Php 三元树的深度-传销级别
我正在用php开发一个MLM,其中每个节点只能有3个子节点。传销有N个级别。 任何父节点的级别将等于其子节点的最低级别+1。深度n处的子级的级别为1Php 三元树的深度-传销级别,php,wordpress,tree,mlm,ternary-tree,Php,Wordpress,Tree,Mlm,Ternary Tree,我正在用php开发一个MLM,其中每个节点只能有3个子节点。传销有N个级别。 任何父节点的级别将等于其子节点的最低级别+1。深度n处的子级的级别为1 function calculate_level($starId=0) { if($starId == null) return 1; $result = getAllChildren($starId); if($result && count((array)$result) == 3){ return 1 + mi
function calculate_level($starId=0) {
if($starId == null) return 1;
$result = getAllChildren($starId);
if($result && count((array)$result) == 3){
return 1 + min(calculate_level($result[0]->user_ref_id),calculate_level($result[1]->user_ref_id),calculate_level($result[2]->user_ref_id));
}else{ return 1;}
}
}
数据库布局图像
预期结果
我怎样才能做到这一点
谢谢
function level_calculator($starId = 0) {
$result = getAllParents($starId);
//now we have all parents of current star now we need to find the level for each.
foreach($result as $res){
if($res->user_status == 3 ){
//we only update level if status == 3
$level = calculate_level($res->user_ref_id);
echo $level .':' . $res->user_ref_id .',';
}
}