PHP-implemented clue binary tree and binary tree traversal

Source: Internet
Author: User
Clue binary tree and binary tree traversal: detailed description of clue binary tree and binary tree traversal methods implemented by PHP: this example describes the clue binary tree and binary tree traversal methods implemented by PHP. For your reference, see createThreadTree (); echo $ tree-& gt; threadList (). n; traverse the clue binary tree from the first node. ech this article describes the clue binary tree and the binary tree traversal method implemented by PHP. We will share this with you for your reference. The details are as follows:

<? Php require 'bitree. php '; $ str = 'Ko # be8 # tr #### acy #####'; $ tree = new BiTree ($ str ); $ 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:

<? /*** PHP implement binary tree ** @ author zhaojiangwei * @ since 2011/10/25 * // Node class Node {private $ data = NULL; private $ left = NULL; private $ right = NULL; private $ lTag = 0; private $ rTag = 0; public function Node ($ data = false) {$ this-> 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 () {retu Rn $ 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;} public function setLTag ($ tag) {$ this-> lTag = $ tag;} public function getRTag () {return $ this-> rTag;} public function setRTag ($ tag) {$ this-> rTag = $ tag ;}// clue binary tree class cl Ass BiTree {private $ datas = NULL; // string to be imported; private $ root = NULL; // root node private $ leafCount = 0; // The number of leaf nodes is private $ headNode = NULL; // The header node of the clue binary tree is private $ preNode = NULL; // 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 root node public function c ReateTree ($ root = FALSE) {if (emptyempty ($ 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 ;}} // return 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 private function checkEmpty ($ node) {if ($ node-> getLeft () = NULL & $ node-> getRight () = NULL) {return true;} return false ;} // returns the binary tree depth public function getDepth (){ Return $ this-> traversDepth ($ this-> root);} // Retrieve the 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 ;}}?>

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.