数据结构练习(39)二叉树前中后序遍历的非递归实现

很久没有写过二叉树的非递归实现了,感觉有点生疏,总结了几种常见的方法,

最后几种采用pair的形式压栈的方法,是十分可取且易于理解的:

#include <iostream>
#include <stack>
using namespace std;

struct TreeNode {
    int m_value;
    TreeNode* m_lhs;
    TreeNode* m_rhs;
};

void PreOrderVisit_1(TreeNode* root)
{
    stack<TreeNode*> s;
    TreeNode* pNode = root;

    while (pNode || !s.empty())
    {
        while (pNode)
        {
            s.push(pNode);
            cout << pNode->m_value << " ";
            pNode = pNode->m_lhs;
        }
        if (!s.empty())
        {
            pNode = s.top();
            s.pop();
            pNode = pNode->m_rhs;
        }
    }
}

void PreOrderVisit_2(TreeNode* root)
{
    if (root == NULL)
        return ;

    stack<TreeNode*> s;
    s.push(root);

    while (!s.empty())
    {
        TreeNode* pNode = s.top();
        s.pop();
        cout << pNode->m_value << " ";

        if (pNode->m_rhs)
            s.push(pNode->m_rhs);
        if (pNode->m_lhs)
            s.push(pNode->m_lhs);
    }
}

void InOrderVisit(TreeNode* root)
{
    stack<TreeNode*> s;
    TreeNode* pNode = root;

    while (pNode || !s.empty())
    {
        while (pNode)
        {
            s.push(pNode);
            pNode = pNode->m_lhs;
        }
        if (!s.empty())
        {
            pNode = s.top();
            s.pop();
            cout << pNode->m_value << " ";
            pNode = pNode->m_rhs;
        }
    }
}

void PostOrderVisit(TreeNode* root)
{
    if (root == NULL)
        return ;

    stack<TreeNode*> s;
    s.push(root);
    TreeNode* preNode = NULL;

    while (!s.empty())
    {
        TreeNode* curNode = s.top();
        TreeNode* lastNode = curNode->m_rhs;

        if (lastNode == NULL)
            lastNode = curNode->m_lhs;

        if (lastNode == NULL || lastNode == preNode)
        {
            cout << curNode->m_value << " ";
            preNode = curNode;
            s.pop();
        }
        else if (curNode->m_lhs == NULL || curNode->m_lhs == preNode)
        {
            s.push(curNode->m_rhs);
        }
        else
            s.push(curNode->m_lhs);
    }
}

enum _travel_type {
    visit, travel
};

typedef pair<enum _travel_type, TreeNode*> NodePair;

void InOrderVisit_2(TreeNode* root)
{
    if (root == NULL)
        return;

    stack<NodePair> s;
    s.push(NodePair(travel, root));

    while (!s.empty())
    {
        NodePair np = s.top();
        s.pop();

        TreeNode* pNode = np.second;

        if (np.first == visit)
            cout << pNode->m_value << " ";
        else 
        {
            if (pNode->m_rhs)
                s.push(NodePair(travel, pNode->m_rhs));

            s.push(NodePair(visit, pNode));

            if (pNode->m_lhs)
                s.push(NodePair(travel, pNode->m_lhs));
        }
    }
}

void PostOrderVisit_2(TreeNode* root)
{
    if (root == NULL)
        return ;

    stack<NodePair> s;
    s.push(NodePair(travel, root));

    while (!s.empty())
    {
        NodePair np = s.top();
        s.pop();
        TreeNode* pNode = np.second;

        if (np.first == visit)
            cout << pNode->m_value << " ";
        else
        {
            s.push(NodePair(visit, pNode));
            if (pNode->m_rhs)
                s.push(NodePair(travel, pNode->m_rhs));
            if (pNode->m_lhs)
                s.push(NodePair(travel, pNode->m_lhs));
        }
    }
}

 

posted @ 2012-12-21 22:38  kedebug  阅读(304)  评论(0编辑  收藏  举报