二叉树遍历算法

云晨同学_4363

云晨同学_4363

2016-06-05

1365人浏览

原创

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	二叉树遍历,是值从根节点出发,按照某种次序依次访问二叉树中的所有节点,使得每个节点被访问一次且仅被访问依次。
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;text-align:center;">
	<img src="http://lanecn-upload.stor.sinaapp.com/image/20140709_1404896527_874807.gif?x-oss-process=image/resize,p_40" title="20140709_1404896527_874807.gif" alt="tupan062.gif"  style="max-width:90%"></p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;text-align:center;">
	图是百度搜的。。。谢谢提供图的英雄。。
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    前序遍历二叉树:如果二叉树为空则返回,若二叉树非空,则先遍历左树,再遍历右树,遍历顺序为ABCDEGF。
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    中序遍历二叉树:如果二叉树为空则返回,若二叉树非空,则从根节点开始,中序遍历根节点的左子树,然后是访问根节点,最后中序遍历右子树,遍历顺序为CBEGDFA。
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    后序遍历二叉树:如果二叉树为空则返回,若二叉树非空,则从左到右先叶子后节点的访问遍历访问左右子树,最后是访问根节点。访问顺序为CGEFDBA。
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    层序遍历二叉树:如果二叉树为空则返回,若二叉树非空,则从树的第一层,也就是根节点开始访问,从上而下逐层遍历,在同一层中,按照从左到右的顺序对节点逐个访问。访问顺序为ABCDEFG。
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    现在,我们用PHP代码,来遍历二叉树结构。二叉树是放一个大数组,每一个节点都有三个字段,data表示这个节点的值,lChild表示这个节点的左边子节点,rChild表示这个节点的右边子节点。二叉树的结构我们用上面那张图。
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	二叉树结构代码如下:
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>

<pre class="prettyprint linenums" style="box-sizing:border-box;overflow:auto;font-family:Menlo, Monaco, Consolas, 'Courier New', monospace;padding:9.5px;margin-top:0px;margin-bottom:10px;line-height:1.42857;word-break:break-all;word-wrap:break-word;border:1px solid #CCCCCC;border-radius:4px;background-color:#F5F5F5;">
<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<?php </p></p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	//二叉树
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	$arr = array(
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    'data' => 'A',
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    'lChild' => array(
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	        'data' => 'B',
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	        'lChild' => array(
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	            'data' => 'C',
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	            'lChild' => array(),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	            'rChild' => array(),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	        ),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	        'rChild' => array(
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	            'data' => 'D',
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	            'lChild' => array(
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                'data' => 'E',
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                'lChild' => array(),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                'rChild' => array(
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                    'data' => 'G',
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                    'lChild' => array(),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                    'rChild' => array(),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                ),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	            ),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	            'rChild' => array(
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                'data' => 'F',
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                'lChild' => array(),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	                'rChild' => array(),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	            ),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	        ),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    ),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    'rChild' => array(),
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>



遍历算法一:前序遍历二叉树


<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<?php </p></p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	//前序遍历二叉树算法
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	echo '前序遍历二叉树算法:';
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	PreOrderTraverse($arr);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	echo '<br>';
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	function PreOrderTraverse($node){
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    if(empty($node)){
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	        return;
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    }
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //输出值
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    print_r($node['data']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //左节点
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    PreOrderTraverse($node['lChild']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //右节点
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    PreOrderTraverse($node['rChild']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	}
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>



遍历算法二:中序遍历二叉树


<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<?php </p></p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	//中序遍历二叉树算法
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	echo '中序遍历二叉树算法:';
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	inOrderTraverse($arr);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	echo '<br>';
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	function inOrderTraverse($node){
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    if(empty($node)){
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	        return;
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    }
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //左节点
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    inOrderTraverse($node['lChild']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //输出值
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    print_r($node['data']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //右节点
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    inOrderTraverse($node['rChild']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	}
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>



遍历算法三:后序遍历二叉树


<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	<br></p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;white-space:normal;">
	<?php </p></p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	//后序遍历二叉树算法
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	echo '后序遍历二叉树算法:';
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	postOrderTraverse($arr);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	echo '<br>';
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	function postOrderTraverse($node){
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    if(empty($node)){
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	        return;
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    }
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //左节点
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    postOrderTraverse($node['lChild']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //右节点
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    postOrderTraverse($node['rChild']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    //输出值
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	    print_r($node['data']);
</p>

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:10px;">
	}
</p>
PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

20

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

60

15

GoFrame框架零基础入门
GoFrame框架零基础入门

本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

2026.11.17

60

15

SQLAlchemy 一对多关系怎么建立
SQLAlchemy 一对多关系怎么建立

SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

2026.11.17

20

10

SQLAlchemy 如何增删改查
SQLAlchemy 如何增删改查

SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

2026.11.17

60

11

SQLAlchemy ORM 模型怎么写
SQLAlchemy ORM 模型怎么写

SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

2026.11.17

40

16

Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

180

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

140

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

160

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.3万人学习