Depth of ternary tree - MLM Level

281 Views Asked by At

I am working on a MLM in php where every node can have only 3 children. the MLM have N Levels. the Level of any parent node will be equal to the lowest level of its child node + 1. child at depth n has Level = 1.

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 .',';


    }
}

}

database layout image.

enter image description here

Expected Result

enter image description here

How can i achieve this.

Thanks.

0

There are 0 best solutions below