📜  在C++中使用开放式寻址线性探测实现自己的哈希表

📅  最后修改于: 2021-05-30 08:13:23             🧑  作者: Mango

先决条件–哈希介绍,使用Java的单独链接实现我们自己的哈希表

在开放式寻址中,所有元素都存储在哈希表本身中。因此,表的大小在任何时候都必须大于或等于键的总数(请注意,如果需要,我们可以通过复制旧数据来增加表的大小)。

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

在这里,为了标记删除的节点,我们使用了具有键和值-1的虚拟节点
插入可以在删除的插槽中插入项目,但搜索不会在删除的插槽中停止。

整个过程确保对于任何键,我们都在哈希表的大小内获得一个整数位置,以插入相应的值。
因此,该过程很简单,用户将一组(键,值)对设置为输入,并根据哈希函数生成的值生成索引,以存储对应于特定键的值的存储位置。因此,无论何时需要获取与仅O(1)的键相对应的值。

代码 –

#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;
             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 ; ikey != -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() <deleteNode(2) << endl;
    cout << h->sizeofMap() <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等的更多准备工作,请参阅“完整面试准备课程”