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

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

  • 相关阅读:
    26 转义符 re模块 方法 random模块 collection模块的Counter方法
    25 正则表达式
    24 from 模块 import 名字
    24 from 模块 import 名字
    24 from 模块 import 名字
    23 析构方法 items系列 hash方法 eq方法
    21 isinstance issubclass 反射 _str_ _new_ _len_ _call_
    20 属性, 类方法, 静态方法. python2与python3的区别.
    python(1)
    python之字符串格式化
  • 原文地址:https://www.cnblogs.com/kedebug/p/2828607.html
Copyright © 2011-2022 走看看