PHP Class&Object -- PHP 自排序二叉树的深入解析
发布时间:2024-06-05 09:48:33
标签: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>';
?>


猜你喜欢
- MySQL使用环境变量TMPDIR的值作为保存临时文件的目录的路径名。如果未设置TMPDIR,MySQL将使用系统的默认值,通常为/tmp、
- 1、安装 nvmcurl -o- https://raw.githubusercontent.com/creationix/nvm/v0.3
- 如下所示:dicFile = open('train_1.txt', 'r')
- 当然还是要使用FileSystemObject(FSO)来创建了。不过在创建前,要先检查以下目录是否存在,如果存在,就不用创建了: 
- SQL Server ISNULL 不生效原因数据库:SQL Server 2008 R2原始SQL:historyval 字段没有数据显示
- 导包效果展示以下截图显示的撤回消息类型依次是文字消息、微信自带表情、图片、语音、定位地图、名片、公众号文章、音乐、视频。有群里撤回的,也有个
- 表空间概述Oracle的表空间属于Oracle中的存储结构,是一种用于存储数据库对象(如:数据文件)的逻辑空间,是Oracle中信息存储的最
- 发现问题最近由于卸载Mysql时将很多相关依赖包都卸载了,重装mysql后启动django出现如下错误:django.core.except
- <!--#include file="Include/Conn.asp"--><%If(Request
- 一:unittest是python自带的一个单元测试框架,类似于java的junit,基本结构是类似的。基本用法如下: 1.用import
- 第一种np矩阵可以直接与标量运算>>>import numpy as np>>>arr1 = np.ar
- 前言在安装MySQL的时候会默认初始化几个MySQL运行所需的数据库:mysql, sys, information_schema, per
- 1. floor 函数1.1 floor 函数的作用floor() 函数的作用是返回小于等于该值的最大整数举例说明:select floor
- ASP在线压缩ACCESS数据库原理很简单:利用JRO.JetEngine的压缩功能建立一个新的数据库文件,然后把原来的删掉、替换!既然这样
- 学习https://matplotlib.org/gallery/index.html 记录,描述不一定准确,具体请参考官网Matplotl
- 给定一个字典,然后按键(key)或值(value)对字典进行排序。def dictionairy(): &nbs
- 介绍Matplotlib是Python中使用最广泛的数据可视化库之一。无论是简单还是复杂的可视化项目,它都是大多数人的首选库。在本教程中,我
- 刚才帮一位朋友做跳转的时候做的,为了获取完整的url地址,还是花了那么点时间不过现在看来,原来是那么简单,没有网上那么多复杂的东东,相信一定
- 书 名:细节决定交互设计的成败国际书号:ISBN 978-7-121-08232-0作 &nb
- 这是最近碰到一个问题,先描述下问题:首先我有一个训练好的模型(例如vgg16),我要对这个模型进行一些改变,例如添加一层全连接层,用于种种原