Print binary tree from top down

Source: Internet
Author: User

Topic

From top to bottom, each node of the two-fork tree is printed, and the nodes of the same layer are printed in order from left to right. For example:

Print Result: 8,6,10,5,7,9,11.

Analysis

Binary Tree Traversal mode: pre-order, sequence, order, there is a breadth of priority traversal, in the graph is involved in the diachronic, while the binary tree can be regarded as a degenerate diagram, from the example can be seen, the printing sequence is determined by the layer, observed that after printing the root node, and then print its left and right nodes, And then continue to print left side of the child and the right node of the child, so you can find that if the children of their nodes in a container, print the parent node, and then take their children to print sequentially, to illustrate the above example:
1. After printing 8, place 6, 10 in the container;
2. Print 6, place 5, 7 inside the container;
3. Print 10, put 9, 11 into the container;
4. Print 5,5 for leaf node, no Child left and right, so print the next element in the container 7, and then print out 9, 11 in turn.

From the above can be observed, advanced container first printing, first in first out of the pattern, and the data structure in the queue is this mode, so this container using the queue mode, but with the normal queue slightly different, to note that the elements of the queue is stored a binary tree node, there are left and right children members, so it is best to use array queue, Linked list queues are also possible, but pointers are relatively complex.

As a result, the following ideas are summarized as follows:
1, the root node into the queue;
2, print the root node, left and right children into the queue;
3. Loop the second step until no element in the queue has entered.

"Test Code"

//print binary tree from top down (sequence traversal, breadth-first traversal)#include <stdio.h>#include <queue>using namespace STD;structbinarytreenode{intM_nvalue;    binarytreenode* M_plef; Binarytreenode* m_pright;}; binarytreenode* Createbinarytreenode (intValue) {binarytreenode* Pnode =NewBinarytreenode ();    Pnode->m_nvalue = value;    Pnode->m_plef = NULL; Pnode->m_pright = NULL;returnPnode;}voidConnectbinarytreenode (binarytreenode* pparent, binarytreenode* pleftchild, binarytreenode* PRigh Tchild) {if(!pparent | |!pleftchild | |!prightchild)return;    Pparent->m_plef = Pleftchild; Pparent->m_pright = Prightchild;}voidPrinttreefromtoptobottom (binarytreenode* proot) {if(Proot = = NULL)return; queue<BinaryTreeNode*>Btnqueue; Btnqueue.push (Proot); while(!btnqueue.empty ())        {binarytreenode* ptemp = Btnqueue.front (); Btnqueue.pop ();printf("%d", Ptemp->m_nvalue);if(Ptemp->m_plef) Btnqueue.push (Ptemp->m_plef);if(ptemp->m_pright) Btnqueue.push (ptemp->m_pright); }}voidTest () {binarytreenode* pNode1 = Createbinarytreenode (8); binarytreenode* PNode2 = Createbinarytreenode (6); binarytreenode* pNode3 = Createbinarytreenode (Ten); binarytreenode* pNode4 = Createbinarytreenode (5); binarytreenode* PNODE5 = Createbinarytreenode (7); binarytreenode* PNode6 = Createbinarytreenode (9); binarytreenode* PNode7 = Createbinarytreenode ( One);    Connectbinarytreenode (PNODE1,PNODE2,PNODE3);    Connectbinarytreenode (PNODE2,PNODE4,PNODE5);    Connectbinarytreenode (PNODE3,PNODE6,PNODE7); Printtreefromtoptobottom (PNODE1);}intMain () {test ();return 0;}

Output

"Program description"

1. For the subject, if the pure use of C to achieve, will be a lot of work, and considering that the existing compiler C and C + + are compatible, it is possible to take advantage of the advantages of the C + + packaging library, in the C + + library, there is a queue, here just add
#include<queue>
However, it is important to note that the C + + library must be added
using namespace std;
Specific reference to C + + related basic explanations, no longer repeat.
Relevant applications in 2.queue Library explained:

queue<BinaryTreeNode*> btnQueue;

Initialize queue, element type binarytreenode*, queue name Btnqueue

btnQueue.push(pRoot)

Press the root node into the queue

btnQueue.pop()

Out queue

btnQueue.empty()

Whether the queue is empty, can be understood as null as 1, non-empty is 0

btnQueue.front()

Header of the queue

"Reference Document"
Http://blog.csdn.net/walkerkalr/article/details/21109933?utm_source=tuicool

Print binary tree from top down

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.