二叉树的遍历--递归和非递归

PS:数据结构老师让写下二叉树的后序遍历的非递归算法,结果把所有的都写了一下

CODE

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<stack>
using namespace std;
typedef  struct Binode
{
    char data;
    struct Binode *l, *r;
};
typedef struct node
{
    struct Binode *x;
    bool flag;
};
char ls[150];
int k;
void creat(Binode *&T)
{
    char c;
     c = ls[k++];
    if(c == '#')
    {
        return ;
    }
    else
    {
        T = new Binode;
        T->l = NULL;
        T->r = NULL;
        T->data = c;
        creat(T->l);
        creat(T->r);
    }
}
void pre(Binode *T)///先序递归--中左右
{
    if(T)
    {
        cout<<T->data;
        pre(T->l);
        pre(T->r);
    }
}
void preBitree(Binode *T)///先序遍历非递归
{
    stack<node*> A;
    Binode *p;
    p = T;
    while(p || !A.empty())
    {
        while(p)
        {
            cout<<p->data;
            node *q;
            q = new node;
            q->x = p;
            A.push(q);
            p =p->l;
        }
        if(!A.empty())
        {
            node *q;
            q = A.top();
            A.pop();
            if(q->x->r)
            {
                p = q->x->r;
            }
        }
    }
    cout<<endl;
}
void In(Binode *T)///中序递归--左中右
{
    if(T)
    {
        In(T->l);
        cout<<T->data;
        In(T->r);
    }
}
void InBitree(Binode *T)///中序遍历非递归
{
    stack<node*> A;
    Binode *p;
    p  = T;
    while(p || !A.empty())
    {
        while(p)
        {
             node *q;
            q = new node;
            q->x = p;
            A.push(q);
            p =p->l;
        }
        if(!A.empty())
        {
            node *q;
            q = A.top();
            A.pop();
            cout<<q->x->data;
            if(q->x->r)
            {
                p = q->x->r;
            }
        }
    }
    cout<<endl;
}
void post(Binode *T)///后序递归--左右中
{
    if(T)
    {
        post(T->l);
        post(T->r);
        printf("%c",T->data);
    }
}
void postBitree(Binode *T)///后序非递归
{
     stack<node*> A;
     Binode *p;
     p = T;
    while(p||!A.empty())
    {
        while(p){
            node *q;
            q = new node;
            q->x = p;
            q->flag = true;
            A.push(q);
            p =p->l;
        }
        if(!A.empty())
        {
        node *q;
        q = A.top();
        A.pop();
        if(q->flag){
                p = q->x->r;
                if(p){
                q->flag = false;
                A.push(q);
                }
                else{
                        cout<<q->x->data;
                }
        }
        else{
            cout<<q->x->data;
        }
    }
    }
    cout<<endl;
}
int main()
{
       while(~scanf("%s",ls))
       {
           k = 0;
        Binode *root;
        creat(root);
        preBitree(root);
       }
    return 0;
}
/*
abc##de#g##f###
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值