【问题描述】
动态查找表的特点是表结构本身在查找过程中动态生成,即对给定的关键字key,若表中存在其关键字等于key的记录,则查找成功返回,否则插入关键字等于key的记录。设计一个有关动态查找表(以二叉排序树为例)的建立、查找、插入和删除等基本操作的演示程序。
【数据描述】
动态查找表的抽象数据类型定义:
ADT DynamicSearchTable{
D: 由具有相同特性的数据元素的集合。各元素含有类型相同,可唯一标识数据元素的关键字。
R:数据元素同属一个集合。
P:InitDSTable(&DT) 构造一个空的动态查找表DT
DestroyDSTable(&DT) 销毁一个已存在的动态查找表DT
SearchDSTable(&DT,key) 在表DT中查找其关键字等于key的数据元素是否存在
InsertDSTable(&DT,e) 在表DT中插入一个表中没有的数据元素e
DeleteDSTable(&DT,key) 在表DT中删除关键字为key的数据元素
TraverseDSTable(ST,visit()) 按某种次序对DT的每个元素进行访问
}ADT DynamicSearchTable;
【算法描述】
//动态查找表(二叉排序树)的链式存储结构定义
typedef struct BiTNode{
ElemType data;
Struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
// 二叉排序树的建立、查找、算法的实现和分析
BiTree SearchBST(BiTree bt,KeyType key){
if (bt==NULL) return NULL;//查找失败
else {
if EQ(bt->data.key,key) return bt;//查找成功
else if LT(bt->data.key,key) return(SearchBST(bt->lchild,key));
else return(SearchBST(bt->lchild,key));
}
}
void InsertBST(BiTree &bt,BiTree s){
// 在二叉排序树bt中插入一个结点s
if (bt==NULL) bt=s;
else if EQ(s->data.key,bt->data.key) return ();//不插入结点
else if LT(s->data.key,bt->data.key) InsertBST(bt->lchild,s);//不插入结点
else InsertBST(bt->rchild,s);
}
void CreateBST(Bitree &bt){
//建立一棵二叉排序树 ,bt指向根结点
bt=NULL;
do {
scanf(&x);
s=(BiTree)malloc(sizeof(BiTNode));s->data.key=x;s->lchild=s->rchild=NULL;
InsertBST(bt,s);}
While (x!=-1);//重复输入一系列值,直至输入的关键字等于-1结束
}//CreateBST
Status DeleteBST(BiTree &bt,KeyType key){
//若bt指向的二叉排序树中存在其关键字等于key的数据元素,则删除它。
if(bt==NULL) return FALSE;
else{ if EQ(s->data.key,bt->data.key) Delete(bt);//不插入结点
else if LT(s->data.key,bt->data.key) DeleteBST(bt->lchild,key);
else DeleteBST(bt->rchild,key);
}
【C源程序】
/*(二叉排序树的建立和查找算法实现示例)*/
#include <stdlib.h>
#include <stdio.h>
#define NULL 0
typedef int KeyType;
typedef struct {
KeyType key;
}ElemType; /*元素类型(其他数据项略,读者可根据实际情况加入)*/
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree searchBST(BiTree bt,KeyType key){
/*在二叉排序树 bt 中查找其关键字等于给定值的结点是否存在,并输出相应信息*/
if (bt==NULL) return NULL;
else if (bt->data.key==key) return bt;
else if (key<bt->data.key) return searchBST(bt->lchild,key);
else return searchBST(bt->rchild,key);
}
void insertBST(BiTree *bt,BiTree s){
/*在二叉排序树中插入一个新结点*/
if (*bt==NULL) *bt=s;
else if (s->data.key<(*bt)->data.key) insertBST(&((*bt)->lchild),s);
else if (s->data.key>(*bt)->data.key) insertBST(&((*bt)->rchild),s);
}
void inorder(BiTree bt){
/*对已经建立好的二叉排序树进行中序遍历,将得到一个按关键字有序的元素序列*/
if (bt!=NULL){
inorder(bt->lchild);
printf("%5d",(bt->data).key);
inorder(bt->rchild);
}
}
main(){
char ch;
KeyType key;
BiTree bt,s;
int i=0;
/*建立一棵二叉排序树,元素值从键盘输入,直到输入关键字等于-1为止*/
printf("/nPlease input data(-1:end)/n");
scanf("%d",&key);
bt=NULL;
while (key!=-1){
s=(BiTree)malloc(sizeof(BiTNode));
(s->data).key=key;s->lchild=s->rchild=NULL;
insertBST(&bt,s);
scanf("%d",&key);
}
printf("/nCreate is complete/n");
/*中序遍历已建立的二叉排序树*/
inorder(bt);
/*二叉排序树的查找,可多次查找,并输出查找的结果*/
do {
printf("/nInput the key you want to search:");
scanf("%d",&key);
s=searchBST(bt,key);
if (s!=NULL) printf("/nsuccess,the value is %d ",s->data.key);
else printf("/nunsuccess");
printf("/ncontinue(y/n):/n");
ch=getch();
}
while (ch=='y' || ch=='Y') ;
}
【测试数据】
1.根据运行提示,在建立时输入:
Please input data(-1:end)
45 24 53 45 12 24 90 –1
Input the key you want to search: 90
success,the value is 90
continue(y/n):y
Input the key you want to search:100
unsuccess
continue(y/n):n
动态查找表的特点是表结构本身在查找过程中动态生成,即对给定的关键字key,若表中存在其关键字等于key的记录,则查找成功返回,否则插入关键字等于key的记录。设计一个有关动态查找表(以二叉排序树为例)的建立、查找、插入和删除等基本操作的演示程序。
【数据描述】
动态查找表的抽象数据类型定义:
ADT DynamicSearchTable{
D: 由具有相同特性的数据元素的集合。各元素含有类型相同,可唯一标识数据元素的关键字。
R:数据元素同属一个集合。
P:InitDSTable(&DT) 构造一个空的动态查找表DT
DestroyDSTable(&DT) 销毁一个已存在的动态查找表DT
SearchDSTable(&DT,key) 在表DT中查找其关键字等于key的数据元素是否存在
InsertDSTable(&DT,e) 在表DT中插入一个表中没有的数据元素e
DeleteDSTable(&DT,key) 在表DT中删除关键字为key的数据元素
TraverseDSTable(ST,visit()) 按某种次序对DT的每个元素进行访问
}ADT DynamicSearchTable;
【算法描述】
//动态查找表(二叉排序树)的链式存储结构定义
typedef struct BiTNode{
ElemType data;
Struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
// 二叉排序树的建立、查找、算法的实现和分析
BiTree SearchBST(BiTree bt,KeyType key){
if (bt==NULL) return NULL;//查找失败
else {
if EQ(bt->data.key,key) return bt;//查找成功
else if LT(bt->data.key,key) return(SearchBST(bt->lchild,key));
else return(SearchBST(bt->lchild,key));
}
}
void InsertBST(BiTree &bt,BiTree s){
// 在二叉排序树bt中插入一个结点s
if (bt==NULL) bt=s;
else if EQ(s->data.key,bt->data.key) return ();//不插入结点
else if LT(s->data.key,bt->data.key) InsertBST(bt->lchild,s);//不插入结点
else InsertBST(bt->rchild,s);
}
void CreateBST(Bitree &bt){
//建立一棵二叉排序树 ,bt指向根结点
bt=NULL;
do {
scanf(&x);
s=(BiTree)malloc(sizeof(BiTNode));s->data.key=x;s->lchild=s->rchild=NULL;
InsertBST(bt,s);}
While (x!=-1);//重复输入一系列值,直至输入的关键字等于-1结束
}//CreateBST
Status DeleteBST(BiTree &bt,KeyType key){
//若bt指向的二叉排序树中存在其关键字等于key的数据元素,则删除它。
if(bt==NULL) return FALSE;
else{ if EQ(s->data.key,bt->data.key) Delete(bt);//不插入结点
else if LT(s->data.key,bt->data.key) DeleteBST(bt->lchild,key);
else DeleteBST(bt->rchild,key);
}
【C源程序】
/*(二叉排序树的建立和查找算法实现示例)*/
#include <stdlib.h>
#include <stdio.h>
#define NULL 0
typedef int KeyType;
typedef struct {
KeyType key;
}ElemType; /*元素类型(其他数据项略,读者可根据实际情况加入)*/
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree searchBST(BiTree bt,KeyType key){
/*在二叉排序树 bt 中查找其关键字等于给定值的结点是否存在,并输出相应信息*/
if (bt==NULL) return NULL;
else if (bt->data.key==key) return bt;
else if (key<bt->data.key) return searchBST(bt->lchild,key);
else return searchBST(bt->rchild,key);
}
void insertBST(BiTree *bt,BiTree s){
/*在二叉排序树中插入一个新结点*/
if (*bt==NULL) *bt=s;
else if (s->data.key<(*bt)->data.key) insertBST(&((*bt)->lchild),s);
else if (s->data.key>(*bt)->data.key) insertBST(&((*bt)->rchild),s);
}
void inorder(BiTree bt){
/*对已经建立好的二叉排序树进行中序遍历,将得到一个按关键字有序的元素序列*/
if (bt!=NULL){
inorder(bt->lchild);
printf("%5d",(bt->data).key);
inorder(bt->rchild);
}
}
main(){
char ch;
KeyType key;
BiTree bt,s;
int i=0;
/*建立一棵二叉排序树,元素值从键盘输入,直到输入关键字等于-1为止*/
printf("/nPlease input data(-1:end)/n");
scanf("%d",&key);
bt=NULL;
while (key!=-1){
s=(BiTree)malloc(sizeof(BiTNode));
(s->data).key=key;s->lchild=s->rchild=NULL;
insertBST(&bt,s);
scanf("%d",&key);
}
printf("/nCreate is complete/n");
/*中序遍历已建立的二叉排序树*/
inorder(bt);
/*二叉排序树的查找,可多次查找,并输出查找的结果*/
do {
printf("/nInput the key you want to search:");
scanf("%d",&key);
s=searchBST(bt,key);
if (s!=NULL) printf("/nsuccess,the value is %d ",s->data.key);
else printf("/nunsuccess");
printf("/ncontinue(y/n):/n");
ch=getch();
}
while (ch=='y' || ch=='Y') ;
}
【测试数据】
1.根据运行提示,在建立时输入:
Please input data(-1:end)
45 24 53 45 12 24 90 –1
Input the key you want to search: 90
success,the value is 90
continue(y/n):y
Input the key you want to search:100
unsuccess
continue(y/n):n