Data structure of PHP 9: stores binary trees, creates binary trees, and performs basic operations on binary trees, and traverses binary tree algorithms.

Source: Internet
Author: User
Data structure of PHP 9: stores binary trees, creates binary trees, and performs basic operations on binary trees, and traverses binary tree algorithms.

Create a binary tree and basic operations PHP stores a binary tree. create a binary tree and perform basic operations on the binary tree to traverse the binary tree algorithm.

 * @ Version $ Id: BinaryTree. class. php, v 1.0 13:33:00 uw Exp * @ copyright©2011, xudianyang */header ('content-type: text/html; charset = gb2312 '); // the implementation of PHP on the fifth stack of the PHP Data structure and the basic operations on the stack can be found in this class of include_once (". /StackLinked. class. php "); // you can find the include_once ('. /QueueLinked. class. php '); class BTNode {// left subtree "pointer" public $ mLchild = null; // right subtree "pointer" public $ mRchild = null; // public $ mData = null; // indicates the left flag field. if the value is 1, mLchild points to the left child of the node, 2 indicates that "pointing" the node directly precursor public $ intLeftTag = null; // the right flag field. if it is 1, it indicates mRchi. Ld points to the right child of the node. 2 indicates that the node is directed to the public $ intRightTag = null;} class BinaryTree {// root node public $ mRoot; // traverse the input binary tree data in the first order public $ mPBTdata = null;/*** constructor, initialize the establishment of a binary tree ** @ param array $ btdata traverse the data of the input binary tree in the first order, one-dimensional array, each element represents a binary tree node value, expand the node value to ''[string with a length of 0] * @ return void */public function _ construct ($ btdata = array ()) {$ this-> mPBTdata = $ btdata; $ this-> mRoot = null; $ this-> getPreorderTraversalCreate ($ this-> mRoot );} /*** create a binary tree in the first traversal mode ** @ p Aram BTNode binary tree node, passed by reference * @ return void */public function getPreorderTraversalCreate (& $ btnode) {$ elem = array_shift ($ this-> mPBTdata ); if ($ elem = '') {$ btnode = null;} else if ($ elem = null) {return ;} else {$ btnode = new BTNode (); $ btnode-> mData = $ elem; $ this-> getPreorderTraversalCreate ($ btnode-> mLchild ); $ this-> getPreorderTraversalCreate ($ btnode-> mRchild) ;}}/*** determines whether the binary tree is empty. ** @ return boolean: true is returned if the binary tree is not empty. otherwise, fa is returned. Lse **/public function getIsEmpty () {if ($ this-> mRoot instanceof BTNode) {return false;} else {return true ;}} /*** leave the binary tree blank ** @ return void */public function setBinaryTreeNull () {$ this-> mRoot = null ;} /*** traverse the binary tree in sequence ** @ param BTNode $ the root node in the rootnode traversal process * @ param array $ btarr receives the array variable of the value, pass * @ return void */public function getPreorderTraversal ($ rootnode, & $ btarr) {if ($ rootnode! = Null) {$ btarr [] = $ rootnode-> mData; $ this-> getPreorderTraversal ($ rootnode-> mLchild, $ btarr ); $ this-> getPreorderTraversal ($ rootnode-> mRchild, $ btarr );}} /*** a non-recursive algorithm for first-order traversal ** @ param BTNode $ objRootNode binary tree root node * @ param array $ array variable of the value received by arrBTdata, pass * @ return void */public function getPreorderTraversalNoRecursion ($ objRootNode, & $ arrBTdata) {if ($ objRootNode instanceof BTNode) {$ objNode = $ objRootNode; $ objStack = new StackLinked (); do {$ arrBTdata [] = $ objNode-> mData; $ objRNode = $ objNode-> mRchild; if ($ objRNode! = Null) {$ objStack-> getPushStack ($ objRNode);} $ objNode = $ objNode-> mLchild; if ($ objNode = null) {$ objStack-> getPopStack ($ objNode) ;}} while ($ objNode! = Null);} else {$ arrBTdata = array ();}} /*** traverse a binary tree in ascending order ** @ param BTNode $ root node in the objRootNode process * @ param array $ array variable of the arrBTdata receiving value, pass * @ return void */public function getInorderTraversal ($ objRootNode, & $ arrBTdata) {if ($ objRootNode! = Null) {$ this-> getInorderTraversal ($ objRootNode-> mLchild, $ arrBTdata); $ arrBTdata [] = $ objRootNode-> mData; $ this-> getInorderTraversal ($ objRootNode-> mRchild, $ arrBTdata );}} /*** non-recursive algorithm for sequential traversal ** @ param BTNode $ objRootNode binary tree root node * @ param array $ array variable of the value received by arrBTdata, pass * @ return void */public function getInorderTraversalNoRecursion ($ objRootNode, & $ arrBTdata) {if ($ objRootNode instanceof BTNode) {$ objNode = $ objRootN Ode; $ objStack = new StackLinked (); // traverse the left subtree and access the root node do {while ($ objNode! = Null) {$ objStack-> getPushStack ($ objNode); $ objNode = $ objNode-> mLchild;} $ objStack-> getPopStack ($ objNode ); $ arrBTdata [] = $ objNode-> mData; $ objNode = $ objNode-> mRchild;} while (! $ ObjStack-> getIsEmpty (); // traverse the right subtree do {while ($ objNode! = Null) {$ objStack-> getPushStack ($ objNode); $ objNode = $ objNode-> mLchild;} $ objStack-> getPopStack ($ objNode ); $ arrBTdata [] = $ objNode-> mData; $ objNode = $ objNode-> mRchild;} while (! $ ObjStack-> getIsEmpty ();} else {$ arrBTdata = array ();}} /*** post-order traversal of a binary tree ** @ param BTNode $ root node during objRootNode traversal * @ param array $ array variable of the value received by arrBTdata, pass the * @ return void */public function getPostorderTraversal ($ objRootNode, & $ arrBTdata) {if ($ objRootNode! = Null) {$ this-> getPostorderTraversal ($ objRootNode-> mLchild, $ arrBTdata); $ this-> getPostorderTraversal ($ objRootNode-> mRchild, $ arrBTdata ); $ arrBTdata [] = $ objRootNode-> mData ;}/ *** post-order traversal of non-recursive algorithms * BTNode $ objRootNode binary tree root node array $ array variable of the arrBTdata receiving value, pass void */public function getPostorderTraversalNoRecursion ($ objRootNode, & $ arrBTdata) {if ($ objRootNode instanceof BTNode) {$ objNode = $ objRootNode; $ objStack = new Stack Linked (); $ objTagStack = new StackLinked (); $ tag = 1; do {while ($ objNode! = Null) {$ objStack-> getPushStack ($ objNode); $ objTagStack-> getPushStack (1); $ objNode = $ objNode-> mLchild ;} $ objTagStack-> getPopStack ($ tag); $ objTagStack-> getPushStack ($ tag); if ($ tag = 1) {$ objStack-> getPopStack ($ objNode ); $ objStack-> getPushStack ($ objNode); $ objNode = $ objNode-> mRchild; $ objTagStack-> getPopStack ($ tag); $ objTagStack-> getPushStack (2 );} else {$ objStack-> getPopStack ($ objNode); $ arrBTdata [] = $ objNode-> mData; $ ObjTagStack-> getPopStack ($ tag); $ objNode = null ;}} while (! $ ObjStack-> getIsEmpty ();} else {$ arrBTdata = array ();}} /*** traverse a binary tree hierarchically ** @ param BTNode $ objRootNode the root node of the binary tree * @ param array $ array variable of the value received by arrBTdata, pass the * @ return void */public function getLevelorderTraversal ($ objRootNode, & $ arrBTdata) {if ($ objRootNode instanceof BTNode) {$ objNode = $ objRootNode; $ objQueue = new QueueLinked (); $ objQueue-> getInsertElem ($ objNode); while (! $ ObjQueue-> getIsEmpty () {$ objQueue-> getDeleteElem ($ objNode); $ arrBTdata [] = $ objNode-> mData; if ($ objNode-> mLchild! = Null) {$ objQueue-> getInsertElem ($ objNode-> mLchild);} if ($ objNode-> mRchild! = Null) {$ objQueue-> getInsertElem ($ objNode-> mRchild) ;}} else {$ arrBTdata = array ();}} /*** calculate the number of leaf nodes of a binary tree ** @ param BTNode $ objRootNode root node * @ return int parameter transfer error return-1 **/public function getLeafNodeCount ($ objRootNode) {if ($ objRootNode instanceof BTNode) {$ intLeafNodeCount = 0; $ objNode = $ objRootNode; $ objStack = new StackLinked (); do {if ($ objNode-> mLchild = null & $ objNode-> mRchild = null) {$ intLeafNodeCount ++;} $ obj RNode = $ objNode-> mRchild; if ($ objRNode! = Null) {$ objStack-> getPushStack ($ objRNode);} $ objNode = $ objNode-> mLchild; if ($ objNode = null) {$ objStack-> getPopStack ($ objNode) ;}} while ($ objNode! = Null); return $ intLeafNodeCount;} else {return-1 ;}} /*** calculate the depth of a binary tree ** @ param BTNode $ objRootNode root node * @ return int parameter transfer error return-1 */public function getBinaryTreeDepth ($ objRootNode) {if ($ objRootNode instanceof BTNode) {$ objNode = $ objRootNode; $ objQueue = new QueueLinked (); $ queue = 0; $ objQueue-> getInsertElem ($ objNode ); $ objLevel = $ objNode; while (! $ ObjQueue-> getIsEmpty () {$ objQueue-> getDeleteElem ($ objNode); if ($ objNode-> mLchild! = Null) {$ objQueue-> getInsertElem ($ objNode-> mLchild);} if ($ objNode-> mRchild! = Null) {$ objQueue-> getInsertElem ($ objNode-> mRchild);} if ($ objLevel = $ objNode) {$ intBinaryTreeDepth ++; $ objLevel =@ $ objQueue-> mRear-> mElem;} return $ intBinaryTreeDepth;} else {return-1 ;}} echo"
"; $ Bt = new BinaryTree (array ('A', 'B', 'D','', '', 'e', 'e','', 'G ', '','', 'C', 'F', ''); echo" binary tree structure: \ r \ n "; var_dump ($ bt); $ btarr = array (); echo "first-order recursive traversal of binary trees: \ r \ n"; $ bt-> getPreorderTraversal ($ bt-> mRoot, $ btarr); var_dump ($ btarr); echo "first-order non-recursive traversal of binary trees: \ r \ n"; $ arrBTdata = array (); $ bt-> getPreorderTraversalNoRecursion ($ bt-> mRoot, $ arrBTdata); var_dump ($ arrBTdata); echo "sequential recursive traversal of binary trees: \ r \ n "; $ arrBTdata = array (); $ bt-> getInorderTraversal ($ bt-> mRoot, $ arrBTdata); var_dump ($ arrBTdata); echo "Ordinal Non-recursive traversal of a binary tree: \ r \ n "; $ arrBTdata = array (); $ bt-> getInorderTraversalNoRecursion ($ bt-> mRoot, $ arrBTdata); var_dump ($ arrBTdata ); echo "recursive binary tree traversal in descending order: \ r \ n"; $ arrBTdata = array (); $ bt-> getPostorderTraversal ($ bt-> mRoot, $ arrBTdata ); var_dump ($ arrBTdata); echo "post-order non-recursive traversal binary tree: \ r \ n"; $ arrBTdata = array (); $ bt-> getPostorderTraversalNoRecursion ($ bt-> mRoot, $ arrBTdata); var_dump ($ arrBTdata); echo "traverse binary trees by hierarchy: \ r \ n"; $ arrBTdata = array (); $ bt-> getLevelorderTraversal ($ bt-> mRoot, $ arrBTdata); var_dump ($ arrBTdata); echo "number of leaf nodes :". $ bt-> getLeafNodeCount ($ bt-> mRoot); echo "\ r \ n"; echo "binary tree depth :". $ bt-> getBinaryTreeDepth ($ bt-> mRoot); echo "\ r \ n"; echo :"; var_dump ($ bt-> getIsEmpty (); echo "leave the binary tree empty:"; $ bt-> setBinaryTreeNull (); var_dump ($ bt); echo"
";?>

 

 

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.