如何将二叉树存储为一维数组?

时间:2019-01-27 07:28:26

标签: php binary-tree

如何将数据构造成二叉树排序以输出一维数组?

现在,我已将数据构造为二叉树,如何使用以下代码和数据将二叉树作为一维数组递归求解:

数据

$nodes = array(8,3,10,1,6,14,4,7,13);

构造二叉树代码

function insertNode($node,$newNode){

    //var_dump($node);
    //var_dump($newNode);
    //exit;
    if ($node['key'] < $newNode['key']){

        if (empty($node['right'])){
            $node['right'] = $newNode;
        }else{
            $node['right'] = insertNode($node['right'],$newNode);
        }
    }elseif ($node['key'] > $newNode['key']){

        if (empty($node['left'])){
            $node['left'] = $newNode;
        }else{
            $node['left'] = insertNode($node['left'],$newNode);
        }
    }

    return $node;
}

function tree($nodes)
{
    $node = [
        'key' => '',
        'left' => '',
        'right' => ''
    ];
    $newNode = [
        'key' => '',
        'left' => '',
        'right'=> ''
    ];

    foreach ($nodes as $key => $value){
        //insert($value,$key);
        if($key == 0)
        {
            $node['key'] = $value;  
            continue;
        }

        $newNode['key'] = $value;
        //Constructing a binary tree
        $node = insertNode($node,$newNode);

    }

    //Recursive solution
    $node = midSortNode($node);

    return $node;
}


var_dump(tree($nodes));

以下是我构造的二叉树

array (size=3)
  'key' => int 8
  'left' => 
    array (size=3)
      'key' => int 3
      'left' => 
        array (size=3)
          'key' => int 1
          'left' => string '' (length=0)
          'right' => string '' (length=0)
      'right' => 
        array (size=3)
          'key' => int 6
          'left' => 
            array (size=3)
              ...
          'right' => 
            array (size=3)
              ...
  'right' => 
    array (size=3)
      'key' => int 10
      'left' => string '' (length=0)
      'right' => 
        array (size=3)
          'key' => int 14
          'left' => 
            array (size=3)
              ...
          'right' => string '' (length=0)

我需要将二叉树递归地分类为一个有序的一维数组。

我的代码如下

function midSortNode($node){
    $sortArr = [];
    if (!empty($node)){
        $sortArr[] = midSortNode($node['left']);
        //$sortArr['left'] = midSortNode($node['left']);

        array_push($sortArr,$node['key']);

        $sortArr[] = midSortNode($node['right']);
        //$sortArr['right'] = midSortNode($node['right']);
    }

    return $sortArr;
}

var_dump(midSortNode($node));

这是结果,但不是我想要的

  0 => 
    array (size=3)
      0 => 
        array (size=3)
          0 => 
            array (size=0)
              ...
          1 => int 1
          2 => 
            array (size=0)
              ...
      1 => int 3
      2 => 
        array (size=3)
          0 => 
            array (size=3)
              ...
          1 => int 6
          2 => 
            array (size=3)
              ...
  1 => int 8
  2 => 
    array (size=3)
      0 => 
        array (size=0)
          empty
      1 => int 10
      2 => 
        array (size=3)
          0 => 
            array (size=3)
              ...
          1 => int 14
          2 => 
            array (size=0)
              ...

如何解决二叉树如下

array (size=9)
  0 => int 1
  1 => int 3
  2 => int 4
  3 => int 6
  4 => int 7
  5 => int 8
  6 => int 10
  7 => int 13
  8 => int 14

1 个答案:

答案 0 :(得分:1)

我假设您对到目前为止的步骤感到满意,因此主要代码不变。我想您需要做的就是将最终树中的数据提取到一维数组中。由于项目都是按顺序排列的所有叶节点,因此您只需使用array_walk_recursive()遍历所有节点并将它们添加到新数组中即可。

$tree = tree($nodes);
array_walk_recursive( $tree, 
    function ($data) use (&$output) { $output[] = $data;} );
print_r($output);

给予...

Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 6
    [4] => 7
    [5] => 8
    [6] => 10
    [7] => 13
    [8] => 14
)

编辑: 要更新现有代码以执行此操作,可以将midSortNode()更改为在输出列表中传递,而仅添加当前节点...

function midSortNode($node, $sortArr = []){
    if (!empty($node)){
        $sortArr = midSortNode($node['left'], $sortArr);
        $sortArr[] = $node['key'];
        $sortArr = midSortNode($node['right'], $sortArr);
    }
    return $sortArr;
}