【C语言】二叉树基本操作

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

typedef struct BiTree
{
    char data;
    struct BiTree *lchild;
    struct BiTree *rchild;
} BiTNode, *BinaryTree; // 二叉树结点

typedef struct LNode
{
    BinaryTree data;
    struct LNode *next;
} LinkNode;

typedef struct
{
    LinkNode *front;
    LinkNode *rear;
} LinkQueue; // 链队列

typedef struct
{
    LinkNode *top;
} LinkStack; // 链栈

// 链队列操作
void InitLinkQueue(LinkQueue *LQ);
void EnQueue(LinkQueue *LQ, BinaryTree e);
void DeQueue(LinkQueue *LQ, BinaryTree *e);
int IsQueueEmpty(LinkQueue LQ);

// 链栈操作
void InitLinkStack(LinkStack *LS);
void push(LinkStack *LS, BinaryTree e);
void pop(LinkStack *LS, BinaryTree *e);
int IsStackEmpty(LinkStack LS);
void GetTop(LinkStack LS, BinaryTree *e);

// 二叉树操作
void CreateBinaryTree(BinaryTree *BT);
void PreOrder(BinaryTree BT);
void InOrder(BinaryTree BT);
void PostOrder(BinaryTree BT);
int CountLeaf(BinaryTree BT);
int CountNode(BinaryTree BT);
int Depth(BinaryTree BT);
void PreOrder2(BinaryTree BT);
void InOrder2(BinaryTree BT);
void PostOrder2(BinaryTree BT);
void LevelOrder(BinaryTree BT);
int Depth2(BinaryTree BT);
int main()
{
    BinaryTree BT; // 测试用例:ABD#G###CE##F##
    CreateBinaryTree(&BT);
    printf("先序遍历:\n");
    PreOrder(BT);
    printf("\n");
    printf("先序遍历(非递归):\n");
    PreOrder2(BT);
    printf("\n");
    printf("中序遍历:\n");
    InOrder(BT);
    printf("\n");
    printf("中序遍历(非递归):\n");
    InOrder2(BT);
    printf("\n");
    printf("后序遍历:\n");
    PostOrder(BT);
    printf("\n");
    printf("后序遍历(非递归):\n");
    PostOrder2(BT);
    printf("\n");
    printf("层序遍历:\n");
    LevelOrder(BT);
    printf("\n叶子节点数: %d\n总结点数: %d\n二叉树深度: %d\n",
           CountLeaf(BT), CountNode(BT), Depth(BT));
    system("pause");
    return 0;
}

void InitLinkQueue(LinkQueue *LQ)
{
    LQ->front = LQ->rear = (LinkNode *)malloc(sizeof(LinkNode));
    LQ->front->next = NULL;
}
void EnQueue(LinkQueue *LQ, BinaryTree e)
{
    LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
    p->data = e;
    p->next = NULL;
    LQ->rear->next = p;
    LQ->rear = p;
}
void DeQueue(LinkQueue *LQ, BinaryTree *e)
{
    if (LQ->front == LQ->rear)
    {
        return;
    }
    LinkNode *p = LQ->front->next;
    LQ->front->next = p->next;
    *e = p->data;
    if (p == LQ->rear)
    {
        LQ->rear = LQ->front;
    }
    free(p);
}

int IsQueueEmpty(LinkQueue LQ)
{
    return LQ.front == LQ.rear;
}

void InitLinkStack(LinkStack *LS)
{
    LS->top = NULL;
}
void push(LinkStack *LS, BinaryTree e)
{
    LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
    p->data = e;
    p->next = LS->top;
    LS->top = p;
}
void pop(LinkStack *LS, BinaryTree *e)
{
    if (LS->top == NULL)
    {
        return;
    }
    LinkNode *p = LS->top;
    LS->top = p->next;
    *e = p->data;
    free(p);
}
int IsStackEmpty(LinkStack LS)
{
    return LS.top == NULL;
}

void GetTop(LinkStack LS, BinaryTree *e)
{
    if (!IsStackEmpty(LS))
    {
        *e = LS.top->data;
    }
    else
    {
        *e = NULL;
    }
}

void CreateBinaryTree(BinaryTree *BT)
{
    char e;
    scanf("%c", &e);
    if (e == '#')
    {
        *BT = NULL;
        return;
    }
    else
    {
        *BT = (BiTNode *)malloc(sizeof(BiTNode));
        (*BT)->data = e;
        CreateBinaryTree(&(*BT)->lchild);
        CreateBinaryTree(&(*BT)->rchild);
    }
}
void PreOrder(BinaryTree BT)
{
    if (BT)
    {
        printf("%c ", BT->data);
        PreOrder(BT->lchild);
        PreOrder(BT->rchild);
    }
}

void InOrder(BinaryTree BT)
{
    if (BT)
    {
        InOrder(BT->lchild);
        printf("%c ", (BT)->data);
        InOrder(BT->rchild);
    }
}

void PostOrder(BinaryTree BT)
{
    if (BT)
    {
        PostOrder(BT->lchild);
        PostOrder(BT->rchild);
        printf("%c ", BT->data);
    }
}

int CountLeaf(BinaryTree BT)
{
    if (BT == NULL)
    {
        return 0;
    }
    if (BT->lchild == NULL && BT->rchild == NULL)
    {
        return 1;
    }
    else
    {
        return CountLeaf(BT->lchild) + CountLeaf(BT->rchild);
    }
}

int CountNode(BinaryTree BT)
{
    if (BT == NULL)
    {
        return 0;
    }
    else
    {
        return CountNode(BT->lchild) + CountNode(BT->rchild) + 1;
    }
}

int Depth(BinaryTree BT)
{
    if (BT == NULL)
    {
        return 0;
    }
    else
    {
        int L = Depth(BT->lchild);
        int R = Depth(BT->rchild);
        return (L > R ? L : R) + 1;
    }
}

void PreOrder2(BinaryTree BT)
{
    LinkStack LS;
    InitLinkStack(&LS);
    BinaryTree p = BT;
    while (p || !IsStackEmpty(LS))
    {
        if (p)
        {
            printf("%c ", p->data);
            push(&LS, p);
            p = p->lchild;
        }
        else
        {
            pop(&LS, &p);
            p = p->rchild;
        }
    }
}

void InOrder2(BinaryTree BT)
{
    LinkStack LS;
    InitLinkStack(&LS);
    BinaryTree p = BT;
    while (p || !IsStackEmpty(LS))
    {
        if (p)
        {
            push(&LS, p);
            p = p->lchild;
        }
        else
        {
            pop(&LS, &p);
            printf("%c ", p->data);
            p = p->rchild;
        }
    }
}
void LevelOrder(BinaryTree BT)
{
    LinkQueue LQ;
    BinaryTree p = BT;
    InitLinkQueue(&LQ);
    EnQueue(&LQ, p);
    while (!IsQueueEmpty(LQ))
    {
        DeQueue(&LQ, &p);
        printf("%c ", p->data);
        if (p->lchild)
        {
            EnQueue(&LQ, p->lchild);
        }
        if (p->rchild)
        {
            EnQueue(&LQ, p->rchild);
        }
    }
}
int Depth2(BinaryTree BT)
{
    if(!BT)
    {
        return 0;
    }
    int front=-1;
    int rear=-1;
    BinaryTree Queue[100]={NULL};
    int last=0;
    int level=0;
    BinaryTree p;
    Queue[++rear]=BT;
    while(front<rear)
    {
        p=Queue[++front];
        if(p->lchild)
        {
            Queue[++rear]=p->lchild;
        }
        if(p->rchild)
        {
            Queue[++rear]=p->rchild;
        }
        if(front==last)
        {
            level++;
            last=rear;
        }
        
    }
    return level;
}
void PostOrder2(BinaryTree BT)
{
    LinkStack LS;
    InitLinkStack(&LS);
    BinaryTree p = BT;
    BinaryTree tag = NULL; // 标志指针
    while (p || !IsStackEmpty(LS))
    {
        if (p)
        {
            push(&LS, p);
            p = p->lchild;
        }
        else
        {
            GetTop(LS, &p);
            if (p->rchild && p->rchild != tag)
            {
                p = p->rchild;
            }
            else
            {
                pop(&LS, &p);
                printf("%c ", p->data);
                tag = p;
                p = NULL;
            }
        }
    }
}

测试结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值