二叉树的遍历--非递归操作

// 先序(附带叶子标记)建树
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
#include<string>
using namespace std;
struct node
{
    char data;
    node* lchild;
    node* rchild;
};
void buildtree(char* str,node* root)
{
    stack<node*> sta;
    int pos = 0;
    int len = strlen(str);
    node* tmp;
    root->data = str[pos++];
    root->lchild = NULL;
    root->rchild = NULL;
    sta.push(root);
    while(pos < len)
    {
          while(true)
          {
                  node* nl = new node;
                  nl->data = str[pos];
                  nl->lchild = NULL;
                  nl->rchild = NULL;
                  if(str[pos]=='*')
                 {
                        free(nl);
                        break;
                 }
                  sta.top()->lchild = nl;
                  sta.push(nl);
                  ++pos;
           }
           tmp = sta.top();
           sta.pop();
           ++pos;
           while(str[pos]=='*')
           {
                 if(!sta.empty())
                 {
                     tmp = sta.top();
                     sta.pop();
                 }
                 ++pos;
           }
           if(pos >= len)
            break;
            node* nr = new node;
            nr->data = str[pos];
            nr->lchild = NULL;
            nr->rchild = NULL;
            sta.push(nr);
            tmp->rchild = nr;
            ++pos;
    }
    return;
}
// 先根遍历,入栈的时候输出
// 注意,有可能入栈的为空
void visit_rootfirst(node* root)
{
     stack<node*> sta;
     bool _end = false;
     node* p ;
     sta.push(root);
     while(!sta.empty())
     {
         p = sta.top();
         while(p)
         {
             if(p)
             {
                 printf("%c",p->data);
             }
             p = p->lchild;
             sta.push(p);
         }
         sta.pop();
         p = sta.top();
         sta.pop();// 左子树访问完,根节点出栈
         p = p->rchild; // 遍历右子树
         if(sta.empty() && (p == NULL))break;
         sta.push(p);
     }
     printf("\n");
     return ;
}
void visit_rootsecond(node* root)
{
      stack<node*> sta;
      bool _end = false;
      node* p;
      sta.push(root);
      while(!sta.empty())
      {
          p = sta.top();
          while(p)
          {
               p = p->lchild;
               sta.push(p);
          }
          sta.pop();
          p = sta.top();
          printf("%c",p->data);
          sta.pop();
          p = p->rchild;// 直接访问右子树,为空说明栈内当前节点左子树访问完成
          if(sta.empty()&&p==NULL)
            break;
          sta.push(p);
      }
      printf("\n");
      return;
}
void visit_rootlast(node* root)
{
    stack<node*> sta;
    bool _end = false;
    node* p;
    node* mark = NULL;
    sta.push(root);
    while(!sta.empty())
    {
        p = sta.top();
        while(p)
        {
               p = p->lchild;
               sta.push(p);
        }
        mark = p;
        sta.pop();
        p = sta.top();
        while(mark ==  p->rchild)
        {
            mark = sta.top();
            sta.pop();
            printf("%c",mark->data); // 访问根节点
            if(sta.empty()) // 访问完根节点栈为空结束循环
             {
                 _end = true; // 虽然栈为空但是 p指针还是没有改变,后面还有将p->rchild压栈的操作
                 break;
             }
            p = sta.top();
        }
        if(!_end)
        {
            p = p->rchild;
            sta.push(p);
        }
    }
     printf("\n");
    return ;
}
int main()
{
    char* str = new char[100];
    gets(str);
    //printf("%s",str);
    node* root = new node;
    buildtree(str,root);
    visit_rootfirst(root);
    visit_rootsecond(root);
    visit_rootlast(root);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zm_zsy/article/details/78606892