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 相关文章推荐
一个高ai的分页函数和一个url函数
Oct 09 PHP
IIS php环境配置PHP5 MySQL5 ZendOptimizer phpmyadmin安装与配置
Nov 18 PHP
解析用PHP实现var_export的详细介绍
Jun 20 PHP
Zend Framework 2.0事件管理器(The EventManager)入门教程
Aug 11 PHP
php数组添加元素方法小结
Dec 20 PHP
分享PHP源码批量抓取远程网页图片并保存到本地的实现方法
Dec 01 PHP
PHP5.5迭代生成器用法实例详解
Mar 16 PHP
PHP获取数组中单列值的方法
Jun 10 PHP
php+ajax实现仿百度查询下拉内容功能示例
Oct 20 PHP
PHP实现从上往下打印二叉树的方法
Jan 18 PHP
PHP设计模式之单例模式定义与用法分析
Mar 26 PHP
laravel model 两表联查示例
Oct 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
Php Mssql操作简单封装支持存储过程
2009/12/11 PHP
mac系统下为 php 添加 pcntl 扩展
2016/08/28 PHP
var与Javascript变量隐式声明
2009/09/17 Javascript
jQuery 第二课 操作包装集元素代码
2010/03/14 Javascript
Extjs4.0设置Ext.data.Store传参的请求方式(默认为GET)
2013/04/02 Javascript
JS解决url传值出现中文乱码的另类办法
2013/04/08 Javascript
js实现模拟计算器退格键删除文字效果的方法
2015/05/07 Javascript
简介AngularJS的HTML DOM支持情况
2015/06/17 Javascript
微信小程序 UI与容器组件总结
2017/02/21 Javascript
详解js中let与var声明变量的区别
2020/04/05 Javascript
Vue实现剪贴板复制功能
2019/12/31 Javascript
nodejs脚本centos开机启动实操方法
2020/03/04 NodeJs
Python基本数据类型详细介绍
2014/03/11 Python
用pywin32实现windows模拟鼠标及键盘动作
2014/04/22 Python
跟老齐学Python之集成开发环境(IDE)
2014/09/12 Python
Python数据结构之Array用法实例
2014/10/09 Python
Python中使用双下划线防止类属性被覆盖问题
2019/06/27 Python
Python考拉兹猜想输出序列代码实践
2019/07/05 Python
pytorch 实现模型不同层设置不同的学习率方式
2020/01/06 Python
pytorch 模型的train模式与eval模式实例
2020/02/20 Python
python爬虫泛滥的解决方法详解
2020/11/25 Python
各大浏览器 CSS3 和 HTML5 兼容速查表 图文
2010/04/01 HTML / CSS
美国顶尖折扣时尚购物网:Bluefly
2016/08/28 全球购物
微软香港官网及网上商店:Microsoft HK
2016/09/01 全球购物
简历自荐信
2013/12/02 职场文书
实习自我评价怎么写
2013/12/02 职场文书
皮肤科医师岗位职责
2013/12/04 职场文书
三峡人家导游词
2015/01/31 职场文书
慰问信模板
2015/02/14 职场文书
李强为自己工作观后感
2015/06/11 职场文书
2016关于学习党章的心得体会
2016/01/15 职场文书
如何在pycharm中快捷安装pip命令(如pygame)
2021/05/31 Python
MySQL系列之一 MariaDB-server安装
2021/07/02 MySQL
缓存替换策略及应用(以Redis、InnoDB为例)
2021/07/25 Redis
图文详解nginx日志切割的实现
2022/01/18 Servers
彻底卸载VMware虚拟机的超详细步骤记录
2022/07/15 Servers