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

 更新时间:2013年06月25日 09:00:21   作者:  
本篇文章是对PHP中的自排序二叉树进行了详细的分析介绍,需要的朋友参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

在节点之间再应用一些排序逻辑,二叉树就能提供出色的组织方式。对于每个节点,都让满足所有特定条件的元素都位于左节点及其子节点。在插入新元素时,我们需要从树的第一个节 点(根节点)开始,判断它属于哪一侧的节点,然后沿着这一侧找到恰当的位置,类似地,在读取数据时,只需要使用按序遍历方法来遍历二叉树。
复制代码 代码如下:

<?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之对抗Web扫描器的脚本技巧

    php之对抗Web扫描器的脚本技巧

    我们很难保证一个Web程序的安全性,因为鬼知道明天会有什么新的漏洞出现,鬼知道某个模块是不是一个毫无安全意识的程序员编写的。
    2008-10-10
  • PHP删除数组中指定值的元素常用方法实例分析【4种方法】

    PHP删除数组中指定值的元素常用方法实例分析【4种方法】

    这篇文章主要介绍了PHP删除数组中指定值的元素常用方法,结合实例形式对比分析了4种常用的数组遍历与元素删除方法,并总结分析了相关算法优缺点,需要的朋友可以参考下
    2018-08-08
  • PHP获取某个月最大天数(最后一天)的方法

    PHP获取某个月最大天数(最后一天)的方法

    这篇文章主要介绍了PHP获取某个月最大天数(最后一天)的方法,涉及php流程控制及数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 一段实用的php验证码函数

    一段实用的php验证码函数

    这篇文章主要为大家详细介绍了php验证码函数,还分享了PHP生成图片验证码的函数,供大家参考,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • PHP中include和require的区别实例分析

    PHP中include和require的区别实例分析

    网上太多关于PHP中include与require区别。然而事实真的如此吗,今天我们就通过一个具体的实例来简单分析验证下
    2017-05-05
  • 详解cookie验证的php应用的一种SSO解决办法

    详解cookie验证的php应用的一种SSO解决办法

    这篇文章主要介绍了详解cookie验证的php应用的一种SSO解决办法的相关资料,希望通过本文能帮助到大家让大家遇到这种类似问题解决,需要的朋友可以参考下
    2017-10-10
  • PHP多线程类及用法实例

    PHP多线程类及用法实例

    这篇文章主要介绍了PHP多线程类及用法,实例分析了多线程类的具体实现方法及应用技巧,并结合下载远程图片的实例予以深入分析,需要的朋友可以参考下
    2014-12-12
  • php strtotime 函数UNIX时间戳

    php strtotime 函数UNIX时间戳

    int strtotime ( string time [, int now]) 本函数预期接受一个包含英文日期格式的字符串并尝试将其解析为 UNIX 时间戳。
    2009-01-01
  • php调用云片网接口发送短信的实现方法

    php调用云片网接口发送短信的实现方法

    这篇文章主要介绍了php调用云片网接口发送短信的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
    2017-10-10
  • 10个对初学者非常有用的PHP技巧

    10个对初学者非常有用的PHP技巧

    这篇文章主要为大家详细介绍了10个对初学者非常有用的PHP技巧,这些PHP技巧适用于初学者,而不是那些已经在使用MVC框架的人,感兴趣的小伙伴们可以参考一下
    2016-04-04

最新评论

?


http://www.vxiaotou.com