php PHP二叉树递归算法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5020738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:33:44  来源:igfitidea点击:

PHP Binary Tree Recursion Algorithm

phprecursionbinary-tree

提问by jkushner

I want to create a PHP recursive program using a Binary Tree and Recursion.

我想使用二叉树和递归创建一个 PHP 递归程序。

I want to print the binary tree level by level using recursion. I want to recurse through the tree, push the node into a hashmap that has the level as the reference point.

我想使用递归逐级打印二叉树。我想通过树递归,将节点推送到以级别为参考点的哈希图中。

Here's what I have so far:

这是我到目前为止所拥有的:

$binary_tree = array(array(1 => array(2 => array(4,5),4=>array(5,6))));

            1
            |
    ------------------
    |                |
    2                4
    |                |
----------        ----------
|        |        |        |
4        5        5        6

And the end result should look like this:

最终结果应该是这样的:

$data[0] = array(1);  
$data[1] = array(2,4);  
$data[2] = array(4,5,5,6);  

I can easily loop through the array and print out the numbers, which are corresponding to the level(the array index).

我可以轻松地遍历数组并打印出与级别(数组索引)相对应的数字。

Here's the function, which is wrong, but my first shot:

这是函数,这是错误的,但我的第一个镜头:

function recurse_tree($data,$level=0){
    $final = array();
    $tmp = array()
    // first check if data is array
    if(is_array($data)){
        // loop through data 
        foreach($data as $elm){
            // push data to the tmp array
            $tmp[] = recurse_tree($elm,$level+1);
        }
        // not an array
    } else {
            // push data to the final array. can we push to the tmp array.
            array_push($final[level],$elm);
    }
    // merge final and tmp array and return
    return ($final + $tmp);
 }

I am not too experienced with recursion nor problem solving, so I wrote out the steps below which are occurring. The problem I have here now is that on step 9 I an unsure how to handle the keys 2 and 4, and then finally to handle the root node, key 1. Also, when I get to steps 5-8, im at level 4, which is correct, however according to the tree, its level 2.

我对递归和解决问题的经验都不是很丰富,所以我写出了下面正在发生的步骤。我现在遇到的问题是,在第 9 步我不确定如何处理键 2 和 4,然后最后处理根节点,键 1。此外,当我到达第 5-8 步时,我处于第 4 级,这是正确的,但是根据树,它的级别为 2。

$flattened_tree = recurse_tree($data);

// STEPS:  
1./ called recurse_tree  
    data: array(array(1 => array(2 => array(4,5),4=>array(5,6))));  
    level: 0  
    final:  array();  
    tmp:  array();  
2./ called recurse_tree:  
    data: array(1 => array(2 => array(4,5),4=>array(5,6)));  
    level: 1  
    final: array();  
    tmp: array();  
3./ called recurse_tree:  
    data: array(2 => array(4,5),4=>array(5,6));  
    level: 2
    final: array();  
    tmp: array();
4./ called recurse_tree:
    data: array(4,5)
    level: 3
    final: array();  
    tmp: array();
5./ called recurse_tree:
    data: 4
    level: 4
    final: array(4 => 4)
    tmp: array()
6./ called recurse_tree:
    data: 5
    level: 4
    final: array(4 => 5)
    tmp: array(4 => 4)
7./ called recurse_tree:
    data: 5
    level: 4
    final: array(4=> 5)
    tmp: array(4 => array(4,5))
8./ called recurse_tree:
    data: 6
    level: 4
    final: array(4 => 6)
    tmp: array(4 => array(4,5,5))         

What is the best way to implement a Binary Tree recursion algorithm?

实现二叉树递归算法的最佳方法是什么?

回答by netcoder

If I understand correctly, this is what you want:

如果我理解正确,这就是你想要的:

function tree(array $data, &$tree = array(), $level = 0) {
    // init
    if (!isset($tree[$level])) $tree[$level] = array();

    foreach ($data as $key => $value) {
        // if value is an array, push the key and recurse through the array
        if (is_array($value)) {
            $tree[$level][] = $key;
            tree($value, $tree, $level+1);
        }

        // otherwise, push the value
        else {
            $tree[$level][] = $value;
        }
    }
}

Use it like this:

像这样使用它:

$binary_tree = array(1 => array(2 => array(4,5),4=>array(5,6)));
tree($binary_tree, $output);
var_dump($output);

This gives you:

这给你:

array(3) {
  [0]=>
  array(1) {
    [0]=>
    int(1)
  }
  [1]=>
  array(2) {
    [0]=>
    int(2)
    [1]=>
    int(4)
  }
  [2]=>
  array(4) {
    [0]=>
    int(4)
    [1]=>
    int(5)
    [2]=>
    int(5)
    [3]=>
    int(6)
  }
}

回答by vinay bhardwaj

This function in class Tree view

类树视图中的此函数

you have got a return of array which has all node value then you can arrange on below tree view .

你得到了一个包含所有节点值的数组的返回,然后你可以在树视图下面进行排列。

function getTreeDataFromReg($setid)
{
    if(!empty($setid))
    {
        for($in=0 ;$in<7;$in++ )
        {
            if($setid[$in]>0)
            {
                $result=$this->selectQuery(
                    "tbl_registration"," * "," fl_reg_id ='".$setid[$in]."'",
                    " fl_placment_side ASC ");
                $setar=mysql_fetch_array($result);
                $leftid=$setar['fl_left_id'];
                $rightid=$setar['fl_right_id'];
            }else
            {
                $leftid=0;
                $rightid=0;
            }
            switch($in)
            {
                case 0: $setid[1]=$leftid;
                        $setid[2]=$rightid;
                        break;
                case 1: $setid[3]=$leftid;
                        $setid[4]=$rightid;
                        break;      
                case 2: $setid[5]=$leftid;
                        $setid[6]=$rightid;
                        break;
                case 3: $setid[7]=$leftid;
                        $setid[8]=$rightid;
                        break;  
                case 4: $setid[9]=$leftid;
                        $setid[10]=$rightid;
                        break;
                case 5: $setid[11]=$leftid;
                        $setid[12]=$rightid;
                        break;  
                case 6: $setid[13]=$leftid;
                        $setid[14]=$rightid;
                        break;                                                      
            }
        }  
    }   
    return $setid;
}   
function printTreeView($parentid)
{
    $setid=array($parentid);
    $setarra=$this->getTreeDataFromReg($setid);
    return $setarra;
}

Which creates a binary tree:

这创建了一个二叉树:

            0
         /     \
        1        2
       / \      / \
      3   4    5   6
     /\  / \  /\   /\