Software Technology Experimental Course - Three Find

Software Technology Experiment Class Code

III. To find part 

Devc ++ test is valid, other compilers test the advance Ha!

Find a binary tree 

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
	int key;
	struct bitnode *lchild;
	struct bitnode *rchild;
}bnode;
void ins_bitree(bnode *p, int k)
{
	bnode *q;
	if(p->key > k && p->lchild)
		ins_bitree(p->lchild,k);
	else if(p->key <= k && p->rchild)
		ins_bitree(p->rchild,k);
	else
	{
		q = (bnode *)malloc(sizeof(bnode));
		q->key = k;
		q->lchild = NULL;
		q->rchild = NULL;
		if(p->key > k)
			p->lchild = q;
		else
			p->rchild = q;
	}
}
void bit_search(bnode *p, int k)
{
	if(p->key > k && p->lchild)
		bit_search(p->lchild,k);
	else 
	{
	if(p->key < k && p->rchild)
		bit_search(p->rchild,k);
	else
	{
		if(p->key == k)
			printf("查找成功\n");
		else
			printf("查找失败,%d不存在\n",k);
	}
}
}
void inorder(bnode *p)
{
	if(p)
	{
		inorder(p->lchild);
		printf("%4d",p->key);
		inorder(p->rchild);
	}
}
void main()
{
	int k;
	bnode *p;
	p = NULL;
	printf("请输入二叉树结点的值,输入0结束:\n");
	scanf("%d",&k);
	p = (bnode *)malloc(sizeof(bnode));
	p->key = k;
	p->lchild = NULL;
	p->rchild = NULL;
	scanf("%d",&k);
	while(k > 0)
	{
		ins_bitree(p, k);
		scanf("%d",&k);
	}
	printf("\n");
	printf("二叉树排序的结果:\n");
	inorder(p);
	printf("\n请输要查找的值:\n");
	scanf("%d",&k);
	bit_search(p,k);
}

Binary search

#include<stdio.h>
#define MAX 100
typedef struct
{
	int elem[MAX+1];
	int length;
}Stable;
void creat_seq(Stable *list);
int sort_seq(Stable *list);
int bin_search(Stable *list,int k,int low,int high);
void main()
{
	Stable *list,table;
	int i,key;
	list = &table;
	printf("请输入线性表的长度:");
	scanf("%d",&list->length);
	creat_seq(list);
	sort_seq(list);
	printf("排序后的数据\n");
	for(i=1;i<=list->length;i++)
	printf("list.elem[%d].key=%d\n",i,list->elem[i]);
	printf("\n请输入查找的值:");
	scanf("%d",&key);
	bin_search(list,key,1,list->length);
}
void creat_seq(Stable *list)
{
	int i;
	printf("请输入顺序表的内容:\n");
	for(i=1;i<=list->length;i++)
	{
		printf("list.elem[%d].key=",i);
		scanf("%d",&list->elem[i]);
	}
}
int sort_seq(Stable *list)
{
	int i,j,flag;
	for(i=1;i<list->length;i++)
	{
		flag = 0;
		for(j=1;j<list->length-i+1;j++)
		if(list->elem[j]>list->elem[j+1])
		{
			list->elem[0]=list->elem[j+1];
			list->elem[j+1]=list->elem[j];
			list->elem[j]=list->elem[0];
			flag=1;
		}
		if(flag==0)return 1;
	}
}
int bin_search(Stable *list,int k,int low,int high)
{
	int mid;
	if(low>high)
	{
		printf("没有找到要查找的值\n");
		return(0);
	}
	mid=(low+high)/2;
	if(list->elem[mid] == k)
	{
		printf("查找成功\n");
		printf("list[%d]=%d\n",mid,k);
		return(mid);
	}
	else
	 if(list->elem[mid]<k)
	  return(bin_search(list,k,mid+1,high));
	 else
	  return(bin_search(list,k,low,mid-1));	  
}

 Hash table lookup

#include<stdio.h>
#define MAX 11
void ins_hash(int hash[],int key)
{
	int k,k1,k2;
	k = key%MAX;
	if(hash[k] == 0)
	{
		hash[k] = key;
		return;
	}
	else
	{
		k1 = k+1;
		while(k1 < MAX && hash[k1] != 0)
			k1++;
		if(k1 < MAX)
		{
			hash[k1] = key;
			return;
		}
		k2 = 0;
		while(k2 < k && hash[k2] != 0)
			k2++;
		if(k2 < k)
		{
			hash[k2] = key;
			return;
		}
	}
}
void out_hash(int hash[])
{
	int i;
	for(i = 0; i < MAX; i++)
		if(hash[i])
			printf("hash[%d]=%d\n",i,hash[i]);
}
void hash_search(int hash[],int key)
{
	int k,k1,k2,flag=0;
	k=key%MAX;
	if(hash[k] == key)
	{
		printf("hash[%d]=%d",k,key);
		flag = 1;
	}
	else
	{
		k1 = k+1;
		//找k1的右边 
		while(k1 < MAX && hash[k1] != key)
			k1++;
		if(k1 < MAX)
		{
			printf("hash[%d] = %d",k1,key);
			flag=1;
		}
		k2 = 0;
		if(!flag)
		{
			//k1的左边 
			while(k2 < k && hash[k2] != key)
				k2++;
			if(k2 < k)
			{
				printf("hash[%d]=%d",k2,key);
				flag = 1;
			}
		}
		if(flag)
		{
			printf("查找成功!\n");
			return;
		}
		else
		{
			printf("查找失败!\n");
			return;
		}
	}
}
void main()
{
	int i, key, k, sum = 0;
	int hash[MAX];
	for(i=0; i < MAX; i++)
		hash[i]=0;
	printf("请输入数据,以0结束:\n");
	scanf("%d",&key);
	sum++;
	while(key != 0 && sum < MAX)
	{
		ins_hash(hash,key);
		scanf("%d",&key);
		sum++;
	}
	printf("\n");
	out_hash(hash);
	printf("\n");
	printf("请输入查找的值:");
	scanf("%d",&k);
	hash_search(hash,k);
	printf("\n");
}

 

Guess you like

Origin blog.csdn.net/CSDNsabo/article/details/91865224