关闭

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

标签: 数据结构二叉树
350人阅读 评论(0) 收藏 举报
分类:
#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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:105835次
    • 积分:3247
    • 等级:
    • 排名:第10446名
    • 原创:194篇
    • 转载:12篇
    • 译文:0篇
    • 评论:87条