• 数据结构-二叉树(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("
    InorderTraverse_1:");
        InorderTraverse(T, visit);
        printf("
    InorderTraverse_2:");
        InorderTraverse2(T, visit);
        printf("
    PostorderTraverse:");
        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;
    }
    
    
    

  • 相关阅读:
    WebService安全机制的思考与实践
    Typora
    谷歌隐藏功能开启
    在Idea中jdk11和jdk8环境变量的切换
    win10安装两个不同版本的mysql(mysql5.7和mysql8.0.19)
    Guava中这些Map的骚操作,让我的代码量减少了50%
    MySQL TransactionMySQL锁升级引发的死锁问题
    mac 在 home 目录下创建文件夹
    忍者系统
    DNS服务简介和配置详解
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/9451400.html
Copyright © 2020-2023  润新知