动态查找表

【问题描述】

动态查找表的特点是表结构本身在查找过程中动态生成,即对给定的关键字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
 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
动态查找可以使用各种数据结构来实现,比如哈希、红黑树、AVL树等等。以下是一个使用哈希实现的动态查找代码示例: ```python class DynamicHashTable: def __init__(self): self.capacity = 16 # 初始容量 self.size = 0 # 元素个数 self.table = [None] * self.capacity # 哈希 # 哈希函数 def _hash(self, key): return hash(key) % self.capacity # 扩容 def _resize(self): self.capacity *= 2 new_table = [None] * self.capacity for i in range(len(self.table)): item = self.table[i] if item is not None: key, value = item index = self._hash(key) while new_table[index] is not None: index = (index + 1) % self.capacity new_table[index] = (key, value) self.table = new_table # 插入 def insert(self, key, value): if self.size >= self.capacity * 0.75: self._resize() index = self._hash(key) while self.table[index] is not None: if self.table[index][0] == key: self.table[index] = (key, value) return index = (index + 1) % self.capacity self.table[index] = (key, value) self.size += 1 # 查找 def find(self, key): index = self._hash(key) while self.table[index] is not None: if self.table[index][0] == key: return self.table[index][1] index = (index + 1) % self.capacity return None # 删除 def delete(self, key): index = self._hash(key) while self.table[index] is not None: if self.table[index][0] == key: self.table[index] = None self.size -= 1 return index = (index + 1) % self.capacity ``` 这个示例使用哈希来实现动态查找,哈希函数使用了Python内置的`hash`函数。在插入元素时,如果哈希的负载因子大于0.75,则进行扩容操作。在查找和删除元素时,使用线性探测解决哈希冲突。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值