奇迹也是有人会创造出来的,况且是只需要努力一点的事,为何我不来完成它?...

二叉树的遍历(数据结构)

#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
    TElemType data;
    struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
typedef BiTree SElemType;
#define STACK_INIT_SIZE 5
#define STACKINCREMENT 2
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
} SqStack;
Status InitStack(SqStack &S)
{
    S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base)
        exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}
Status GetTop(SqStack &S,SElemType &e)
{
    if(S.top==S.base)
        return ERROR;
    e=*(S.top-1);
    return OK;
}
Status Push(SqStack &S,SElemType e)
{
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(!S.base)
            exit(OVERFLOW);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
    if(S.top==S.base)
        return ERROR;
    e=*--S.top;
    return OK;
}
Status StackEmpty(SqStack &S)
{
    if(S.top==S.base)
        return OK;
    return ERROR;
}
Status CreateBiTree(BiTree &T)
{
    TElemType ch;
    scanf("%c",&ch);
    if(ch==' ')
        T=NULL;
    else
    {
        if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
            exit(OVERFLOW);
        T->data=ch;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
    return OK;
}
Status PrintElement(TElemType e)
{
    printf("%c ",e);
    return OK;
}
Status PreOrderTraverse(BiTree T,Status(* visit)(TElemType e))
{
    if(T)
    {
        if(visit(T->data))
            if(PreOrderTraverse(T->lchild,visit))
                if(PreOrderTraverse(T->rchild,visit))
                    return OK;
        return ERROR;
    }
    else
        return OK;
}
Status InOrderTraverse1(BiTree T,Status(*visit)(TElemType))
{
    SqStack S;
    SElemType p;
    InitStack(S);
    Push(S,T);
    while(!StackEmpty(S))
    {
        while(GetTop(S,p)&&p)
            Push(S,p->lchild);
        Pop(S,p);
        if(!StackEmpty(S))
        {
            Pop(S,p);
            if(!visit(p->data))
                return ERROR;
            Push(S,p->rchild);
        }
    }
    return OK;
}
Status InOrderTraverse2(BiTree T,Status(* Visit)(TElemType e))
{
    SqStack S;
    BiTree p;
    InitStack(S);
    p=T;
    while(p||!StackEmpty(S))
    {
        if(p)
        {
            Push(S,p);
            p=p->lchild;
        }
        else
        {
            Pop(S,p);
            if(!Visit(p->data))
                return ERROR;
            p=p->rchild;
        }
    }
    return OK;
}
int main()
{
    BiTree T;
    printf("Please input char: ");
    CreateBiTree(T);
    printf("\n");
    printf("PreOrderTraverse: ");
    PreOrderTraverse(T,PrintElement);
    printf("\n");
    printf("InOrderTraverse1: ");
    InOrderTraverse1(T,PrintElement);
    printf("\n");
    printf("InOrderTraverse2: ");
    InOrderTraverse2(T,PrintElement);
    printf("\n");
    return 0;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30076791/article/details/49903947
文章标签: 数据结构 二叉树
个人分类: 数据结构
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭