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.哈希查找

相关推荐
CSharp精选营2 天前
关系型 vs 非关系型:从原理到选型,一文搞定数据库核心分类
数据结构·nosql·关系型数据库·非关系型数据库·技术选型
刘马想放假5 天前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
北域码匠6 天前
冒泡排序太慢?鸡尾酒排序双向优化,原生 C# 零第三方库完整代码
数据结构·排序算法·泛型·c# 算法·鸡尾酒排序·原生 c# 开发·冒泡排序优化·嵌入式算法
Darling噜啦啦13 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠14 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾14 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres82114 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q14 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒14 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
WL学习笔记14 天前
单项不带头不循环链表
数据结构·链表