二叉树的3种遍历6种实现(C++实现)

(本博客旨在个人总结回顾)

直接上代码:

// OfferMemo6.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
using namespace std;

#include <stack>

struct BinaryTreeNode
{
    int m_nValue;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;
    BinaryTreeNode(int value = -1)
        : m_nValue(value)
        , m_pLeft(NULL)
        , m_pRight(NULL)
    {  
    }
};

//递归遍历
//先序遍历(先访问父节点,再访问左节点,再访问右节点)
void PreorderRecursion(BinaryTreeNode* pRoot)
{
    if (pRoot != NULL)
    {
        cout << pRoot->m_nValue << endl;
        PreorderRecursion(pRoot->m_pLeft);
        PreorderRecursion(pRoot->m_pRight);
    }
}

//中序遍历(先访问左节点,再访问父节点,再访问右节点)
void InorderRecursion(BinaryTreeNode* pRoot)
{
    if (pRoot != NULL)
    {        
        InorderRecursion(pRoot->m_pLeft);
        cout << pRoot->m_nValue << endl;
        InorderRecursion(pRoot->m_pRight);
    }
}

//后序遍历(先访问左节点,再访问右节点,再访问父节点)
void PostorderRecursion(BinaryTreeNode* pRoot)
{
    if (pRoot != NULL)
    {
        PostorderRecursion(pRoot->m_pLeft);
        PostorderRecursion(pRoot->m_pRight);
        cout << pRoot->m_nValue << endl;
    }
}

//非递归
//先序遍历(先访问父节点,再访问左节点,再访问右节点)
void PreorderNonRecursion(BinaryTreeNode* pRoot)
{
    if (NULL != pRoot)
    {
        std::stack<BinaryTreeNode*> stackTree;
        stackTree.push(pRoot);
        BinaryTreeNode* pNode;
        while (!stackTree.empty())
        {
            pNode = stackTree.top();
            cout << pNode->m_nValue << endl;
            stackTree.pop();
            if (NULL != pNode->m_pRight)
            {
                stackTree.push(pNode->m_pRight);
            }            

            if (NULL != pNode->m_pLeft)
            {
                stackTree.push(pNode->m_pLeft);
            }
        }
    }
}

//中序遍历(先访问左节点,再访问父节点,再访问右节点)
void InorderNonRecursion(BinaryTreeNode* pRoot)
{
    if (NULL != pRoot)
    {
        std::stack<BinaryTreeNode*> stackTree;
        while (!stackTree.empty() || pRoot != NULL)
        {
            if (pRoot != NULL)
            {
                stackTree.push(pRoot);
                pRoot = pRoot->m_pLeft;
            }
            else
            {
                pRoot = stackTree.top();
                cout << pRoot->m_nValue << endl;
                stackTree.pop();
                pRoot = pRoot->m_pRight;
            }
        }
    }
}

//后序遍历(先访问左节点,再访问右节点,再访问父节点)
void PostorderNonRecursion(BinaryTreeNode* pRoot)
{
    if (NULL != pRoot)
    {
        std::stack<BinaryTreeNode*> stackTree;
        std::stack<BinaryTreeNode*> stackTmpTree;//辅助栈
        stackTmpTree.push(pRoot);
        while (!stackTmpTree.empty())
        {
            pRoot = stackTmpTree.top();
            stackTree.push(pRoot);
            stackTmpTree.pop();

            if (pRoot->m_pLeft != NULL)
            {
                stackTmpTree.push(pRoot->m_pLeft);
            }


            if (pRoot->m_pRight != NULL)
            {
                stackTmpTree.push(pRoot->m_pRight);
            }
        }

        while (!stackTree.empty())
        {
            pRoot = stackTree.top();
            cout << pRoot->m_nValue << endl;
            stackTree.pop();
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    //创建二叉树
    /*
                 0
                 ^
            1         4
            ^         ^
          2   3     5    6
    */

    BinaryTreeNode* pRoot = new BinaryTreeNode(0);
    pRoot->m_pLeft = new BinaryTreeNode(1);
    pRoot->m_pRight = new BinaryTreeNode(4);
    pRoot->m_pLeft->m_pLeft = new BinaryTreeNode(2);
    pRoot->m_pLeft->m_pRight = new BinaryTreeNode(3);
    pRoot->m_pRight->m_pLeft = new BinaryTreeNode(5);
    pRoot->m_pRight->m_pRight = new BinaryTreeNode(6);

    cout << "先序遍历<递归>:" << endl;
    PreorderRecursion(pRoot);

    cout << "先序遍历<非递归>:" << endl;
    PreorderNonRecursion(pRoot);

    cout << endl << "中序遍历<递归>:" << endl;
    InorderRecursion(pRoot);

    cout << "中序遍历<非递归>:" << endl;
    InorderNonRecursion(pRoot);

    cout << endl <<"后序遍历<递归>:" << endl;

    PostorderRecursion(pRoot);
    cout << "后序遍历<非递归>:" << endl;
    PostorderNonRecursion(pRoot);

    system("pause");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值