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动态创建Flash动画
Oct 09 PHP
一个PHP分页类的代码
May 18 PHP
php 调试利器debug_print_backtrace()
Jul 23 PHP
几个实用的PHP内置函数使用指南
Nov 27 PHP
php中instanceof 与 is_a()区别分析
Mar 03 PHP
如何通过Linux命令行使用和运行PHP脚本
Jul 29 PHP
THINKPHP在添加数据的时候获取主键id的值方法
Apr 03 PHP
通过php动态传数据到highcharts
Apr 05 PHP
浅谈PHP中pack、unpack的详细用法
Mar 12 PHP
PHP输出Excel PHPExcel的方法
Jul 26 PHP
php两点地理坐标距离的计算方法
Dec 29 PHP
php 原生分页
Apr 01 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文件缓存smarty模板应用实例分析
2016/02/26 PHP
PHP实现原生态图片上传封装类方法
2016/11/08 PHP
网页右键ie不支持event.preventDefault和event.returnValue (需要加window)
2013/02/22 Javascript
浅析Cookie中的Path与domain
2013/12/18 Javascript
AngularJS 视图详解及示例代码
2016/08/17 Javascript
js初始化验证实例详解
2016/11/26 Javascript
利用node.js实现自动生成前端项目组件的方法详解
2017/07/12 Javascript
基于require.js的使用(实例讲解)
2017/09/07 Javascript
vue 粒子特效的示例代码
2017/09/19 Javascript
js 原生判断内容区域是否滚动到底部的实例代码
2017/11/15 Javascript
如何使用 vue + d3 画一棵树
2018/12/03 Javascript
vue过滤器用法实例分析
2019/03/15 Javascript
一文搞懂ES6中的Map和Set
2019/05/20 Javascript
vue3为什么要用proxy替代defineProperty
2020/10/19 Javascript
Python切片知识解析
2016/03/06 Python
Python基于辗转相除法求解最大公约数的方法示例
2018/04/04 Python
Python 输出详细的异常信息(traceback)方式
2020/04/08 Python
CSS3 制作旋转的大风车(充满童年回忆)
2013/01/30 HTML / CSS
前端实现弹幕效果的方法总结(包含css3和canvas的实现方式)
2018/07/12 HTML / CSS
H5页面适配iPhoneX(就是那么简单)
2019/12/02 HTML / CSS
PHP数据运算类型都有哪些
2013/11/05 面试题
电子商务专业在校生实习自我鉴定
2013/09/29 职场文书
计算机应用专业推荐信
2013/11/13 职场文书
清华大学自主招生自荐信
2014/01/29 职场文书
安全标准化汇报材料
2014/02/03 职场文书
《小松树和大松树》教学反思
2014/02/20 职场文书
《真想变成大大的荷叶》教学反思
2014/04/14 职场文书
股票投资建议书
2014/05/19 职场文书
2014年秋季开学典礼主持词
2014/08/02 职场文书
走群众路线学习心得体会
2014/10/31 职场文书
2014年团支部年度工作总结
2014/12/24 职场文书
黄山导游词
2015/01/31 职场文书
人生遥控器观后感
2015/06/11 职场文书
生死牛玉儒观后感
2015/06/11 职场文书
新闻发布会新闻稿
2015/07/17 职场文书
2016年幼儿园庆六一开幕词
2016/03/04 职场文书