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 ©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->mpbtdata= $btdata;
$this->mroot=null;
$this->getpreordertraversalcreate ($this->mroot);
}
/**
* Build two-prong tree by first-order traversal method
*
* @param btnode two fork 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);
}
}
/**
* 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->mroot instanceof Btnode) {
return false;
}else{
return true;
}
}
/**
* Empty the two fork tree
*
* @return void
*/
Public Function Setbinarytreenull () {
$this->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,& $btarr) {
if ($rootnode!=null) {
$btarr []= $rootnode->mdata;
$this->getpreordertraversal ($rootnode->mlchild, $btarr);
$this->getpreordertraversal ($rootnode->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,& $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 ();
}
}
/**
* 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,& $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 Two-fork root node
* @param array $arrBTdata to receive the value of a variable, passed by reference
* @return void
*/
Public Function getinordertraversalnorecursion ($objRootNode,& $arrBTdata) {
if ($objRootNode instanceof Btnode) {
$objNode = $objRootNode;
$objStack =new stacklinked ();
Middle-order traversal Zuozi and access to root node
do{
while ($objNode!=null) {
$objStack->getpushstack ($objNode);
$objNode = $objNode->mlchild;
}
$objStack->getpopstack ($objNode);
$arrBTdata []= $objNode->mdata;
$objNode = $objNode->mrchild;
}while (! $objStack->getisempty ());
Middle-order traversal 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 ();
}
}
/**
* 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,& $arrBTdata) {
if ($objRootNode!=null) {
$this->getpostordertraversal ($objRootNode->mlchild, $arrBTdata);
$this->getpostordertraversal ($objRootNode->mrchild, $arrBTdata);
$arrBTdata []= $objRootNode->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,& $arrBTdata) {
if ($objRootNode instanceof Btnode) {
$objNode = $objRootNode;
$objStack =new stacklinked ();
$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 ();
}
}
/**
* 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,& $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 ();
}
}
/**
* 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->mlchild = = NULL && $objNode->mrchild = = null) {
$intLeafNodeCount + +;
}
$objRNode = $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;
}
}
/**
* 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->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 "<pre>";
$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->getpreordertraversal ($bt->mroot, $btarr);
Var_dump ($btarr);
echo "First-order non-recursive traversal binary tree: \ r \ n";
$arrBTdata =array ();
$BT->getpreordertraversalnorecursion ($bt->mroot, $arrBTdata);
Var_dump ($arrBTdata);
echo "Middle sequence recursive traversal binary tree: \ r \ n";
$arrBTdata =array ();
$BT->getinordertraversal ($bt->mroot, $arrBTdata);
Var_dump ($arrBTdata);
echo "Middle order non-recursive traversal binary tree: \ r \ n";
$arrBTdata =array ();
$BT->getinordertraversalnorecursion ($bt->mroot, $arrBTdata);
Var_dump ($arrBTdata);
echo "Recursive traversal of binary tree: \ r \ n";
$arrBTdata =array ();
$BT->getpostordertraversal ($bt->mroot, $arrBTdata);
Var_dump ($arrBTdata);
echo "second-sequence non-recursive traversal binary tree: \ r \ n";
$arrBTdata =array ();
$BT->getpostordertraversalnorecursion ($bt->mroot, $arrBTdata);
Var_dump ($arrBTdata);
echo "traverse the binary tree hierarchically: \ r \ n";
$arrBTdata =array ();
$BT->getlevelordertraversal ($bt->mroot, $arrBTdata);
Var_dump ($arrBTdata);
echo "leaves the number of nodes:". $BT->getleafnodecount ($BT->mroot);
echo "\ r \ n";
echo "Two fork Tree Depth:". $BT->getbinarytreedepth ($BT->mroot);
echo "\ r \ n";
echo "Determines whether the binary tree is empty:";
Var_dump ($BT->getisempty ());
echo "Put two fork tree empty after:";
$BT->setbinarytreenull ();
Var_dump ($BT);
echo "</pre>";
?>