【c语言数据结构】二叉树程序设计

一、实验目的及要求
1.掌握二叉树的定义及其链式存储结构。
2.掌握二叉树的先序遍历、中序遍历和后序遍历,并将结果序列输出
二、实验内容
1.先序创建二叉树。
2.定义队列,进行入队,出队操作。
3.树的层次、先序、中序和后序遍历
4.并输出叶子结点个数以及二叉树的高度
三、实验步骤和要求
以下图所示的二叉树为例编制程序,实现实验内容中所述功能。
在这里插入图片描述
二叉树定义:

//定义二叉树节点结构体
typedef struct node
{
    char stu;
    struct node *lchild, *rchild;
} Node;
//定义队列结构体
typedef struct
{
    Node data[MAX];
    int head, rear;
} SqQueue;

创建新节点:

//创建新节点
Node *createNode(char stu)
{
    // 1.创建节点
    Node *node = (Node *)malloc(sizeof(Node));
    // 2.判断是否拿到内存
    if (!node)
        exit(-1);
    // 3.将输入的参数赋值给新拿到的内存空间
    node->stu = stu;
    node->lchild = node->rchild = NULL;
    // 4.将输入的参数赋值给新拿到的内存空间
    // 5.返回创建好的节点
    return node;
}

初始化一个二叉树:

//初始化一个二叉树
Node *createTree(Node *tree)
{
    // 1.设置一个标记,告诉程序是否继续添加节点
    int tag = 0;
    printf("是否有左子树或者右子树,如果有,输入1,否则输入0:");
    scanf("%d", &tag);
    getchar();
    // 2.如果继续添加节点,需要给节点初始化
    if (tag != 0)
    {
        char stu;
        printf("请输入节点值:");
        scanf("%s", &stu);
        tree = createNode(stu);
        // 3.继续添加左子树,继续添加右子树
        tree->lchild = createTree(tree->lchild);
        tree->rchild = createTree(tree->rchild);
    }

    // 4.要返回创建的子树
    return tree;
}

销毁二叉树;

//销毁二叉树
void destroyTree(Node *tree)
{
    if (tree != NULL)
    {
        destroyTree(tree->lchild);
        destroyTree(tree->rchild);
        free(tree);
    }
}

先序遍历二叉树:

//先序遍历二叉树
void preOrderTree(Node *tree)
{
    if (tree != NULL)
    {
        printf("%s \n", tree->stu);
        preOrderTree(tree->lchild);
        preOrderTree(tree->rchild);
    }
}

中序遍历二叉树:

//中序遍历二叉树
void inOrderTree(Node *tree)
{
    if (tree != NULL)
    {
        inOrderTree(tree->lchild);
        printf("%s \n", tree->stu);
        inOrderTree(tree->rchild);
    }
}

后序遍历二叉树:

//后序遍历二叉树
void postOrderTree(Node *tree)
{
    if (tree != NULL)
    {
        postOrderTree(tree->lchild);
        postOrderTree(tree->rchild);
        printf("%s \n", tree->stu);
    }
}

统计二叉树中的节点个数;

//统计二叉树中的节点个数
void numNode(Node *tree, int *num)
{
    if (tree != NULL)
    {
        numNode(tree->lchild, num);
        numNode(tree->rchild, num);
        (*num)++;
    }
}

层次遍历二叉树:

//层次遍历二叉树
// 1.初始化队列
//判断队是否为空
//队不为空则返回1,队空则返回0
int EmptyQueue(SqQueue *Q)
{
    if (Q->head == Q->rear)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

//2.入队
void PushQueue(SqQueue *Q, Node t)
{
    Q->rear = (Q->rear + 1) % MAX;
    Q->data[Q->rear] = t;
}

//3.出队
void PopQueue(SqQueue *Q)
{
    Q->head = (Q->head + 1) % MAX;
}

//层次遍历二叉树
void Leavel_Order(Node *tree)
{
    SqQueue Q;
    //定义一个循环队列
    Q.head = 0;
    Q.rear = 0;
    //循环队列初始化
    //队为空的条件:队头等于队尾
    Node *temp;
    //定义一个二叉树类型的指针
    PushQueue(&Q, *tree);
    //根节点入队
    while (EmptyQueue(&Q))
    //当队不为空时,循环
    {
        *temp = Q.data[Q.head + 1];
        //将队头元素赋值给变量*temp
        printf("%s \n", temp->stu);
        //输出队头元素的值
        PopQueue(&Q);
        //队头元素出队
        if (temp->lchild != NULL)
        //如果左孩子不为空,则左子树入队
        {
            PushQueue(&Q, *temp->lchild);
        }
        //如果右孩子不为空,则右子树入队
        if (temp->rchild != NULL)
        {
            PushQueue(&Q, *temp->rchild);
        }
    }
}

计算树的高度:

int tree_height(Node *root)
{
    // Get the height of the tree
    if (!root)
        return 0;
    else
    {
        // Find the height of both subtrees
        // and use the larger one
        int left_height = tree_height(root->lchild);
        int right_height = tree_height(root->rchild);
        if (left_height >= right_height)
            return left_height + 1;
        else
            return right_height + 1;
    }
}

全部代码:

/*二叉树*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 40
//定义二叉树节点结构体
typedef struct node
{
    char stu;
    struct node *lchild, *rchild;
} Node;
//定义队列结构体
typedef struct
{
    Node data[MAX];
    int head, rear;
} SqQueue;

//创建新节点
Node *createNode(char stu)
{
    // 1.创建节点
    Node *node = (Node *)malloc(sizeof(Node));
    // 2.判断是否拿到内存
    if (!node)
        exit(-1);
    // 3.将输入的参数赋值给新拿到的内存空间
    node->stu = stu;
    node->lchild = node->rchild = NULL;
    // 4.将输入的参数赋值给新拿到的内存空间
    // 5.返回创建好的节点
    return node;
}
//初始化一个二叉树
Node *createTree(Node *tree)
{
    // 1.设置一个标记,告诉程序是否继续添加节点
    int tag = 0;
    printf("是否有左子树或者右子树,如果有,输入1,否则输入0:");
    scanf("%d", &tag);
    getchar();
    // 2.如果继续添加节点,需要给节点初始化
    if (tag != 0)
    {
        char stu;
        printf("请输入节点值:");
        scanf("%s", &stu);
        tree = createNode(stu);
        // 3.继续添加左子树,继续添加右子树
        tree->lchild = createTree(tree->lchild);
        tree->rchild = createTree(tree->rchild);
    }

    // 4.要返回创建的子树
    return tree;
}
//销毁二叉树
void destroyTree(Node *tree)
{
    if (tree != NULL)
    {
        destroyTree(tree->lchild);
        destroyTree(tree->rchild);
        free(tree);
    }
}
//先序遍历二叉树
void preOrderTree(Node *tree)
{
    if (tree != NULL)
    {
        printf("%s \n", tree->stu);
        preOrderTree(tree->lchild);
        preOrderTree(tree->rchild);
    }
}
//中序遍历二叉树
void inOrderTree(Node *tree)
{
    if (tree != NULL)
    {
        inOrderTree(tree->lchild);
        printf("%s \n", tree->stu);
        inOrderTree(tree->rchild);
    }
}
//后序遍历二叉树
void postOrderTree(Node *tree)
{
    if (tree != NULL)
    {
        postOrderTree(tree->lchild);
        postOrderTree(tree->rchild);
        printf("%s \n", tree->stu);
    }
}
//统计二叉树中的节点个数
void numNode(Node *tree, int *num)
{
    if (tree != NULL)
    {
        numNode(tree->lchild, num);
        numNode(tree->rchild, num);
        (*num)++;
    }
}

//层次遍历二叉树
// 1.初始化队列
//判断队是否为空
//队不为空则返回1,队空则返回0
int EmptyQueue(SqQueue *Q)
{
    if (Q->head == Q->rear)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

//2.入队
void PushQueue(SqQueue *Q, Node t)
{
    Q->rear = (Q->rear + 1) % MAX;
    Q->data[Q->rear] = t;
}

//3.出队
void PopQueue(SqQueue *Q)
{
    Q->head = (Q->head + 1) % MAX;
}

//层次遍历二叉树
void Leavel_Order(Node *tree)
{
    SqQueue Q;
    //定义一个循环队列
    Q.head = 0;
    Q.rear = 0;
    //循环队列初始化
    //队为空的条件:队头等于队尾
    Node *temp;
    //定义一个二叉树类型的指针
    PushQueue(&Q, *tree);
    //根节点入队
    while (EmptyQueue(&Q))
    //当队不为空时,循环
    {
        *temp = Q.data[Q.head + 1];
        //将队头元素赋值给变量*temp
        printf("%s \n", temp->stu);
        //输出队头元素的值
        PopQueue(&Q);
        //队头元素出队
        if (temp->lchild != NULL)
        //如果左孩子不为空,则左子树入队
        {
            PushQueue(&Q, *temp->lchild);
        }
        //如果右孩子不为空,则右子树入队
        if (temp->rchild != NULL)
        {
            PushQueue(&Q, *temp->rchild);
        }
    }
}

int tree_height(Node *root)
{
    // Get the height of the tree
    if (!root)
        return 0;
    else
    {
        // Find the height of both subtrees
        // and use the larger one
        int left_height = tree_height(root->lchild);
        int right_height = tree_height(root->rchild);
        if (left_height >= right_height)
            return left_height + 1;
        else
            return right_height + 1;
    }
}

int main()
{
    int num = 0, height = 0;
    Node *tree = createTree(tree);
    printf("先序遍历:\n");
    preOrderTree(tree);
    printf("中序遍历:\n");
    inOrderTree(tree);
    printf("后序遍历:\n");
    postOrderTree(tree);
    printf("层次遍历:\n");
    Leavel_Order(tree);
    height = tree_height(tree);
    printf("树的高度是:%d\n", height);
    numNode(tree, &num);
    printf("节点个数是%d个\n", num);
    destroyTree(tree);
    system("pause");
    return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兜兜里有好多糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值