奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

php語言

php如何實(shí)現(xiàn)的二叉樹遍歷(示例)

時(shí)間:2025-02-07 16:02:29 php語言 我要投稿
  • 相關(guān)推薦

php如何實(shí)現(xiàn)的二叉樹遍歷(示例)

  本文主要介紹了php實(shí)現(xiàn)的二叉樹遍歷算法,結(jié)合具體實(shí)例形式分析了php針對(duì)二叉樹的常用前序、中序及后序遍歷算法實(shí)現(xiàn)技巧,需要的朋友可以參考一下!想了解更多相關(guān)信息請(qǐng)持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  創(chuàng)建的二叉樹如下圖所示

  php代碼如下所示:

  <?php

  class Node {

  public $value;

  public $child_left;

  public $child_right;

  }

  final class Ergodic {

  //前序遍歷:先訪問根節(jié)點(diǎn),再遍歷左子樹,最后遍歷右子樹;并且在遍歷左右子樹時(shí),仍需先遍歷根節(jié)點(diǎn),然后訪問左子樹,最后遍歷右子樹

  public static function preOrder($root){

  $stack = array();

  array_push($stack, $root);

  while(!empty($stack)){

  $center_node = array_pop($stack);

  echo $center_node->value . ' ';

  //先把右子樹節(jié)點(diǎn)入棧,以確保左子樹節(jié)點(diǎn)先出棧

  if($center_node->child_right != null) array_push($stack, $center_node->child_right);

  if($center_node->child_left != null) array_push($stack, $center_node->child_left);

  }

  }

  //中序遍歷:先遍歷左子樹、然后訪問根節(jié)點(diǎn),最后遍歷右子樹;并且在遍歷左右子樹的時(shí)候。仍然是先遍歷左子樹,然后訪問根節(jié)點(diǎn),最后遍歷右子樹

  public static function midOrder($root){

  $stack = array();

  $center_node = $root;

  while (!empty($stack) || $center_node != null) {

  while ($center_node != null) {

  array_push($stack, $center_node);

  $center_node = $center_node->child_left;

  }

  $center_node = array_pop($stack);

  echo $center_node->value . ' ';

  $center_node = $center_node->child_right;

  }

  }

  //后序遍歷:先遍歷左子樹,然后遍歷右子樹,最后訪問根節(jié)點(diǎn);同樣,在遍歷左右子樹的時(shí)候同樣要先遍歷左子樹,然后遍歷右子樹,最后訪問根節(jié)點(diǎn)

  public static function endOrder($root){

  $push_stack = array();

  $visit_stack = array();

  array_push($push_stack, $root);

  while (!empty($push_stack)) {

  $center_node = array_pop($push_stack);

  array_push($visit_stack, $center_node);

  //左子樹節(jié)點(diǎn)先入$pushstack的棧,確保在$visitstack中先出棧

  if ($center_node->child_left != null) array_push($push_stack, $center_node->child_left);

  if ($center_node->child_right != null) array_push($push_stack, $center_node->child_right);

  }

  while (!empty($visit_stack)) {

  $center_node = array_pop($visit_stack);

  echo $center_node->value . ' ';

  }

  }

  }

  //創(chuàng)建二叉樹

  $a = new Node();

  $b = new Node();

  $c = new Node();

  $d = new Node();

  $e = new Node();

  $f = new Node();

  $g = new Node();

  $h = new Node();

  $i = new Node();

  $a->value = 'A';

  $b->value = 'B';

  $c->value = 'C';

  $d->value = 'D';

  $e->value = 'E';

  $f->value = 'F';

  $g->value = 'G';

  $h->value = 'H';

  $i->value = 'I';

  $a->child_left = $b;

  $a->child_right = $c;

  $b->child_left = $d;

  $b->child_right = $g;

  $c->child_left = $e;

  $c->child_right = $f;

  $d->child_left = $h;

  $d->child_right = $i;

  //前序遍歷

  Ergodic::preOrder($a); //結(jié)果是:A B D H I G C E F

  echo '<br/>';

  //中序遍歷

  Ergodic::midOrder($a); //結(jié)果是: H D I B G A E C F

  echo '<br/>';

  //后序遍歷

  Ergodic::endOrder($a); //結(jié)果是: H I D G B E F C A


【php如何實(shí)現(xiàn)的二叉樹遍歷(示例)】相關(guān)文章:

PHP使用遞歸算法無限遍歷數(shù)組示例05-20

關(guān)于php中hashtable實(shí)現(xiàn)示例08-02

PHP中多態(tài)如何實(shí)現(xiàn)09-04

php如何實(shí)現(xiàn)快速排序09-18

如何用PHP實(shí)現(xiàn)找回密碼11-11

php如何實(shí)現(xiàn)驗(yàn)證碼06-13

PHP弱類型變量是如何實(shí)現(xiàn)的05-31

如何實(shí)現(xiàn)PHP圖片裁剪與縮放07-13

PHP中如何實(shí)現(xiàn)crontab代碼05-30

主站蜘蛛池模板: 桑日县| 泰宁县| 渭源县| 泸定县| 大庆市| 军事| 碌曲县| 桐庐县| 炎陵县| 宁海县| 吉安县| 凌云县| 宜兰市| 商河县| 蓬溪县| 绥化市| 修武县| 邵东县| 阆中市| 南宫市| 鄂伦春自治旗| 腾冲县| 会东县| 集安市| 宿松县| 南城县| 巩义市| 若羌县| 房产| 抚宁县| 红桥区| 巴青县| 普格县| 南安市| 新宾| 裕民县| 盐城市| 巴彦淖尔市| 通化市| 阳城县| 托克逊县|