PHP Cashing Balanced binary tree (AVL tree)

Source: Internet
Author: User
PHP Implements Balanced binary tree (AVL tree)
 
  Deletenode (' n ');(non-balanced tree can be deleted, balance tree not write delete operation)    Print_r ($tree->gettree ());?>


bstorder.php
 data = $data;            $this->left = $left;            $this->right = $right;        $this-&GT;BF = $BF;        } public Function GETBF () {return $this->bf;        The Public Function SETBF ($BF) {$this-&GT;BF = $BF;        } public Function GetData () {return $this->data;        The Public Function SetData ($data) {$this->data = $data;        } public Function &getleft () {return $this->left;        } public Function &getright () {return $this->right;        The Public Function Setleft ($left) {$this->left = $left;        The Public Function Setright ($right) {$this->right = $right;        }} class bst{private $head;//head node private $data;//Initial input data, array form, such as Array (' A ', ' B ');         private $tag;//The previous node set at lookup (invalid, useless)//$BST: Whether to create the 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->in               Sertbfnode ($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->getrigh                    T (), $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);                          Break                            Case 1: $left-&GT;SETBF (0);                            $node-&GT;SETBF (-1);                    Break                     } $right-&GT;SETBF (0);                    $this->excuteleft ($left);                    $this->excuteright ($node);                Break                    Case 1: $node-&GT;SETBF (0);                    $left-&GT;SETBF (0);                    $this->excuteright ($node);            Break }} Public Function Createbst () {foreach ($this->data as $value) {$this->inse            Rtnode ($value);            }}//$pre: If the node is not found, returns the pre-value public function &searchbst (& $node, $key, $pre = FALSE) {                if ($node = = NULL) {if ($pre) {return $this->tag;     }else{               return FALSE;                }} if ($key > $node->getdata ()) {$this->tag = $node;               return $this->searchbst ($node->getright (), $key, $pre);                }elseif ($key < $node->getdata ()) {$this->tag = $node;            return $this->searchbst ($node->getleft (), $key, $pre);            }else{return $node; }} Public Function Insertnode ($key) {if (! $this->head) {$this->head = new No            De ($key);                }else{$pre = $this->searchbst ($this->head, $key, TRUE);                               $node = new node ($key);                   if ($pre->getdata () > $key) {$pre->setleft ($node);                }else{$pre->setright ($node); }}} Public function Deletenode ($key) {$node = &$this->searchbst ($this->head, $key);               if (! $node) {return FALSE;             } if ($node->getleft () = = NULL) {$node = $node->getright ();             }elseif ($node->getright () = = NULL) {$node = $node->getleft ();                }else{$leftBig = & $this->searchleftbig ($node);                                $node->setdata ($leftBig->getdata ());                if ($leftBig->getleft ()) {$leftBig = $leftBig->getleft (); }}} Private Function &searchleftbig (& $key) {$result = & $key->getleft            ();                while ($result) {if ($result->getright () = = NULL) {return $result;            } $result = & $result->getright ();        }} Public Function Gettree () {return $this->head; }}?>
  • 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.