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 相关文章推荐
Adodb的十个实例(清晰版)
Dec 31 PHP
在普通HTTP上安全地传输密码
Jul 21 PHP
php采集时被封ip的解决方法
Aug 29 PHP
php 数组动态添加实现代码(最土团购系统的价格排序)
Dec 30 PHP
解析PHP中$_FILES的使用以及注意事项
Jul 05 PHP
PHP PDOStatement:bindParam插入数据错误问题分析
Nov 13 PHP
php第一次无法获取cookie问题处理
Dec 15 PHP
PHP使用CURL模拟登录的方法
Jul 08 PHP
PHP使用SOAP扩展实现WebService的方法
Apr 01 PHP
PHP编程之设置apache虚拟目录
Jul 08 PHP
yii2实现Ueditor百度编辑器的示例代码
Nov 02 PHP
PHP封装的page分页类定义与用法完整示例
Dec 24 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
在MongoDB中模拟Auto Increment的php代码
2011/03/06 PHP
PHP中文分词 自动获取关键词介绍
2012/11/13 PHP
PHP命名空间(Namespace)的使用详解
2013/05/04 PHP
Yii的CDbCriteria查询条件用法实例
2014/12/04 PHP
PHP扩展Memcache分布式部署方案
2015/12/06 PHP
Symfony2针对输入时间进行查询的方法分析
2017/06/28 PHP
PHP实现一维数组与二维数组去重功能示例
2018/05/24 PHP
javascript new 需不需要继续使用
2009/07/02 Javascript
JS 时间显示效果代码
2009/08/23 Javascript
深入理解JavaScript系列(13) This? Yes,this!
2012/01/18 Javascript
js为鼠标添加右击事件防止默认的右击菜单弹出
2013/07/29 Javascript
jQuery实现带延迟效果的滑动菜单代码
2015/09/02 Javascript
JQuery日历插件My97DatePicker日期范围限制
2016/01/20 Javascript
JavaScript prototype属性详解
2016/10/25 Javascript
jQuery+vue.js实现的九宫格拼图游戏完整实例【附源码下载】
2017/09/12 jQuery
Vue全家桶实践项目总结(推荐)
2017/11/04 Javascript
three.js中3D视野的缩放实现代码
2017/11/16 Javascript
vue购物车插件编写代码
2017/11/27 Javascript
nodejs结合socket.io实现websocket通信功能的方法
2018/01/12 NodeJs
vue-router项目实战总结篇
2018/02/11 Javascript
在Angular中使用JWT认证方法示例
2018/09/10 Javascript
原生JavaScript之es6中Class的用法分析
2020/02/23 Javascript
vue中提示$index is not defined错误的解决方式
2020/09/02 Javascript
快速入手Python字符编码
2016/08/03 Python
Python实现的堆排序算法原理与用法实例分析
2017/11/22 Python
Python爬虫实例_利用百度地图API批量获取城市所有的POI点
2018/01/10 Python
pygame游戏之旅 游戏中添加显示文字
2018/11/20 Python
Python关键字及可变参数*args,**kw原理解析
2020/04/04 Python
django 将自带的数据库sqlite3改成mysql实例
2020/07/09 Python
python操作toml文件的示例代码
2020/11/27 Python
通过css3动画和opacity透明度实现呼吸灯效果
2019/08/09 HTML / CSS
Nike爱尔兰官方网站:Nike.com (IE)
2018/03/12 全球购物
伦敦著名的运动鞋综合商店:Footpatrol
2019/03/25 全球购物
Can a struct inherit from another struct? (结构体能继承结构体吗)
2016/09/25 面试题
2014年除四害工作总结
2014/12/06 职场文书
励志语录:时光飞逝,请学会珍惜所有的人和事
2020/01/16 职场文书