0724作业+梳理

一、作业

1.二叉树创建和遍历

1.函数声明

#ifndef TREE_H
#define TREE_H
#include<myhead.h>

//定义数据
typedef char datatype;
//定义树
typedef struct Node
{
    datatype data;
    struct Node * left_child;
    struct Node * right_child;
}Tree,*TreePtr;

//创建二叉树
TreePtr tree_creat();
//前序遍历
void tree_prio_show(TreePtr T);
//中序遍历
void tree_in_show(TreePtr T);
//后序遍历
void tree_post_show(TreePtr T);

#endif
ubuntu@ubuntu:1$ 
 

2.源函数

#include"tree.h"


//创建二叉树
TreePtr tree_creat()
{
    char data = 0;
    scanf(" %c",&data);
    TreePtr p = (TreePtr)malloc(sizeof(Tree));
    if(NULL == p)
    {
        printf("创建失败\n");
        return NULL;
    }
    p->data = data;
    if(p->data == '#')
    {
        return NULL;
    }
    p->left_child = tree_creat();
    p->right_child = tree_creat();
}
//前序遍历
void tree_prio_show(TreePtr T)
{
    if(NULL == T)
    {
        return;
    }
    printf("%c\t",T->data);
    tree_prio_show(T->left_child);
    tree_prio_show(T->right_child);
}
//中序遍历
void tree_in_show(TreePtr T)

{
    if(NULL == T)
    {
        return;
    }
    tree_in_show(T->left_child);
    printf("%c\t",T->data);
    tree_in_show(T->right_child);
}
//后序遍历
void tree_post_show(TreePtr T)
{
    if(NULL == T)
    {
        return;
    }
    tree_post_show(T->left_child);
    tree_post_show(T->right_child);
    printf("%c\t",T->data);
}


 

3.主程序

#include"tree.h"
#include<myhead.h>
int main(int argc, const char *argv[])
{
    //调用创建二叉树
    TreePtr T = tree_creat();
    if(NULL == T)
    {
        printf("创建失败\n");
        return -1;
    }
    //调用遍历
    printf("前序遍历结果为:");
    tree_prio_show(T);
    printf("\n");
    printf("中序遍历结果为:");
    tree_in_show(T);
    printf("\n");
    printf("后序遍历结果为:");
    tree_post_show(T);
    printf("\n");
 

2.冒泡,选择,插入,快排

1.代码

#include<myhead.h>
//冒泡排序函数
void sort1(int *arr,int n)
{
    for(int i=1;i<n;i++)
    {
        int flag = 0;
        for(int j=0;j<n;j++)
        {
            if(arr[j]>arr[j+1])
            {
                flag = 1;
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
        if(flag == 0)
        {
            break;
        }
    }
}
//选择排序函数
void select_sotr(int *arr,int n)
{
    for(int i=0;i<n;i++)
    {
        int mini = i;
        for(int j=i+1;j<n;j++)
        {
            if(arr[mini]>arr[j])
            {
                mini = j;
            }
        }
        if(mini != i)
        {
            int temp = arr[mini];
            arr[mini] = arr[i];
            arr[i] = temp;
        }
    }
}
//插入排序函数
void insert_sort(int *arr,int n)
{
    for(int i=1;i<n;i++)
    {
        int temp = arr[i];
        int j=0;
        for(j=i-1;temp<=arr[j]&&j>=0;j--)
        {
            
                arr[j+1] = arr[j];
        }
        arr[j+1] = temp;
    }
}

//快速排序函数
int quick(int *arr,int low,int high)
{
    int X = arr[low];
    while(high>low)
    {
        while(arr[high]>X&&high>low)
        {
            high--;
        }
        arr[low] = arr[high];
        while(arr[low]<X&&high>low)
        {
            low++;
        }
        arr[high] = arr[low];
    }
    arr[low] = X;
    return low;
}
void quick_sort(int *arr,int low,int high)
{
    if(low>=high)
    {
        return;
    }
    int a = quick(arr,low,high);
    quick_sort(arr,low,a-1);
    quick_sort(arr,a+1,high);
}
//输出函数
void out_put(int *arr,int n)
{
    printf("排序后的数组为:\n");
    for(int i=0;i<n;i++)
    {
        printf("%d\t",arr[i]);
    }
    printf("\n");
}

int main(int argc, const char *argv[])
{
    int arr[] = {2,14,6,7,8,1,4,9};
    int n = sizeof(arr)/sizeof(arr[0]);
    //sort1(arr,n);
    //select_sotr(arr,n);
    //insert_sort(arr,n);
    quick_sort(arr,0,n);
    out_put(arr,n);
    return 0;
}
 

2.运行

二、梳理(二叉树,算法部分内容)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值