PHP Class&Object -- PHP 自排序二叉树的深入解析

 更新时间:2013年06月25日 09:00:21   作者:  
本篇文章是对PHP中的自排序二叉树进行了详细的分析介绍,需要的朋友参考下
在节点之间再应用一些排序逻辑,二叉树就能提供出色的组织方式。对于每个节点,都让满足所有特定条件的元素都位于左节点及其子节点。在插入新元素时,我们需要从树的第一个节 点(根节点)开始,判断它属于哪一侧的节点,然后沿着这一侧找到恰当的位置,类似地,在读取数据时,只需要使用按序遍历方法来遍历二叉树。
复制代码 代码如下:

<?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管理依赖(dependency)关系工具 Composer 安装与使用

    PHP管理依赖(dependency)关系工具 Composer 安装与使用

    Composer 是PHP中用来管理依赖(dependency)关系的工具。你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的库文件。
    2014-08-08
  • PHP中数组转换为SimpleXML教程

    PHP中数组转换为SimpleXML教程

    在本篇文章中我们给大家总结了一篇关于PHP中数组转换为SimpleXML教程内容,有需要的朋友们跟着学习参考下。
    2019-01-01
  • php读取csc文件并输出

    php读取csc文件并输出

    本文给大家分享的是php读取csc文件并输出的方法,方法一用到的是fgetcsv函数,方法二用到是fopen函数,有需要的小伙伴可以参考下。
    2015-05-05
  • php从字符串创建函数的方法

    php从字符串创建函数的方法

    这篇文章主要介绍了php从字符串创建函数的方法,涉及php中字符串与create_function函数的使用技巧,需要的朋友可以参考下
    2015-03-03
  • PHP实现json_decode不转义中文的方法

    PHP实现json_decode不转义中文的方法

    这篇文章主要介绍了PHP实现json_decode不转义中文的方法,结合实例形式具体分析了php5.4+及5.3版本针对json_decode实现不转义中文的具体操作技巧与相关注意事项,需要的朋友可以参考下
    2017-05-05
  • PHP判断文件是否被引入的方法get_included_files用法示例

    PHP判断文件是否被引入的方法get_included_files用法示例

    这篇文章主要介绍了PHP判断文件是否被引入的方法get_included_files用法,结合实例形式分析了get_included_files函数获取引入文件及遍历输出的操作技巧,需要的朋友可以参考下
    2016-11-11
  • PHP快速排序quicksort实例详解

    PHP快速排序quicksort实例详解

    这篇文章主要介绍了PHP快速排序quicksort实现方法,结合实例形式分析了快速排序的原理及php实现快速排序的相关操作技巧,需要的朋友可以参考下
    2016-09-09
  • thinkphp框架实现数据添加和显示功能

    thinkphp框架实现数据添加和显示功能

    这篇文章主要为大家详细介绍了thinkphp框架实现数据添加和显示功能的相关资料,需要的朋友可以参考下
    2016-06-06
  • php7.3报preg_match() JIT compilation failed no more memory解决办法

    php7.3报preg_match() JIT compilation failed no more mem

    PHP JIT编译失败,内存不足的解决方法!你是否遇到过这个问题?不用担心,我们将为你提供简单易懂的解决方案,让你摆脱这一困扰,立即阅读我们的指南,轻松解决PHP JIT编译失败的烦恼!
    2023-12-12
  • php生成图形验证码几种方法小结

    php生成图形验证码几种方法小结

    生成图形验证码需要使用php GD库来生成,如果你没开户GD库我们需要在php.ini文件找到extension=php_gd2.dll 去掉前面的;就行了,然后重启apache 或iis环境即可
    2013-08-08

最新评论