二叉树的链式存储的递归和非递归遍历

今天和男友学了机械式转化非递归,然后就以树的遍历为契机写的代码

 
  
 
  
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 10000000000
using namespace std;
struct node
{
    int num;
    struct node*lc;
    struct node*rc;
};
int a[1000];
int n;
int counter=0;
void build(struct node*&p)
{
    if(a[counter]==-1)
    {
        p=NULL;
        counter++;
        return;
    }
    else
    {
        p=(struct node*)malloc(sizeof(struct node));
        p->num=a[counter];
        counter++;
        build(p->lc);
        build(p->rc);
    }

}
void diguixianxubianli(struct node *p)
{
    if(p==NULL)return;
    printf("%d ",p->num);
    diguixianxubianli(p->lc);
    diguixianxubianli(p->rc);
}
void diguizhongxubianli(struct node *p)
{
    if(p==NULL)return;
    diguizhongxubianli(p->lc);
    printf("%d ",p->num);
    diguizhongxubianli(p->rc);
}
void diguihouxubianli(struct node*p)
{
    if(p==NULL)return;
    diguihouxubianli(p->lc);
    diguihouxubianli(p->rc);
    printf("%d ",p->num);
}
void feidiguixianxubianli(struct node*p)
{
    struct node *s1[1000];
    int top1=-1;
    int  s2[1000];
    int top2=-1;
    s1[++top1]=p;
    s2[++top2]=0;
    while(top1!=-1)
    {
        struct node *q=s1[top1];
        top1--;
        int flag=s2[top2];
        top2--;
        if(flag==0)
        {
            if(q==NULL)continue;
            printf("%d ",q->num);
            s1[++top1]=q;
            s2[++top2]=1;
            s1[++top1]=q->lc;
            s2[++top2]=0;
        }
        else if(flag==1)
        {
            s1[++top1]=q;
            s2[++top2]=2;
            s1[++top1]=q->rc;
            s2[++top2]=0;
        }
        else continue;

    }
}
void feidiguizhongxubianli(struct node *p)
{
    struct node *s1[1000];
    int s2[1000];
    int top1=-1;
    int top2=-1;
    s1[++top1]=p;
    s2[++top2]=0;
    while(top1!=-1)
    {
        struct node*q=s1[top1];
        top1--;
        int flag=s2[top2];
        top2--;
        if(flag==0)
        {
            if(q==NULL)continue;
            s1[++top1]=q;
            s2[++top2]=1;
            s1[++top1]=q->lc;
            s2[++top2]=0;
        }
        else if(flag==1)
        {
            printf("%d ",q->num);
            s1[++top1]=q;
            s2[++top2]=2;
            s1[++top1]=q->rc;
            s2[++top2]=0;
        }
        else continue;
    }
}
void feidiguihouxubianli(struct node*p)
{
    struct node *s1[1000];
    int s2[1000];
    int top1=-1;
    int top2=-1;
    s1[++top1]=p;
    s2[++top2]=0;
    while(top1!=-1)
    {
        struct node*q=s1[top1];
        top1--;
        int flag=s2[top2];
        top2--;
        if(flag==0)
        {
            if(q==NULL)continue;
            s1[++top1]=q;
            s2[++top2]=1;
            s1[++top1]=q->lc;
            s2[++top2]=0;
        }
        else if(flag==1)
        {
            s1[++top1]=q;
            s2[++top2]=2;
            s1[++top1]=q->rc;
            s2[++top2]=0;
        }
        else
        {
            printf("%d ",q->num);
        }
    }
}
int main()
{
    scanf("%d",&n);
    int i;
    for(i=0;i

你可能感兴趣的:(二叉树的链式存储的递归和非递归遍历)