PHP two cross tree traversal algorithm and example

Source: Internet
Author: User
Tags php code

Binary tree traversal, is the value from the root node, in some order to access all nodes in the binary tree, so that each node is accessed once and only accessed according to

The picture is Baidu searches ... Thank you for the hero of the map.

Pre-sequence Traversal binary tree: If the binary tree is empty then return, if the binary tree is not empty, first traverse the left tree, and then traverse the right tree, traversal order for ABCDEGF.

In order to traverse the binary tree: If the binary tree is empty then return, if the binary tree is not empty, then start from the root node, in order to traverse the root node of the left subtree, and then access to the root node, the last in the order to traverse the right subtree, traversal order for CBEGDFA.

After the second traversal of the binary tree: If the binary tree is empty then return, if the binary tree is not empty, then from left to right before the leaves of the node access to the left tree, the last is to access the root node. The access order is CGEFDBA.

Sequence Traversal binary tree: If the binary tree is empty, return, if the binary tree is not empty, then from the first layer of the tree, that is, the root node to begin access, from the top down through the layer, in the same layer, in the same level, from left to right in order to access the node. The access order is ABCDEFG.

Now, we use the PHP code to traverse the binary tree structure. Binary tree is put a large array, each node has three fields, data represents the value of this node, Lchild represents the node's left child node, Rchild represents the node's right child node. The structure of the binary tree we use the above picture.

The binary tree structure code is as follows:

<?php

Two fork Tree

$arr = Array (

' Data ' => ' A ',

' Lchild ' => Array (

' Data ' => ' B ',

' Lchild ' => Array (

' Data ' => ' C ',

' Lchild ' => Array (),

' Rchild ' => Array (),

),

' Rchild ' => Array (

' Data ' => ' D ',

' Lchild ' => Array (

' Data ' => ' E ',

' Lchild ' => Array (),

' Rchild ' => Array (

' Data ' => ' G ',

' Lchild ' => Array (),

' Rchild ' => Array (),

),

),

' Rchild ' => Array (

' Data ' => ' F ',

' Lchild ' => Array (),

' Rchild ' => Array (),

),

),

),

' Rchild ' => Array (),

);

Traversal algorithm one: pre-sequence traversal binary tree

<?php

Pre-ordered traversal binary tree algorithm

echo ' pre-sequence traversal binary tree algorithm: ';

Preordertraverse ($arr);

Echo ' <Br> ';

function Preordertraverse ($node) {

if (empty ($node)) {

Return

}

Output value

Print_r ($node [' data ']);

Left node

Preordertraverse ($node [' lchild ']);

Right node

Preordertraverse ($node [' rchild ']);

}

Traversal algorithm two: in the sequence of two-fork tree

<?php

A sequential traversal binary tree algorithm

Echo ' Central sequence traversal binary tree algorithm: ';

Inordertraverse ($arr);

Echo ' <Br> ';

function Inordertraverse ($node) {

if (empty ($node)) {

Return

}

Left node

Inordertraverse ($node [' lchild ']);

Output value

Print_r ($node [' data ']);

Right node

Inordertraverse ($node [' rchild ']);

}

Traversal algorithm three: After the second traversal of the binary tree

<?php

Sequence Traversal binary tree algorithm

echo ' sequence traversal binary tree algorithm: ';

Postordertraverse ($arr);

Echo ' <Br> ';

function Postordertraverse ($node) {

if (empty ($node)) {

Return

}

Left node

Postordertraverse ($node [' lchild ']);

Right node

Postordertraverse ($node [' rchild ']);

Output value

Print_r ($node [' data ']);

}


Example

<?php
/**
* The creation of binary tree and basic operation
*
*1. Construction method, initialization of two-fork tree
*2. Establish two-fork tree by first-order traversal method
*3. Traversing the binary tree by first order
*4. Non-recursive algorithm for first-order traversal
*5. Sequential traversal of binary tree
*6, a non-recursive algorithm for sequence traversal
*7. Subsequent traversal of the binary tree
*8. Sequence traversal non-recursive algorithm
*9. Hierarchical Traverse binary Tree
*10. To find the number of nodes in two-fork tree leaves
*11. To find the depth of the two-fork tree
*12. Determine if the binary tree is an empty tree
*13. Empty two-fork tree
*
* @author xudianyang<>
* @version $Id: binarytree.class.php,v 1.0 2011/02/13 13:33:00 uw Exp
* @copyright &copy;2011,xudianyang
*/
Header (' content-type:text/html;charset=gb2312 ');

PHP data structure in the five stacks of PHP implementation and stack of basic operations can find the class
Include_once ("./stacklinked.class.php");

In the PHP data structure of the seven queues of chained storage and the basic operations of queues can be found in this class


Include_once ('./queuelinked.class.php ');


Class btnode{


Zoozi Tree "Pointers"


Public $mLchild =null;


Right subtree "pointer"


Public $mRchild =null;


Node data field


Public $mData =null; Left flag field, 1 indicates mlchild "point" node left child, 2 for "Point" node direct precursor


Public $intLeftTag =null;


Right flag field, 1 indicates mrchild "point" node right child, 2 for "point" node direct successor


Public $intRightTag =null;


}


Class binarytree{


Root node


Public $mRoot;


Two-fork tree data input based on first order traversal


Public $mPBTdata =null;


/**


* Construct method, initialize to build two fork tree


*


* @param array $btdata based on the first sequence traversal of the input of the two-tree data, one-dimensional array, each element represents a two-fork tree a node value, the extension node value of "[Length 0 of the string]


* @return void


*/


Public function __construct ($btdata =array ()) {


$this-&gt;mpbtdata= $btdata;


$this-&gt;mroot=null;


$this-&gt;getpreordertraversalcreate ($this-&gt;mroot);


}


/**


* Build two-prong tree by first-order traversal method


*


* @param btnode two fork tree node, passed by reference


* @return void


*/


Public Function Getpreordertraversalcreate (&amp; $btnode) {


$elem =array_shift ($this-&gt;mpbtdata);


if ($elem = = = ") {


$btnode =null;


}else if ($elem = = null) {


Return


}else{


$btnode =new Btnode ();


$btnode-&gt;mdata= $elem;


$this-&gt;getpreordertraversalcreate ($btnode-&gt;mlchild);


$this-&gt;getpreordertraversalcreate ($btnode-&gt;mrchild);


}


}


/**


* Determine if the binary tree is empty


*


* @return Boolean returns True if the binary tree is not empty, or false


**/


Public Function Getisempty () {


if ($this-&gt;mroot instanceof Btnode) {


return false;


}else{


return true;


}


}


/**


* Empty the two fork tree


*


* @return void


*/


Public Function Setbinarytreenull () {


$this-&gt;mroot=null;


}


/**


* Traversing the binary tree by first order


*


* @param btnode $rootnode The root node in the traversal process


* @param array $btarr to receive the value of a variable, passed by reference


* @return void


*/


Public Function getpreordertraversal ($rootnode,&amp; $btarr) {


if ($rootnode!=null) {


$btarr []= $rootnode-&gt;mdata;


$this-&gt;getpreordertraversal ($rootnode-&gt;mlchild, $btarr);


$this-&gt;getpreordertraversal ($rootnode-&gt;mrchild, $btarr);


}


}


/**


The non-recursive algorithm for first-order traversal


*


* @param btnode $objRootNode Two-fork root node


* @param array $arrBTdata to receive the value of a variable, passed by reference


* @return void


*/


Public Function getpreordertraversalnorecursion ($objRootNode,&amp; $arrBTdata) {


if ($objRootNode instanceof Btnode) {


$objNode = $objRootNode;


$objStack =new stacklinked ();


do{


$arrBTdata []= $objNode-&gt;mdata;


$objRNode = $objNode-&gt;mrchild;


if ($objRNode!=null) {


$objStack-&gt;getpushstack ($objRNode);


}


$objNode = $objNode-&gt;mlchild;


if ($objNode ==null) {


$objStack-&gt;getpopstack ($objNode);


}


}while ($objNode!=null);


}else{


$arrBTdata =array ();


}


}


/**


* Sequential traversal of binary tree


*


* @param the root node of the Btnode $objRootNode process


* @param array $arrBTdata to receive the value of a variable, passed by reference


* @return void


*/


Public Function getinordertraversal ($objRootNode,&amp; $arrBTdata) {


if ($objRootNode!=null) {


$this-&gt;getinordertraversal ($objRootNode-&gt;mlchild, $arrBTdata);


$arrBTdata []= $objRootNode-&gt;mdata;


$this-&gt;getinordertraversal ($objRootNode-&gt;mrchild, $arrBTdata);


}


}


/**


Non-recursive algorithm for sequential traversal


*


* @param btnode $objRootNode Two-fork root node


* @param array $arrBTdata to receive the value of a variable, passed by reference


* @return void


*/


Public Function getinordertraversalnorecursion ($objRootNode,&amp; $arrBTdata) {


if ($objRootNode instanceof Btnode) {


$objNode = $objRootNode;


$objStack =new stacklinked ();


Middle-order traversal Zuozi and access to root node


do{


while ($objNode!=null) {


$objStack-&gt;getpushstack ($objNode);


$objNode = $objNode-&gt;mlchild;


}


$objStack-&gt;getpopstack ($objNode);


$arrBTdata []= $objNode-&gt;mdata;


$objNode = $objNode-&gt;mrchild;


}while (! $objStack-&gt;getisempty ());


Middle-order traversal right subtree


do{


while ($objNode!=null) {


$objStack-&gt;getpushstack ($objNode);


$objNode = $objNode-&gt;mlchild;


}


$objStack-&gt;getpopstack ($objNode);


$arrBTdata []= $objNode-&gt;mdata;


$objNode = $objNode-&gt;mrchild;


}while (! $objStack-&gt;getisempty ());


}else{


$arrBTdata =array ();


}


}


/**


* Subsequent traversal of the two-fork tree


*


* @param btnode $objRootNode The root node in the traversal process


* @param array $arrBTdata to receive the value of arrays of variables, referred to by the way passed


* @return void


*/


Public Function getpostordertraversal ($objRootNode,&amp; $arrBTdata) {


if ($objRootNode!=null) {


$this-&gt;getpostordertraversal ($objRootNode-&gt;mlchild, $arrBTdata);


$this-&gt;getpostordertraversal ($objRootNode-&gt;mrchild, $arrBTdata);


$arrBTdata []= $objRootNode-&gt;mdata;


}


}


/**


* Sequence traversal non-recursive algorithm


*


Btnode $objRootNode two fork root node


Array $arrBTdata to receive the value of a variable, passed by reference


void


*/


Public Function getpostordertraversalnorecursion ($objRootNode,&amp; $arrBTdata) {


if ($objRootNode instanceof Btnode) {


$objNode = $objRootNode;


$objStack =new stacklinked ();


$objTagStack =new stacklinked ();


$tag = 1;


do{


while ($objNode!=null) {


$objStack-&gt;getpushstack ($objNode);


$objTagStack-&gt;getpushstack (1);


$objNode = $objNode-&gt;mlchild;


}


$objTagStack-&gt;getpopstack ($tag);


$objTagStack-&gt;getpushstack ($tag);


if ($tag = = 1) {


$objStack-&gt;getpopstack ($objNode);


$objStack-&gt;getpushstack ($objNode);


$objNode = $objNode-&gt;mrchild;


$objTagStack-&gt;getpopstack ($tag);


$objTagStack-&gt;getpushstack (2);

}else{


$objStack-&gt;getpopstack ($objNode);


$arrBTdata []= $objNode-&gt;mdata;


$objTagStack-&gt;getpopstack ($tag);


$objNode =null;


}


}while (! $objStack-&gt;getisempty ());


}else{


$arrBTdata =array ();


}


}


/**


* Hierarchical traverse binary tree


*


* @param btnode $objRootNode Two-fork root node


* @param array $arrBTdata to receive the value of a variable, passed by reference


* @return void


*/


Public Function getlevelordertraversal ($objRootNode,&amp; $arrBTdata) {


if ($objRootNode instanceof Btnode) {


$objNode = $objRootNode;


$objQueue =new queuelinked ();


$objQueue-&gt;getinsertelem ($objNode);


while (! $objQueue-&gt;getisempty ()) {


$objQueue-&gt;getdeleteelem ($objNode);


$arrBTdata []= $objNode-&gt;mdata;


if ($objNode-&gt;mlchild!= null) {


$objQueue-&gt;getinsertelem ($objNode-&gt;mlchild);


}


if ($objNode-&gt;mrchild!= null) {


$objQueue-&gt;getinsertelem ($objNode-&gt;mrchild);


}


}


}else{


$arrBTdata =array ();


}


}


/**


* Find the number of nodes in the binary tree leaves


*


* @param btnode $objRootNode Two-fork root node


* @return int parameter pass error return-1


**/


Public Function Getleafnodecount ($objRootNode) {


if ($objRootNode instanceof Btnode) {


$intLeafNodeCount = 0;


$objNode = $objRootNode;


$objStack =new stacklinked ();


do{


if ($objNode-&gt;mlchild = = NULL &amp;&amp; $objNode-&gt;mrchild = = null) {


$intLeafNodeCount + +;


}


$objRNode = $objNode-&gt;mrchild;


if ($objRNode!= null) {


$objStack-&gt;getpushstack ($objRNode);


}


$objNode = $objNode-&gt;mlchild;


if ($objNode = = null) {


$objStack-&gt;getpopstack ($objNode);


}


}while ($objNode!= null);


return $intLeafNodeCount;


}else{


return-1;


}


}


/**


* Find the depth of the two-fork tree


*


* @param btnode $objRootNode Two-fork root node


* @return int parameter pass error return-1


*/


Public Function getbinarytreedepth ($objRootNode) {


if ($objRootNode instanceof Btnode) {


$objNode = $objRootNode;


$objQueue =new queuelinked ();


$intBinaryTreeDepth = 0;


$objQueue-&gt;getinsertelem ($objNode);


$objLevel = $objNode;


while (! $objQueue-&gt;getisempty ()) {


$objQueue-&gt;getdeleteelem ($objNode);


if ($objNode-&gt;mlchild!= null) {


$objQueue-&gt;getinsertelem ($objNode-&gt;mlchild);


}


if ($objNode-&gt;mrchild!= null) {


$objQueue-&gt;getinsertelem ($objNode-&gt;mrchild);


}


if ($objLevel = = $objNode) {


$intBinaryTreeDepth + +;


$objLevel =@ $objQueue-&gt;mrear-&gt;melem;


}


}


return $intBinaryTreeDepth;


}else{


return-1;


}


}


}


echo "&lt;pre&gt;";


$BT =new BinaryTree (Array (' A ', ' B ', ' D ', ', ', ', ', ', ', ', ', ', ' E ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ',


echo "binary tree structure: \ r \ n";


Var_dump ($BT);


$btarr =array ();


echo "First-order recursive traversal of binary tree: \ r \ n";


$BT-&gt;getpreordertraversal ($bt-&gt;mroot, $btarr);


Var_dump ($btarr);


echo "First-order non-recursive traversal binary tree: \ r \ n";


$arrBTdata =array ();


$BT-&gt;getpreordertraversalnorecursion ($bt-&gt;mroot, $arrBTdata);


Var_dump ($arrBTdata);


echo "Middle sequence recursive traversal binary tree: \ r \ n";


$arrBTdata =array ();


$BT-&gt;getinordertraversal ($bt-&gt;mroot, $arrBTdata);


Var_dump ($arrBTdata);


echo "Middle order non-recursive traversal binary tree: \ r \ n";


$arrBTdata =array ();


$BT-&gt;getinordertraversalnorecursion ($bt-&gt;mroot, $arrBTdata);


Var_dump ($arrBTdata);


echo "Recursive traversal of binary tree: \ r \ n";


$arrBTdata =array ();


$BT-&gt;getpostordertraversal ($bt-&gt;mroot, $arrBTdata);


Var_dump ($arrBTdata);


echo "second-sequence non-recursive traversal binary tree: \ r \ n";


$arrBTdata =array ();


$BT-&gt;getpostordertraversalnorecursion ($bt-&gt;mroot, $arrBTdata);


Var_dump ($arrBTdata);


echo "traverse the binary tree hierarchically: \ r \ n";


$arrBTdata =array ();


$BT-&gt;getlevelordertraversal ($bt-&gt;mroot, $arrBTdata);


Var_dump ($arrBTdata);


echo "leaves the number of nodes:". $BT-&gt;getleafnodecount ($BT-&gt;mroot);


echo "\ r \ n";


echo "Two fork Tree Depth:". $BT-&gt;getbinarytreedepth ($BT-&gt;mroot);


echo "\ r \ n";


echo "Determines whether the binary tree is empty:";


Var_dump ($BT-&gt;getisempty ());


echo "Put two fork tree empty after:";


$BT-&gt;setbinarytreenull ();


Var_dump ($BT);


echo "&lt;/pre&gt;";


?&gt;

Related Article

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.