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

2015-01-24信息快讯网

本篇文章是对PHP中二叉树的实现代码进行详细的分析介绍,需要的朋友参考下

二叉树及其变体是数据结构家族里的重要组成部分。最为链表的一种变体,二叉树最适合处理需要一特定次序快速组织和检索的数据。
<?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中session变量的销毁
php中session退出登陆问题
php把session写入数据库示例
Php中使用Select 查询语句的实例
php检测iis环境是否支持htaccess的方法
jQuery中的RadioButton,input,CheckBox取值赋值实现代码
PHP json_decode函数详细解析
php将session放入memcached的设置方法
php的hash算法介绍
php中hashtable实现示例分享
php stripslashes和addslashes的区别
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
两级联动select刷新后其值保持不变的实现方法
PHP 利用Mail_MimeDecode类提取邮件信息示例
php使用base64加密解密图片示例分享
php object转数组示例
深入解读php中关于抽象(abstract)类和抽象方法的问题分析
PHP反射类ReflectionClass和ReflectionObject的使用方法
基于PHP+Ajax实现表单验证的详解
PHP Class&Object -- PHP 自排序二叉树的深入解析
解析php中eclipse 用空格替换 tab键
解析php addslashes()与addclashes()函数的区别和比较
探讨:php中在foreach中使用foreach ($arr as &$value) 这种类型的解释
解析php框架codeigniter中如何使用框架的session
关于更改Zend Studio/Eclipse代码风格主题的介绍
解析PHP中DIRECTORY_SEPARATOR,PATH_SEPARATOR两个常量的作用
关于查看MSSQL 数据库 用户每个表 占用的空间大小
php输出echo、print、print_r、printf、sprintf、var_dump的区别比较
Function eregi is deprecated (解决方法)
深入理解PHP中的Session和Cookie
PHP 使用MySQL管理Session的回调函数详解
浅析PHP中Collection 类的设计
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
IIS+fastcgi下PHP运行超时问题的解决办法详解
php class中public,private,protected的区别以及实例分析
©2014-2024 dbsqp.com