Print a two-fork tree as multiple lines __ Two-fork trees to print multiple lines

Source: Internet
Author: User

problem : Print the binary tree from top to bottom, and the nodes in the same layer are printed to one line from left to right. For example:


Print results:

8

6 10

5 7 9 11

Analysis: In order to print each row of the two-fork tree to a single line, we need two variables: One variable represents the number of nodes that have not been printed in the current layer, and the other represents the next level of nodes.

Print binary tree void print (binarytreenode* proot) {if (Proot = NULL) return by row;
    Std::queue<binarytreenode*> nodes;
    Nodes.push (Proot);
    int nextlevel = 0;
    int tobeprinted = 1;
        while (!nodes.empty ()) {binarytreenode* Pnode = Nodes.front ();

        printf ("%d", pnode->m_nvalue);
            if (pnode->m_pleft!= NULL) {Nodes.push (pnode->m_pleft);
        ++nextlevel;
            } if (Pnode->m_pright!= NULL) {Nodes.push (pnode->m_pright);
        ++nextlevel;
        } nodes.pop ();
        --tobeprinted;
            if (tobeprinted = = 0) {printf ("\ n");
            tobeprinted = Nextlevel;
        Nextlevel = 0; }}//==================== Test Code ====================//8/6 10//5 7 9 1
    1 void Test1 () {binarytreenode* pNode8 = Createbinarytreenode (8); binarytreenode* PNode6 = CreatebinarytreenOde (6);
    binarytreenode* PNode10 = Createbinarytreenode (10);
    binarytreenode* PNODE5 = Createbinarytreenode (5);
    binarytreenode* PNode7 = Createbinarytreenode (7);
    binarytreenode* PNode9 = Createbinarytreenode (9);

    binarytreenode* pNode11 = Createbinarytreenode (11);
    Connecttreenodes (PNode8, PNode6, PNode10);
    Connecttreenodes (PNode6, PNODE5, PNode7);

    Connecttreenodes (PNode10, PNode9, PNODE11);
    printf ("====test1 begins: ====\n");
    printf ("Expected result is:\n");
    printf ("8 \ n");
    printf ("6 \ n");

    printf ("5 7 9 one \ n");
    printf ("Actual result is: \ n");
    Print (PNODE8);

    printf ("\ n");
Destroytree (PNODE8); }//5/4//3//2 void Test2 () {binarytreenode* PNODE5 = Createbinarytreenode (5)
    ;
    binarytreenode* pNode4 = Createbinarytreenode (4);
    binarytreenode* pNode3 = Createbinarytreenode (3);

    binarytreenode* PNode2 = Createbinarytreenode (2); Connecttreenodes (PNODE5, PNode4, NULL);
    Connecttreenodes (PNode4, PNode3, NULL);

    Connecttreenodes (PNode3, PNode2, NULL);
    printf ("====test2 begins: ====\n");
    printf ("Expected result is:\n");
    printf ("5 \ n");
    printf ("4 \ n");
    printf ("3 \ n");

    printf ("2 \ n");
    printf ("Actual result is: \ n");
    Print (PNODE5);

    printf ("\ n");
Destroytree (PNODE5); }//5//4//3//2 void Test3 () {binarytreenode* PNODE5 = Createbinarytreenode (
    5);
    binarytreenode* pNode4 = Createbinarytreenode (4);
    binarytreenode* pNode3 = Createbinarytreenode (3);

    binarytreenode* PNode2 = Createbinarytreenode (2);
    Connecttreenodes (PNODE5, NULL, PNODE4);
    Connecttreenodes (PNode4, NULL, pNode3);

    Connecttreenodes (PNode3, NULL, PNode2);
    printf ("====test3 begins: ====\n");
    printf ("Expected result is:\n");
    printf ("5 \ n");
    printf ("4 \ n");
    printf ("3 \ n");

    printf ("2 \ n");
    printf ("Actual result is: \ n");
    Print (PNODE5); Printf("\ n");
Destroytree (PNODE5);

    } void Test4 () {binarytreenode* PNODE5 = Createbinarytreenode (5);
    printf ("====test4 begins: ====\n");
    printf ("Expected result is:\n");

    printf ("5 \ n");
    printf ("Actual result is: \ n");
    Print (PNODE5);

    printf ("\ n");
Destroytree (PNODE5);
    } void Test5 () {printf ("====TEST5 begins: ====\n");

    printf ("Expected result is:\n");
    printf ("Actual result is: \ n");
    Print (NULL);
printf ("\ n"); }///////////////////////Test6 () {binarytreenode* pNode100 = Crea
    Tebinarytreenode (100);
    binarytreenode* PNode50 = Createbinarytreenode (50);

    binarytreenode* pNode150 = Createbinarytreenode (150);
    Connecttreenodes (pNode100, PNode50, NULL);

    Connecttreenodes (PNode50, NULL, pNode150);
    printf ("====test6 begins: ====\n");
    printf ("Expected result is:\n");
    printf ("\ n");
    printf ("\ n");

    printf ("F \ n"); printf ("Actual result IS: \ n ");
    Print (pNode100);
printf ("\ n");
    int main (int argc, char* argv[]) {Test1 ();
    Test2 ();
    Test3 ();
    Test4 ();
    Test5 ();

	Test6 ();
return 0; }



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.