PHP Class&Object -- PHP 自排序二叉树的深入解析


Posted in PHP onJune 25, 2013

在节点之间再应用一些排序逻辑,二叉树就能提供出色的组织方式。对于每个节点,都让满足所有特定条件的元素都位于左节点及其子节点。在插入新元素时,我们需要从树的第一个节 点(根节点)开始,判断它属于哪一侧的节点,然后沿着这一侧找到恰当的位置,类似地,在读取数据时,只需要使用按序遍历方法来遍历二叉树。

<?php
ob_start();
// Here we need to include the binary tree class
Class Binary_Tree_Node() {
   // You can find the details at
}
ob_end_clean();
// Define a class to implement self sorting binary tree
class Sorting_Tree {
    // Define the variable to hold our tree:
    public $tree;
    // We need a method that will allow for inserts that automatically place
    // themselves in the proper order in the tree
    public function insert($val) {
        // Handle the first case:
        if (!(isset($this->tree))) {
            $this->tree = new Binary_Tree_Node($val);
        } else {
            // In all other cases:
            // Start a pointer that looks at the current tree top:
            $pointer = $this->tree;
            // Iteratively search the tree for the right place:
            for(;;) {
                // If this value is less than, or equal to the current data:
                if ($val <= $pointer->data) {
                    // We are looking to the left ... If the child exists:
                    if ($pointer->left) {
                        // Traverse deeper:
                        $pointer = $pointer->left;
                    } else {
                        // Found the new spot: insert the new element here:
                        $pointer->left = new Binary_Tree_Node($val);
                        break;
                    }
                } else {
                    // We are looking to the right ... If the child exists:
                    if ($pointer->right) {
                        // Traverse deeper:
                        $pointer = $pointer->right;
                    } else {
                        // Found the new spot: insert the new element here:
                        $pointer->right = new Binary_Tree_Node($val);
                        break;
                    }
                }
            }
        }
    }    // Now create a method to return the sorted values of this tree.
    // All it entails is using the in-order traversal, which will now
    // give us the proper sorted order.
    public function returnSorted() {
        return $this->tree->traverseInorder();
    }
}
// Declare a new sorting tree:
$sort_as_you_go = new Sorting_Tree();
// Let's randomly create 20 numbers, inserting them as we go:
for ($i = 0; $i < 20; $i++) {
    $sort_as_you_go->insert(rand(1,100));
}
// Now echo the tree out, using in-order traversal, and it will be sorted:
// Example: 1, 2, 11, 18, 22, 26, 32, 32, 34, 43, 46, 47, 47, 53, 60, 71,
//   75, 84, 86, 90
echo '<p>', implode(', ', $sort_as_you_go->returnSorted()), '</p>';
?>

PHP 相关文章推荐
在WIN98下以apache模块方式安装php
Oct 09 PHP
php去除重复字的实现代码
Sep 16 PHP
解析php addslashes()与addclashes()函数的区别和比较
Jun 24 PHP
PHP小技巧之JS和CSS优化工具Minify的使用方法
May 19 PHP
php定时计划任务与fsockopen持续进程实例
May 23 PHP
php中数字0和空值的区别分析
Jun 05 PHP
PHP生成指定长度随机数最简洁的方法
Jul 14 PHP
Ajax和PHP正则表达式验证表单及验证码
Sep 24 PHP
使用PHPStorm+XDebug搭建单步调试环境
Nov 19 PHP
在Laravel中使用DataTables插件的方法
May 29 PHP
PHP __call()方法实现委托示例
May 20 PHP
关于PhpStorm设置点击编辑文件自动定位源文件的实现方式
Dec 30 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
解析在PHP中使用全局变量的几种方法
Jun 24 #PHP
You might like
繁体中文转换为简体中文的PHP函数
2006/10/09 PHP
Win2003下APACHE+PHP5+MYSQL4+PHPMYADMIN 的简易安装配置
2006/11/18 PHP
php session处理的定制
2009/03/16 PHP
php error_log 函数的使用
2009/04/13 PHP
解析PHP中intval()等int转换时的意外异常情况
2013/06/21 PHP
简介PHP的Yii框架中缓存的一些高级用法
2016/03/29 PHP
Joomla数据库操作之JFactory::getDBO用法
2016/05/05 PHP
EXTjs4.0的store的findRecord的BUG演示代码
2013/06/08 Javascript
移动端JQ插件hammer使用详解
2015/07/03 Javascript
浅谈javascript的call()、apply()、bind()的用法
2016/02/21 Javascript
javascript加减乘除的简单实例
2016/07/12 Javascript
原生js实现无缝轮播图效果
2017/01/11 Javascript
JavaScript循环_动力节点Java学院整理
2017/06/28 Javascript
nodejs中解决异步嵌套循环和循环嵌套异步的问题
2017/07/12 NodeJs
详解js跨域请求的两种方式,支持post请求
2018/05/05 Javascript
对angularJs中2种自定义服务的实例讲解
2018/09/30 Javascript
vue实现抽屉弹窗效果
2020/11/15 Javascript
动态创建类实例代码
2009/10/07 Python
pycharm 使用心得(二)设置字体大小
2014/06/05 Python
python中pass语句用法实例分析
2015/04/30 Python
python实现随机调用一个浏览器打开网页
2018/04/21 Python
Python图像处理之直线和曲线的拟合与绘制【curve_fit()应用】
2018/12/26 Python
pyqt5数据库使用详细教程(打包解决方案)
2020/03/25 Python
Python3 ID3决策树判断申请贷款是否成功的实现代码
2020/05/21 Python
css3图片边框border-image的用法
2017/06/30 HTML / CSS
打架检讨书100字
2014/01/08 职场文书
工程开工庆典邀请函
2014/02/01 职场文书
《雨霖铃》听课反思
2014/02/13 职场文书
揭牌仪式主持词
2014/03/19 职场文书
信电学院毕业生自荐书
2014/05/24 职场文书
医院办公室主任岗位职责
2015/04/01 职场文书
2016年会开场白台词
2015/06/01 职场文书
2015年社区国庆节活动总结
2015/07/30 职场文书
毕业生自我鉴定范文
2019/05/13 职场文书
五年级作文之学校的四季
2019/12/05 职场文书
详解redis在微服务领域的贡献
2021/10/16 Redis