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

相关推荐
-qOVOp-9 分钟前
408第二季 - 组成原理 - 流水线
数据结构·算法
zylyyyyyy3 小时前
Polar (极化)码的译码Ⅱ--BP 译码算法
数据结构·算法
YuTaoShao4 小时前
Java八股文——数据结构「排序算法篇」
java·数据结构·算法·面试·排序算法·八股文
随缘而动,随遇而安11 小时前
第八十二篇 大数据开发基础:树形数据结构深度解析与实战指南(附创新生活案例)
大数据·开发语言·数据结构
药95513 小时前
数据结构 4 (栈和队列)
java·开发语言·数据结构
2401_8582861114 小时前
CD45.【C++ Dev】STL库的list的使用
开发语言·数据结构·c++·list
int型码农14 小时前
数据结构第八章(五)-外部排序和败者树
c语言·数据结构·算法·排序算法
好易学·数据结构14 小时前
可视化图解算法52:数据流中的中位数
数据结构·算法·leetcode·面试·力扣·笔试·牛客
点云SLAM15 小时前
PyTorch 中Tensor常用数据结构(int, list, numpy array等)互相转换和实战示例
数据结构·人工智能·pytorch·算法·list·numpy·tensor