A simple explanation and code example for the traversal of a binary tree by sequence-first order-middle order-post-sequence traversal

Source: Internet
Author: User

The basic properties of binary tree and the establishment of binary tree see the previous two blog posts:
http://blog.csdn.net/why850901938/article/details/51052936
http://blog.csdn.net/why850901938/article/details/51052156

First, in order to explain the convenience, I built a two-fork tree:
Named as: Tree A

1. What is sequence traversal?
Sequence traversal is based on the hierarchy of the binary tree from top to bottom of the traversal, each layer requires access to the order from left to right;
In tree A, for example, the results of the sequence traversal are:

5 2 6 1 4 8 3 7 9 11 10

2. What is First order traversal?

The order of First order traversal is first to the root node, then to the left node, and finally to the right node;

We look at tree A, first starting from the root node 5, the root output 5, and then the left subtree 2, at this point in the position of 2,2 equivalent to 1 and 42 nodes of the root, so after traversing 2, the first to traverse 2 of the left subtree has no child nodes, so the right sub-tree traversing 2 has left dial hand tree traversal 3, This completes the node 5 of the left subtree traversal, and then traverse 5 of the right subtree 6, first traverse 6 of the left subtree, here does not, just traverse 6 right subtree 8, and then traverse 8 of the left subtree, and 7, of course, 7 No child node, so next is 9, then 11 10 similar.
Finally we get the result of the first order traversal:

5 2 1 4 3 6 8 7 9 11 10

So be sure to remember that the operation to access the root node occurs before traversing its left and right subtree, in the case of tree A, Access 2 after 5, then not access 6, but access to the left and right subtree of 2.

3. What is a middle order traversal?

The middle sequence traversal is the first to Zuozi, then to the root node, and finally to the right subtree;
Then we build Tree B:

For Tree B, the middle sequence traversal is:

1 2 3

If at this point the 2 nodes have left and right subtrees, then we build tree C:

In the case of tree C, the middle sequence traversal is:
* 4 2 5 1 3 *

If the 3 node has a right subtree and no left subtree, we build tree D:

So the first access to the 3 node and then access 6, the result is:

4 2 5 1 3 6

And then back to our big guy tree A;
For tree A, the result of the sequential traversal is:

1 2 3 4 5 6 7 8 9 10 11

For the beginning of the situation, when accessing 5, found that 5 have left dial hand tree 2, first 2, and then visit 2 when the left dial hand tree 1 is found, so definitely A1 first, so this sequence is starting from 1.

Isn't it amazing! Yes, in the middle sequence traversal, all the numbers on the left side of the root node are on the left subtree, and all the numbers on the right are on the right subtree, and sometimes the problem is useful.

4. What is a post-order traversal?

For post-order traversal, it accesses the left node first, then accesses the right node, and finally accesses the root node;

For Tree B:
The results of the post-post traversal are:2 3 1

For tree C:
The results of the post-post traversal are:4 5 2 3 1

Pair with Tree D:
The results of the post-post traversal are:4 5 2 6 3 1

Back to Big dick Tree A:
The result of the subsequent sequence traversal should be:

1 3 4 2 7 10 11 9 8 6 5

So it can be seen that in the post-order traversal of the whole number of nodes is the last to traverse to;

The next play is, how can we achieve the above four kinds of traversal?
Let's start with the sequence traversal :

It should be understood that sequence traversal is a typical base bfs (width first search) model;
That is, the width of the first traversal of the object, from left to right to traverse the binary tree, the implementation method should use the queue ,
That is, the flat-discharge pipeline model, in and out for one-way channels:

The queue is a data structure model that is FIFO (first in the first out), and from the root node of the whole tree, we push the left and right nodes of each root node into the queue sequentially, according to the FIFO feature, the order of each node is exactly the same order as the sequence traversal.

Let's take tree D as an example to explain:
Starting from 1 nodes, first pressing into 1, 1 is pressed into the left and right nodes of the operation in turn pressed into 2 and 3,
Then pops up 1, Next comes out is 2, 2 of the Left and right nodes 4 and 5 press into, Pop 2, then press into 3, put the left node 6 press, Pop 3, so that the second layer of the search end, into the third layer, then traverse the node 4 5 6, and then end;

This is the process, followed by the sequence traversal code:

The code is as follows:

voidBFS (Node *root) { queue<Node*>Q;//Queue declarationnode * node; Q.push (Root);//press the root node of the entire tree first     while(! Q.empty ())//If the queue is not empty, keep going{node = Q.front ();//access the first element of a queue        cout<<node->Value<<" ";//Output The value of the current node        if(Node->left!=null) {Q.push (node->left);//If left node is not empty, press into left node}if(Node->right!=null) {Q.push (node->right);//Press into right node if right node is not empty} q.pop ();//Eject current node}cout<<endl;}

BFS code quote from classmate Blog: http://blog.csdn.net/u011613367/article/details/50950408

Knowledge of the underlying operations and other data structures of the queue and STL information Welcome to my blog:
http://blog.csdn.net/why850901938/article/details/51052062

After the sequence is finished, we will talk about the first order, the middle order, the method and code of the post-sequence traversal:
In a nutshell, first, middle, and post-traversal all use DFS (depth-first search) method, in my understanding of the deep search method is recursive, that is, call their own. Why use recursion?
We'll explain it in conjunction with the code:

The first step is to traverse the binary tree in the first order:

void PreOrderTraverse(BiTree T){   if(T)//如果当前节点不为空   {      printf("%d ",T->data);     //先输出当前节点的值      PreOrderTraverse(T->//再调用自己到左节点      PreOrderTraverse(T->Right);//最后到右节点   }   return;}

Then the middle order traversal:

void InOrderTraverse(BiTree T){   if(T)//如果当前节点不为空   {      PreOrderTraverse(T->Left);    //先调用自己到左节点      printf("%d ",T->data);       //再输出当前节点的值      PreOrderTraverse(T->Right);   //最后到右节点   }}

Last Post-post traversal:

void PostOrderTraverse(BiTree T){   if(T)//如果当前节点不为空   {      PreOrderTraverse(T->Left);   //先调用自己到左节点      PreOrderTraverse(T->Right);  //再到右节点      printf("%d ",T->data);       //最后输出当前节点的值   }}

The main functions are as follows:

int main(){   BiTree T;   int d;   T = CreateBiTree();     //建立二叉树   PreOrderTraverse(T);    //先序遍历   printf("\n");   InOrderTraverse(T);     //中序遍历   printf("\n");   PostOrderTraverse(T);   //后序遍历   printf("\n");   return0;}

To establish a two-fork tree function See: http://blog.csdn.net/why850901938/article/details/51052936

Above in addition to note the citation is original, the pictures are produced by themselves, reproduced please explain, give the link to the line ~ ~ ~

Only on behalf of personal views, welcome to Exchange discussion, do not spray ~ ~ ~

Photoby:wlop

Http://weibo.com/wlop

A simple explanation and code example for the traversal of a binary tree by sequence-first order-middle order-post-sequence traversal

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.