• 数据结构练习(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));
            }
        }
    }
    -------------------------------------------------------

    kedebug

    Department of Computer Science and Engineering,

    Shanghai Jiao Tong University

    E-mail: kedebug0@gmail.com

    GitHub: http://github.com/kedebug

    -------------------------------------------------------

  • 相关阅读:
    mybatis 中的稍微复杂些的sql语句
    Why doesn't Genymotion run on Windows 10?
    java.lang.NoSuchMethodError: 属于jar包冲突
    sqlserver修改某列为行号
    手机App开发
    java连接SQLserver
    svn忽略target
    JaxWsDynamicClientFactory弃用了,改成org.codehaus.xfire.client;
    FileDataSource java的文件操作
    解决Navicat无法连接Oracle的问题
  • 原文地址:https://www.cnblogs.com/kedebug/p/2828607.html
Copyright © 2020-2023  润新知