Binary Tree traversal is a process where values start from the root node and all nodes in the binary tree are accessed in order so that each node is accessed once and only accessed sequentially.
Binary Tree traversal is a process where values start from the root node and all nodes in the binary tree are accessed in order so that each node is accessed once and only accessed sequentially.
Figure: Baidu search... Thank you for providing the map hero ..
Forward traversal of a binary tree: if the binary tree is empty, return. if the binary tree is not empty, traverse the left tree first, then traverse the right tree, and the traversal order is ABCDEGF.
In-order binary tree traversal: if the binary tree is empty, return. if the binary tree is not empty, the left subtree of the root node is traversed from the root node, and then the root node is accessed, finally, the right subtree is traversed in the middle order, and the traversal order is CBEGDFA.
Post-sequential binary tree traversal: if the binary tree is empty, it is returned. if the binary tree is not empty, the left-to-right first leaf node access traversal accesses the left and right sub-trees, and finally accesses the root node. The access sequence is CGEFDBA.
Sequence traversal binary tree: if the binary tree is empty, return. if the binary tree is not empty, access from the first layer of the tree, that is, the root node, and traverse from top to bottom. in the same layer, access nodes one by one from left to right. The access sequence is ABCDEFG.
Now, we use PHP code to traverse the binary tree structure. A binary tree stores a large array. each node has three fields. data indicates the value of the node. lChild indicates the left-side subnode of the node, and rChild indicates the right-side subnode of the node. The structure of the binary tree is shown in the figure above.
The binary tree structure code is as follows:
// Binary 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 1: traverse binary trees in the forward order
// Pre-order traversal of the binary tree algorithm
Echo 'forward traversal binary tree algorithm :';
PreOrderTraverse ($ arr );
Echo'
';
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 2: traversing a binary tree in a central order
// Median traversal binary tree algorithm
Echo 'Sequential traversal binary tree algorithm :';
InOrderTraverse ($ arr );
Echo'
';
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 3: post-order binary tree traversal
// Post-order traversal of the binary tree algorithm
Echo 'post-order traversal binary tree algorithm :';
PostOrderTraverse ($ arr );
Echo'
';
Function postOrderTraverse ($ node ){
If (empty ($ node )){
Return;
}
// Left node
PostOrderTraverse ($ node ['lchild ']);
// Right node
PostOrderTraverse ($ node ['rchild ']);
// Output value
Print_r ($ node ['data']);
}