顺序表的查找:
直接循环依次和目标比较就行
有序表的查找(二分查找):
int search(SS *T,Type key){
int mid;
int low=1;
int high=T.length;
while(low<=high){
mid=(low+high)/2;
if(T.elem[mid]<key){
low=mid+1;
}else{
high=mid-1;
}
}
return high;
}
二叉树表的查找:返回从上到下从左到右的序号(层序遍历,也可以用栈非递归遍历):
int LevelSearch(BTNode *T,Type key){
int front=-1;
int rear=-1;
BTNode *Q[100];
Q[++rear]=T;
printf("根节点的数据:",Q[rear]->data);
while(front!=rear){
BTNode *p;
p=Q[++front];
if(p->rTree){
Q[++rear]=p->rTree;
if(Q[rear]==key){
return rear;
}
}
if(p->lTree){
Q[++rear]=p->lTree;
if(Q[rear]==key){
return rear;
}
}
}
}