php编程--二叉树遍历算法实现

今天使用php来实现二叉树的遍历

创建的二叉树如下图所示

php代码如下所示:

  1.  
    <?php
  2.  
    class Node {
  3.  
    public $value;
  4.  
    public $child_left;
  5.  
    public $child_right;
  6.  
    }
  7.  
    final class Ergodic {
  8.  
    //前序遍历:先访问根节点,再遍历左子树,最后遍历右子树;并且在遍历左右子树时,仍需先遍历根节点,然后访问左子树,最后遍历右子树
  9.  
    public static function preOrder($root){
  10.  
    $stack = array();
  11.  
    array_push($stack, $root);
  12.  
    while(!empty($stack)){
  13.  
    $center_node = array_pop($stack);
  14.  
    echo $center_node->value . ' ';
  15.  
     
  16.  
    //先把右子树节点入栈,以确保左子树节点先出栈
  17.  
    if($center_node->child_right != null) array_push($stack, $center_node->child_right);
  18.  
    if($center_node->child_left != null) array_push($stack, $center_node->child_left);
  19.  
    }
  20.  
    }
  21.  
    //中序遍历:先遍历左子树、然后访问根节点,最后遍历右子树;并且在遍历左右子树的时候。仍然是先遍历左子树,然后访问根节点,最后遍历右子树
  22.  
    public static function midOrder($root){
  23.  
    $stack = array();
  24.  
    $center_node = $root;
  25.  
    while (!empty($stack) || $center_node != null) {
  26.  
    while ($center_node != null) {
  27.  
    array_push($stack, $center_node);
  28.  
    $center_node = $center_node->child_left;
  29.  
    }
  30.  
     
  31.  
    $center_node = array_pop($stack);
  32.  
    echo $center_node->value . ' ';
  33.  
     
  34.  
    $center_node = $center_node->child_right;
  35.  
    }
  36.  
    }
  37.  
    //后序遍历:先遍历左子树,然后遍历右子树,最后访问根节点;同样,在遍历左右子树的时候同样要先遍历左子树,然后遍历右子树,最后访问根节点
  38.  
    public static function endOrder($root){
  39.  
    $push_stack = array();
  40.  
    $visit_stack = array();
  41.  
    array_push($push_stack, $root);
  42.  
     
  43.  
    while (!empty($push_stack)) {
  44.  
    $center_node = array_pop($push_stack);
  45.  
    array_push($visit_stack, $center_node);
  46.  
    //左子树节点先入$pushstack的栈,确保在$visitstack中先出栈
  47.  
    if ($center_node->child_left != null) array_push($push_stack, $center_node->child_left);
  48.  
    if ($center_node->child_right != null) array_push($push_stack, $center_node->child_right);
  49.  
    }
  50.  
     
  51.  
    while (!empty($visit_stack)) {
  52.  
    $center_node = array_pop($visit_stack);
  53.  
    echo $center_node->value . ' ';
  54.  
    }
  55.  
    }
  56.  
    }
  57.  
     
  58.  
     
  59.  
    //创建二叉树
  60.  
    $a = new Node();
  61.  
    $b = new Node();
  62.  
    $c = new Node();
  63.  
    $d = new Node();
  64.  
    $e = new Node();
  65.  
    $f = new Node();
  66.  
    $g = new Node();
  67.  
    $h = new Node();
  68.  
    $i = new Node();
  69.  
     
  70.  
    $a->value = 'A';
  71.  
    $b->value = 'B';
  72.  
    $c->value = 'C';
  73.  
    $d->value = 'D';
  74.  
    $e->value = 'E';
  75.  
    $f->value = 'F';
  76.  
    $g->value = 'G';
  77.  
    $h->value = 'H';
  78.  
    $i->value = 'I';
  79.  
     
  80.  
    $a->child_left = $b;
  81.  
    $a->child_right = $c;
  82.  
    $b->child_left = $d;
  83.  
    $b->child_right = $g;
  84.  
    $c->child_left = $e;
  85.  
    $c->child_right = $f;
  86.  
    $d->child_left = $h;
  87.  
    $d->child_right = $i;
  88.  
     
  89.  
     
  90.  
    //前序遍历
  91.  
    Ergodic::preOrder($a); //结果是:A B D H I G C E F
  92.  
    echo '<br/>';
  93.  
    //中序遍历
  94.  
    Ergodic::midOrder($a); //结果是: H D I B G A E C F
  95.  
    echo '<br/>';
  96.  
    //后序遍历
  97.  
    Ergodic::endOrder($a); //结果是: H I D G B E F C A

猜你喜欢

转载自www.cnblogs.com/phpfeng/p/9374278.html