二叉搜索树
首先说这玩意是干啥的,就是以一定的规则存储数据,然后方便搜索的,至于为啥方便,我就不知道了。。。。。。
先说咋构建一个二叉搜索树,假设要存储的数据为:50,30,20,67,80,90,49,32。
然后在开始构建前,规定左子<右子,右子>当前顶点(为了处理值跟当前节点一致的问题)。
其实很简单,就是从根开始比较,一个顶点一个顶点的比下去,如果小于等于当前顶点,就跟当前顶点的左子比,没有左子那么就当其左子。如果大于当前顶点,就跟右子比,如果没有右子就当其右子。来到左子或者右子之后,就是重复上面的比顶点的步骤。
具体代码就是:
<?php
class Leaf{
public $value;
/**
* @var Leaf|null
*/
public $leftLeaf;
/**
* @var Leaf|null
*/
public $rightLeaf;
public function __construct($value)
{
$this->value=$value;
}
}
// 创建二叉搜素树
function createBinarySearchTree($storeData){
$root=new Leaf($storeData[0]);
unset($storeData[0]);
foreach($storeData as $value){
compare($root,$value);
}
return $root;
}
/**
* 搜索一个具体的值,已经如果搜索失败,是否将其存储该值
* @param $leaf Leaf
* @param $searchValue
* @param bool $isStoreData
* @return bool
*/
function searchValue($leaf,$searchValue,$isStoreData=true){
if (!$leaf){
return false;
}
if ($leaf->value==$searchValue){
return true;
}
if ($isStoreData){
compare($leaf,$searchValue);
return true;
}
if ($leaf->value>=$searchValue){
return searchValue($leaf->leftLeaf,$searchValue,$isStoreData);
}else{
return searchValue($leaf->rightLeaf,$searchValue,$isStoreData);
}
}
/**
* 核心操作代码
* @param $leaf Leaf
* @param $value
* @return bool
*/
function compare(&$leaf,$value){
if($leaf->value>=$value){
if($leaf->leftLeaf){
compare($leaf->leftLeaf,$value);
return true;
}else{
$newLeaf=new Leaf($value);
$leaf->leftLeaf=$newLeaf;
return true;
}
}
if($leaf->rightLeaf){
compare($leaf->rightLeaf,$value);
return true;
}else{
$newLeaf=new Leaf($value);
$leaf->rightLeaf=$newLeaf;
return true;
}
}
/**
* 辅助函数,用来展示最终的结果的
* @param $leaf Leaf
* @return bool
*/
function showResult($leaf){
if (!$leaf){
return true;
}
print "当前节点值为:".$leaf->value.PHP_EOL;
$leaf->leftLeaf && (print "左子值为:".$leaf->leftLeaf->value.PHP_EOL);
$leaf->rightLeaf && (print "右子值为:".$leaf->rightLeaf->value.PHP_EOL);
showResult($leaf->leftLeaf);
showResult($leaf->rightLeaf);
}
// 开始存储数据
$storeData=[50,30,20,67,80,90,49,32];
$root=createBinarySearchTree($storeData);
showResult($root);
// 尝试搜索一个存在的值
print "67的".(searchValue($root,67,false)?"值存在":"值不存在");
print PHP_EOL;
// 尝试搜索一个不存在的值
print "100的".(searchValue($root,100,false)?"值存在":"值不存在");
print PHP_EOL;
// 再搜索一次
print "100的".(searchValue($root,100)?"值存在":"值不存在");
print PHP_EOL;
showResult($root);