前言:

深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次。要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历、中序遍历、后序遍历。具体说明如下:

  • 前序遍历:根节点->左子树->右子树
  • 中序遍历:左子树->根节点->右子树
  • 后序遍历:左子树->右子树->根节点

广度优先遍历:又叫层次遍历,从上往下对每一层依次访问,在每一层中,从左往右(也可以从右往左)访问结点,访问完一层就进入下一层,直到没有结点可以访问为止。

例如对于一下这棵树:

深度优先遍历:

  • 前序遍历:10 8 7 9 12 11 13
  • 中序遍历:7 8 9 10 11 12 13
  • 后序遍历:7 9 8 11 13 12 10

广度优先遍历:

  • 层次遍历:10 8 12 7 9 11 13

二叉树的深度优先遍历的非递归的通用做法是采用栈,广度优先遍历的非递归的通用做法是采用队列。

代码示例:

 <?php
header("Content-type: text/html; charset=utf-8");
class Node
{
public $value;
public $left;
public $right; public function __construct($value)
{
$this->value = $value;
}
} class Tree
{
/**
* 先序遍历(递归方法)
*/
public function recursion_preorder($root)
{
static $res = array();
if (!is_null($root))
{
$function = __FUNCTION__;
$res[] = $root->value;
$this->$function($root->left);
$this->$function($root->right);
}
return $res;
} /**
* 中序遍历(递归方法)
*/
public function recursion_midorder($root)
{
static $res = array();
if(!is_null($root))
{
$function = __FUNCTION__;
$this->$function($root->left);
$res[] = $root->value;
$this->$function($root->right);
}
return $res;
} /**
* 后序遍历(递归方法)
*/
public function recursion_postorder($root)
{
static $res = array();
if (!is_null($root))
{
$function = __FUNCTION__;
$this->$function($root->left);
$this->$function($root->right);
$res[] = $root->value;
}
return $res;
} /**
* 先序遍历(非递归)
*/
public function preorder($node)
{
$res = array();
$stack = new splstack();
while(!is_null($node) || !$stack->isEmpty())
{
while(!is_null($node))//节点不为空就入栈
{
$stack->push($node);
$res[] = $node->value;
$node = $node->left;
}
$node = $stack->pop();
$node = $node->right;
}
return $res;
} /**
* 中序遍历(非递归)
*/
public function midorder($node)
{
$res = array();
$stack = new splstack();
while(!is_null($node) || !$stack->isEmpty())
{
while(!is_null($node))
{
$stack->push($node);
$node = $node->left;
}
$node = $stack->pop();
$res[] = $node->value;
$node = $node->right;
}
return $res;
} /**
* 后序遍历(非递归)
*/
public function postorder($node)
{
$stack = new splstack();
$outstack = new splstack(); $stack->push($node);
while(!$stack->isEmpty())
{
$center_node = $stack->pop();
$outstack->push($center_node);//最先压入根节点,最后输出
if(!is_null($center_node->left))
{
$stack->push($center_node->left);
}
if(!is_null($center_node->right))
{
$stack->push($center_node->right);
}
} $res = array();
while(!$outstack->isEmpty())
{
$node = $outstack->pop();
$res[] = $node->value;
}
return $res;
} /**
* 广度优先遍历(层次遍历、非递归)
*/
public function level_order($node)
{
$res = array();
$queue = new splqueue();
$queue->enqueue($node);
while(!$queue->isEmpty())
{
$node = $queue->dequeue();
$res[] = $node->value;
if(!is_null($node->left))
{
$queue->enqueue($node->left);
}
if(!is_null($node->right))
{
$queue->enqueue($node->right);
}
}
return $res;
}
} $a = new Node(10);
$b = new Node(8);
$c = new Node(12);
$d = new Node(7);
$e = new Node(9);
$f = new Node(11);
$g = new Node(13); $a->left = $b;
$a->right = $c;
$b->left = $d;
$b->right = $e;
$c->left = $f;
$c->right = $g; $tree = new Tree();
$res = $tree->recursion_preorder($a);
echo "先序遍历结果(递归):" . implode('-', $res) . "<br/>"; $res = $tree->preorder($a);
echo "先序遍历结果(非递归):" . implode('-', $res) . "<br/>"; $res = $tree->recursion_midorder($a);
echo "中序遍历结果(递归):" . implode('-', $res) . "<br/>"; $res = $tree->midorder($a);
echo "中序遍历结果(非递归):" . implode('-', $res) . "<br/>"; $res = $tree->recursion_postorder($a);
echo "后序遍历结果(递归):" . implode('-', $res) . "<br/>"; $res = $tree->postorder($a);
echo "后序遍历结果(非递归):" . implode('-', $res) . "<br/>"; $res = $tree->level_order($a);
echo "层次遍历结果(非递归):" . implode('-', $res) . "<br/>";

PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)的更多相关文章

  1. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  2. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. LeetCode:二叉树的前、中、后序遍历

    描述: ------------------------------------------------------- 前序遍历: Given a binary tree, return the pr ...

  4. 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现

    文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ...

  5. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  6. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  7. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  8. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

  9. 【C&数据结构】---关于链表结构的前序插入和后序插入

    刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...

  10. 【11】-java递归和非递归二叉树前序中序后序遍历

    二叉树的遍历 对于二叉树来讲最主要.最基本的运算是遍历. 遍历二叉树 是指以一定的次序访问二叉树中的每个结点.所谓 访问结点 是指对结点进行各种操作的简称.例如,查询结点数据域的内容,或输出它的值,或 ...

随机推荐

  1. GSON使用之对特殊字符的转换的处理

    很多人是在转换时特殊字符被替换成了unicode编程格式,而我碰到的类似,只不过是后台转换成json字符串到前端,前端解析时 '' 双引号和 / 斜杠被原样转换,冲突了json的关键字符,导致解析时提 ...

  2. ECharts 报表事件联动系列四:柱状图,折线图,饼状图实现联动

    代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

  3. CAD插入块后坐标不匹配

    有两张图,将一张图复制(CTRL+V),再另一张图中粘贴到原坐标(pasteorig),两张图可以很好匹配,但将一张图以外部参照的方式插入另一张图却发现图形无法匹配.因为没有看到图纸,所以我也没法准确 ...

  4. .net mvc 列名 'Discriminator' 无效

    环境:asp.net 4.0 + MVC 4 + Entity Framework 5异常:使用code first 碰到错误:列名 'Discriminator' 无效.这是一个很少见的错误,搜索了 ...

  5. 安天透过北美DDoS事件解读IoT设备安全——Mirai的主要感染对象是linux物联网设备,包括:路由器、网络摄像头、DVR设备,入侵主要通过telnet端口进行流行密码档暴力破解,或默认密码登陆,下载DDoS功能的bot,运行控制物联网设备

    安天透过北美DDoS事件解读IoT设备安全 安天安全研究与应急处理中心(安天CERT)在北京时间10月22日下午启动高等级分析流程,针对美国东海岸DNS服务商Dyn遭遇DDoS攻击事件进行了跟进分析. ...

  6. burpsuite拦截https数据包(Firefox)

    1.配置浏览器对http/https都使用burpsuite代理 http和https是分开的,对http使用了代理并不代表对https也使用了代理,要配置浏览器让其对https也使用同样的代理. 当 ...

  7. 牛客网 PAT 算法历年真题 1012 : D进制的A+B (20)

    D进制的A+B (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 输入两个非负10进制整数A和B(< ...

  8. CentOS下安装MYSQL8.X并设置忽略大小写

    安装 在官网上下载:mysql80-community-release-el7-2.noarch.rpm.安装方式与5.7基本相同.详细安装过程见:CentOS下安装mysql5.7和mysql8.x ...

  9. weex npm 报错 cb() never called!

    安装环境:windows7 使用npm 安装 出现错误后网上查找并没有解决,在准备放弃的时候试着用cnpm安装了一下,结果安装成功了,感觉应该网络问题,不知原因但完美解决

  10. linux系统管理 启停命令

    mac下Linux的登录命名 'ssh -l root 192.168.10.109' password: xxxx ​ 退出登录 >> logout shutdown命令 要使用这个命令 ...