Binary Tree Traversal algorithm

Source: Internet
Author: User

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 to be accessed sequentially.

The picture is Baidu search ... Thank you for providing the hero of the diagram.

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

Middle sequence Traversal binary tree: If the binary tree is empty then return, if the binary tree is not empty, then start from the root node, the middle sequence traverse the root node of the left subtree, and then access the root node, the final middle sequence traversal right subtree, the traversal order is CBEGDFA.

Post-order Traversal binary tree: If the binary tree is empty then return, if the binary tree is not empty, then left-to-right first leaves after the node's access traversal access to the left and the sub-tree, and finally access to the root node. The access order is CGEFDBA.

Sequence Traversal binary tree: If the binary tree is empty then return, if the binary tree is not empty, then from the first layer of the tree, that is, the root node to access, from the top and bottom-by-layer traversal, in the same layer, in the same level from left to right, the node is accessed one by one. The access order is ABCDEFG.


Now, we use 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 picture above.


The binary tree structure code is as follows:



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



A binary tree algorithm for pre-sequence traversal

echo ' pre-sequence 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 two: Middle sequence traversal binary tree



A binary tree algorithm with middle sequence traversal

echo ' Middle sequence 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 three: Sequential traversal of binary tree



Sequential Traversal binary tree algorithm

Echo ' post-routing 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 ']);

}

  • 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.