数据结构--二叉树

linkqueue.h

#ifndef __LINKQUEUE_H__
#define __LINKQUEUE_H__

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "linkbtree.h"

typedef p_linkbtree data_lqueue;
typedef struct linkqueue_node{
    data_lqueue data;
    struct linkqueue_node *next;
}queuenode,*p_queuenode;
typedef struct node{
    p_queuenode front,rear;
}linkqueue,*p_linkqueue;

extern bool create_linkqueue(p_linkqueue *H);

extern bool is_empty_linkqueue(p_linkqueue H);

extern bool in_linkqueue(p_linkqueue H,data_lqueue x);
extern bool out_linkqueue(p_linkqueue H,data_lqueue *x);

extern void show_linkqueue(p_linkqueue H);

#endif

linkstack.h

#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__

#include <stdio.h>
#include <stdlib.h>
#include "linkbtree.h"

typedef p_linkbtree data_lstack;
typedef struct linkstacknode{
    data_lstack data;
    struct linkstacknode *next;
}linkstack,*p_linkstack;

p_linkstack create_linkstack(void);
void clear_linkstack(p_linkstack L);
void free_linkstack(p_linkstack L);

int is_empty_linkstack(p_linkstack L);

int push_linkstack(p_linkstack L,data_lstack x);
data_lstack pop_linkstack(p_linkstack L);
data_lstack get_top_linkstack(p_linkstack L);

void show_linkstack(p_linkstack L);

#endif

linkbtree.h

#ifndef __LINKBTREE_H__
#define __LINKBTREE_H__

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef char data_btree;
typedef struct btreenode{
    data_btree data;
    struct btreenode *lchild,*rchild;
}linkbtree,*p_linkbtree;

#if 0
extern p_linkbtree create_linkbtree(void);
#else
extern void create_linkbtree(p_linkbtree *H);
#endif

extern void pre_order_linkbtree(p_linkbtree H);
extern void mid_order_linkbtree(p_linkbtree H);
extern void post_order_linkbtree(p_linkbtree H);
extern void level_order_linkbtree(p_linkbtree H);

extern void pre_un_order_linkbtree(p_linkbtree H);

void travel(char const *str,void(*pfun)(p_linkbtree),p_linkbtree H);
#endif

main.c

#include "linkbtree.h"

int main(int argc, const char *argv[])
{
    p_linkbtree H;

    create_linkbtree(&H);
    
    printf("preordef:");
    pre_order_linkbtree(H);
    puts("");

    printf("midordef:");
    mid_order_linkbtree(H);
    puts("");

    printf("postordef:");
    post_order_linkbtree(H);
    puts("");

    printf("levelordef:");
    level_order_linkbtree(H);
    puts("");

    printf("pre_un_ordef:");
    pre_un_order_linkbtree(H);
    puts("");
puts("#########################################");
    travel("preorder:",pre_order_linkbtree,H);
    travel("midorder:",mid_order_linkbtree,H);
    travel("postorder:",post_order_linkbtree,H);
    travel("levelorder:",level_order_linkbtree,H);
    travel("pre_un_order:",pre_un_order_linkbtree,H);

    return 0;
}

linkbtree.c

#include "linkbtree.h"
#include "linkqueue.h"
#include "linkstack.h"

#if 0
p_linkbtree create_linkbtree(void)
{
    p_linkbtree H;
    data_btree x;

    scanf("%c",&x);
    if(x == '#')
    {
        return NULL;
    }
    else
    {
        if((H = (p_linkbtree)malloc(sizeof(linkbtree))) == NULL)
        {
            printf("malloc no memory!\n");
            return NULL;
        }
        H->data = x;

        H->lchild = create_linkbtree();
        H->rchild = create_linkbtree();
    }
}

#else
void create_linkbtree(p_linkbtree *H)
{
    data_btree x;

    scanf("%c",&x);
    if(x == '#')
    {
        (*H) = NULL;
    }
    else
    {
        if(((*H) = (p_linkbtree)malloc(sizeof(linkbtree))) == NULL)
        {
            printf("malloc no memory!\n");
            (*H) = NULL;
            return ;
        }
        (*H)->data = x;

        create_linkbtree(&(*H)->lchild);
        create_linkbtree(&(*H)->rchild);
    }
}
#endif

void pre_order_linkbtree(p_linkbtree H)
{
    if(H != NULL)
    {
        printf("%c",H->data);

        pre_order_linkbtree(H->lchild);
        pre_order_linkbtree(H->rchild);
    }
}

void mid_order_linkbtree(p_linkbtree H)
{
    if(H != NULL)
    {
        mid_order_linkbtree(H->lchild);
        printf("%c",H->data);
        mid_order_linkbtree(H->rchild);
    }
}

void post_order_linkbtree(p_linkbtree H)
{
    if(H != NULL)
    {
        post_order_linkbtree(H->lchild);
        post_order_linkbtree(H->rchild);
        printf("%c",H->data);
    }
}

void level_order_linkbtree(p_linkbtree H)
{
    p_linkqueue Q;

    create_linkqueue(&Q);
    
    while(H != NULL)
    {
        printf("%c",H->data);
        if(H->lchild != NULL)
            in_linkqueue(Q,H->lchild);
        if(H->rchild != NULL)
            in_linkqueue(Q,H->rchild);

        if((out_linkqueue(Q,&H)) == false)
            break;
    }
}

void pre_un_order_linkbtree(p_linkbtree H)
{
    p_linkstack top;
    top = create_linkstack();
     
    while(H != NULL || !is_empty_linkstack(top))
    {
        if(H != NULL)
        {
            printf("%c",H->data);
            if(H->rchild != NULL)
                push_linkstack(top,H->rchild);
            H = H->lchild;
        }
        else
            H = pop_linkstack(top);
    }
}

void travel(char const *str,void(*pfun)(p_linkbtree),p_linkbtree H)
{
    printf("%s",str);
    pfun(H);
    puts("");
}

linkqueue.c

#include "linkqueue.h"

bool create_linkqueue(p_linkqueue *H)
{  
    if(((*H) = (p_linkqueue)malloc(sizeof(linkqueue))) == NULL)
    {
        printf("malloc no memory!\n");
        return false;
    }

    if(((*H)->front = (p_queuenode)malloc(sizeof(queuenode))) == NULL)
    {
        printf("malloc no memory!\n");
        return false;
    }
    (*H)->front->next = NULL;
    (*H)->rear = (*H)->front;

    return true;
}

bool is_empty_linkqueue(p_linkqueue H)
{
    return (H->rear == H->front);
}

bool in_linkqueue(p_linkqueue H,data_lqueue x)
{
    p_queuenode K;
    if((K = (p_queuenode)malloc(sizeof(queuenode))) == NULL)
    {
        printf("malloc no memory!\n");
        return false;
    }
    K->data = x;
    
    K->next = H->rear->next;
    H->rear->next = K;
    H->rear = K;

    return true;
}
bool out_linkqueue(p_linkqueue H,data_lqueue *x)
{
    p_queuenode r;
    
    if(is_empty_linkqueue(H))
    {
    //    puts("");
    //    printf("queue is NULL!\n");
        return false;
    }
    r = H->front;
    H->front = r->next;
    (*x) = H->front->data;

    r->next = NULL;
    free(r);
    r = NULL;
    
    return true;
}

#if 0
void show_linkqueue(p_linkqueue H)
{
    p_queuenode r;
    
    if(is_empty_linkqueue(H))
    {
        printf("queue is NULL!\n");
        return ;
    }
    r = H->front;
    while(r != H->rear)
    {
        r = r->next;
        printf("%d\t",r->data);
    }
    puts("");
}

#endif

linkstack.c

#include "linkstack.h"

p_linkstack create_linkstack(void)
{
    p_linkstack H;
    if((H = (p_linkstack)malloc(sizeof(linkstack))) == NULL)
    {
        printf("malloc no memory!\n");
        return NULL;
    }
    H->data = 0;
    H->next = NULL;
    return H;
}
void clear_linkstack(p_linkstack L)
{
    p_linkstack r;
    r = L->next;
    while(r != NULL)
    {
        L->next = r->next;
        r->next = NULL;
        free(r);
    
        r = L->next;
    }
}
void free_linkstack(p_linkstack L)
{
    p_linkstack r;
    r = L;
    while(!is_empty_linkstack(L))
    {
        L = r->next;
        r->next =NULL;
        free(r);
        r = L;
    }
}

int is_empty_linkstack(p_linkstack L)
{
    return (L->next == NULL);
}

int push_linkstack(p_linkstack L,data_lstack x)
{
    p_linkstack H,r;

    r = L;
    if((H = (p_linkstack)malloc(sizeof(linkstack))) == NULL)
    {
        printf("malloc no memory!\n");
        return -1;
    }
    H->data = x;

    H->next = r->next;
    r->next = H;

    return 0;
}
data_lstack pop_linkstack(p_linkstack L)
{
    data_lstack x;
    p_linkstack r;
    if(is_empty_linkstack(L))
    {
        printf("stack is NULL!\n");
        return NULL;
    }
    r = L->next;
    L->next = r->next;
    r->next = NULL;
    x = r->data;
    free(r);
    r = NULL;

    return x;
}
data_lstack get_top_linkstack(p_linkstack L)
{
    if(is_empty_linkstack(L))
    {
        printf("stack is NULL!\n");
        return NULL;
    }
    return (L->next->data);
}

#if 0
void show_linkstack(p_linkstack L)
{
    while(!is_empty_linkstack(L))
    {
        printf("%d\t",pop_linkstack(L));
    }
    puts("");
}
#endif

运行结果

输入:AB#CD###E#FGH##K###
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值