递归结果栈

发布于 2024-10-15 00:08:51 字数 1852 浏览 0 评论 0 原文

将递归函数的结果传回的最干净的方法是什么?

function recursion_trigger($input, $count = 0){

        if(!empty($input)){
                array_pop($input);
                $count++;
                if(!empty($input)){
                recursion_trigger($input,$count);
                }
        }

echo $count;
return $count;


}

目前它正在返回最上面的调用,这当然是一个。

/////// 作为一个附加问题,这是完整的函数,您可以在这里使用尾递归吗?输出是我在传递值时构造的数组。

<?php    
//Page best viewed from page source 

 //Takes an array of objects that have ID and Parent and organizes the tree into an array representing a set of objectID's by depth 

 // poor spelling ahead =P

function level_keys($array,$depth=-1,$level=0,$output=null){

// initialize the functions parameters run once at start and not in subsequent self calls

if($level == 0 && $depth != 0){
    $output[][]=0;
    $level++;
        foreach($array as $key=>$node){
            if($node->parent==0){
                $output[$level][] = $node->id;
                unset($array[$key]);
            }
        }
            unset($key); unset($node);
$level++;
$depth--;

}

// set recursion loop and run main part of function

if (  !empty($array) && $depth != 0){

    echo 'depth:'.$depth."\n";

    foreach($output[$level-1] as $parent){  
        foreach($array as $key=> $child){
            if( $parent == $child->parent){
            $output[$level][] = $child->id;
            unset($array[$key]);
            }
        }
    }
        unset($id); unset($parent); unset($key); unset($child);
$depth--;
$level++;
        if(!empty($array) && $depth !=0 ){
            // make sure to pass the output back out to the top most level
            $output = level_keys($array,$depth,$level,$output,$depth_at);
        }
}

return $output;

}
?>

What is the cleanest way to pass the result of this recursive function back out?

function recursion_trigger($input, $count = 0){

        if(!empty($input)){
                array_pop($input);
                $count++;
                if(!empty($input)){
                recursion_trigger($input,$count);
                }
        }

echo $count;
return $count;


}

At the moment it is returning the top most call which is of course one.

/////// as an additional question this is the full function can you use tail recursion here? The output is an array that I am constructing as I pass through the values.

<?php    
//Page best viewed from page source 

 //Takes an array of objects that have ID and Parent and organizes the tree into an array representing a set of objectID's by depth 

 // poor spelling ahead =P

function level_keys($array,$depth=-1,$level=0,$output=null){

// initialize the functions parameters run once at start and not in subsequent self calls

if($level == 0 && $depth != 0){
    $output[][]=0;
    $level++;
        foreach($array as $key=>$node){
            if($node->parent==0){
                $output[$level][] = $node->id;
                unset($array[$key]);
            }
        }
            unset($key); unset($node);
$level++;
$depth--;

}

// set recursion loop and run main part of function

if (  !empty($array) && $depth != 0){

    echo 'depth:'.$depth."\n";

    foreach($output[$level-1] as $parent){  
        foreach($array as $key=> $child){
            if( $parent == $child->parent){
            $output[$level][] = $child->id;
            unset($array[$key]);
            }
        }
    }
        unset($id); unset($parent); unset($key); unset($child);
$depth--;
$level++;
        if(!empty($array) && $depth !=0 ){
            // make sure to pass the output back out to the top most level
            $output = level_keys($array,$depth,$level,$output,$depth_at);
        }
}

return $output;

}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人练习一个人 2024-10-22 00:08:51

我想你真正需要的不是计算数组中元素的数量。

当你执行像这样的递归函数时,如果它们是尾递归的,那么对性能会有好处(事实上,我不确定 php 是否有这种优化,我希望如此)。这里有 $count 可以用作累加器,但不要使用它。

function recursion_trigger ($input, $count = 0)
{
  if (!empty($input)) {
    array_pop($input);
    return recursion_trigger($input, $count + 1);
  }
  return $count;
}

这样它就可以工作并且是尾递归的:-)。

I guess what you really need is not to count the number of elements in an array.

When you do recursive functions like this one, it is good for performance if they are tail-recursive (in fact, I'm not sure php has this optimisation, I hope so). Here you have $count that can serve as accumulator but don't use it.

function recursion_trigger ($input, $count = 0)
{
  if (!empty($input)) {
    array_pop($input);
    return recursion_trigger($input, $count + 1);
  }
  return $count;
}

This way it works and is tail recursive :-).

泅渡 2024-10-22 00:08:51

您应该使用 recursion_trigger 的返回值更新您的 $count 变量

if(!empty($input)){
    $count = recursion_trigger($input,$count);
}

编辑:

希望以下内容能够帮助您直观地了解它的工作原理:

recursion_trigger ( array("This", "Is", "A", "Sample"), 0)
  recursion_trigger ( array("This", "Is", "A"), 1)
    recursion_trigger ( array("This", "Is"), 2)
      recursion_trigger ( array("This"), 3)
        recursion_trigger ( array(), 4)

You should update your $count variable with the return value of recursion_trigger

if(!empty($input)){
    $count = recursion_trigger($input,$count);
}

EDIT:

Hopefully the following will help you visualise how it's working:

recursion_trigger ( array("This", "Is", "A", "Sample"), 0)
  recursion_trigger ( array("This", "Is", "A"), 1)
    recursion_trigger ( array("This", "Is"), 2)
      recursion_trigger ( array("This"), 3)
        recursion_trigger ( array(), 4)
岁月无声 2024-10-22 00:08:51

您的想法可能是沿着 $count 是持久的,而它不是因为按值调用。这个版本,使用参考文献,也有效。

function recursion_trigger($input, &$count = 0){

        if(!empty($input)){
                array_pop($input);
                $count++;
                if(!empty($input)){
                recursion_trigger($input,$count);
                }
        }

echo $count;
return $count;


}

The way you were thinking probably was along the lines that $count was persistent, whereas it isn't because of call by value. This version, using references, also works.

function recursion_trigger($input, &$count = 0){

        if(!empty($input)){
                array_pop($input);
                $count++;
                if(!empty($input)){
                recursion_trigger($input,$count);
                }
        }

echo $count;
return $count;


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文