2024.01.30作业

1. 1.二叉树递归创建  2.二叉树先中后序遍历 3.二叉树计算节点4.二叉树计算深度

myfunc.h

#ifndef __MYFUNC_H__
#define __MYFUNC_H__

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

typedef char datatype;
typedef struct Node
{
    datatype data;
    struct Node* lchild;
    struct Node* rchild;
} *Btree;

Btree create_node();
Btree create_tree();
void pre_order(Btree node);
void mid_order(Btree node);
void post_order(Btree node);
void count(Btree node, int* n0, int* n1, int* n2);
int height(Btree node);

#endif

myfunc.c

#include "myfunc.h"

Btree create_node()
{
    Btree node = (Btree)malloc(sizeof(struct Node));
    if (NULL == node)
    {
        return NULL;
    }

    node->data = ' ';
    node->lchild = NULL;
    node->rchild = NULL;

    return node;
}

Btree create_tree()
{
    datatype element;

    scanf("%c", &element);
    getchar();
    if ('#' == element)
    {
        return NULL;
    }

    Btree node = create_node();
    node->data = element;

    node->lchild = create_tree();
    node->rchild = create_tree();

    return node;
}

void pre_order(Btree node)
{
    if (NULL == node)
    {
        return;
    }

    printf("%c", node->data);
    pre_order(node->lchild);
    pre_order(node->rchild);
}

void mid_order(Btree node)
{
    if (NULL == node)
    {
        return;
    }

    mid_order(node->lchild);
    printf("%c", node->data);
    mid_order(node->rchild);
}

void post_order(Btree node)
{
    if (NULL == node)
    {
        return;
    }

    post_order(node->lchild);
    post_order(node->rchild);
    printf("%c", node->data);
}

void count(Btree node, int* n0, int* n1, int* n2)
{
    if (NULL == node)
    {
        return;
    }

    if (!node->lchild && !node->rchild)
    {
        ++*n0;
    }
    else if (node->lchild && node->rchild)
    {
        ++*n2;
    }
    else{
        ++*n1;
    }

    count(node->lchild, n0, n1, n2);
    count(node->rchild, n0, n1, n2);
}

int height(Btree node)
{
    if (NULL == node)
    {
        return 0;
    }

    int l = 1 + height(node->lchild);
    int r = 1 + height(node->rchild);

    return l > r ? l : r;
}

test.c

#include "myfunc.h"

int main()
{
    Btree root = create_tree();

    printf("先序遍历:");
    pre_order(root);
    puts("");
    printf("中序遍历:");
    mid_order(root);
    puts("");
    printf("后序遍历:");
    post_order(root);
    puts("");

    int n0 = 0, n1 = 0, n2 = 0;
    count(root, &n0, &n1, &n2);
    printf("n0: %d, n1: %d, n2: %d\n", n0, n1, n2);

    printf("树深度:%d\n", height(root));

    return 0;
}

结果

2.编程实现快速排序

#include <stdio.h>

void quick_sort(int* a, int l, int r)
{
    if (l < r)
    {
        int i = l, j = r, x = a[l];
        while (i < j)
        {
            while (i < j && a[j] >= x)
            {
                j--;
            }
            if (i < j)
            {
                a[i++] = a[j];
            }

            while (i < j && a[i] < x)
            {
                i++;
            }
            if (i < j)
            {
                a[j--] = a[i];
            }
        }
        a[i] = x;
        quick_sort(a, l, i - 1);
        quick_sort(a, i + 1, r);
    }
}

int main()
{
    int n;
    scanf("%d", &n);

    int a[n];
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    for (int i = 0; i < n; i++)
    {
        printf("%2d", a[i]);
    }
    puts("");

    quick_sort(a, 0, n - 1);

    printf("sort\n");
    for (int i = 0; i < n; i++)
    {
        printf("%2d", a[i]);
    }
    puts("");

    return 0;
}

3.思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值