Balanced binary tree: PHP to achieve a balanced binary tree (AVL tree)

Source: Internet
Author: User
Tags array

<?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-&GT;BF = $BF;
}
Public Function GETBF () {
return $this->bf;
}
Public Function SETBF ($BF) {
$this-&GT;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-&GT;GETBF ()) {
Case 0:
$node-&GT;SETBF (1);
return true;
Case 1:
$this->rightrotate ($node);
return false;
Case-1:
$node-&GT;SETBF (0);
return false;
}
}elseif ($node->getdata () < $value) {
if (! $this->insertbfnode ($node->getright (), $value)) {
return false;
}
Switch ($node-&GT;GETBF ()) {
Case 0:
$node-&GT;SETBF (-1);
return true;
Case 1:
$node-&GT;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-&GT;GETBF ()) {
Case 1:
$left = & $right->getleft ();
Switch ($left-&GT;GETBF ()) {
Case-1:
$right-&GT;SETBF (0);
$node-&GT;SETBF (1);
Break
Case 0:
$right-&GT;SETBF (0);
$node-&GT;SETBF (0);
Break
Case 1:
$right-&GT;SETBF (-1);
$node-&GT;SETBF (0);
Break
}
$left-&GT;SETBF (0);
$this->excuteright ($right);
$this->excuteleft ($node);
Break
Case-1:
$node-&GT;SETBF (0);
$right-&GT;SETBF (0);
$this->excuteleft ($node);
Break
}
}
Private Function Rightrotate (& $node) {
$left = & $node->getleft ();
Switch ($left-&GT;GETBF ()) {
Case-1:
$right = & $left->getright ();
Switch ($right-&GT;GETBF ()) {
Case-1:
$left-&GT;SETBF (1);
$node-&GT;SETBF (0);
Break
Case 0:
$left-&GT;SETBF (0);
$node-&GT;SETBF (0); This article links http://www.cxybl.com/html/wlbc/Php/20120607/28509.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.