ホームページ  >  記事  >  php教程  >  バランスのとれたバイナリ ツリー: PHP はバランスのとれたバイナリ ツリー (avl ツリー) を実装します。

バランスのとれたバイナリ ツリー: PHP はバランスのとれたバイナリ ツリー (avl ツリー) を実装します。

WBOY
WBOYオリジナル
2016-06-21 08:51:351989ブラウズ

require 'bstorder.php';
$test = range(1, 10);
//$test = array(3,9,1,4,8, 5,7,6,2,10);
$tree = new bst($test, true);
//$tree->deletenode('30');(非平衡树可删除,平衡树的没写删除操作)
print_r($tree->gettree());
?>
bstorder.php
/**
* PHP はバイナリソートツリーを実装します
* @author zhaojiangwei
* @since 2011/11/16 16:29
*
*/
クラス ノード{
プライベート $data;
プライベート $left;
プライベート $right;
プライベート $bf;//平衡度
パブリック関数ノード($data = null, $bf = 0, $left = null, $right = null){
$this->data = $data;
$this->left = $left;
$this ->right = $right;
$this->bf = $bf;
}
public function getbf(){
return $this->bf;
}
パブリック関数 setbf($bf){
$this->bf = $bf;
}
パブリック関数 getdata(){
return $this->data;
}
パブリック関数 setdata($data){
$this->data = $data;
}
パブリック関数 &getleft(){
return $this->left;
}
public function &getright(){
return $this->right;
}
public function setleft($left){
$this->left = $left ;
}
public function setright($right){
$this->right = $right;
}
}
class bst{
private $head; //头结点
private $data;//最初の入力データ、数組の形式、array('a','b');
private $tag;//查找時の設置の前结点(已無し效,没用)
//$bst:否か创建avl树
public function bst($data, $bst = false){
$this->data = $data;
$this->head = null;
if(!$bst){
$this->createbst();
}else{
$this->createbfbst( );
}
}
public function createbfbst(){
foreach($this->data as $value){
$this->insertbfnode($this->>; head, $value);
}
}
プライベート関数 insertbfnode(&$node, $value){
if($node == null){
$node = 新しいノード( $value, 0);
return true;
}else{
if($node->getdata() > $value){
if(!$this->insertbfnode($node->getleft(), $value)){
return false;
}
switch($node-> ;getbf()){
ケース 0:
$node->setbf(1);
true を返す;
ケース 1:
$this->rightrotate($node) ;
return false;
case -1:
$node->setbf(0);
return false;
}
}elseif($node->getdata( ) < $value){
if(!$this->insertbfnode($node->getright(), $value)){
return false;
}
switch($ node->getbf()){
ケース 0:
$node->setbf(-1);
true を返す;
ケース 1:
$node->setbf (0);
return false;
case -1:
$this->leftrotate($node);
return false;
}
}else{
return false;
}
}
}
プライベート関数 excuteleft(&$node){
$temp = $node;
$node = $node->getright() ;
$temp->setright($node->getleft());
$node->setleft($temp);
}
プライベート関数 excuteright(&$node) {
$temp = $node;
$node = $node->getleft();
$temp->setleft($node->getright());
$node ->setright($temp);
}
プライベート関数 leftrotate(&$node){
$right = &$node->getright();
switch($right-> ;getbf()){
ケース 1:
$left = &$right->getleft();
switch($left->getbf()){
ケース -1:
$right->setbf(0);
$node->setbf(1);
break;
ケース 0:
$right->setbf(0);
$node->setbf(0);
ブレーク;
ケース 1:
$right->setbf(-1);
$node->setbf(0) ;
break;
}
$left->setbf(0);
$this->excuteright($right);
$this->gt;excuteleft($node) ;
break;
case -1:
$node->setbf(0);
$right->setbf(0);
$this->excuteleft($ node);
break;
}
}
プライベート関数 rightrotate(&$node){
$left = &$node->getleft();
switch($ left->getbf()){
case -1:
$right = &$left->getright();
switch($right->getbf()){
case -1:
$left->setbf(1);
$node->setbf(0);
break;
case 0:
$left->setbf (0);
$node->setbf(0); 本文http://www.cxybl.com/html/wlbc/Php/20120607/28509.html



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。