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 相关文章推荐
[FAQ]PHP中的一些常识:类篇
Oct 09 PHP
人大复印资料处理程序_输入篇
Oct 09 PHP
php 应用程序安全防范技术研究
Sep 25 PHP
关于Sphinx创建全文检索的索引介绍
Jun 25 PHP
php 启动时报错的简单解决方法
Jan 27 PHP
php通过ksort()函数给关联数组按照键排序的方法
Mar 18 PHP
php程序内部post数据的方法
Mar 31 PHP
PHP工程师VIM配置分享
Dec 15 PHP
PHP中new static()与new self()的比较
Aug 19 PHP
php基于websocket搭建简易聊天室实践
Oct 24 PHP
详解PHP防止盗链防止迅雷下载的方法
Apr 26 PHP
PHP设计模式之模板模式定义与用法详解
Dec 20 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设计模式 Delegation(委托模式)
2011/06/26 PHP
PDO版本问题 Invalid parameter number: no parameters were bound
2013/01/06 PHP
PHP传值到不同页面的三种常见方式及php和html之间传值问题
2015/11/19 PHP
Thinkphp和onethink实现微信支付插件
2016/04/13 PHP
php实现留言板功能(会话控制)
2017/05/23 PHP
Yii框架操作cookie与session的方法实例详解
2019/09/04 PHP
Prototype最新版(1.5 rc2)使用指南(1)
2007/01/10 Javascript
js中scrollHeight,scrollWidth,scrollLeft,scrolltop等差别介绍
2012/05/16 Javascript
jquery mobile changepage的三种传参方法介绍
2013/09/13 Javascript
Bootstrap页面布局基础知识全面解析
2016/06/13 Javascript
js时间戳和c#时间戳互转方法(推荐)
2017/02/15 Javascript
详细讲解vue2+vuex+axios
2017/05/27 Javascript
React Native第三方平台分享的实例(Android,IOS双平台)
2017/08/04 Javascript
NodeJS爬虫实例之糗事百科
2017/12/14 NodeJs
JavaScript时间与时间戳的转换操作实例分析
2018/12/07 Javascript
js实现拖拽元素选择和删除
2020/08/25 Javascript
JavaScript实现网页tab栏效果制作
2020/11/20 Javascript
[03:03]DOTA2校园争霸赛 济南城市决赛欢乐发奖活动
2013/10/21 DOTA
python执行get提交的方法
2015/04/29 Python
python使用turtle库绘制时钟
2020/03/25 Python
Python中分支语句与循环语句实例详解
2018/09/13 Python
python判断完全平方数的方法
2018/11/13 Python
详解用python实现基本的学生管理系统(文件存储版)(python3)
2019/04/25 Python
基于torch.where和布尔索引的速度比较
2020/01/02 Python
解决tensorflow添加ptb库的问题
2020/02/10 Python
Python装饰器用法与知识点小结
2020/03/09 Python
Python Web项目Cherrypy使用方法镜像
2020/11/05 Python
详解html2canvas截图不能截取圆角图片的解决方案
2018/01/30 HTML / CSS
美国花园雕像和家居装饰网上商店:Design Toscano
2019/03/09 全球购物
英国家喻户晓的家居商店:The Range
2019/03/25 全球购物
优秀演讲稿范文
2013/12/29 职场文书
村捐赠仪式答谢词
2014/01/21 职场文书
环境建设实施方案
2014/03/14 职场文书
重阳节简报
2015/07/20 职场文书
DE1107机评
2022/04/05 无线电
TypeScript实用技巧 Nominal Typing名义类型详解
2022/09/23 Javascript