前言:

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

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

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

例如对于一下这棵树:

深度优先遍历:

  • 前序遍历: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. Buffer和Stream

    Buffer JavaScript 语言自身只有字符串数据类型,没有二进制数据类型.但在处理像TCP流或文件流时,必须使用到二进制数据. 因此在 Node.js中,定义了一个 Buffer 类,该类用 ...

  2. Python迭代和列表生成器

    使用for循环遍历list和tuple,这种遍历成为迭代 在如C语言中都是通过下标拿到值,for...in这种方式其实是相同的. 在函数的一节,这样说--->‘求和函数sum(),sum(ite ...

  3. 一、Redis的数据类型

    一Redis的数据类型 string:字符串 hash:哈希 list:列表 set:集合 zset:有序集合(sorted set) 1.string(字符串) redis最基本的类型.可以理解成与 ...

  4. Oracle X$Tables

    前言 最早从Yong Huang那里看到关于比较详细的X$表的介绍,后来陆续从其他Oracle专家那里得到了不少信息.在Steve Adams 的书中对X$表多有提及,而且他的站点也是个资源比较丰富的 ...

  5. 【转】vue项目重构技术要点和总结

    vue数据更新, 视图未更新 这个问题我们经常会遇到,一般是vue数据赋值的时候,vue数据变化了,但是视图没有更新.这个不算是项目重构的技术要点,也和大家分享一下vue2.0通常的解决方案吧! 解决 ...

  6. vue+vue-resource+vue-cookie随笔

    vue-resource http拦截器interceptors: Vue.http.interceptors.push(function(request, next) {...} V-cookie: ...

  7. api资源

    转:https://blog.csdn.net/qq_37187976/article/details/79160050

  8. linux 文件解锁

    //文件 sudo chmod 777 文件名 //文件夹内的文件 sudo chmod 777 文件夹/ * //遍历文件夹下的所有文件 sudo chmod -R 777 文件夹/ *

  9. git Please move or remove them before you can merge.

    git clean -d -fx "" 其中  x -----删除忽略文件已经对git来说不识别的文件 d -----删除未被添加到git的路径中的文件 f -----强制运行

  10. getopts的使用方法

    getopts的使用 语法格式:getopts [option[:]] [DESCPRITION] VARIABLE option:表示为某个脚本可以使用的选项 ":":如果某个选 ...