PHP cash balance binary tree (AVL tree)

Source: Internet
Author: User
PHP implements a balanced binary tree (AVL tree) & lt ;? Phprequire 'bstorder. php '; $ test = range (); // $ test = array ); $ tree = newBst (PHP achieves a balanced binary tree (AVL tree)
 DeleteNode ('30'); (the non-balance tree can be deleted, and the balance tree does not have a 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;} 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 ($ l Eft) {$ this-> left = $ left;} public function setRight ($ right) {$ this-> right = $ right ;}} class Bst {private $ head; // header node private $ data; // The initial input data, in the form of an array, such as array ('A', 'B'); private $ tag; // the previous node set during search (invalid, useless) // $ bst: whether to create 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: $ le Ft-> 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-> insertNode ($ value) ;}// $ pre: whether to return the 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 Node ($ 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.