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数据库操作面向对象的优点
Oct 09 PHP
php adodb介绍
Mar 19 PHP
php mysql Errcode: 28 终极解决方法
Jul 01 PHP
PHP采集腾讯微博的实现代码
Jan 19 PHP
关于PHP堆栈与列队的学习
Jun 21 PHP
PHP输出缓存ob系列函数详解
Mar 11 PHP
编写PHP脚本清除WordPress头部冗余代码的方法讲解
Mar 01 PHP
php+mysql+ajax实现单表多字段多关键词查询的方法
Apr 15 PHP
PHP 实现人民币小写转换成大写的方法及大小写转换函数
Nov 17 PHP
PHP中散列密码的安全性分析
Jul 26 PHP
PHP保存Base64图片base64_decode的问题整理
Nov 04 PHP
PHP使用递归按层级查找数据的方法
Nov 10 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获取网站域名和地址的代码
2008/08/17 PHP
PHP clearstatcache()函数详解
2010/03/02 PHP
删除html标签得到纯文本可处理嵌套的标签
2014/04/28 PHP
PHP简单实现HTTP和HTTPS跨域共享session解决办法
2015/05/27 PHP
详解PHP函数 strip_tags 处理字符串缺陷bug
2017/06/11 PHP
简单实现php上传文件功能
2017/09/21 PHP
Laravel使用swoole实现websocket主动消息推送的方法介绍
2019/10/20 PHP
Avengerls vs Newbee BO3 第二场2.18
2021/03/10 DOTA
prototype class详解
2006/09/07 Javascript
JavaScript 语法集锦 脚本之家基础推荐
2009/11/15 Javascript
Jquery截取中文字符串的实现代码
2010/12/22 Javascript
jQuery Tools tab(幻灯片)
2012/07/14 Javascript
定时器(setTimeout/setInterval)调用带参函数失效解决方法
2013/03/26 Javascript
js中switch case循环实例代码
2013/12/30 Javascript
javascript中tostring()和valueof()的用法及两者的区别
2015/11/16 Javascript
javascript学习小结之prototype
2015/12/03 Javascript
jQuery ajax方法传递中文时出现中文乱码的解决方法
2016/07/25 Javascript
Javascript实现时间倒计时效果
2017/07/15 Javascript
深入理解JavaScript的async/await
2018/08/05 Javascript
vue组件开发props验证的实现
2019/02/12 Javascript
在Python中marshal对象序列化的相关知识
2015/07/01 Python
全面理解Python中self的用法
2016/06/04 Python
Python smtplib实现发送邮件功能
2018/05/22 Python
python中的常量和变量代码详解
2018/07/25 Python
计算机二级python学习教程(2) python语言基本语法元素
2019/05/16 Python
Python模块的制作方法实例分析
2019/12/21 Python
Python提取视频中图片的示例(按帧、按秒)
2020/10/22 Python
Python lxml库的简单介绍及基本使用讲解
2020/12/22 Python
Sam’s Club山姆会员商店:沃尔玛旗下高端会员制商店
2017/01/16 全球购物
夏洛特和乔治婴儿和儿童时装精品店:Charlotte and George
2018/06/06 全球购物
TripAdvisor日本:全球领先的旅游网站
2019/02/14 全球购物
公司员工的自我评价范例
2013/11/01 职场文书
女大学生自我鉴定
2013/12/09 职场文书
法律专业学生的自我评价
2014/02/07 职场文书
三八节标语
2014/06/27 职场文书
《酸的和甜的》教学反思
2016/02/18 职场文书