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

相关推荐
CCPC不拿奖不改名2 分钟前
Python基础:python语言中的文件操作+面试题目
开发语言·数据结构·人工智能·python·学习·面试·职场和发展
橘颂TA9 分钟前
【剑斩OFFER】算法的暴力美学——力扣 43 题:字符串相乘
数据结构·算法·leetcode·职场和发展·哈希算法·结构与算法
lalala_lulu12 分钟前
MySQL数据库存储引擎的数据结构(超详细版)
数据结构·数据库·mysql
漫随流水16 分钟前
leetcode算法(199.二叉树的右视图)
数据结构·算法·leetcode·二叉树
黎雁·泠崖22 分钟前
二叉树入门全攻略:从树的概念到遍历实现
c语言·数据结构
海奥华233 分钟前
Golang Slice深度解析
开发语言·数据结构·后端·链表·golang
多米Domi0111 小时前
0x3f 第24天 黑马web (安了半天程序 )hot100普通数组
数据结构·python·算法·leetcode
黎雁·泠崖3 小时前
栈与队列实战通关:3道经典OJ题深度解析
c语言·数据结构·leetcode
AlenTech10 小时前
160. 相交链表 - 力扣(LeetCode)
数据结构·leetcode·链表
会周易的程序员11 小时前
多模态AI 基于工业级编译技术的PLC数据结构解析与映射工具
数据结构·c++·人工智能·单例模式·信息可视化·架构