📜  使用开放寻址线性探测实现自己的哈希表

📅  最后修改于: 2021-10-27 17:00:42             🧑  作者: Mango

先决条件 –哈希介绍,在Java使用单独的链接实现我们自己的哈希表
在开放寻址中,所有元素都存储在哈希表本身中。所以在任何时候,表的大小都必须大于或等于键的总数(请注意,如果需要,我们可以通过复制旧数据来增加表的大小)。

  • Insert(k) –继续探测直到找到一个空槽。找到空槽后,插入k。
  • Search(k) –继续探测直到槽的键不等于 k 或到达一个空槽。
  • Delete(k) –删除操作很有趣。如果我们简单地删除一个键,那么搜索可能会失败。因此,已删除键的插槽被特别标记为“已删除”。

在这里,为了标记一个节点已删除,我们使用了键和值为 -1 的虚拟节点
Insert 可以在已删除的插槽中插入项目,但搜索不会在已删除的插槽处停止。
整个过程保证了对于任意一个key,我们都得到了Hash Table大小内的一个整数位置来插入对应的值。
所以这个过程很简单,用户给出一个(键,值)对集作为输入,并根据哈希函数生成的值生成一个索引,以存储与特定键对应的值。因此,无论何时我们需要获取与仅 O(1) 的键对应的值。

代码 –

CPP
#include 
using namespace std;
  
// template for generic type
template 
  
// Hashnode class
class HashNode {
public:
    V value;
    K key;
  
    // Constructor of hashnode
    HashNode(K key, V value)
    {
        this->value = value;
        this->key = key;
    }
};
  
// template for generic type
template 
  
// Our own Hashmap class
class HashMap {
    // hash element array
    HashNode** arr;
    int capacity;
    // current size
    int size;
    // dummy node
    HashNode* dummy;
  
public:
    HashMap()
    {
        // Initial capacity of hash array
        capacity = 20;
        size = 0;
        arr = new HashNode*[capacity];
  
        // Initialise all elements of array as NULL
        for (int i = 0; i < capacity; i++)
            arr[i] = NULL;
  
        // dummy node with value and key -1
        dummy = new HashNode(-1, -1);
    }
    // This implements hash function to find index
    // for a key
    int hashCode(K key)
    {
        return key % capacity;
    }
  
    // Function to add key value pair
    void insertNode(K key, V value)
    {
        HashNode* temp = new HashNode(key, value);
  
        // Apply hash function to find index for given key
        int hashIndex = hashCode(key);
  
        // 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
    V deleteNode(int key)
    {
        // Apply hash function
        // to find index for given key
        int hashIndex = hashCode(key);
  
        // finding the node with given key
        while (arr[hashIndex] != NULL) {
            // if node found
            if (arr[hashIndex]->key == key) {
                HashNode* temp = arr[hashIndex];
  
                // Insert dummy node here for further use
                arr[hashIndex] = dummy;
  
                // Reduce size
                size--;
                return temp->value;
            }
            hashIndex++;
            hashIndex %= capacity;
        }
  
        // If not found return null
        return NULL;
    }
  
    // Function to search the value for a given key
    V get(int key)
    {
        // Apply hash function to find index for given key
        int hashIndex = hashCode(key);
        int counter = 0;
  
        // finding the node with given key
        while (arr[hashIndex] != NULL) { // int counter =0; // BUG!
  
            if (counter++ > capacity) // to avoid infinite loop
                return NULL;
  
            // if node found return its value
            if (arr[hashIndex]->key == key)
                return arr[hashIndex]->value;
            hashIndex++;
            hashIndex %= capacity;
        }
  
        // If not found return null
        return NULL;
    }
  
    // Return current size
    int sizeofMap()
    {
        return size;
    }
  
    // Return true if size is 0
    bool isEmpty()
    {
        return size == 0;
    }
  
    // Function to display the stored key value pairs
    void display()
    {
        for (int i = 0; i < capacity; i++) {
            if (arr[i] != NULL && arr[i]->key != -1)
                cout << "key = " << arr[i]->key
                     << "  value = "
                     << arr[i]->value << endl;
        }
    }
};
  
// Driver method to test map class
int main()
{
    HashMap* h = new HashMap;
    h->insertNode(1, 1);
    h->insertNode(2, 2);
    h->insertNode(2, 3);
    h->display();
    cout << h->sizeofMap() << endl;
    cout << h->deleteNode(2) << endl;
    cout << h->sizeofMap() << endl;
    cout << h->isEmpty() << endl;
    cout << h->get(2);
  
    return 0;
}


输出 –

key = 1 value = 1
key = 2 value = 3
2
3
1
0
0
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程