数据结构-二叉树(Binary Tree)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define true 1
#define false 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define OPSETSIZE 7
#define MAXQSIZE 100

typedef char TelemType;
typedef int Status;
typedef struct BiTNode
{
    TelemType data;
    struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
typedef BiTree SElemType;

typedef struct
{
    SElemType *base;
    SElemType  *top;
    int stacksize;
} SqStack;

Status InitStack(SqStack *S);
Status Push(SqStack *S, SElemType e);
Status Pop(SqStack *S, SElemType *e);
Status StackEmpty(SqStack S);
Status CreateBiTree(BiTree *T);
Status PrintElement(TelemType e);
Status visit(TelemType e);
Status PreorderTraverse(BiTree T, Status (*visit)(TelemType e));
Status InorderTraverse(BiTree T, Status (*visit)(TelemType e));
Status PostorderTraverse(BiTree T, Status (*visit)(TelemType e));

int main()
{
    BiTree T;
    CreateBiTree(&T);
    printf("PreorderTraverse:");
    PreorderTraverse(T, visit);
    printf("\nInorderTraverse_1:");
    InorderTraverse(T, visit);
    printf("\nInorderTraverse_2:");
    InorderTraverse2(T, visit);
    printf("\nPostorderTraverse:");
    PostorderTraverse(T, visit);
    return 0;
}

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 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;
    }  // if
    *S->top++ = e;
    return OK;
}  //Push
Status Pop(SqStack *S, SElemType *e)
{
    if(S->top == S->base)return ERROR;
    *e = *--S->top;
    return OK;
} //Pop
Status StackEmpty(SqStack S)
{
    if (S.base == S.top)
        return TRUE;
    return FALSE;
}

Status CreateBiTree(BiTree *T)
{
    char ch;
    ch = getchar();
    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 visit(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;
    }
    return OK;
}

Status InorderTraverse(BiTree T, Status (*visit)(TelemType e) )
{
    if (T)
    {
        if (InorderTraverse(T->lchild, visit))
            if (visit(T->data))
                if (InorderTraverse(T->rchild, visit)) return OK;
        return ERROR;
    }
    return OK;
}

Status InorderTraverse2(BiTree T, Status (*visit)(TelemType e))
{
    SqStack S;
    InitStack(&S);
    BiTree p = T;
    while( p || !StackEmpty(S) )
    {
        while (p)
        {
            Push(&S, p);
            p = p -> lchild;
        }
        if( !StackEmpty(S))
        {
            Pop(&S, &p);
            if (!visit(p -> data))
                return  ERROR;
            p = p -> rchild;
        }
    }
    return OK;
}

Status  PostorderTraverse(BiTree T, Status (*visit)(TelemType e))
{
    if (T)
    {
        if (PostorderTraverse(T->lchild, visit))
            if (PostorderTraverse(T->rchild, visit))
                if (visit(T->data))  return OK;
        return ERROR;
    }
    return OK;
}



转载于:https://www.cnblogs.com/chinashenkai/p/9451400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值