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

yipeiwu_com6年前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>';
?>

相关文章

Eclipse的PHP插件PHPEclipse安装和使用

Eclipse的PHP插件PHPEclipse安装和使用

PHPEclipse是Eclipse的一个插件,提供了包括PHP语法分析、运行、调试等功能的集成开发环境。它基于Eclipse的插件机制,即插即用,配置和使用都非常方便。如果平时需要同时...

zend framework多模块多布局配置

许多人在使用过程中都会遇到这样那样的问题,而且zend framework现在已经到1.11版本了,网络上的很多资料都还停留在旧版本上,因此我在这里以当前的最新版本1.11为例,来简单介...

Smarty foreach控制循环次数的实现详解

1.可以用{$array| count} 来试试。2.复制代码 代码如下:{foreach from=$variable key=key name=name iteam=value}&n...

PHP 中文处理技巧

折腾了一天的时间,才弄出点眉目来。 做AJAX应用或Flash应用,提交中文内容到后台,涉及到编码解码(encode、decode)及编码格式的转换。 网上的PHP端escape une...

php入门教程之Zend Studio设置与开发实例

php入门教程之Zend Studio设置与开发实例

本文实例讲述了php入门教程之Zend Studio设置与开发方法。分享给大家供大家参考,具体如下: 新建文档的模板设置 新建文档的模板设置 Demo1.php: <...