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+Html+缓存
Dec 20 PHP
php 调用远程url的六种方法小结
Nov 02 PHP
php使用curl访问https示例分享
Jan 17 PHP
php多功能图片处理类分享(php图片缩放类)
Mar 14 PHP
PHP计算当前坐标3公里内4个角落的最大最小经纬度实例
Feb 26 PHP
php函数mkdir实现递归创建层级目录
Oct 27 PHP
详解Yii实现分页的两种方法
Jan 14 PHP
使用php实现网站验证码功能【推荐】
Feb 09 PHP
Windows下php+mysql5.7配置教程
May 16 PHP
PHP空值检测函数与方法汇总
Nov 19 PHP
thinkPHP5框架中widget的功能与用法详解
Jun 11 PHP
PHP抽象类和接口用法实例详解
Jul 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获取网络上文件
2006/10/09 PHP
php 更新数据库中断的解决方法
2009/06/05 PHP
探讨捕获php错误信息方法的详解
2013/06/09 PHP
PHP数组操作――获取数组最后一个值的方法
2015/04/14 PHP
如何使用PHP Embed SAPI实现Opcodes查看器
2015/11/10 PHP
php实现简单爬虫的开发
2016/03/28 PHP
Ajax提交表单时验证码自动验证 php后端验证码检测
2016/07/20 PHP
基于jquery的内容循环滚动小模块(仿新浪微博未登录首页滚动微博显示)
2011/03/28 Javascript
jquery插件制作简单示例说明
2012/02/03 Javascript
js给页面加style无效果的解决方法
2014/01/20 Javascript
js判断字符长度及中英文数字等
2014/03/19 Javascript
Angularjs 基础入门
2014/12/26 Javascript
AngularJS基础学习笔记之简单介绍
2015/05/10 Javascript
JS+CSS实现自动切换的网页滑动门菜单效果代码
2015/09/14 Javascript
Bootstrap组件学习之导航、标签、面包屑导航(精品)
2016/05/17 Javascript
原生js实现图片放大缩小计时器效果
2017/01/20 Javascript
Angular中的interceptors拦截器
2017/06/25 Javascript
vue-cli脚手架build目录下utils.js工具配置文件详解
2018/09/14 Javascript
webpack4与babel配合使es6代码可运行于低版本浏览器的方法
2018/10/12 Javascript
vue如何根据网站路由判断页面主题色详解
2018/11/02 Javascript
JS实现数组去重,显示重复元素及个数的方法示例
2019/01/21 Javascript
JavaScript定时器常见用法实例分析
2019/11/15 Javascript
JS实现手风琴特效
2020/11/08 Javascript
[04:02]2014DOTA2国际邀请赛 BBC每日综述中国战队将再度登顶
2014/07/21 DOTA
python列表去重的二种方法
2014/02/14 Python
使用wxpython实现的一个简单图片浏览器实例
2014/07/10 Python
Python生成随机验证码的两种方法
2015/12/22 Python
numpy.where() 用法详解
2019/05/27 Python
深入了解python中元类的相关知识
2019/08/29 Python
大学生求职自我评价
2014/01/16 职场文书
一年级学生评语
2014/04/23 职场文书
法制宣传月活动方案
2014/05/11 职场文书
青年干部培训班学习心得体会
2016/01/06 职场文书
《分一些蚊子进来》读后感3篇
2020/01/09 职场文书
Python中X[:,0]和X[:,1]的用法
2021/05/10 Python
CSS变量实现主题切换的方法
2021/06/23 HTML / CSS