PHP遍历二叉树
遍历二叉树,这个相对比较复杂。
二叉树的便利,主要有两种,一种是广度优先遍历,一种是深度优先遍历。
什么是广度优先遍历?就是根节点进入,水平一行一行的便利。
什么是深度优先遍历呢?就是根节点进入,然后按照一个固定的规律,一直向下走,一个方向的子树遍历之后再遍历另一个方向的子树。
深度优先遍历,主要有三种顺序遍历:先序(先输出根节点),中序(第二个输出根节点),后序(最后输出根节点)。
直接上代码吧。
class Node {
public $data = null;
public $left = null;
public $right = null;
} $node1 = new Node();
$node2 = new Node();
$node3 = new Node();
$node4 = new Node();
$node5 = new Node();
$node6 = new Node();
$node7 = new Node();
$node8 = new Node();
$node9 = new Node(); $node1->data = 1;
$node2->data = 2;
$node3->data = 3;
$node4->data = 4;
$node5->data = 5;
$node6->data = 6;
$node7->data = 7;
$node8->data = 8;
$node9->data = 9; $node1->left = $node2;
$node1->right = $node3;
$node2->left = $node4;
$node2->right = $node5;
$node3->left = $node6;
$node3->right = $node7;
$node4->left = $node8;
$node4->right = $node9;
以上代码,简单建立一个二叉树,如下图:
广度优先遍历
// 二叉树广度优先遍历
function binary_tree1($node) {
$result = [];//保存结果
$memc = [];
array_push($memc, $node);//根节点入队
while (!empty($memc)) {//持续输出节点,直到队列为空
$cnode = array_shift($memc);//最前边的元素出队
$result[] = $cnode->data;//记录当前节点的值
//左节点先入队,然后右节点入队
if ($cnode->left != null)
array_push($memc, $cnode->left);
if ($cnode->right != null)
array_push($memc, $cnode->right);
}
return $result;
} print_r(binary_tree2($node1));
深度优先遍历(先序)
// 二叉树深度优先遍历(先序)
function binary_tree2($node)
{
$result = [];//结果数组
$memc = [];
array_push($memc, $node);//将根节点压栈
while (!empty($memc)) {
$cnode = array_pop($memc);//弹出刚刚压栈的节点
$result[] = $cnode->data;
//因为先进后出原则,想先显示左子树,所以先压入右子树
if ($cnode->right != null) {
array_push($memc, $cnode->right);
}
if ($cnode->left != null) {
array_push($memc, $cnode->left);
}
}
return $result;
} print_r(binary_tree2($node1));
PHP遍历二叉树的更多相关文章
- 数据结构算法C语言实现(二十)--- 6.3.1遍历二叉树
一.简述 二叉树的遍历主要是先序.中序.后序及对应的递归和非递归算法,共3x2=6种,其中后序非递归在实现上稍复杂一些.二叉树的遍历是理解和学习递归及体会栈的工作原理的绝佳工具! 此外,非递归所用的栈 ...
- 【面经】用递归方法对二叉树进行层次遍历 && 二叉树深度
void PrintNodeAtLevel(BiTree T,int level) { // 空树或层级不合理 ) return; == level) { cout << T->da ...
- YTU 2345: 后序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2345/2611.html 2345: 后序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: ...
- YTU 2346: 中序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2346/2606.html 2346: 中序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 12 解决: ...
- C++ 创建和遍历二叉树
一个简单的创建和遍历二叉树的C++程序,二叉树的其他操作程序待更新. #include <iostream> using namespace std; struct BiTNode{ ch ...
- YTU 2344: 先序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2344/2603.html 2344: 先序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 4 解决: ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- 基于visual Studio2013解决面试题之0401非递归遍历二叉树
题目
- Java遍历二叉树深度宽度
节点数据结构 class TreeNode { TreeNode left = null; TreeNode right = null; } 最大深度,基本思路是:使用递归,分别求出左子树的深度.右子 ...
- Morris遍历-如何用空间复杂度O(1)来遍历二叉树
参照和学习: https://www.cnblogs.com/AnnieKim/archive/2013/06/15/morristraversal.html 解决的问题:如何使用空间复杂度O(1), ...
随机推荐
- 测试自动化学习3-python3简单操作
1.列表操作 增 stu = []stus.append('lili') #在list的末尾增加一个元素 stus.insert(9,'yjk') #在指定的位置插入元素, 查 print('单个取, ...
- Python全栈-JavaScript】jQuery工具
jQuery工具 一.jQuery.browser.version 显示当前 IE 浏览器版本号. if ( $.browser.msie ) alert( $.browser.version ); ...
- 11.全局变量(static)
1.数组 数组名是常量 2. 指针数组 4.局部变量 (1).作用域 作用的范围: (2).普通局部变量 在{}内定义: 只有执行到定义变量的这个语句,系统才会给这个变量分配空间. 当离开{},这个非 ...
- Spark架构原理
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- app ios info权限配置:
info权限配置: Privacy - Bluetooth Peripheral Usage Description --> App需要您的同意,才能访问蓝牙 Privacy - Calenda ...
- Beep函数实现硬件蜂鸣声
#include <Windows.h> #include <tchar.h> int WINAPI _tWinMain(HINSTANCE hInstance, HINSTA ...
- 关于SimpleDateFormat安全的时间格式化线程安全问题
想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调 ...
- idea 2018.1 for mac破解激活方法---到2099
https://blog.csdn.net/weixin_41470864/article/details/81607169
- PHP图像处理(GD库)
一.图像处理概述 1.开启GD2图像扩展库 ①PHP不仅限于只产生HTML的输出,还可以创建与操作多种不同格式的图像文件.PHP提供了一些内置的图像处理函数,也可以使用GD函数库创建新图像或处理已有的 ...