任务是设计一个通用哈希表数据结构,处理碰撞情况并支持Insert() 、 Find()和Delete()函数。
例子:
Suppose the operations are performed on an array of pairs, {{1, 5}, {2, 15}, {3, 20}, {4, 7}}. And an array of capacity 20 is used as a Hash Table:
- Insert(1, 5): Assign the pair {1, 5} at the index (1%20 =1) in the Hash Table.
- Insert(2, 15): Assign the pair {2, 15} at the index (2%20 =2) in the Hash Table.
- Insert(3, 20): Assign the pair {3, 20} at the index (3%20 =3) in the Hash Table.
- Insert(4, 7): Assign the pair {4, 7} at the index (4%20 =4) in the Hash Table.
- Find(4): The key 4 is stored at the index (4%20 = 4). Therefore, print the 7 as it is the value of the key, 4, at index 4 of the Hash Table.
- Delete(4): The key 4 is stored at the index (4%20 = 4). After deleting Key 4, the Hash Table has keys {1, 2, 3}.
- Find(4): Print -1, as the key 4 does not exist in the Hash Table.
方法:给定的问题可以通过使用模哈希函数并使用结构数组作为哈希表来解决,其中每个数组元素将存储要哈希的{key, value}对。冲突情况可以通过线性探测、开放寻址来处理。请按照以下步骤解决问题:
- 为要散列的键值对定义一个节点,结构为HashNode 。
- 初始化一个HashNode类型的指针数组,比如*arr[]来存储所有的键值对。
- Insert(Key, Value):在哈希表中插入一对{Key, Value} 。
- 用值{Key, Value}初始化一个HashNode变量,比如temp 。
- 使用 Hash函数找到可以存储键的索引,然后将索引存储在一个变量中,比如HashIndex 。
- 如果arr[HashIndex]不为空或存在另一个Key ,则通过不断更新HashIndex作为HashIndex =(HashIndex+1)%capacity进行线性探测。
- 如果arr[HashIndex]不为空,则通过将temp的地址分配给arr[HashIndex]来插入给定节点。
- 查找(密钥):查找在哈希表中的项的值。
- 使用哈希函数查找键可能存在的索引,然后将索引存储在一个变量中,比如HashIndex 。
- 如果arr[HashIndex]包含键,则Key返回它的值。
- 否则,通过将HashIndex连续更新为HashIndex =(HashIndex+1)%capacity 来进行线性探测。然后,如果找到Key ,则返回该HashIndex处的Key值,然后返回true 。
- 如果未找到Key ,则返回-1表示未找到。否则,返回Key的值。
- Delete(Key):从哈希表中删除Key 。
- 使用哈希函数查找键可能存在的索引,然后将索引存储在一个变量中,比如HashIndex 。
- 如果arr[HashIndex ] 包含键,则Key然后通过将{-1, -1}分配给arr[HashIndex]来删除,然后返回true 。
- 否则,通过将HashIndex连续更新为HashIndex =(HashIndex+1)%capacity 来进行线性探测。然后,如果找到Key ,则删除该HashIndex处的Key值,然后返回true 。
- 如果未找到Key ,则返回 false。
下面是上述方法的实现:
C
// C program for the above approach
#include
#include
struct HashNode {
int key;
int value;
};
const int capacity = 20;
int size = 0;
struct HashNode** arr;
struct HashNode* dummy;
// Function to add key value pair
void insert(int key, int V)
{
struct HashNode* temp
= (struct HashNode*)malloc(sizeof(struct HashNode));
temp->key = key;
temp->value = V;
// Apply hash function to find
// index for given key
int hashIndex = key % capacity;
// Find next free space
while (arr[hashIndex] != NULL
&& arr[hashIndex]->key != key
&& arr[hashIndex]->key != -1) {
hashIndex++;
hashIndex %= capacity;
}
// If new node to be inserted
// increase the current size
if (arr[hashIndex] == NULL
|| arr[hashIndex]->key == -1)
size++;
arr[hashIndex] = temp;
}
// Function to delete a key value pair
int delete (int key)
{
// Apply hash function to find
// index for given key
int hashIndex = key % capacity;
// Finding the node with given
// key
while (arr[hashIndex] != NULL) {
// if node found
if (arr[hashIndex]->key == key) {
// Insert dummy node here
// for further use
arr[hashIndex] = dummy;
// Reduce size
size--;
// Return the value of the key
return 1;
}
hashIndex++;
hashIndex %= capacity;
}
// If not found return null
return 0;
}
// Function to search the value
// for a given key
int find(int key)
{
// Apply hash function to find
// index for given key
int hashIndex = (key % capacity);
int counter = 0;
// Find the node with given key
while (arr[hashIndex] != NULL) {
int counter = 0;
// If counter is greater than
// capacity
if (counter++ > capacity)
break;
// If node found return its
// value
if (arr[hashIndex]->key == key)
return arr[hashIndex]->value;
hashIndex++;
hashIndex %= capacity;
}
// If not found return
// -1
return -1;
}
// Driver Code
int main()
{
// Space allocation
arr = (struct HashNode**)malloc(sizeof(struct HashNode*)
* capacity);
// Assign NULL initially
for (int i = 0; i < capacity; i++)
arr[i] = NULL;
dummy
= (struct HashNode*)malloc(sizeof(struct HashNode));
dummy->key = -1;
dummy->value = -1;
insert(1, 5);
insert(2, 15);
insert(3, 20);
insert(4, 7);
if (find(4) != -1)
printf("Value of Key 4 = %d\n", find(4));
else
printf("Key 4 does not exists\n");
if (delete (4))
printf("Node value of key 4 is deleted "
"successfully\n");
else {
printf("Key does not exists\n");
}
if (find(4) != -1)
printf("Value of Key 4 = %d\n", find(4));
else
printf("Key 4 does not exists\n");
}
输出
Value of Key 4 = 7
Node value of key 4 is deleted successfully
Key 4 does not exists
时间复杂度: O(容量),对于每个操作
辅助空间: O(容量)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。