PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)
前言:
深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次。要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历、中序遍历、后序遍历。具体说明如下:
- 前序遍历:根节点->左子树->右子树
- 中序遍历:左子树->根节点->右子树
- 后序遍历:左子树->右子树->根节点
广度优先遍历:又叫层次遍历,从上往下对每一层依次访问,在每一层中,从左往右(也可以从右往左)访问结点,访问完一层就进入下一层,直到没有结点可以访问为止。
例如对于一下这棵树:

深度优先遍历:
- 前序遍历: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实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)的更多相关文章
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- LeetCode:二叉树的前、中、后序遍历
描述: ------------------------------------------------------- 前序遍历: Given a binary tree, return the pr ...
- 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现
文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树: SDUT 1489 Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t& ...
- 【C&数据结构】---关于链表结构的前序插入和后序插入
刷LeetCode题目,需要用到链表的知识,忽然发现自己对于链表的插入已经忘得差不多了,以前总觉得理解了记住了,但是发现真的好记性不如烂笔头,每一次得学习没有总结输出,基本等于没有学习.连复盘得机会都 ...
- 【11】-java递归和非递归二叉树前序中序后序遍历
二叉树的遍历 对于二叉树来讲最主要.最基本的运算是遍历. 遍历二叉树 是指以一定的次序访问二叉树中的每个结点.所谓 访问结点 是指对结点进行各种操作的简称.例如,查询结点数据域的内容,或输出它的值,或 ...
随机推荐
- Hadoop介绍-4.Hadoop中NameNode、DataNode、Secondary、NameNode、JobTracker TaskTracker
Hadoop是一个能够对大量数据进行分布式处理的软体框架,实现了Google的MapReduce编程模型和框架,能够把应用程式分割成许多的 小的工作单元,并把这些单元放到任何集群节点上执行.在MapR ...
- jackson实体转json时 为NULL不参加序列化的汇总
首先加入依赖 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson ...
- python爬虫---requests库的用法
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...
- Python 正则实现计算器
# !/usr/bin/env/ python3 # -*- coding: utf-8 -*- """用户输入计算表达式,显示计算结果""" ...
- 循环中点击单个事件(巧用this,指向当前对象)
<em id='show' value="<?php echo $member['phone']; ?>" class="sui">&l ...
- Qt中漂亮的几款QSS
/* === Shared === */QStackedWidget, QLabel, QPushButton, QRadioButton, QCheckBox, QGroupBox, QStatus ...
- Python爬虫关于多层嵌套iframe的解决
近期由于公司资源需要,我爬取了一个视频网站,结果以为一个很容易的小爬虫,却步步是坑啊,费了一天终于都解决了(太菜了!!!). 前面导航页的爬虫就不多说了,无非就是webdriver和PhantomJS ...
- Spring整合Hystrix
1.添加maven依赖 <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId> ...
- 你应该这样理解JVM内存管理
在进行Java程序设计时,一般不涉及内存的分配和内存回收的相关代码,此处引用一句话: Java和C++之间存在一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外的人想进去,墙里面的人想出来 ,个人从这 ...
- java IO实例
import java.io.*; /** * Created by CLY on 2017/7/23. */ public class Main { public static void main( ...