顺序查找&折半查找
#include<iostream>
#include<string>
using namespace std;
#define MAXSIZE 100
#define OK 1
typedef struct{
int number;
string name;
char gender;
}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.R[0].number = number;
int i;
for( i = ST.length; ST.R[i].number!=number; --i) ;
return i;
}
int Search_Bin(SSTable ST,int number) {
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;
}
return 0;
}
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;
BSTree SearchBST(BSTree T,char 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);
}
void InsertBST(BSTree &T,ElemType e ) {
if(!T) {
BSTree S = new BSTNode;
S->data = e;
S->lchild = S->rchild = NULL;
T =S;
}
else if (e.key< T->data.key)
InsertBST(T->lchild, e );
else if (e.key> T->data.key)
InsertBST(T->rchild, e);
}
void CreateBST(BSTree &T ) {
T=NULL;
ElemType e;
cin>>e.key;
while(e.key!=ENDFLAG){
InsertBST(T, e);
cin>>e.key;
}
}
void DeleteBST(BSTree &T,char key) {
BSTree p=T;BSTree f=NULL;
BSTree q;
BSTree s;
while(p){
if (p->data.key == key) break;
f=p;
if (p->data.key> key) p=p->lchild;
else p=p->rchild;
}
if(!p) return;
if ((p->lchild)&& (p->rchild)) {
q = p;
s = p->lchild;
while (s->rchild)
{q = s; s = s->rchild;}
p->data = s->data;
if(q!=p){
q->rchild = s->lchild;
}
else q->lchild = s->lchild;
delete s;
}
else{
if(!p->rchild) {
q = p; p = p->lchild;
}
else if(!p->lchild) {
q = p; p = p->rchild;
}
if(!f) T=p;
else if (q==f->lchild) f->lchild = p;
else f->rchild = p;
delete q;
}
}
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);
}