PHP Class&Object -- PHP 自排序二叉树的深入解析
2018-10-12 13:53
811 查看
在节点之间再应用一些排序逻辑,二叉树就能提供出色的组织方式。对于每个节点,都让满足所有特定条件的元素都位于左节点及其子节点。在插入新元素时,我们需要从树的第一个节 点(根节点)开始,判断它属于哪一侧的节点,然后沿着这一侧找到恰当的位置,类似地,在读取数据时,只需要使用按序遍历方法来遍历二叉树。
<?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 Class&Object -- PHP 自排序二叉树的深入解析
- PHP Class&Object -- PHP 自排序二叉树
- PHP Class&Object -- 解析PHP实现二叉树
- PHP Class&Object -- 解析PHP实现二叉树
- PHP Class&Object -- PHP实现二叉树
- PHP Class&Object -- PHP 实现链表
- 面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】
- Case class和Case object代码实战解析之Scala学习笔记-20
- BOOST应用 无法解析的外部符号 "void __cdecl boost::throw_exception(class std::exception const &)"
- UIView CALayer 属性不显示错误 Property cannot be found in forward class object 'CALayer
- PHP <?= Uri::base(false) ?> 无法解析
- PHP register_shutdown_function函数的深入解析
- PHP“Cannot use object of type stdClass as array”
- PHP get_object_vars 和 get_class_vars
- Delphi - SuperObject : raised exception class eintoverflow with message'Integer overflow'.
- 深入解析array_merge函数的用法 php
- PHP:Fatal error: Class 'COM' not found in … 的处理办法
- php读取二进制流(C语言结构体struct数据文件)的深入解析
- 29.Case class 和Case object代码实战解析
- PHP设计模式之命令模式的深入解析