二叉树创建以及遍历

来源:互联网 发布:阿迪达斯淘宝旗舰店 编辑:程序博客网 时间:2024/05/22 16:59

char *ps = “ABCDEFGH”;
char *is = “CBEDFAGH”;
char *ls = “CEFDBHGA”;

BtNode * Buynode(){    BtNode *s = (BtNode*)malloc(sizeof(BtNode));    if(s == NULL) exit(1);    memset(s,0,sizeof(BtNode));    return s;}void Freenode(BtNode *p){    free(p);}BtNode * CreateTree1()    //创建二叉树(输入){    BtNode *s = NULL;    ElemType item;    scanf("%c",&item);    if(item != '#')    {        s = Buynode();        s->data = item;        s->leftchild = CreateTree1();        s->rightchild = CreateTree1();    }    return s;}BtNode * CreateTree2(char *&str)  // 创建二叉树(给定字符串){    BtNode *s = NULL;    if(str!= NULL && *str!='#')    {        s = Buynode();        s->data = *str;        s->leftchild = CreateTree2(++str);        s->rightchild = CreateTree2(++str);    }    return s;}BtNode *CreateTree3(char ** const str){    BtNode *s=NULL;    if(*str!=NULL && **str!='#'&& str!=NULL)    {        s=Buynode();        s->data=**str;        s->leftchild=CreateTree3(&++*str);        s->rightchild=CreateTree3(&++*str);    }    return s;}// 前序中序建立二叉树int FindIs(char *is,int n,char x){    for(int i=0;i<n;i++)    {        if(is[i]==x)        {            return i;        }    }}BtNode * Create1(char *ps,char *is,int n){        BtNode *s=NULL;        if(n>0)        {            s=Buynode();            s->data=ps[0];            int pos=FindIs(is,n,ps[0]);            if(pos==-1)exit(1);            s->leftchild =Create1(ps+1,is,pos);            s->rightchild =Create1(ps+pos+1,is+pos+1,n-pos-1);        }        return s;}BtNode * CreatePI(char *ps,char *is,int n){    if(ps==NULL || is==NULL || n<1)    {        return NULL;    }    else    {    return Create1(ps,is,n);    }}//中序后序建立二叉树int FindIs2(char *is,int n,char x){    for(int i=0;i<n;i++)    {        if(is[i]==x)        {            return i;        }    }}BtNode * Create2(char *is,char *ls,int n){    BtNode *s=NULL;    if(n>0)    {        int pos=FindIs2(is,n,ls[n-1]);        if(pos=-1) exit(1);        s=Buynode();        s->data=ls[n-1];        s->leftchild =Create2(is,ls,pos);        s->rightchild =Create2(is+pos+1,ls+pos,n-pos-1);    }    return s;}BtNode * CreateIL(char *is,char *ls,int n){    if(is==NULL ||  ls==NULL || n<1)    {        return NULL;    }    else    {        return Create2(is,ls,n);    }}void PreOrder(BtNode *ptr)    //递归前序遍历二叉树{    if(ptr!= NULL)    {        printf("%c ",ptr->data);        PreOrder(ptr->leftchild);        PreOrder(ptr->rightchild);    }}void InOrder(BtNode *ptr)    //中序遍历二叉树{    if(ptr != NULL)    {        InOrder(ptr->leftchild);        printf("%c ",ptr->data);        InOrder(ptr->rightchild);    }}void PastOrder(BtNode *ptr)   //后序遍历二叉树{    if(ptr != NULL)    {        PastOrder(ptr->leftchild);        PastOrder(ptr->rightchild);         printf("%c ",ptr->data);    }}void NicePerOrder(BtNode *ptr)   //非递归前序遍历(栈实现){    if(ptr==NULL)    {        return ;    }    stack st;    init_stack(&st);    push(&st,ptr);    while(!is_empty(&st))      //栈非空时    {        ptr=top(&st);        pop(&st);        printf("%c ",ptr->data);        if(ptr->rightchild!=NULL)        {            push(&st,ptr->rightchild );        }         if(ptr->leftchild!=NULL)        {            push(&st,ptr->leftchild) ;        }    }}void NiceInOrder(BtNode *ptr)  //非递归中序遍历{    if(ptr==NULL)    {        return ;    }    stack st;   // BtNode *;    init_stack(&st);while(ptr!=NULL ||! is_empty(&st)){    while(ptr!=NULL)    {        push(&st,ptr);        ptr=ptr->leftchild;    }}    ptr=top(&st);    pop(&st);    printf("%c",ptr->data);    ptr=ptr->rightchild;}void NicePastOrder(BtNode *ptr)  //非递归后序遍历{    if(ptr==NULL)    {        return ;    }    stack st;     init_stack(&st);    BtNode *tag=NULL;    while(ptr!=NULL ||! is_empty(&st))    {        while(ptr!=NULL)        {            push(&st,ptr);            ptr=ptr->leftchild;        }        ptr=top(&st);        pop(&st);        if(ptr->rightchild ==NULL || ptr->rightchild ==tag)        {            printf("%c ",ptr->data);            tag=ptr;            ptr=NULL;    //保证继续出栈;而不是将ptr push到栈里;        }        else        {            push(&st,ptr);            ptr=ptr->rightchild ;        }    }}
原创粉丝点击