• 剑指offer——面试题32.1:分行从上到下打印二叉树


    void BFSLayer(BinaryTreeNode* pRoot)
    {
        if(pRoot==nullptr)
            return;
        queue<BinaryTreeNode*> pNode;
        int curLayer=1,nextLayer=0;
        pNode.push(pRoot);
        while(!pNode.empty())
        {
            BinaryTreeNode* pFront=pNode.front();
            cout<<pFront->m_Value<<' ';
            pNode.pop();
            curLayer--;
            if(pFront->m_pLeft!=nullptr)
            {
                nextLayer++;
                pNode.push(pFront->m_pLeft);
            }
            if(pFront->m_pRight!=nullptr)
            {
                nextLayer++;
                pNode.push(pFront->m_pRight);
            }
            if(curLayer==0)
            {
                curLayer=nextLayer;
                nextLayer=0;
                cout<<endl;
            }
        }
    }
    函数
      1 #include"BinaryTree.h"
      2 
      3 // ====================测试代码====================
      4 //            8
      5 //        6      10
      6 //       5 7    9  11
      7 void Test1()
      8 {
      9     BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
     10     BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
     11     BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
     12     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
     13     BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
     14     BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
     15     BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);
     16 
     17     ConnectTreeNodes(pNode8, pNode6, pNode10);
     18     ConnectTreeNodes(pNode6, pNode5, pNode7);
     19     ConnectTreeNodes(pNode10, pNode9, pNode11);
     20 
     21     printf("====Test1 Begins: ====
    ");
     22     printf("Expected Result is:
    ");
     23     printf("8 
    ");
     24     printf("6 10 
    ");
     25     printf("5 7 9 11 
    
    ");
     26 
     27     printf("Actual Result is: 
    ");
     28     BFSLayer(pNode8);
     29     printf("
    ");
     30 
     31     DestroyTree(pNode8);
     32 }
     33 
     34 //            5
     35 //          4
     36 //        3
     37 //      2
     38 void Test2()
     39 {
     40     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
     41     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
     42     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
     43     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
     44 
     45     ConnectTreeNodes(pNode5, pNode4, nullptr);
     46     ConnectTreeNodes(pNode4, pNode3, nullptr);
     47     ConnectTreeNodes(pNode3, pNode2, nullptr);
     48 
     49     printf("====Test2 Begins: ====
    ");
     50     printf("Expected Result is:
    ");
     51     printf("5 
    ");
     52     printf("4 
    ");
     53     printf("3 
    ");
     54     printf("2 
    
    ");
     55 
     56     printf("Actual Result is: 
    ");
     57     BFSLayer(pNode5);
     58     printf("
    ");
     59 
     60     DestroyTree(pNode5);
     61 }
     62 
     63 //        5
     64 //         4
     65 //          3
     66 //           2
     67 void Test3()
     68 {
     69     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
     70     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
     71     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
     72     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
     73 
     74     ConnectTreeNodes(pNode5, nullptr, pNode4);
     75     ConnectTreeNodes(pNode4, nullptr, pNode3);
     76     ConnectTreeNodes(pNode3, nullptr, pNode2);
     77 
     78     printf("====Test3 Begins: ====
    ");
     79     printf("Expected Result is:
    ");
     80     printf("5 
    ");
     81     printf("4 
    ");
     82     printf("3 
    ");
     83     printf("2 
    
    ");
     84 
     85     printf("Actual Result is: 
    ");
     86     BFSLayer(pNode5);
     87     printf("
    ");
     88 
     89     DestroyTree(pNode5);
     90 }
     91 
     92 void Test4()
     93 {
     94     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
     95 
     96     printf("====Test4 Begins: ====
    ");
     97     printf("Expected Result is:
    ");
     98     printf("5 
    
    ");
     99 
    100     printf("Actual Result is: 
    ");
    101     BFSLayer(pNode5);
    102     printf("
    ");
    103 
    104     DestroyTree(pNode5);
    105 }
    106 
    107 void Test5()
    108 {
    109     printf("====Test5 Begins: ====
    ");
    110     printf("Expected Result is:
    ");
    111 
    112     printf("Actual Result is: 
    ");
    113     BFSLayer(nullptr);
    114     printf("
    ");
    115 }
    116 
    117 //        100
    118 //        /
    119 //       50
    120 //         
    121 //         150
    122 void Test6()
    123 {
    124     BinaryTreeNode* pNode100 = CreateBinaryTreeNode(100);
    125     BinaryTreeNode* pNode50 = CreateBinaryTreeNode(50);
    126     BinaryTreeNode* pNode150 = CreateBinaryTreeNode(150);
    127 
    128     ConnectTreeNodes(pNode100, pNode50, nullptr);
    129     ConnectTreeNodes(pNode50, nullptr, pNode150);
    130 
    131     printf("====Test6 Begins: ====
    ");
    132     printf("Expected Result is:
    ");
    133     printf("100 
    ");
    134     printf("50 
    ");
    135     printf("150 
    
    ");
    136 
    137     printf("Actual Result is: 
    ");
    138     BFSLayer(pNode100);
    139     printf("
    ");
    140 }
    141 
    142 int main(int argc, char* argv[])
    143 {
    144     Test1();
    145     Test2();
    146     Test3();
    147     Test4();
    148     Test5();
    149     Test6();
    150 
    151     return 0;
    152 }
    测试代码
  • 相关阅读:
    关于SharePoint 2010 Beta2的安装
    SharePoint 2010新体验10 文档评分
    SharePoint 2010 新体验3 文档集
    我在SharePoint 2010 Day上做的一个演示课程
    下载 SharePoint Server 2010 Public Beta (包含中文版)
    关于QuickPart的Q&A
    SharePoint服务器连接配置数据库的连接字符串保存在哪里?
    为SharePoint 2010创建Application Page
    Let's talk about SharePoint 2010
    SharePoint 2010 新体验8 内容类型集线器 (Content Type Hub)
  • 原文地址:https://www.cnblogs.com/acm-jing/p/10432611.html
Copyright © 2020-2023  润新知