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 相关文章推荐
PHP 魔术函数使用说明
May 14 PHP
php中文字符串截取方法实例总结
Sep 30 PHP
php内存缓存实现方法
Jan 24 PHP
php获取本周星期一具体日期的方法
Apr 20 PHP
java模拟PHP的pack和unpack类
Apr 13 PHP
php+ajax无刷新上传图片的实现方法
Dec 06 PHP
php计算给定日期所在周的开始日期和结束日期示例
Feb 06 PHP
详解PHP函数 strip_tags 处理字符串缺陷bug
Jun 11 PHP
360搜索引擎自动收录php改写方案
Apr 28 PHP
php实现微信企业付款到个人零钱功能
Oct 09 PHP
使用tp框架和SQL语句查询数据表中的某字段包含某值
Oct 18 PHP
PHP 对象继承原理与简单用法示例
Apr 21 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脚本的10个技巧(8)
2006/10/09 PHP
PHP入门速成教程
2007/03/19 PHP
基于PHP CURL获取邮箱地址的详解
2013/06/03 PHP
windows环境下php配置memcache的具体操作步骤
2013/06/09 PHP
php强制文件下载而非在浏览器打开的自定义函数分享
2014/05/08 PHP
php简单计算年龄的方法(周岁与虚岁)
2016/12/06 PHP
再次更新!MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类 Ver 1.6)
2007/02/05 Javascript
javascript 读取XML数据,在页面中展现、编辑、保存的实现
2009/10/27 Javascript
javascript最常用与实用的创建类的代码
2010/08/12 Javascript
JQuery显示、隐藏div的几种方法简明总结
2015/04/16 Javascript
借助FileReader实现将文件编码为Base64后通过AJAX上传
2015/12/24 Javascript
js HTML5上传示例代码完整版
2016/10/10 Javascript
JS模拟实现ECMAScript5新增的数组方法
2017/03/20 Javascript
JS控件bootstrap suggest plugin使用方法详解
2017/03/25 Javascript
详解vue-router 2.0 常用基础知识点之router-link
2017/05/10 Javascript
写给小白看的JavaScript异步
2017/11/29 Javascript
vue实现模态框的通用写法推荐
2018/02/26 Javascript
vue动态删除从数据库倒入列表的某一条方法
2018/09/29 Javascript
JS中min函数实例讲解
2019/02/18 Javascript
[51:22]Fnatic vs IG 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
解决python执行不输出系统命令弹框的问题
2019/06/24 Python
Python3打包exe代码2种方法实例解析
2020/02/17 Python
tensorflow 分类损失函数使用小记
2020/02/18 Python
JupyterNotebook 输出窗口的显示效果调整方法
2020/04/13 Python
解决paramiko执行命令超时的问题
2020/04/16 Python
python数据抓取3种方法总结
2021/02/07 Python
Html5页面点击遮罩层背景关闭遮罩层
2020/11/30 HTML / CSS
南非最大的在线时尚商店:Zando
2019/07/21 全球购物
简单说下OSPF的操作过程
2014/08/13 面试题
电大物流学生的自我评价
2013/10/25 职场文书
带薪年假请假条
2014/02/04 职场文书
2014年三八妇女节活动总结
2014/03/01 职场文书
班级学雷锋活动总结
2014/06/26 职场文书
国际语言毕业生求职信
2014/07/08 职场文书
2014教师年度工作总结
2014/11/10 职场文书
2015年小学体育工作总结
2015/05/22 职场文书