• 二叉树系列四:二叉树的遍历(非递归)


    在二叉树系列三中讲述了二叉树的前序遍历、中序遍历和后序遍历的递归实现,可以看到采用递归实现的代码非常简单,但是代码简单不代表实际运行过程中也能达到最简。下面将要介绍二叉树几种遍历的非递归实现实现。

    (1)前序遍历 

     1 template<class T>
     2 void Tree<T>::PreOrderNonRec(TreeNode<T>* root)
     3 {
     4     if(root != NULL)
     5     {
     6         stack<TreeNode<T>*> s;
     7         TreeNode<T>* curr = root;
     8         while(curr || !s.empty())
     9         {
    10             while(curr)
    11             {
    12                 cout<<curr->data<<endl;
    13                 s.push(curr);
    14                 curr = curr->leftChild;
    15             }
    16             if(!s.empty())
    17             {
    18                 curr = s.top();
    19                 s.pop();
    20                 curr = curr->rightChild;
    21             }
    22         }
    23     }
    24 }
    View Code

    (2)中序遍历

     1 template<class T>
     2 void Tree<T>::MidOrderNonRec(TreeNode<T>* root)
     3 {
     4     if(root != NULL)
     5     {
     6         TreeNode<T>* curr = root;
     7         stack<TreeNode<T>*> s;
     8         while(curr || !s.empty())
     9         {
    10             while(curr)
    11             {
    12                 s.push(curr);
    13                 curr = curr->leftChild;
    14             }
    15             if(!s.empty())
    16             {
    17                 curr = s.top();
    18                 s.pop();
    19                 cout<<curr->data<<endl;
    20                 curr = curr->rightChild;
    21             }
    22         }
    23     }
    24 }
    View Code

    (3)后序遍历

     1 template<class T>
     2 struct StackItem
     3 {
     4     TreeNode<T>* node;
     5     bool RCVisited;
     6 };
     7 
     8 template<class T>
     9 void Tree<T>::PostOrderNonRec(TreeNode<T>* root)
    10 {
    11     if(root != NULL)
    12     {
    13         TreeNode<T>* curr = root;
    14         stack<StackItem<T>> s;
    15         StackItem<T> item;
    16         while(curr || !s.empty())
    17         {
    18             while(curr)
    19             {
    20                 item.node = curr;
    21                 item.RCVisited = false;
    22                 s.push(item);
    23                 curr = curr->leftChild;
    24             }
    25             if(!s.empty())
    26             {
    27                 item = s.top();
    28                 s.pop();
    29                 curr = item.node;
    30                 if(item.RCVisited)
    31                 {
    32                     cout<<curr->data<<endl;
    33                     curr = NULL;
    34                 }
    35                 else
    36                 {
    37                     item.RCVisited = true;
    38                     s.push(item);
    39                     curr = curr->rightChild;
    40                 }
    41             }
    42         }
    43     }
    44 }
    View Code
  • 相关阅读:
    斜二进制数
    贝贝的波浪数
    1058: 电文保密
    1065: 贝贝的加密工作
    低危漏洞- X-Frame-Options Header未配置
    1064: 不明飞行物(ufo)
    支付
    1067: 密室寻宝(find)
    c++10进制转换为任意2-16进制数字
    1066: 单词游戏(game)
  • 原文地址:https://www.cnblogs.com/sophia-yun/p/3144440.html
Copyright © 2020-2023  润新知