Balanced binary tree: PHP to achieve a balanced binary tree (AVL tree)
Last Update:2017-02-28
Source: Internet
Author: User
<?php
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 (');(non-balanced tree can be deleted, the balance tree is not write delete operation)
Print_r ($tree->gettree ());
?>
bstorder.php
<?php
/**
* PHP Implementation two fork sort tree
* @author Zhaojiangwei
* @since 2011/11/16 16:29
*
*/
Class node{
Private $data;
Private $left;
Private $right;
Private $BF/Balance
Public Function node ($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;
}
Public Function SETBF ($BF) {
$this->BF = $BF;
}
Public Function GetData () {
return $this->data;
}
Public Function SetData ($data) {
$this->data = $data;
}
Public Function &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;//Head knot
private $data;//Initial input data, array form, such as Array (' A ', ' B ');
private $tag;//The previous node set on lookup (invalid, useless)
$BST: Whether to create an AVL tree
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);
}
}
Private Function Insertbfnode (& $node, $value) {
if ($node = = null) {
$node = new node ($value, 0);
return true;
}else{
if ($node->getdata () > $value) {
if (! $this->insertbfnode ($node->getleft (), $value)) {
return false;
}
Switch ($node->GETBF ()) {
Case 0:
$node->SETBF (1);
return true;
Case 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 ()) {
Case 0:
$node->SETBF (-1);
return true;
Case 1:
$node->SETBF (0);
return false;
Case-1:
$this->leftrotate ($node);
return false;
}
}else{
return false;
}
}
}
Private Function Excuteleft (& $node) {
$temp = $node;
$node = $node->getright ();
$temp->setright ($node->getleft ());
$node->setleft ($temp);
}
Private Function Excuteright (& $node) {
$temp = $node;
$node = $node->getleft ();
$temp->setleft ($node->getright ());
$node->setright ($temp);
}
Private Function Leftrotate (& $node) {
$right = & $node->getright ();
Switch ($right->GETBF ()) {
Case 1:
$left = & $right->getleft ();
Switch ($left->GETBF ()) {
Case-1:
$right->SETBF (0);
$node->SETBF (1);
Break
Case 0:
$right->SETBF (0);
$node->SETBF (0);
Break
Case 1:
$right->SETBF (-1);
$node->SETBF (0);
Break
}
$left->SETBF (0);
$this->excuteright ($right);
$this->excuteleft ($node);
Break
Case-1:
$node->SETBF (0);
$right->SETBF (0);
$this->excuteleft ($node);
Break
}
}
Private Function 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); This article links http://www.cxybl.com/html/wlbc/Php/20120607/28509.html