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

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

相关文章

使用Codeigniter重写insert的方法(推荐)

使用Codeiginter 框架插入数据时有唯一索引键值存在解决办法 对数据进行存储的时候,会有一些唯一索引的字段已经有值了,插入数据的时候会失败我们通常解决办法是先查询这个值是否存在,...

PHP实现断点续传乱序合并文件的方法

本文实例讲述了PHP实现断点续传乱序合并文件的方法。分享给大家供大家参考,具体如下: 分割成多个文件发送,由于网络原因并不上先发就能发接收到。所以我们不能按顺序合并。 分割文件源码前面一...

php中二分法查找算法实例分析

本文实例讲述了php中二分法查找算法实现方法。分享给大家供大家参考,具体如下: 二分法查找在高级点的开发可能会用到了,当然在大公司找工作时都会有面试题是这种了,下面我们来看一篇关于二分法...

PHP读取文件内容后清空文件示例代码

复制代码 代码如下: $fh = fopen($path, "r+"); if( flock($fh, LOCK_EX) ){//加写锁 $old_content=json_decode...

php正则表达式学习笔记

php正则表达式学习笔记

php正则表达式学习笔记分享: 1.创建正则表达式 $regex = '/\d/i'; 与JavaScript中的第一个方式有点像,只是这里的话是个字符串。  2.正则表达式中...