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

相关推荐
晚云与城8 分钟前
【数据结构】-----排序的艺术画卷
数据结构·算法·排序算法
j_xxx404_42 分钟前
数据结构:算法复杂度与空间复杂度
c语言·数据结构·算法
自由随风飘2 小时前
旅游城市数量最大化 01背包问题
数据结构·c++·算法·动态规划·旅游
好好先森&3 小时前
C语言:冒泡排序
c语言·数据结构·算法·遍历·冒牌排序
肉夹馍不加青椒4 小时前
第二十三天(数据结构:链表补充【希尔表】)
数据结构·链表
草莓熊Lotso5 小时前
【LeetCode刷题指南】--单值二叉树,相同的树
c语言·数据结构·算法·leetcode·刷题
Asu52026 小时前
链表反转中最常用的方法————三指针法
java·数据结构·学习·链表
闪电麦坤956 小时前
数据结构:在链表中查找(Searching in a Linked List)
数据结构·链表
泥泞开出花朵8 小时前
LRU缓存淘汰算法的详细介绍与具体实现
java·数据结构·后端·算法·缓存
KarrySmile9 小时前
Day17--二叉树--654. 最大二叉树,617. 合并二叉树,700. 二叉搜索树中的搜索,98. 验证二叉搜索树
数据结构·算法·二叉树·二叉搜索树·合并二叉树·最大二叉树·验证二叉搜索树