zoukankan      html  css  js  c++  java
  • 数据结构-二叉树(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;
    }
    
    
    

  • 相关阅读:
    Netty实战
    ObjectTools反射实例
    SpringBoot新增监听器Listener
    拦截器(Inteceptor),过滤器(Filter),切面(Aspect)处理HttpServiceReqeust请求
    Linux CentOS7.2下安装Redis && 配置Redis开机自启动
    JVM命令行工具&垃圾收集器&垃圾收集策略思维导图
    Redis总结
    Java自定义注解的实现
    反射实例
    系统管理员常用的Linux命令
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/9451400.html
Copyright © 2011-2022 走看看