PHP Class&Object -- 解析PHP实现二叉树


Posted in PHP onJune 25, 2013

二叉树及其变体是数据结构家族里的重要组成部分。最为链表的一种变体,二叉树最适合处理需要一特定次序快速组织和检索的数据。

<?php
// Define a class to implement a binary tree
class Binary_Tree_Node {
    // Define the variable to hold our data:
    public $data;
    // And a variable to hold the left and right objects:
    public $left;
    public $right;    // A constructor method that allows for data to be passed in
    public function __construct($d = NULL) {
        $this->data = $d;
    }
    // Traverse the tree, left to right, in pre-order, returning an array
    // Preorder means that each node's value preceeds its children.
    public function traversePreorder() {
        // Prep some variables.
        $l = array();
        $r = array();
        // Read in the left and right children appropriately traversed:
        if ($this->left) { $l = $this->left->traversePreorder(); }
        if ($this->right) { $r = $this->right->traversePreorder(); }
        // Return a merged array of the current value, left, and right:
        return array_merge(array($this->data), $l, $r); 
    }
    // Traverse the tree, left to right, in postorder, returning an array
    // Postorder means that each node's value follows its children.
    public function traversePostorder() {
        // Prep some variables.
        $l = array();
        $r = array();
        // Read in the left and right children appropriately traversed:
        if ($this->left) { $l = $this->left->traversePostorder(); }
        if ($this->right) { $r = $this->right->traversePostorder(); }
        // Return a merged array of the current value, left, and right:
        return array_merge($l, $r, array($this->data)); 
    }
    // Traverse the tree, left to right, in-order, returning an array.
    // In-order means that values are ordered as left children, then the
    //  node value, then the right children.
    public function traverseInorder() {
        // Prep some variables.
        $l = array();
        $r = array();
        // Read in the left and right children appropriately traversed:
        if ($this->left) { $l = $this->left->traverseInorder(); }
        if ($this->right) { $r = $this->right->traverseInorder(); }
        // Return a merged array of the current value, left, and right:
        return array_merge($l, array($this->data), $r); 
    }
}
// Let's create a binary tree that will equal the following:    3
//                                                             / /      
//                                                            h   9      
//                                                               / /     
// Create the tree:                                             6   a    
$tree = new Binary_Tree_Node(3);
$tree->left = new Binary_Tree_Node('h');
$tree->right = new Binary_Tree_Node(9);
$tree->right->left = new Binary_Tree_Node(6);
$tree->right->right = new Binary_Tree_Node('a');
// Now traverse this tree in all possible orders and display the results:
// Pre-order: 3, h, 9, 6, a
echo '<p>', implode(', ', $tree->traversePreorder()), '</p>';
// Post-order: h, 9, 6, a, 3
echo '<p>', implode(', ', $tree->traversePostorder()), '</p>';
// In-order: h, 3, 6, 9, a
echo '<p>', implode(', ', $tree->traverseInorder()), '</p>';
?>

PHP 相关文章推荐
无数据库的详细域名查询程序PHP版(4)
Oct 09 PHP
相对路径转化成绝对路径
Apr 10 PHP
谨慎使用PHP的引用原因分析
Sep 06 PHP
php 启动报错如何解决
Jan 17 PHP
ThinkPHP中__initialize()和类的构造函数__construct()用法分析
Nov 29 PHP
DEDECMS首页调用图片集里的多张图片
Jun 05 PHP
使用PHP生成二维码的方法汇总
Jul 22 PHP
浅谈php7的重大新特性
Oct 23 PHP
PHP实现仿百度文库,豆丁在线文档效果(word,excel,ppt转flash)
Mar 10 PHP
php递归函数怎么用才有效
Feb 24 PHP
PHP7.3.10编译安装教程
Oct 08 PHP
PHP实现15位身份证号转18位的方法分析
Oct 16 PHP
PHP Class&amp;Object -- PHP 自排序二叉树的深入解析
Jun 25 #PHP
通过PHP current函数获取未知字符键名数组第一个元素的值
Jun 24 #PHP
PHP多例模式介绍
Jun 24 #PHP
PHP获取和操作配置文件php.ini的几个函数介绍
Jun 24 #PHP
PHP垃圾回收机制引用计数器概念分析
Jun 24 #PHP
PHP随机字符串生成代码(包括大小写字母)
Jun 24 #PHP
PHP 读取大文件的X行到Y行内容的实现代码
Jun 24 #PHP
You might like
PHP获取和操作配置文件php.ini的几个函数介绍
2013/06/24 PHP
Thinkphp+smarty+uploadify实现无刷新上传
2015/07/30 PHP
浅谈PHP中的
2016/04/23 PHP
Javascript客户端将指定区域导出到Word、Excel的代码
2008/10/22 Javascript
Prototype Class对象学习
2009/07/19 Javascript
vs2003 js文件编码问题的解决方法
2010/03/20 Javascript
单击浏览器右上角的X关闭窗口弹出提示的小例子
2013/06/12 Javascript
JS 退出系统并跳转到登录界面的实现代码
2013/06/29 Javascript
javascript中日期函数new Date()的浏览器兼容性问题
2015/09/05 Javascript
全面解析Bootstrap排版使用方法(标题)
2015/11/30 Javascript
教你如何在Node.js中使用jQuery
2016/08/28 Javascript
Javascript vue.js表格分页,ajax异步加载数据
2016/10/24 Javascript
Extjs让combobox写起来简洁又漂亮
2017/01/05 Javascript
js 将canvas生成图片保存,或直接保存一张图片的实现方法
2018/01/02 Javascript
Vue项目中设置背景图片方法
2018/02/21 Javascript
React学习笔记之高阶组件应用
2018/06/02 Javascript
基于Vue实现图片在指定区域内移动的思路详解
2018/11/11 Javascript
js实现的格式化数字和金额功能简单示例
2019/07/30 Javascript
微信小程序实现上拉加载功能示例【加载更多数据/触底加载/点击加载更多数据】
2020/05/29 Javascript
使用Python &amp; Flask 实现RESTful Web API的实例
2017/09/19 Python
学习python中matplotlib绘图设置坐标轴刻度、文本
2018/02/07 Python
PyQt4实时显示文本内容GUI的示例
2019/06/14 Python
python range实例用法分享
2020/02/06 Python
Win10下安装并使用tensorflow-gpu1.8.0+python3.6全过程分析(显卡MX250+CUDA9.0+cudnn)
2020/02/17 Python
Django 实现将图片转为Base64,然后使用json传输
2020/03/27 Python
django实现后台显示媒体文件
2020/04/07 Python
Keras Convolution1D与Convolution2D区别说明
2020/05/22 Python
Python Pillow(PIL)库的用法详解
2020/09/19 Python
在加拿大在线租赁和购买电子游戏:Game Access
2019/09/02 全球购物
Chemist Warehouse中文网:澳洲连锁大药房
2021/02/05 全球购物
PHP面试题附答案
2015/11/28 面试题
NULL是什么,它是怎么定义的
2015/05/09 面试题
怎样写留学自荐信
2013/11/11 职场文书
财政局个人年终总结
2015/03/03 职场文书
JavaWeb 入门篇(3)ServletContext 详解 具体应用
2021/07/16 Java/Android
TV动画《八十龟酱观察日记》第四季宣传PV公布
2022/04/06 日漫