查找算法的实现

顺序查找&折半查找

//算法7.3 采用折半查找的方式查找学号为:106的学生信息
#include<iostream>
#include<string>
using namespace std;

#define MAXSIZE 100
#define OK 1

// 定义学生数据类型,包含:学号、姓名、性别
typedef struct{
	int number;		//关键字域
	string name;
	char gender;	// f:男;m:女
}Student;

typedef struct{
	Student *R;
	int length;
}SSTable;

// 初始化顺序表
int InitList_SSTable(SSTable &L)
{ 
	L.R = new Student[MAXSIZE];
	if (!L.R)
	{
		cout<<"初始化错误";
		return 0;
	}
	L.length=0;
	return OK;
}
// 向顺序表中写入学生信息
int Insert_SSTable(SSTable &L) 
{
	int number =100;
	string names[10] = {"张三", "李四", "王五", "赵六", "田七", "张三1", "李四1", "王五1", "赵六1", "田七1"};
	for(int j=1;j<=10;j++){
		L.R[j].number=number+j;
		L.R[j].name = names[j-1];
		L.R[j].gender = 'f';
		L.length++;
	}
	for(int j=1;j<=10;j++){
		cout<<"学号:"<<L.R[j].number<<" 姓名:"<<L.R[j].name<<" 性别:"<<L.R[j].gender<<endl;
	}
	cout<<"*********************end********************"<<endl;
	return 	OK;
}

// 采用顺序查找进行实现
/*
int Search_Seq(SSTable ST, int number){
    //在顺序表ST中顺序查找其关键字等于key的数据元素。若找到,则函数值为
    //该元素在表中的位置,否则为0
     for (int i=ST.length; i>=1; --i)  
             if (ST.R[i].number==number) return i;		//从后往前找        
     return 0;
   }// Search_Seq
   */

int Search_Seq(SSTable ST, int number){
      //在顺序表ST中顺序查找其关键字等于number的数据元素。若找到,则函数值为
      //该元素在表中的位置,否则为0
     ST.R[0].number = number;                          			//“哨兵”
     int i;
	 for( i = ST.length; ST.R[i].number!=number; --i) ; 	//从后往前找
     return i;                                         
}// Search_Seq


// 采用折半查找方法进行实现
int Search_Bin(SSTable ST,int number) {
   // 在有序表ST中折半查找其关键字等于number的数据元素。若找到,则函数值为
   // 该元素在表中的位置,否则为0
   int low=1,high=ST.length;							//置查找区间初值
   int  mid;
   while(low<=high) {
	   mid=(low+high) / 2;
      if (number==ST.R[mid].number)  return mid;      		//找到待查元素
      else if (number<ST.R[mid].number)  high = mid -1;		//继续在前一子表进行查找
      else  low =mid +1;                       			//继续在后一子表进行查找
   }//while
   return 0;										//表中不存在待查元素
}// Search_Bin


int main()
{
	SSTable ST;
	InitList_SSTable(ST);
	Insert_SSTable(ST);
	cout<<"1:采用顺序查找的方式;"<<endl;
	cout<<"2:采用折半查找的方式;"<<endl;
	int testNum = -1;
	int res =0;
	int choose =-1;
	while (choose != 0) {
		cout << "请选择:";
		cin >> choose;
		switch (choose) {
				case 1:
					cout<<"1:采用顺序查找的方式;"<<endl;
					cout<<"请输入要查找的学号:";
					cin>>testNum;
					res = Search_Seq(ST, testNum);
					if(res == 0){
						cout<<"未找到学号为:"<<testNum<<" 的学生记录;"<<endl;
					}else{
						cout<<"查找成功,此学生的信息为:"<<"学号:"<<ST.R[res].number<<" 姓名:"<<ST.R[res].name<<" 性别:"<<ST.R[res].gender<<endl;
					}
					break;
				case 2:
					cout<<"2:采用折半查找的方式;"<<endl;
					cout<<"请输入要查找的学号:";
					cin>>testNum;
					res = Search_Bin(ST, testNum);
					if(res == 0){
						cout<<"未找到学号为:"<<testNum<<" 的学生记录;"<<endl;
					}else{
						cout<<"查找成功,此学生的信息为:"<<"学号:"<<ST.R[res].number<<" 姓名:"<<ST.R[res].name<<" 性别:"<<ST.R[res].gender<<endl;
					}
					break;
			}
	}
	
	return 0;
}

二叉树的查找

#include<iostream>
using namespace std;
#define ENDFLAG -1		// 结束标志

// 数据类型定义
typedef struct ElemType{	
	int key;		// 数据元素关键字
}ElemType;

// 二叉排序树存储结构定义
typedef struct BSTNode{
	ElemType data;	//结点数据域
	BSTNode *lchild,*rchild;	//左右孩子指针
}BSTNode,*BSTree;


//算法7.4 二叉排序树的递归查找
BSTree SearchBST(BSTree T,char key) {
  //在根指针T所指二叉排序树中递归地查找某关键字等于key的数据元素
  //若查找成功,则返回指向该数据元素结点的指针,否则返回空指针
  if((!T)|| key==T->data.key) return T;       	            	//查找结束
  else if (key<T->data.key)  return SearchBST(T->lchild,key);	//在左子树中继续查找
  else return SearchBST(T->rchild,key);    		   			//在右子树中继续查找
} // SearchBST



//算法7.5 二叉排序树的插入
void InsertBST(BSTree &T,ElemType e ) {
  //当二叉排序树T中不存在关键字等于e.key的数据元素时,则插入该元素
  if(!T) {                				//找到插入位置,递归结束
		 BSTree S = new BSTNode;            		//生成新结点*S
         S->data = e;                  		//新结点*S的数据域置为e   
         S->lchild = S->rchild = NULL;	//新结点*S作为叶子结点
         T =S;            				//把新结点*S链接到已找到的插入位置
  }
  else if (e.key< T->data.key) 
      InsertBST(T->lchild, e );			//将*S插入左子树
  else if (e.key> T->data.key) 
      InsertBST(T->rchild, e);			//将*S插入右子树
}// InsertBST



//算法7.6 二叉排序树的创建
void CreateBST(BSTree &T ) {
  //依次读入一个关键字为key的结点,将此结点插入二叉排序树T中
  T=NULL;
  ElemType e;
  cin>>e.key;        //???
  while(e.key!=ENDFLAG){   	//ENDFLAG为自定义常量,作为输入结束标志
    InsertBST(T, e);          	//将此结点插入二叉排序树T中
    cin>>e.key;			//???
  }//while            
}//CreatBST

//算法 7.7 二叉排序树的删除
void DeleteBST(BSTree &T,char key) {
  //从二叉排序树T中删除关键字等于key的结点
  BSTree p=T;BSTree f=NULL;                     			//初始化
  BSTree q;
  BSTree s;
  /*------------下面的while循环从根开始查找关键字等于key的结点*p-------------*/
  while(p){                  
   if (p->data.key == key) break;  	      	//找到关键字等于key的结点*p,结束循环
   f=p;                                			//*f为*p的双亲结点
   if (p->data.key> key)  p=p->lchild;     	//在*p的左子树中继续查找
   else p=p->rchild;  	                  		//在*p的右子树中继续查找
  }//while
if(!p) return;                         		//找不到被删结点则返回
/*―考虑三种情况实现p所指子树内部的处理:*p左右子树均不空、无右子树、无左子树―*/
if ((p->lchild)&& (p->rchild)) {     		//被删结点*p左右子树均不空
     q = p;
	 s = p->lchild;
     while (s->rchild)                			//在*p的左子树中继续查找其前驱结点,即最右下结点
       {q = s; s = s->rchild;}	         		//向右到尽头
     p->data = s->data;               			//s指向被删结点的“前驱”
     if(q!=p){
		 q->rchild = s->lchild;     	//重接*q的右子树
	 }
     else q->lchild = s->lchild;        		//重接*q的左子树
     delete s;
  }//if
else{
	if(!p->rchild) {               		//被删结点*p无右子树,只需重接其左子树
		  q = p; p = p->lchild; 
	  }//else if
	else if(!p->lchild) {               		//被删结点*p无左子树,只需重接其右子树
		 q = p; p = p->rchild;
	  }//else if
	/*――――――――――将p所指的子树挂接到其双亲结点*f相应的位置――――――――*/
	  if(!f) T=p;                       			//被删结点为根结点
	  else if (q==f->lchild) f->lchild = p;   	//挂接到*f的左子树位置
	  else f->rchild = p;                 		//挂接到*f的右子树位置
	  delete q;
	}
}//DeleteBST




//中序遍历
void InOrderTraverse(BSTree &T)
{
	if(T)
	{
	InOrderTraverse(T->lchild);
	cout<<T->data.key<<" ";
	InOrderTraverse(T->rchild);
	}
}

int main()
{
	BSTree T;
	cout<<"请输入若干数值,用回车区分,以-1结束输入"<<endl;
	CreateBST(T);
	cout<<"当前二叉排序树中序遍历结果为:"<<endl;
	InOrderTraverse(T);
	cout<<endl;
	int key;	//待查找或待删除内容
	cout<<"请输入待查找数据:";
	cin>>key;
	BSTree result=SearchBST(T,key);
	if(result)
	{cout<<"找到数据元素:"<<key<<endl;}
	else
	{cout<<"未找到数据元素:"<<key<<endl;}
	cout<<"请输入待删除的数据:";
	cin>>key;
	DeleteBST(T,key);
	cout<<"当前二叉排序树中序遍历结果为"<<endl;
	InOrderTraverse(T);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值