バイナリ木の枝が上下逆さまに印刷されています。三十タイトルII「の安全プランを証明」

// 顔質問32(B):二分木の枝が逆さまに印刷されている
 // 同じノード層を印刷するために左から右へ、上から下に層にバイナリツリーを印刷する、各層:タイトル
 // 印刷にライン。

#include <cstdioを> 
の#include " BinaryTree.h " 
の#include <キュー> 無効印刷(BinaryTreeNode * PROOT)
{ IF(PROOT == nullptr a)に
         戻り
    STDを両端キュー:: <BinaryTreeNode *> dequeTreeNode; 
    dequeTreeNode.push_back(PROOT) ; INT nextLevel = 0 ; // 回数は、次の層に印刷するために必要なINT toBePrinted =を1。 ; // レイヤの現在の数は、印刷されます


    

    
    

    しばらく(!dequeTreeNode.empty())
    { 
        BinaryTreeNode * PNODE = dequeTreeNode.front(); 
        dequeTreeNode.pop_front(); 

        printf(" %d個"、pNode-> m_nValue)。

        もし(pNode-> m_pLeft)
        { 
            dequeTreeNode.push_back(PNODE - > m_pLeft)。
            ++ nextLevel; 
        } 
            
        であれば(pNode-> m_pRight)
        { 
            dequeTreeNode.push_back(PNODE - > m_pRight)。
            ++ nextLevel; 
        }

        --toBePrinted; // 印刷枚数-1 
        IF(toBePrinted == 0// 、データを完全にリセットされ、現在層の印刷
        { 
            toBePrinted = nextLevel; 
            nextLevel = 0 ; 
            のprintf(" \ N-を" ); 
        } 
    } 
}
// ====================测试代码====================
 //             8
 //         6 10
 //        5 7 9 11 
ボイドの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を開始しますが:==== \ N " ); 
    printf(" 期待される結果は次のとおりです。\ n個" ); 
    printf(" 8 \ n " ); 
    printf(" 6 10 \ n " ); 
    printf(" 5 7 9 11 \ n \ n " );

    printf(" 実際の結果は次のとおりです。\ n個" ); 
    印刷(pNode8)。
    printf(" \ n個" ); 

    DestroyTree(pNode8)。
} 

//             5
 //           4
 //         3
 //       2 
空隙Test2を()
{ 
    BinaryTreeNode * pNode5 = CreateBinaryTreeNode(5 )。
    BinaryTreeNode * pNode4 = CreateBinaryTreeNode(4 )。
    BinaryTreeNode * pNode3 = CreateBinaryTreeNode(3 )。
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2 )。

    ConnectTreeNodes(pNode5、pNode4、nullptr)。
    ConnectTreeNodes(pNode4、pNode3、nullptr)。
    ConnectTreeNodes(pNode3、pNode2、nullptr)。

    printf(" ==== Test2を開始しますが:==== \ N " ); 
    printf(" 期待される結果は次のとおりです。\ n個" ); 
    printf(" 5 \ N " ); 
    printf(" 4 \ n " ); 
    printf(" 3 \ n " ); 
    printf(" 2 \ n \ n " ); 

    printf("実際の結果は:\ n " ); 
    印刷(pNode5); 
    のprintf(" \ n " ); 

    DestroyTree(pNode5); 
} 

//         5
 //          4
 //           3
 //            2 
空隙Test3は()
{ 
    BinaryTreeNode * pNode5 = CreateBinaryTreeNode (5 ); 
    BinaryTreeNode * pNode4 = CreateBinaryTreeNode(4 ); 
    BinaryTreeNode * pNode3 = CreateBinaryTreeNode(3 ); 
    BinaryTreeNode * pNode2 = CreateBinaryTreeNode(2); 

    ConnectTreeNodes(pNode5、nullptr、pNode4)。
    ConnectTreeNodes(pNode4、nullptr、pNode3)。
    ConnectTreeNodes(pNode3、nullptr、pNode2)。

    printf(" ==== Test3はを開始します:==== \ N " ); 
    printf(" 期待される結果は次のとおりです。\ n個" ); 
    printf(" 5 \ N " ); 
    printf(" 4 \ n " ); 
    printf(" 3 \ n " ); 
    printf(" 2 \ n \ n " ); 

    printf(実際の結果は次のとおりです。\ nは
    印刷(pNode5)。
    printf(" \ n個" ); 

    DestroyTree(pNode5)。
} 

ボイドTEST4()
{ 
    BinaryTreeNode * pNode5 = CreateBinaryTreeNode(5 )。

    printf(" ==== TEST4が始まる:==== \ N " ); 
    printf(" 期待される結果は次のとおりです。\ n個" ); 
    printf(" 5 \ N \ N " ); 

    printf(" 実際の結果は次のとおりです。\ n個" ); 
    印刷(pNode5)。
    printf(" \ n個" );

    DestroyTree(pNode5)。
} 

ボイドTEST5()
{ 
    のprintf(" ==== TEST5が始まる:==== \ n " ); 
    printf(" 期待される結果は次のとおりです。\ n個" ); 

    printf(" 実際の結果は次のとおりです。\ n個" ); 
    印刷(nullptr); 
    printf(" \ n個" ); 
} 

//         100
 //         /
 //        50   
 //          \
 //          150 
ボイドTEST6()
{ 
    BinaryTreeNode* pNode100 = CreateBinaryTreeNode(100 )。
    BinaryTreeNode * pNode50 = CreateBinaryTreeNode(50 )。
    BinaryTreeNode * pNode150 = CreateBinaryTreeNode(150 )。

    ConnectTreeNodes(pNode100、pNode50、nullptr)。
    ConnectTreeNodes(pNode50、nullptr、pNode150)。

    printf(" ==== TEST6が始まる:==== \ N " ); 
    printf(" 期待される結果は次のとおりです。\ n個" ); 
    printf(" 100 \ N " ); 
    printf(" 50 \ n " ); 
    printf("150 \ n \ n " ); 

    のprintf(" 実績があります:\ n " ); 
    印刷(pNode100); 
    のprintf(" \ n " ); 
} 

int型のmain(int型 ARGC、CHAR * ARGV [])
{ 
    Test1を() ; 
    Test2を(); 
    Test3は(); 
    TEST4(); 
    TEST5(); 
    TEST6(); 

    戻り 0 ; 
}
テストコード

分析:入力制御する余分なセットフラグ。

 

おすすめ

転載: www.cnblogs.com/ZSY-blog/p/12602011.html