DAY8-二叉树

文章展示了C语言实现的快速排序和插入排序算法,以及二叉树的先序、中序和后序遍历。通过这些基础算法的实现,读者可以深入理解数据结构和排序原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作业

快速排序

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

int one_sort(int a[],int low,int high);
void insert(int a[],int low,int high);
void Out(int a[],int len);

int main(int argc,const char * argv[])
{
    int a[]={9,8,7,6,5,4,3,2,1};
    int len=sizeof(a)/sizeof(a[0]);
    insert(a,0,len-1);

    Out(a,len);
    return 0;
}

void Out(int a[],int len)
{
    for(int i=0;i<len;i++)
    {
        printf("%d\t",a[i]);
    }
}

int one_sort(int a[],int low,int high)
{
    int key=a[low];
    while(low<high)
    {
        while(low<high && key<=a[high])
        {
            high--;
        }
        a[low]=a[high];

        while(low<high && key>=a[low])
        {
            low++;
        }
        a[high]=a[low];

    }

    a[low]=key;
    return low;
}

void insert(int a[],int low,int high)
{
    if(low<high)
    {
        int mid=one_sort(a,low,high);

        one_sort(a,low,mid-1);

        one_sort(a,mid+1,high);

    }
}

直接排序

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

int insert(int *p,int n);

int main(int argc,const char * argv[])
{
    int a[]={9,8,7,6,5,4,3,2,1};
    int n=sizeof(a)/sizeof(a[0]);
    int *p=a;
    insert(p,n);


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


    return 0;
}

int insert(int *p,int n)
{
    int j=0;
    for(int i=1;i<n;i++)
    {
        int t=*(p+i);
        
        for(j=i-1;j>=0 && t<*(p+j);j--)
        {
          
            *(p+j+1)=*(p+j);
        }
           
            *(p+j+1)=t;
    }

    return 0;
}

二叉数的插入遍历

main.c

#include "head.h"

int main(int argc,const char * argv[])
{
    Btree T=create();

    printf("\n先序遍历的结果是:");
    first(T);
    printf("\n中序遍历的结果:");
    mid(T);
    printf("\n后续遍历的结果是:");
    last(T);
    
    return 0;
}

test.c


#include "head.h"

Btree create()
{
    datetype e;
    printf("请输入您要的数据:");
    scanf(" %c",&e);

    if(e=='#')
        return NULL;
    
    Btree T=(Btree)malloc(sizeof(struct Node));
    if(T==NULL)
        return NULL;
    
    T->date=e;
    puts("左子树");
    T->left=create();
    puts("右子树");
    T->right=create();

    return T;
}

void first(Btree T)
{
    if(T==NULL)
     return ;
    
    printf("%c\t",T->date);//根
    first(T->left);//左
    first(T->right);//右
}

void mid(Btree T)
{
    if(T==NULL)
        return ;
    
    mid(T->left);//左
    printf("%c\t",T->date);//根
    mid(T->right);//右

}

void last(Btree T)
{
    if(T==NULL)
        return ;
    
    last(T->left);//左
    last(T->right);//右
    printf("%c\t",T->date);//根
    
}

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

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



typedef char datetype;

typedef struct Node
{
    datetype date;

    struct Node *left;
    struct Node *right;

    
}*Btree;

Btree create();


#include "test.c"
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值