剑指offer-二叉树的层次遍历实现

 
BinaryTree.h
/*******************************************************************
Copyright(c) 2016, Tyrone Li
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//

typedef int ElemType;

struct BinaryTreeNode
{
    ElemType m_nValue;
    BinaryTreeNode*m_pLeft;
    BinaryTreeNode*m_pRight;
};

BinaryTreeNode*createBinaryTreeNode(ElemType value);
void connectTreeNodes(BinaryTreeNode*pParent, BinaryTreeNode*pLeft, BinaryTreeNode*pRight);
void printTreeNode(const BinaryTreeNode*pNode);
void printNode(const BinaryTreeNode*pNode);
void printTree(const BinaryTreeNode*pRoot);
void printTreePreOrder(const BinaryTreeNode*pRoot);
void printTreeInOrder(const BinaryTreeNode*pRoot);
void printTreePostOrder(const BinaryTreeNode*pRoot);
void printTreeLevelOrder(BinaryTreeNode*pRoot);
void destroyTree(BinaryTreeNode*pRoot);

BinaryTree.cpp

/*******************************************************************
Copyright(c) 2016, Tyrone Li
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//

#include "BinaryTree.h"
#include <iostream>
#include <deque>
#include <cstdio>
#include <cstdlib>

typedef int ElemType;

BinaryTreeNode*createBinaryTreeNode(ElemType value)
{
    BinaryTreeNode*pNode = new BinaryTreeNode();
    pNode->m_nValue = value;
    pNode->m_pLeft = nullptr;
    pNode->m_pRight = nullptr;
    return pNode;
}

void connectTreeNodes(BinaryTreeNode*pParent, BinaryTreeNode*pLeft, BinaryTreeNode*pRight)
{
    if(pParent != nullptr)
    {
        pParent->m_pLeft = pLeft;
        pParent->m_pRight = pRight;
    }
}

void printTreeNode(const BinaryTreeNode*pNode)
{
    if(pNode != nullptr)
    {
        std::cout << "Value of this node is " << pNode->m_nValue << std::endl;

        if(pNode->m_pLeft != nullptr)
        {
            std::cout << "Value of its left child is " << pNode->m_pLeft->m_nValue << std::endl;
        }else{
            std::cout << "left child is nullptr." << std::endl;
        }

        if(pNode->m_pRight != nullptr)
        {
            std::cout << "Value of its right child is " << pNode->m_pRight->m_nValue << std::endl;
        }else{
            std::cout << "right child is nullptr." << std::endl;
        }
    }else{
        std::cout << "this node is nullptr." << std::endl;
    }
}

void printTree(const BinaryTreeNode*pRoot)
{
    printTreeNode(pRoot);

    if(pRoot != nullptr)
    {
        if(pRoot->m_pLeft != nullptr)
            printTree(pRoot->m_pLeft);
        
        if(pRoot->m_pRight != nullptr)
            printTree(pRoot->m_pRight);
    }
}

void printNode(const BinaryTreeNode*pNode)
{
    if(pNode != nullptr)
        std::cout << pNode->m_nValue << " ";
}

void printTreePreOrder(const BinaryTreeNode*pRoot)
{
    printNode(pRoot);

    if(pRoot != nullptr)
    {
        if(pRoot->m_pLeft != nullptr)
            printTreePreOrder(pRoot->m_pLeft);
        
        if(pRoot->m_pRight != nullptr)
            printTreePreOrder(pRoot->m_pRight);
    }
}

void printTreeInOrder(const BinaryTreeNode*pRoot)
{
    if(pRoot != nullptr)
    {
        if(pRoot->m_pLeft != nullptr)
            printTreeInOrder(pRoot->m_pLeft);
    }

    printNode(pRoot);

    if(pRoot != nullptr)
    {
        if(pRoot->m_pRight != nullptr)
            printTreeInOrder(pRoot->m_pRight);
    }
}

void printTreePostOrder(const BinaryTreeNode*pRoot)
{
    if(pRoot != nullptr)
    {
        if(pRoot->m_pLeft != nullptr)
            printTreePostOrder(pRoot->m_pLeft);

        if(pRoot->m_pRight != nullptr)
            printTreePostOrder(pRoot->m_pRight);
    }

    printNode(pRoot);
}

void printTreeLevelOrder(BinaryTreeNode*pRoot)
{
    if(pRoot != nullptr)
    {
        BinaryTreeNode*pNode = pRoot;
        std::deque<BinaryTreeNode*> treeDeque;
        treeDeque.push_back(pRoot);
        while(treeDeque.size() > 0)
        {
            pNode = treeDeque.front();
            std::cout << pNode->m_nValue << " ";
            treeDeque.pop_front();
            if(pNode->m_pLeft)
                treeDeque.push_back(pNode->m_pLeft);
            if(pNode->m_pRight)
                treeDeque.push_back(pNode->m_pRight);
        }
    }
}

void destroyTree(BinaryTreeNode*pRoot)
{
    if(pRoot != nullptr)
    {
        BinaryTreeNode*pLeft = pRoot->m_pLeft;
        BinaryTreeNode*pRight = pRoot->m_pRight;

        delete pRoot;
        pRoot = nullptr;

        destroyTree(pLeft);
        destroyTree(pRight);

    }
}

test.cpp

/*******************************************************************
Copyright(c) 2016, Tyrone Li
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//

/*
Q:
    实二叉树的层次遍历算法。
S:
    使用一个辅助队列,当访问到根节点的时候,如果该根节点存在左右子树,则依次
    将他们压入队列尾部,然后再从队列头部弹出一个结点,访问其值。
    
*/

#include "../utils/BinaryTree.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>

void test_1()
{
    std::cout << "multi nodes ad multi child branches" << std::endl;
    std::cout << "Print tree nodes in pre order" << std::endl;
    BinaryTreeNode*pRoot_1 = createBinaryTreeNode(1);
    BinaryTreeNode*pRoot_2 = createBinaryTreeNode(2);
    BinaryTreeNode*pRoot_3 = createBinaryTreeNode(3);
    BinaryTreeNode*pRoot_4 = createBinaryTreeNode(4);
    BinaryTreeNode*pRoot_5 = createBinaryTreeNode(5);
    BinaryTreeNode*pRoot_6 = createBinaryTreeNode(6);
    BinaryTreeNode*pRoot_7 = createBinaryTreeNode(7);
    BinaryTreeNode*pRoot_8 = createBinaryTreeNode(8);
    BinaryTreeNode*pRoot_9 = createBinaryTreeNode(9);
    BinaryTreeNode*pRoot_10 = createBinaryTreeNode(10);
    BinaryTreeNode*pRoot_11 = createBinaryTreeNode(11);
    BinaryTreeNode*pRoot_12 = createBinaryTreeNode(12);
    BinaryTreeNode*pRoot_13 = createBinaryTreeNode(13);
    BinaryTreeNode*pRoot_14 = createBinaryTreeNode(14);
    BinaryTreeNode*pRoot_15 = createBinaryTreeNode(15);
    connectTreeNodes(pRoot_1, pRoot_2, pRoot_3);
    connectTreeNodes(pRoot_2, pRoot_4, pRoot_5);
    connectTreeNodes(pRoot_3, pRoot_6, pRoot_7);
    connectTreeNodes(pRoot_4, pRoot_8, pRoot_9);
    connectTreeNodes(pRoot_5, pRoot_10, pRoot_11);
    connectTreeNodes(pRoot_6, pRoot_12, pRoot_13);
    connectTreeNodes(pRoot_7, pRoot_14, pRoot_15);

    printTreePreOrder(pRoot_1);
    std::cout << std::endl;

    std::cout << "Print tree nodes in level order" << std::endl;
    printTreeLevelOrder(pRoot_1);
    std::cout << std::endl;
}

void test_2()
{
    std::cout << "only left child binary tree" << std::endl;
    std::cout << "Print tree nodes in pre order" << std::endl;
    BinaryTreeNode*pRoot_1 = createBinaryTreeNode(1);
    BinaryTreeNode*pRoot_2 = createBinaryTreeNode(2);
    BinaryTreeNode*pRoot_3 = createBinaryTreeNode(4);
    BinaryTreeNode*pRoot_4 = createBinaryTreeNode(8);

    connectTreeNodes(pRoot_1, pRoot_2, nullptr);
    connectTreeNodes(pRoot_2, pRoot_3, nullptr);
    connectTreeNodes(pRoot_3, pRoot_4, nullptr);

    printTreePreOrder(pRoot_1);
    std::cout << std::endl;

    std::cout << "Print tree nodes in level order" << std::endl;
    printTreeLevelOrder(pRoot_1);
    std::cout << std::endl;
}

void test_3()
{
    std::cout << "only right child binary tree" << std::endl;
    std::cout << "Print tree nodes in pre order" << std::endl;
    BinaryTreeNode*pRoot_1 = createBinaryTreeNode(1);
    BinaryTreeNode*pRoot_2 = createBinaryTreeNode(2);
    BinaryTreeNode*pRoot_3 = createBinaryTreeNode(4);
    BinaryTreeNode*pRoot_4 = createBinaryTreeNode(8);

    connectTreeNodes(pRoot_1, nullptr, pRoot_2);
    connectTreeNodes(pRoot_2, nullptr, pRoot_3);
    connectTreeNodes(pRoot_3, nullptr, pRoot_4);

    printTreePreOrder(pRoot_1);
    std::cout << std::endl;

    std::cout << "Print tree nodes in level order" << std::endl;
    printTreeLevelOrder(pRoot_1);
    std::cout << std::endl;
}

void test_4()
{
    std::cout << "only node binary tree" << std::endl;
    std::cout << "Print tree nodes in pre order" << std::endl;
    BinaryTreeNode*pRoot_1 = createBinaryTreeNode(1);

    printTreePreOrder(pRoot_1);
    std::cout << std::endl;

    std::cout << "Print tree nodes in level order" << std::endl;
    printTreeLevelOrder(pRoot_1);
    std::cout << std::endl;
}

void test_5()
{
    std::cout << "nullptr tree" << std::endl;
    std::cout << "Print tree nodes in pre order" << std::endl;
    BinaryTreeNode*pRoot_1 = nullptr;

    printTreePreOrder(pRoot_1);
    std::cout << std::endl;

    std::cout << "Print tree nodes in level order" << std::endl;
    printTreeLevelOrder(pRoot_1);
    std::cout << std::endl;
}

void test_printBinaryTreeLevelOrder()
{
    test_1();
    test_2();
    test_3();
    test_4();
    test_5();
}

int main(int argc, char**argv)
{

    test_printBinaryTreeLevelOrder();

    return 0;
}

 

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值