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 相关文章推荐
PHP4中实现动态代理
Oct 09 PHP
支持php4、php5的mysql数据库操作类
Jan 10 PHP
PHP开发框架总结收藏
Apr 24 PHP
php cookie的操作实现代码(登录)
Dec 29 PHP
PHP文件锁定写入实例解析
Jul 14 PHP
php实现webservice实例
Nov 06 PHP
WordPress开发中自定义菜单的相关PHP函数使用简介
Jan 05 PHP
PHP isset()与empty()的使用区别详解
Feb 10 PHP
PHP中递归的实现实例详解
Nov 14 PHP
php微信公众号开发之简答题
Oct 20 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
Feb 21 PHP
php使用fputcsv实现大数据的导出操作详解
Feb 27 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中“简单工厂模式”实例代码讲解
2012/09/04 PHP
PHP 通过Socket收发十六进制数据的实现代码
2013/08/16 PHP
php实现执行某一操作时弹出确认、取消对话框
2013/12/30 PHP
php绘图之在图片上写中文和英文的方法
2015/01/24 PHP
PHP实现的分解质因数操作示例
2018/08/01 PHP
Javascript事件实例详解
2013/11/06 Javascript
jquery实现手风琴效果实例代码
2013/11/15 Javascript
javascript调试过程中找不到哪里出错的可能原因
2013/12/16 Javascript
js 立即调用的函数表达式如何写
2014/01/12 Javascript
node.js中的fs.writeFile方法使用说明
2014/12/14 Javascript
jquery.cookie.js使用指南
2015/01/05 Javascript
JQuery实现动态适时改变字体颜色的方法
2015/03/10 Javascript
JavaScript字符串常用的方法
2016/03/10 Javascript
jQuery 监控键盘一段时间没输入
2016/04/22 Javascript
JavaScript入门教程之引用类型
2016/05/04 Javascript
angularjs 表单密码验证自定义指令实现代码
2016/10/27 Javascript
Vue.extend构造器的详解
2017/07/17 Javascript
d3.js实现自定义多y轴折线图的示例代码
2018/05/30 Javascript
快速解决select2在bootstrap模态框中下拉框隐藏的问题
2018/08/10 Javascript
bootstrap table表格插件之服务器端分页实例代码
2018/09/12 Javascript
python中二维阵列的变换实例
2014/10/09 Python
pygame学习笔记(2):画点的三种方法和动画实例
2015/04/15 Python
使用python socket分发大文件的实现方法
2019/07/08 Python
关于python导入模块import与常见的模块详解
2019/08/28 Python
使用python matploblib库绘制准确率,损失率折线图
2020/06/16 Python
python pillow库的基础使用教程
2021/01/13 Python
全天然狗零食:Best Bully Sticks
2016/09/22 全球购物
欧铁通票官方在线销售网站:Eurail.com
2017/10/14 全球购物
学前教育求职自荐信范文
2013/12/25 职场文书
大学校园活动策划书
2014/02/04 职场文书
群众路线个人自我剖析材料
2014/10/07 职场文书
结婚老公保证书
2015/02/26 职场文书
2015年为民办实事工作总结
2015/05/26 职场文书
使用SQL实现车流量的计算的示例代码
2022/02/28 SQL Server
Python pyecharts绘制条形图详解
2022/04/02 Python