4
4
4sadly2020-02-03 14:24:48
PHP
4sadly, 2020-02-03 14:24:48

Why does the algorithm select this cell and not another?

$tree = R::findOne('users', 'id = 1');
$parentid = 0;
function check_node($x){
    global $parentid;
    global $choosenbranch;
        $right = R::findOne('users', 'parentid = ? AND branch = 1', array($x));
        if($right){
            $choosenbranch = 0;
            check_node($right->id);
        }else{
            $parentid = $x;
        }

        $left = R::findOne('users', 'parentid = ? AND branch = 0', array($x));
        if($left){
            $choosenbranch = 1;
            check_node($left->id);
        }else{
            $parentid = $x;
        }
}
check_node($tree->id);
echo $parentid;
echo '<br>';
echo $choosenbranch;

there tree
5e381b05732ff016953451.png
$parentidshould be equal to 3, because there is free and right and left space (filling from right to left), but he chooses 4, why is that, where is the error in the logic?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2020-02-03
@politon

Literally recently I came across the need for such a solution.
Here is the most suitable one from my
mortgage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question