2024/01/30

本文介绍了如何用C语言实现二叉树的递归创建、先中后序遍历、节点度数计算以及深度计算,同时还展示了快速排序算法的编程实现,以降序排列数组。
摘要由CSDN通过智能技术生成

作业1:1.二叉树递归创建  2.二叉树先中后序遍历 3.二叉树计算节点4.二叉树计算深度。

#include<stdio.h>
#include<string.h>
#include<stdlib.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(NULL==s)
		return NULL;
	//初始化
	s->data='\0';
	s->lchild=s->rchild=NULL;
}
//创建二叉树
Btree create_tree()
{
	datatype element;//插入的数据域
	printf("please enter element:");
	scanf(" %c",&element);
	if(element=='#')
		return NULL;
	//创建节点
	Btree tree=create_node();
	tree->data=element;
	//递归创建左孩子
	printf("%c_lchild:\n",element);
//	puts("lchild");
	tree->lchild=create_tree();
	//递归创建右孩子
	printf("%c_rchild:\n",element);
//	puts("rchild");
	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 rear(Btree tree)
{
	if(tree==NULL)
		return;
	//递归遍历左孩子
	rear(tree->lchild);
	//递归遍历右孩子
	rear(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();
	//先序遍历二叉树
	printf("first:\n");
	first(tree);
	printf("\n");
	//中序遍历二叉树
	printf("mid:\n");
	mid(tree);
	printf("\n");
	//后序遍历二叉树
	printf("rear:\n");
	rear(tree);
	printf("\n");
	//计算各度数节点和
	int n0=0,n1=0,n2=0;
	Count(tree,&n0,&n1,&n2);
	printf("n0=%d n1=%d n2=%d\n",n0,n1,n2);
	//计算二叉树深度
	int len=high(tree);
	printf("len=%d\n",len);
	return 0;
}

作业2:编程实现快速排序降序 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//一次排序
//返回基准值下标
int one_sort(int arr[],int low,int high)
{
	//确定基准值
	int key=arr[low];
	//当low==high结束
	//循环low<high
	while(low<high)
	{
		while(low<high&&key>=arr[high])
			high--;
		arr[low]=arr[high];
		//
		//从low开始比较
		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[]={42,63,25,263,64,64,54,92,59};
	int len=sizeof(arr)/sizeof(arr[0]);
	quick_sort(arr,0,len-1);
	for(int i=0;i<len;i++)
	{
		printf("%d ",arr[i]);
	}
	return 0;
}

 思维导图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值