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

 更新时间:2013年06月25日 09:05:59   作者:  
本篇文章是对PHP中二叉树的实现代码进行详细的分析介绍,需要的朋友参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

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

<?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基于非递归算法实现先序、中序及后序遍历二叉树操作示例

    这篇文章主要介绍了PHP基于非递归算法实现先序、中序及后序遍历二叉树操作,结合实例形式分析了php采用非递归算法对二叉树进行先序、中序及后序遍历操作的原理与具体实现技巧,需要的朋友可以参考下
    2018-01-01
  • php生成静态文件的多种方法分享

    php生成静态文件的多种方法分享

    最近需要将自己写的php文件生成静态,经过查找,发现有多种方法,不同需求可以选择不同的方法
    2012-07-07
  • php数组索引的Key加引号和不加引号的区别

    php数组索引的Key加引号和不加引号的区别

    这篇文章主要介绍了php数组索引的Key加引号和不加引号的区别,加引号和不加引号是有严重的区别的,需要的朋友可以参考下
    2014-08-08
  • php简单实现查询数据库返回json数据

    php简单实现查询数据库返回json数据

    这篇文章主要介绍了php简单实现查询数据库返回json数据,并附上2则示例代码,非常的简单实用,有需要的小伙伴可以参考下。
    2015-04-04
  • PHP APC的安装与使用详解

    PHP APC的安装与使用详解

    本篇文章是对PHP中APC的安装与使用进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php异常处理方法实例汇总

    php异常处理方法实例汇总

    这篇文章主要介绍了php异常处理方法,实例汇总了常见的php异常处理技巧,非常具有实用价值,需要的朋友可以参考下
    2015-06-06
  • php字符串分割函数用法实例

    php字符串分割函数用法实例

    这篇文章主要介绍了php字符串分割函数用法,实例分析了php中explode和split函数的使用技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • 浅谈php安全性需要注意的几点事项

    浅谈php安全性需要注意的几点事项

    这段时间一直在写一个整站,前几天才基本完成了,所以抽个时间写了一篇对于php安全的总结。技术含量不高,过不了也没关系,希望能一些准备写网站的朋友一点引导。
    2014-07-07
  • php自定义错误处理用法实例

    php自定义错误处理用法实例

    这篇文章主要介绍了php自定义错误处理用法,实例分析了php通过自定义函数进行错误处理的技巧,需要的朋友可以参考下
    2015-03-03
  • ThinkPHP中RBAC类的四种用法分析

    ThinkPHP中RBAC类的四种用法分析

    这篇文章主要介绍了ThinkPHP中RBAC类的四种用法,较为详细的分析了登陆控制器及公共控制器中的常见用法,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-11-11

最新评论

?


http://www.vxiaotou.com