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 相关文章推荐
为php4加入动态flash文件的生成的支持
Oct 09 PHP
PHP如何解决网站大流量与高并发的问题
Jun 25 PHP
PHP json格式和js json格式 js跨域调用实现代码
Sep 08 PHP
浅析ThinkPHP的模板输出功能
Jul 01 PHP
正确的PHP匹配UTF-8中文的正则表达式
May 13 PHP
php编写批量生成不重复的卡号密码代码
May 14 PHP
[原创]php集成安装包wampserver修改密码后phpmyadmin无法登陆的解决方法
Nov 23 PHP
详解Yii实现分页的两种方法
Jan 14 PHP
PHP中一个有趣的preg_replace函数详解
Aug 15 PHP
php 中htmlentities导致中文无法查询问题
Sep 10 PHP
Ajax+PHP实现的模拟进度条功能示例
Feb 11 PHP
php抽象方法和普通方法的区别点总结
Oct 13 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
Home Coffee Roasting
2021/03/03 咖啡文化
php中将字符串转为HTML的实体引用的一个类
2013/02/03 PHP
PHP中array_merge和array相加的区别分析
2013/06/17 PHP
PHP 获取文件权限函数介绍
2013/07/11 PHP
教你如何用php实现LOL数据远程获取
2014/06/10 PHP
php使用ob_start()实现图片存入变量的方法
2014/11/14 PHP
PHP+Ajax+JS实现多图上传
2016/05/07 PHP
php根据年月获取当月天数及日期数组的方法
2016/11/30 PHP
PHPStudy下如何为Apache安装SSL证书的方法步骤
2019/01/23 PHP
thinkphp框架无限级栏目的排序功能实现方法示例
2020/03/29 PHP
IE与Firefox在JavaScript上的7个不同句法分享
2011/10/30 Javascript
fancybox modal的完美解决(右上的X)
2012/10/30 Javascript
Jquery判断$(&quot;#id&quot;)获取的对象是否存在的方法
2013/09/25 Javascript
设置jsf的选择框h:selectOneMenu为不可编辑状态的方法
2014/01/07 Javascript
jQuery+JSON实现AJAX二级联动实例分析
2015/12/18 Javascript
jQuery实现手机自定义弹出输入框
2016/06/13 Javascript
Vue对象赋值视图不更新问题及解决方法
2019/06/03 Javascript
[03:00]DOTA2-DPC中国联赛1月18日Recap集锦
2021/03/11 DOTA
python使用点操作符访问字典(dict)数据的方法
2015/03/16 Python
详解supervisor使用教程
2017/11/21 Python
Python 使用folium绘制leaflet地图的实现方法
2019/07/05 Python
用Python获取摄像头并实时控制人脸的实现示例
2019/07/11 Python
用Python徒手撸一个股票回测框架搭建【推荐】
2019/08/05 Python
深入浅析Python科学计算库Scipy及安装步骤
2019/10/12 Python
Python Django搭建网站流程图解
2020/06/13 Python
用Python制作mini翻译器的实现示例
2020/08/17 Python
使用HTML和CSS3绘制基本卡通图案的示例分享
2015/11/06 HTML / CSS
CSS超出文本指定宽度用省略号代替和文本不换行
2016/05/05 HTML / CSS
在线吉他课程,学习如何弹吉他:Fender Play
2019/02/28 全球购物
生物化学研究助理员求职信
2013/10/09 职场文书
学校司机岗位职责
2013/11/14 职场文书
公司部门司机岗位职责
2014/01/03 职场文书
地质工程专业毕业生求职信
2014/08/08 职场文书
向国旗敬礼活动总结
2014/09/27 职场文书
2015年检察院个人工作总结
2015/05/20 职场文书
《世界多美呀》教学反思
2016/02/22 职场文书