数据结构day7

递归

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int rec(int n)
{
	if(n==1 || n==2)
		return 1;
	else
	{
		return rec(n-2)+rec(n-1);
	}
}

int main(int argc, const char *argv[])
{
	int n,sum=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int p=rec(i);
		if(i<n)
			printf("%d+",p);
		else
			printf("%d\n",p);
		sum+=p;
	}
	printf("sum=%d\n",sum);
	return 0;
}

直接插入排序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	int a[]={5423,32,32,4,3,23,54,657,6,45,32,1,6};
	int *p=a;
	int i,j;
	int len=sizeof(a)/sizeof(a[0]);	
	printf("%d\n",len);
	for(i=1;i<len;i++)
	{
		int t=*(p+i);
		for(j=i-1;j>=0 && *(p+j)>t;j--)
		{
			*(p+j+1)=*(p+j);
		}
		*(p+j+1)=t;
	}
	for(int i=0;i<len;i++)
	{
		printf("%d\t",*(p+i));
	}
	puts("");
	return 0;
}

二分查找

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int half(int low,int high,int key,int *p)
{
	while(low<=high)
	{
		int mid=(low+high)/2;
		if(*(p+mid)==key)
		{
			return mid;
		}
		else if(*(p+mid)<key)
		{
			high=mid-1;
		}
		else
		{
			low=mid+1;
		}
	}
	return -1;
}
int main(int argc, const char *argv[])
{
	int a[]={100,98,87,76,64,54,43,36,23,10,2};
	int *p=a;
	int len=sizeof(a)/sizeof(a[0]);
	int low=0,high=len-1;
	int key;
	printf("enter key:");
	scanf("%d",&key);
	int sub;
	sub=half(low,high,key,p);
	if(sub==-1)
	{
		printf("没找到\n");
	}
	else
	{
		printf("找得了在a[%d]上\n",sub);
	}
	return 0;
}

哈希

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
typedef int datatype;
typedef struct Node
{
	datatype data;
	struct Node *next;
}*node;
int max_prime(int m)
{
	for(int i=m;i>=2;i--)
	{
		int count=0;
		for(int j=2;j<=sqrt(m);j++)
		{
			if(i%j==0)
			{
				count++;
				break;
			}
		}
		if(count==0)
			return i;
	}
}
node create_node()
{
	node s=(node)malloc(sizeof(struct Node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=NULL;
	return s;
}
node insert_hash(int key,int p,node hash[])
{
	int sub=key%p;
	node L=hash[sub];
	node s=create_node();
	s->data=key;
	if(L!=NULL)
		s->next=L;
	L=s;
	return L;
}
//输出哈希
void Output(node hash[],int p)
{
	for(int i=0;i<p;i++)
	{
		node L=hash[i];
		if(L==NULL)
		{
			printf("NULL\n");
			continue;
		}
		while(L!=NULL)
		{
			printf("%d\t",L->data);
			L=L->next;
		}
		puts("");
	}
}
int search_hash(datatype key,int p,node hash[])
{
	int sub=key%p;
	node L=hash[sub];
	if(L==NULL)
		return -1;
	while(L!=NULL)
	{
		if(L->data==key)
		{
			printf("sub=%d\n",sub);
			return 0;
		}
		L=L->next;
	}
	return -1;
}
void free_hash(node hash[],int p)
{
	for(int i=0;i<p;i++)
	{
		while(hash[i]!=NULL)
		{
			node q=hash[i];
			hash[i]=hash[i]->next;
			free(q);
			q=NULL;
		}
	}
}
int main(int argc, const char *argv[])
{
	int arr[]={67,54,41,43,42,100,67,54,34,87};
	int len=sizeof(arr)/sizeof(arr[0]);
	int m=len*4/3;
	int p=max_prime(m);
	//构建哈希表
	node hash[p];
	for(int i=0;i<p;i++)
	{
		hash[i]=NULL;
	}
	//存入哈希
	//循环数组元素存入
	for(int i=0;i<len;i++)
	{
		hash[arr[i]%p]=insert_hash(arr[i],p,hash);
	}
	//输出哈希表
	Output(hash,p);
	datatype key;
	printf("enter find data:");
	scanf("%d",&key);
	int flag=search_hash(key,p,hash);
	if(flag==0)
		printf("成功\n");
	else
		printf("失败\n");
	//释放
	free_hash(hash,p);
	Output(hash,p);
	return 0;
}

二叉树

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef char datatype;
typedef struct Node
{
	datatype data;
	struct Node* left;
	struct Node* right;
}*Btree;
//创建二叉树并输入
Btree create_tree()
{
	datatype e;
	printf("please enter tree data:");
	scanf("%c",&e);
	gets();
	if(e=='#')
		return NULL;
	
	Btree tree=(Btree)malloc(sizeof(struct Node));
	if(tree==NULL)
		return NULL;
	tree->data=e;//节点数据域赋值
	tree->left=create_tree();//递归左孩子
	tree->right=create_tree();//递归右孩子
	return tree;
}
//先序遍历
void first_output(Btree tree)
{
	if(tree==NULL)
		return;
	printf("%c\t",tree->data);
	first_output(tree->left);//递归遍历左子树
	first_output(tree->right);//递归右子树
}
//中序遍历
void mid_output(Btree tree)
{
	if(tree==NULL)
		return;
	mid_output(tree->left);//递归遍历左子树
	printf("%c\t",tree->data);
	mid_output(tree->right);//递归右子树
}
//后序遍历
void last_output(Btree tree)
{
	if(tree==NULL)
		return;
	last_output(tree->left);//递归遍历左子树
	last_output(tree->right);//递归右子树
	printf("%c\t",tree->data);
}
//计算二叉树0,1,2个度的节点个数
void count_tree(Btree tree,int *n0,int *n1,int *n2)
{
	if(tree==NULL)
		return;
	if(tree->left==NULL && tree->right==NULL)
		(*n0)++;
	else if(tree->left!=NULL && tree->right!=NULL)
		(*n2)++;
	else
		(*n1)++;
	count_tree(tree->left,n0,n1,n2);
	count_tree(tree->right,n0,n1,n2);
}
//计算深度
int high_tree(Btree tree)
{
	if(NULL==tree)
		return 0;
	int len_left=high_tree(tree->left)+1;
	int len_right=high_tree(tree->right)+1;
	return len_left>=len_right?len_left:len_right;
}
//释放
Btree free_tree(Btree tree)
{
	if(NULL==tree)
		return NULL;
	free_tree(tree->left);
	free_tree(tree->right);
	free(tree);
	tree=NULL;
}
int main(int argc, const char *argv[])
{
	//创建和输入递归实现
	Btree tree=create_tree();
	//先序遍历
	printf("first data is\n");
	first_output(tree);
	puts("");
	printf("mid data is\n");
	mid_output(tree);
	puts("");
	printf("last data is\n");
	last_output(tree);
	puts("");
	int n0=0,n1=0,n2=0;
	count_tree(tree,&n0,&n1,&n2);
	printf("n0=%d\tn1=%d\tn2=%d\n",n0,n1,n2);
	int high=high_tree(tree);
	printf("high=%d\n",high);
	tree=free_tree(tree);
	return 0;
}

思维导图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值