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->BF = $BF; } public Function GETBF () {return $this->bf; The Public Function SETBF ($BF) {$this->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->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->getrigh T (), $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); Break Case 1: $left->SETBF (0); $node->SETBF (-1); Break } $right->SETBF (0); $this->excuteleft ($left); $this->excuteright ($node); Break Case 1: $node->SETBF (0); $left->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; }}?>