PHP implements a binary tree, a clue binary tree & lt ;? Phprequire 'bitree. php '; $ str = 'Ko # be8 # tr #### acy #####'; $ tree = newBiTree ($ str); $ tree PHP implements a binary tree, clue binary tree
CreateThreadTree (); echo $ tree-> threadList (). "\ n"; traverse the clue binary tree from the first node echo $ tree-> threadListReserv (); reverse traversal from the last node?>
BiTree. php
Data = $ data;} // I do not like to use the magic method public function getData () {return $ this-> data;} public function setData ($ data) {$ this-> data = $ data;} public function getLeft () {return $ this-> left;} public function setLeft ($ left) {$ this-> left = $ left;} public function getRight () {return $ this-> right;} public function setRight ($ right) {$ this-> right = $ right;} public function getLTag () {return $ this-> lTag;} publi C function setLTag ($ tag) {$ this-> lTag = $ tag;} public function getRTag () {return $ this-> rTag;} public function setRTag ($ tag) {$ this-> rTag = $ tag ;}// class BiTree {private $ datas = NULL; // string to be imported; private $ root = NULL; // root node private $ leafCount = 0; // number of leaf nodes private $ headNode = NULL; // head node private $ preNode = NULL of the clue binary tree; // save the public function BiTree ($ datas) {is_array ($ datas) of the previous traversal node when traversing the clue-based binary tree) | $ Datas = str_split ($ datas); $ this-> datas = $ datas; $ this-> backupData = $ this-> datas; $ this-> createTree (TRUE);} // traverse the creation tree in the forward order // $ root determines whether to create the public function createTree ($ root = FALSE) of the root node) {if (empty ($ this-> datas) return NULL; $ first = array_shift ($ this-> datas); if ($ first = '#') {return NULL;} else {$ node = new Node ($ first); $ root & $ this-> root = $ node; $ node-> setLeft ($ this-> createTree (); $ node-> SetRight ($ this-> createTree (); return $ node ;}// returns the number of leaf nodes of a binary tree. public function getLeafCount () {$ this-> figureLeafCount ($ this-> root); return $ this-> leafCount;} private function figureLeafCount ($ node) {if ($ node = NULL) return false; if ($ this-> checkEmpty ($ node) {$ this-> leafCount ++ ;} else {$ this-> figureLeafCount ($ node-> getLeft (); $ this-> figureLeafCount ($ node-> getRight ());}} // Determine whether the node is a leaf node priva Te function checkEmpty ($ node) {if ($ node-> getLeft () = NULL & $ node-> getRight () = NULL) {return true ;} return false;} // return the binary tree depth public function getDepth () {return $ this-> traversDepth ($ this-> root );} // traverse binary tree depth public function traversDepth ($ node) {if ($ node = NULL) {return 0 ;} $ u = $ this-> traversDepth ($ node-> getLeft () + 1; $ v = $ this-> traversDepth ($ node-> getRight () + 1; return $ u> $ v? $ U: $ v;} // returns the traversal result in the form of a string. // $ order is returned in the form of traversal. the public function getList ($ order = 'front ') {if ($ this-> root = NULL) return NULL; $ nodeList = array (); switch ($ order) {case "front ": $ this-> frontList ($ this-> root, $ nodeList); break; case "middle": $ this-> middleList ($ this-> root, $ nodeList ); break; case "last": $ this-> lastList ($ this-> root, $ nodeList); break; default: $ this-> frontList ($ this-> root, $ node List); break;} return implode ($ nodeList);} // Create a clue binary tree public function createThreadTree () {$ this-> headNode = new Node (); $ this-> preNode = & $ this-> headNode; $ this-> headNode-> setLTag (0 ); $ this-> headNode-> setLeft ($ this-> root); $ this-> headNode-> setRTag (1 ); $ this-> threadTraverse ($ this-> root); $ this-> preNode-> setRight ($ this-> headNode ); $ this-> preNode-> setRTag (1); $ this-> headNode-> setRight ($ this-> preNod E);} // clue-based binary tree private function threadTraverse ($ node) {if ($ node! = NULL) {if ($ node-> getLeft () = NULL) {$ node-> setLTag (1 ); $ node-> setLeft ($ this-> preNode);} else {$ this-> threadTraverse ($ node-> getLeft ();} if ($ this-> preNode! = $ This-> headNode & $ this-> preNode-> getRight () = NULL) {$ this-> preNode-> setRTag (1 ); $ this-> preNode-> setRight ($ node);} $ this-> preNode = & $ node; // note that the parameter $ this-> threadTraverse ($ node-> getRight ();} // traverses the hash tree public function threadList () from the first node () {$ arr = array (); for ($ node = $ this-> getFirstThreadNode ($ this-> root); $ node! = $ This-> headNode; $ node = $ this-> getNextNode ($ node) {$ arr [] = $ node-> getData ();} return implode ($ arr);} // reverse traversing from the end node the middle-order clue binary tree public function threadListReserv () {$ arr = array (); for ($ node = $ this-> headNode-> getRight (); $ node! = $ This-> headNode; $ node = $ this-> getPreNode ($ node) {$ arr [] = $ node-> getData ();} return implode ($ arr);} // return the public function getPreNode ($ node) {if ($ node-> getLTag () = 1) of a node) {return $ node-> getLeft ();} else {return $ this-> getLastThreadNode ($ node-> getLeft ());}} // return the public function getNextNode ($ node) {if ($ node-> getRTag () = 1) {return $ node-> getRight ();} else {return $ this-> getFirstThr EadNode ($ node-> getRight () ;}// return public function getFirstThreadNode ($ node) {while ($ node-> getLTag () = 0) {$ node = $ node-> getLeft ();} return $ node;} // returns the public function getLastThreadNode ($ node), the last node of the binary tree in the middle order) {while ($ node-> getRTag () = 0) {$ node = $ node-> getRight () ;}return $ node ;} // traverse the private function frontList ($ node, & $ nodeList) {if ($ node! = NULL) {$ nodeList [] = $ node-> getData (); $ this-> frontList ($ node-> getLeft (), $ nodeList ); $ this-> frontList ($ node-> getRight (), $ nodeList) ;}// traverse the private function middleList ($ node, & $ nodeList) in the middle order) {if ($ node! = NULL) {$ this-> middleList ($ node-> getLeft (), $ nodeList); $ nodeList [] = $ node-> getData (); $ this-> middleList ($ node-> getRight (), $ nodeList) ;}// traverse the private function lastList ($ node, & $ nodeList) in a descending order) {if ($ node! = NULL) {$ this-> lastList ($ node-> getLeft (), $ nodeList); $ this-> lastList ($ node-> getRight (), $ nodeList ); $ nodeList [] = $ node-> getData () ;}} public function getData () {return $ this-> data;} public function getRoot () {return $ this-> root ;}}?>