php編程--二叉樹遍歷算法實現

今天使用php來實現二叉樹的遍歷php

建立的二叉樹以下圖所示node

php代碼以下所示:spa

 

  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
相關文章
相關標籤/搜索