Day05_数据结构(二叉树&快速排序&插入排序&二分查找)

01.思维导图

02.二叉树的前序遍历,中序遍历,后序遍历

main.c

cpp 复制代码
#include "tree.h"
int main()
{
    T_p T=(T_p)create_tree();
	printf("前序遍历:\n");
    pre_show(T);
	printf("\n");
	printf("中序遍历:\n");
	mid_show(T);
	printf("\n");
	printf("后序遍历:\n");
	tail_show(T);
	printf("\n");
    return 0;
}
                              

tree.c

cpp 复制代码
#include "tree.h"
//1、创建二叉树
T_p create_tree()
{
	//定义一个char类型的变量,接收用户输入的内容创建结点
	char data;
	scanf(" %c",&data);
	T_p T;
	//判断用户输入是否为#
	//如果是#说明没有该节点
	if(data=='#')   //递归出口
	{
		return NULL;
	}
	T = create_node(data);
	//结点的左子树,也是一个二叉树,也需要创建
	T->lchild = create_tree();
	//创建结点的右子树
	T->rchild = create_tree();
	return T;
}
//2、创建结点
T_p create_node(char data)
{
	T_p new = (T_p)malloc(sizeof(tree_node));
	if(new==NULL){return -1;}
	new->data = data;
	new->lchild = NULL;
	new->rchild = NULL;
	return new;
}
//3、前序遍历 根左右
void pre_show(T_p T)
{
	if(T==NULL){return;}
	//输出根节点的数据
	printf("%c",T->data);
	//左子树也要前序遍历
	pre_show(T->lchild);
	//右子树也要前序遍历
	pre_show(T->rchild);
}
//4、中序遍历 左根右
void mid_show(T_p T)
{
	if(T==NULL){return;}
	mid_show(T->lchild);
	printf("%d",T->data);
	mid_show(T->rchild);
}
//5、后序遍历
void tail_show(T_p T)
{
	if(T==NULL){return;}
	tail_show(T->lchild);
	tail_show(T->rchild);
	printf("%d",T->data);
}

tree.h

cpp 复制代码
#ifndef __TREE_H__
#define __TREE_H__
#include <stdio.h>
#include <stdlib.h>
typedef struct tree_node
{
    char data;
    struct tree_node *lchild;
    struct tree_node *rchild;
}tree_node,*T_p;
//1、创建二叉树
T_p create_tree();
//2、创建结点
T_p create_node(char data);
//3、前序遍历 根左右
void pre_show(T_p T);
//4、中序遍历 左根右
void mid_show(T_p T);
//5、后序遍历
void tail_show(T_p T);

#endif

03.快速排序

cpp 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//1.一次快排需要返回最后基准的位置
int on_sort(int *p,int low,int high)
{
	int base=*(p+low);
	while(high>low)
	{
		//high一侧的数据比基准更大
		//因为内存循环中每次high和low的值都会改变所以必须加到循环里面
		while(*(p+high)>=base&&high>low)
		{
			high--;
		}
		*(p+low)=*(p+high);
		while(*(p+low)<=base&&high>low)
		{
			low++;
		}
		*(p+high)=*(p+low);
	}
	*(p+low)=base;//将基准放在中间位置
	return low;
}
void sort(int *p,int low,int high)
{
	int ret;
	if(high>low)
	{
		ret=on_sort(p,low,high);
		sort(p,low,ret-1);
		sort(p,ret+1,high);
	}
}
int main(int argc, const char *argv[])
{
	int arr[]={50,36,66,76,36,12,25,95};
	on_sort(arr,0,sizeof(arr)/sizeof(int)-1);
	printf("一次快排的结果:");
	for(int i=0;i<sizeof(arr)/sizeof(int);i++)
	{
		printf("%-3d",arr[i]);
	}
	sort(arr,0,sizeof(arr)/sizeof(int)-1);
	printf("\n");
	printf("完全快排的结果:");
	for(int i=0;i<sizeof(arr)/sizeof(int);i++)
	{
		printf("%-3d",arr[i]);
	}	
	printf("\n");
	return 0;
}

04.插入排序

cpp 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void insert_sort(int *p,int len)
{
	int i,j,temp;
	//外层循环获取要插入的每一个元素
	for(i=1;i<len;++i){
		//把每次插入的数据保存
		temp=p[i];
		//内层循环找到元素应该插入的位置
		//因为是顺序结构,插入的同时需要保证后面的位置不变
		//需要将插入位置后面的元素后移动
		//后移的就是比我插入元素更大的数
		for(j=i;j>0&&p[j-1]>temp;j--)
		{
			p[j]=p[j-1];
		}
		//退出循环说名找到零要插入的位置
		p[j]=temp;
	}
}


int main(int argc, const char *argv[])
{
	int arr[]={76,36,12,25,95};
	int len=sizeof(arr)/sizeof(arr[0]);
	insert_sort(arr,len);
	printf("输出插入排序的数组:");
	for(int i=0;i<len;i++){
		printf("%-3d",arr[i]);
	}
	printf("\n");
	
	return 0;
}

05.二分查找/折半查找

cpp 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int half_search(int *p,int start,int end,int key)
{
    int mid;
    while(end>=start){
        //1.找到中间值,中间值的下标
        mid=(start+end)/2;
        if(p[mid]>key){
            end=mid-1;//更新序列的终止位置
        }
        else if(p[mid]<key){
            start=mid+1;//更新序列的起始值
        }
        else if(p[mid]==key){
            return mid;
        }
    }
    return -1;                                              

}
int main(int argc, const char *argv[])
{
    
    int arr[]={50,60,78,56,23,89,156};
    int end=sizeof(arr)/sizeof(arr[0])-1;
    int ret=half_search(arr,0,end,110);
    printf("输在数组中的下标为:%d\n",ret);
    return 0;
}
                                                            

06.哈希查找

相关推荐
纵有疾風起7 分钟前
线性表的定义与基本操作 — 从逻辑结构到 ADT 接口
数据结构·算法·408·线性表·adt
wuyk55534 分钟前
1.栈:后进先出的线性数据结构
c语言·数据结构·stm32·单片机
LuminousCPP1 小时前
数据结构-时间与复杂度|时间/空间复杂度 + 两道力扣练习 + 二分查找复盘
c语言·数据结构·经验分享·算法·leetcode
Nil2081 小时前
leetcode 三数之和
数据结构·算法·leetcode
青山木3 小时前
Hot 100 --- 最小栈
java·数据结构·算法·leetcode
疯狂打码的少年3 小时前
【数据结构】栈的应用:表达式求值(后缀表达式)
java·数据结构·笔记·算法
SunnyByte3 小时前
排序进阶:快速排序与归并排序的迭代实现,并与递归版本对比
c语言·数据结构·算法·排序算法
wabs6664 小时前
关于图论【最短路径之Bellman_ford 算法(判断负权回路)|卡码网95.城市间货物运输II的思考】
数据结构·算法·图论·bellman_ford算法·卡码网·判断负权回路
wabs6665 小时前
关于哈希表【力扣15.三数之和的思考】
数据结构·算法·leetcode·散列表·哈希表·三数之和
疯狂打码的少年6 小时前
【数据结构】队列的应用:循环队列
数据结构·笔记·算法