数据结构 24/1/30 作业

1、二叉树递归创建 ,二叉树先中后序遍历,二叉树计算节点,二叉树计算深度

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char datatype;
typedef struct Node
{
	datatype data;
	struct Node *lchild;
	struct Node *rchild;
}*Btree;
Btree create_node()
{
	Btree s=(Btree)malloc(sizeof(struct Node));
	if(s==NULL)
		return NULL;
	s->data='\0';
	s->lchild=s->rchild=NULL;
	return s;
}
Btree create_tree()
{
	datatype element;
	printf("please enter element:");
	scanf(" %c",&element);
	if(element=='#')
		return NULL;
	Btree tree=create_node();
	tree->data=element;
	//循环创建左孩子
	tree->lchild=create_tree();
	//循环创建右孩子
	tree->rchild=create_tree();
	return tree;
}
//先序遍历
void first(Btree tree)
{
	if(tree==NULL)
		return;
	printf("%c",tree->data);
	first(tree->lchild);
	first(tree->rchild);
}
//中序
void mid(Btree tree)
{
	if(tree==NULL)
		return;
	mid(tree->lchild);
	printf("%c",tree->data);
	mid(tree->rchild);
}
//后序
void last(Btree tree)
{
	if(tree==NULL)
		return;
	last(tree->lchild);
	last(tree->rchild);
	printf("%c",tree->data);
}
//计算总节点
void count(Btree tree,int *n0,int *n1,int *n2)
{
	if(tree==NULL)
		return;
	if(!tree->lchild && !tree->rchild)
		++*n0;
	else if(tree->lchild && tree->rchild)
		++*n2;
	else
		++*n1;
	count(tree->lchild,n0,n1,n2);
	count(tree->rchild,n0,n1,n2);
}
//计算深度
int high(Btree tree)
{
	if(tree==NULL)
		return 0;
	int left=1+high(tree->lchild);
	int right=1+high(tree->rchild);
	return left>right?left:right;
}
int main(int argc, const char *argv[])
{
	Btree tree=create_tree();
	//先序(根左右)
	first(tree);
	puts("");
	//中序
	mid(tree);
	puts("");
	//后序
	last(tree);
	puts("");
	//计算总节点
	int n0,n1,n2;
	n0=n1=n2=0;
	count(tree,&n0,&n1,&n2);
	printf("n0=%d,n1=%d,n2=%d,n=%d\n",n0,n1,n2,n0+n1+n2);
	//计算深度
	int len=high(tree);
	printf("len=%d\n",len);
	return 0;
}

2、编程实现快速排序降序

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//一次比较
int one_sort(int *arr,int low,int high)
{
	int key=*(arr+low);
	while(low<high)
	{
		while(low<high&&key>=*(arr+high))
		{
			high--;
		}
		*(arr+low)=*(arr+high);
		while(low<high&&key<=*(arr+low))
		{
			low++;
		}
		*(arr+high)=*(arr+low);
	}
	*(arr+low)=key;
	return low;
}
void quick_sort(int *arr,int low,int high)
{
	if(low>=high)
		return;
	//一次比较
	int mid=one_sort(arr,low,high);
	//递归左边子序列
	quick_sort(arr,low,mid-1);
	//递归右边子序列
	quick_sort(arr,mid+1,high);

}
int main(int argc, const char *argv[])
{
	int arr[]={12,454,23,77,34,67,48};
	int len=sizeof(arr)/sizeof(arr[0]);
	quick_sort(arr,0,len-1);
	for(int i=0;i<len;i++)
	{
		printf("%d ",arr[i]);
	}
	puts("");
	return 0;
}

3、思维导图

相关推荐
★Orange★4 分钟前
Linux Kernel kfifo 实现和巧妙设计
linux·运维·算法
尘世闲鱼8 分钟前
解数独(C++版本)
开发语言·c++·算法·解数独
qqxhb13 分钟前
零基础数据结构与算法——第四章:基础算法-排序(中)
数据结构·算法·排序算法·归并·快排·堆排
木叶丸1 小时前
编程开发中,那些你必须掌握的基本概念
前端·数据结构·编程语言
Y1nhl2 小时前
力扣_链表_python版本
开发语言·python·算法·leetcode·链表·职场和发展
qq_401700412 小时前
C语言中位运算以及获取低8位和高8位、高低位合并
c语言·开发语言·算法
CoovallyAIHub2 小时前
YOLO模型优化全攻略:从“准”到“快”,全靠这些招!
深度学习·算法·计算机视觉
闻缺陷则喜何志丹2 小时前
【BFS】 P10864 [HBCPC2024] Genshin Impact Startup Forbidden II|普及+
c++·算法·宽度优先·洛谷
MicroTech20252 小时前
微算法科技(NASDAQ: MLGO)探索Grover量子搜索算法,利用量子叠加和干涉原理,实现在无序数据库中快速定位目标信息的效果。
数据库·科技·算法
今天背单词了吗9803 小时前
算法学习笔记:8.Bellman-Ford 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·开发语言·后端·算法·最短路径问题