二叉查找树 PHP,PHP实现二叉查找树

树的节点存储的是学生的信息

节点的比较以信息中的学号为准;

class Node{

private $num;//学生学号

private $name;//学生姓名

private $scoreChinese;//学生语文成绩

private $scoreMath;//数学成绩

private $scoreEnglish;//英语成绩

private $left;

private $right;

public function __construct(){

$this->left = null;

$this->right = null;

}

public function __set($key,$value){

$this->$key = $value;

}

public function __get($key){

if(isset($this->$key)){

return $this->$key;

}

}

}

class Tree{

private $top;//树顶节点

public function __construct($array){

//生成树

$falses = $this->makeTree($array);

$this->readTree();

}

public function makeTree($array){

if(empty($array)){

$this->top = null;

return;

}

//选择树顶节点

$temp = $array[floor(count($array)/2)];

$this->top->num = $temp['num'];

$this->top->name = $temp['name'];

$this->top->scoreChinese= $temp['scoreChinese'];

$this->top->scoreMath = $temp['scoreMath'];

$this->top->scoreEnglish= $temp['scoreEnglish'];

$this->top->left = null;

$this->top->right = null;

unset($array[floor(count($array)/2)]);

$false = 0;//建树失败的节点

foreach($array as $value){

if(false == $this->insert($value)){

$false++;

}

}

}

/**

插入节点

*/

public function insert($info){

$aNode = new Node();

$aNode->num = $info['num'];

$aNode->name = $info['name'];

$aNode->scoreChinese = $info['scoreChinese'];

$aNode->scoreMath = $info['scoreMath'];

$aNode->scoreEnglish = $info['scoreEnglish'];

$aNode->left = null;

$aNode->right = null;

if(null == $this->top){

$this->top = $aNode;

return;

}

$nowNode = $this->top;

while(true){

if($nowNode->num == $aNode->num){

return false;

}elseif($nowNode->num>$aNode->num && null == $nowNode->left){

$nowNode->left = $aNode;

return true;

}elseif($nowNode->numnum && null == $nowNode->right){

$nowNode->right = $aNode;

return true;

}elseif($nowNode->num>$aNode->num && $nowNode->left != null){

$nowNode = $nowNode->left;

}elseif($nowNode->numnum && $nowNode->right != null){

$nowNode = $nowNode->right;

}

}

}

/**

查询节点

*/

public function search($num){

$nowNode = $this->top;

while(true){

if($nowNode->num == $num){

return $nowNode;

}elseif($nowNode->num>$num && null == $nowNode->left){

return null;

}elseif($nowNode->numright){

return null;

}elseif($nowNode->num>$num && $nowNode->left!=null){

$nowNode = $nowNode->left;

}elseif($nowNode->numright!=null){

$nowNode = $nowNode->right;

}

}

}

/**

树是否为空

*/

public function isEmpty(){

if(is_null($this->top)){

return true;

}else{

return false;

}

}

/**

中序便利树

*/

public function readTree(){

$result = array();

$this->read($this->top,$result);

return $result;

}

private function read($nowNode,&$result){

if(null != $nowNode->left){

$this->read($nowNode->left,$result);

}

array_push($result,$nowNode);

if(null != $nowNode->right){

$this->read($nowNode->right,$result);

}

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值