先根遍历__非递归操作

// 先序(附带叶子标记)建树
#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);
     printf("%c",root->data);
     while(!sta.empty())
     {
         p = sta.top();
         while(p)
         {
             p = p->lchild;
             sta.push(p);
             if(p)
             {
                 printf("%c",p->data);
             }
         }
         sta.pop();
         p = sta.top();
         // 左子树访问完,根节点退栈但不输出
         sta.pop();
         p = p->rchild;
         if(p)
         {
              sta.push(p);
              printf("%c",p->data);
              continue;
         }
         while( p == NULL)
         {
             if(sta.empty())
            {
                    _end = true;
                    break;
            }
             p = sta.top();
             sta.pop();
             p = p->rchild;
         }
         if(!_end)
         {
             sta.push(p);
             printf("%c",p->data);
         }
     }
     return ;
}
int main()
{
    char* str = new char[100];
    gets(str);
    //printf("%s",str);
    node* root = new node;
    buildtree(str,root);
    visit_rootfirst(root);
    return 0;
}

1.先序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void PreOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { visite(p->data); push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历 { p=pop(s); p=p->rchild; }//endif }//endwhile }//PreOrderUnrec2.中序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void InOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) { p=pop(s); visite(p->data); //访问根结点 p=p->rchild; //通过下一次循环实现右子树遍历 }//endif }//endwhile}//InOrderUnrec3.后序遍历非递归算法#define maxsize 100typedef enum{L,R} tagtype;typedef struct { Bitree ptr; tagtype tag;}stacknode;typedef struct{ stacknode Elem[maxsize]; int top;}SqStack;void PostOrderUnrec(Bitree t){ SqStack s; stacknode x; StackInit(s); p=t; do { while (p!=null) //遍历左子树 { x.ptr = p; x.tag = L; //标记为左子树 push(s,x); p=p->lchild; } while (!StackEmpty(s) && s.Elem[s.top].tag==R) { x = pop(s); p = x.ptr; visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点 } if (!StackEmpty(s)) { s.Elem[s.top].tag =R; //遍历右子树 p=s.Elem[s.top].ptr->rchild; } }while (!StackEmpty(s));}//PostOrderUnrec
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值