zoukankan      html  css  js  c++  java
  • 数据结构练习(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

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

  • 相关阅读:
    如何把python最小化安装在客户机上面
    c++的历史-异常
    插件系统的构建
    ora-01445:无法从不带保留关键字的表的连接视图中选择ROWID或采样
    转--Oracle 审计和测试操作
    foreach属性-动态-mybatis中使用map类型参数,其中key为列名,value为列值
    C语言中 *.c和*.h文件的区别!
    解决方法:loadrunner 场景下执行webservice脚本是---报错10492 Error: Exception was raised when calling per-process-init function in extens
    NoSQL之Redis高级实用命令详解--安全和主从复制
    Expect安装方法
  • 原文地址:https://www.cnblogs.com/kedebug/p/2828607.html
Copyright © 2011-2022 走看看