头歌-查找的实现及其应用

第一关、顺序查找

#include <stdio.h>  
 #include <stdlib.h>  
 #include <string.h>  
 #include "search.h"  
   int Search_Seq(SSTable L, KeyType key)  
 {  
 int i;  
 for (i = 1; i <= L.length; i++) {  
 if (L.r[i].key == key) {  
 return i; // 找到关键字等于key的记录,返回其在表中的位置  
 }  
 }  
 return 0; // 没有找到关键字等于key的记录,返回0  
 }  
   void SSTableInput(SSTable &L)  
 {  
 int i = 1; KeyType x;  
 scanf("%d", &x);  
 while (x != -1) {  
 L.r[i].key = x;  
 i++;  
 scanf("%d", &x);  
 }  
 L.length = i - 1;  
 }  
   void SSTableOutput(SSTable L)  
 {  
 int i;  
 for (i = 1; i <= L.length; i++) {  
 printf("%d ", L.r[i].key);  
 }  
 printf("\n");  
 } 

第2关:折半查找

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include "search.h"   
  
int Search_Bin(SSTable L, KeyType key) /*在递增有序的顺序表L中折半查找其关键字等于key的记录*/  
{  
    int low = 1; //定义最低索引  
    int high = L.length; //定义最高索引  
    while (low <= high) { //当最低索引不大于最高索引时,执行循环  
        int mid = (low + high) / 2; //计算中间索引  
        if (L.r[mid].key == key) { //如果中间元素的关键字等于要查找的关键字,返回中间索引  
            return mid;  
        } else if (L.r[mid].key < key) { //如果中间元素的关键字小于要查找的关键字,更新最低索引为中间索引+1  
            low = mid + 1;  
        } else { //如果中间元素的关键字大于要查找的关键字,更新最高索引为中间索引-1  
            high = mid - 1;  
        }  
    }  
    return 0; //如果找不到关键字等于key的记录,返回0  
}  
void SSTableInput(SSTable &L) /*输入若干记录的关键字,存放到静态查找表L中*/  
{  
    int i=1; KeyType x;  
    scanf("%d",&x);  
    while(x!=-1)  
    {  
        L.r[i++].key=x; scanf("%d",&x);  
    }  
    L.length=i-1;  
}  
void SSTableOutput(SSTable L) /*输出静态查找表L中各记录的关键字*/  
{  
    int i;  
    for(i=1;i<=L.length;i++)  
        printf("%d ",L.r[i].key);  
    printf("\n");  
}

第3关:二叉排序树的查找

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

BiTree Search_BST(BiTree T, KeyType key, BiTNode **parent)
{/*在二叉排序树T上查找其关键字等于key的记录结点。若找到返回该结点指针,parent指向其双亲;否则返回空指针,parent指向访问路径上最后一个结点。*/
	// 请在这里补充代码,完成本关任务
    /********** Begin *********/
		/*
	if(T!=NULL)
	{
		if(key==T->data.key)
			return T;

			*parent = T;//改变parent true 
						//parent = &T;error

		 if(key<T->data.key)
			return Search_BST(T->lchild, key,parent);
	 	if(key>T->data.key)
			return Search_BST(T->rchild, key,parent);
		
	}
	else
		return NULL;

	*/
if(!T||key==T->data.key)
		return T;
		/*
			由 !T 进入if分支
			说明T为空,此时直接返回T也可达到返回空的效果
		*/
	else
	{
		*parent = T;
		if(key<T->data.key)
			return Search_BST(T->lchild, key,parent);
	 	else
			return Search_BST(T->rchild, key,parent);
	}

    /********** End **********/	
}
void Insert_BST(BiTree *T, RedType r)/*若二叉排序树T中没有关键字为r.key的记录,则插入*/
{
	BiTNode *p,*q,*parent;
	parent=NULL;
	p=Search_BST(*T,r.key,&parent); /*查找*/
	if(p) printf("BST中有结点r,无需插入\n");
	else
	{
		p=parent;
		q=(BiTNode *)malloc(sizeof(BiTNode)); q->data=r; q->lchild=q->rchild=NULL;
		if(*T==NULL) *T=q; /*若T为空,则q为新的根*/
		else if(r.key<p->data.key) p->lchild=q;
		else p->rchild=q;
	}
}
BiTree Create_BST( ) /*二叉排序树的构造*/
{/*输入若干记录的关键字(以-1标志结束),生成一棵BST,采用二叉链表存储,返回其根指针T*/
	BiTree T; RedType r;
	T=NULL; /*建空树*/
	scanf("%d",&r.key);
	while(r.key!=-1)
	{
		Insert_BST(&T, r);
		scanf("%d",&r.key);
	}
	return T;
}
void PreOrder(BiTree bt) /*先序遍历*/
{
	if(bt)
	{
		printf("%d ",bt->data.key);
		PreOrder(bt->lchild);
		PreOrder(bt->rchild);
	}
}
void InOrder(BiTree bt) /*中序遍历*/
{
	if(bt)
	{
		InOrder(bt->lchild);
		printf("%d ",bt->data.key);
		InOrder(bt->rchild);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值