开放地址法散列

开放地址法

开放地址法是另一种(相对于分离链接法)解决散列冲突的方法。适用于装填因子(散列表中元素个数和散列表长度比)较小(小于0.5)的散列表。

开放地址法中索引的计算方法为$$h_{i}(x) = (Hash(X) + F(i)) % TableSize$$,其中:

  • Hash(x)为索引的计算方法
  • F(i)为冲突的解决函数,有F(0) = 0,i为已经尝试计算索引的次数

F(i)一般有:

  • 线性探测法:$$F(i) = i$$,即每次冲突则向下寻找1个位置,直到找到不冲突的位置,容易产生“一次聚集”的现象(数据集中在某一个地址区域)
  • 平方探测法:$$F(i)=i^{2}$$,每次冲突按平方寻找下一个位置,直到找到不冲突的位置
  • 双散列:$$F(i) = i\cdot hash_{2}(x)$$,即发生冲突后使用第二个散列函数计算下一个位置

代码实现

数据结构

结构体

// 节点数据
type tableData struct {
    data int
}

// 节点
type tableNode struct {
    flag bool       //是否已经插入数据,用于冲突检测
    key  string     //关键字
    data tableData  //节点数据
}

构造函数

func newTableNode(key string, data tableData) *tableNode {
    return &tableNode{false, key, data}
}

散列表

结构体

type hashTable struct {
    table  [17]tableNode
    length int
}

方法

计算散列值
func (h *hashTable) hashCompute(key string) int {
    hash := 0
    for i := range key {
        hash = hash + int(key[i])*32
    }
    return hash % h.length
}
插入方法
func (h *hashTable) insert(key string, data tableData) error {
    hash, i := h.hashCompute(key), 0
  
    // 若发生冲突,则搜索下一个位置 
    for i = 0; h.table[(i+hash)%h.length].flag != false && h.table[(i+hash)%h.length].key != key; i++ {
        if i == h.length {
            // 若找不到,则表满,返回错误
            return errors.New("table full")
        }
    }
    hash = (i + hash) % h.length
    
    // 插入数据
    h.table[hash].data = data
    h.table[hash].flag = true
    h.table[hash].key = key
    return nil
}
访问方法
func (h *hashTable) visit(key string) (tableData, error) {
    hash := h.hashCompute(key)
    for index := 0; h.table[(index+hash)%h.length].flag == true; index++ {
        if h.table[(index+hash)%h.length].key == key {
            return h.table[(index+hash)%h.length].data, nil
        }
    }
    return tableData{}, errors.New("not find")
}

构造函数

func newHashTable() *hashTable {
    data := &hashTable{}
    data.length = 17
    for i := range data.table {
        data.table[i] = *newTableNode("", tableData{})
    }
    return data
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现代码及其原理如下: 原理: 哈希表开放地址法,是一种处理哈希碰撞(哈希冲突)的方法。刚始时,哈希表中全部元素都未被占用,当要将一个新元素添加到哈希表中时,通过哈希函数计算其所对应的哈希地址,如果该地址已经被占用,就继续往下查找,直到找到一个空的地址为止。 实现代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #define HASHSIZE 12 // 定义哈希表数组长度为12 #define NULLKEY -32768 // 定义空关键字,同时也可以做删除标记 typedef struct { int key; // 关键字 } DataNode; // 数据节点类型 DataNode HashTable[HASHSIZE]; // 定义哈希表数组 // 初始化哈希表 void InitHashTable() { for (int i = 0; i < HASHSIZE; ++i) { HashTable[i].key = NULLKEY; // 将哈希表中全部元素的关键字值初始化为NULLKEY } } // 散列函数 int Hash(int key) { return key % HASHSIZE; // 除留余数法 } // 插入元素 void InsertElement(int key) { int hashVal = Hash(key); // 计算key的哈希地址 while (HashTable[hashVal].key != NULLKEY) // 如果该地址已被占用 { // 线性探测,继续往下查找 hashVal = (hashVal + 1) % HASHSIZE; } HashTable[hashVal].key = key; // 将新元素插入哈希表 } // 查找元素 void SearchElement(int key) { int hashVal = Hash(key); // 计算key的哈希地址 while (HashTable[hashVal].key != key) // 未找到时继续查找 { if (HashTable[hashVal].key == NULLKEY) { printf("该关键字不存在!\n"); // 哈希表中没有该元素,退出查找 return; } // 线性探测,继续往下查找 hashVal = (hashVal + 1) % HASHSIZE; } printf("该元素的位置是:%d\n", hashVal); // 找到该元素,输出其在哈希表中的位置 } // 删除元素 void DeleteElement(int key) { int hashVal = Hash(key); // 计算key的哈希地址 while (HashTable[hashVal].key != key) // 未找到时继续查找 { if (HashTable[hashVal].key == NULLKEY) { printf("该关键字不存在!\n"); // 哈希表中没有该元素,退出删除 return; } // 线性探测,继续往下查找 hashVal = (hashVal + 1) % HASHSIZE; } HashTable[hashVal].key = NULLKEY; // 将该元素标记为NULLKEY,即删除该元素 } int main() { InitHashTable(); // 初始化哈希表 InsertElement(56); // 插入元素 InsertElement(22); InsertElement(37); InsertElement(15); InsertElement(23); InsertElement(24); printf("输入要查找的数据:"); int key; scanf("%d", &key); SearchElement(key); // 查找元素 printf("输入要删除的数据:"); scanf("%d", &key); DeleteElement(key); // 删除元素 return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值