Javatpoint Logo
Javatpoint Logo

cuckoo hashing in C++

A hashing technique known as "cuckoo hashing" uses two or more hash tables to resolve collisions. It is predicated on the concept of multiple hash tables and two (or more) hash functions. An element is shifted to the next available location in the other hash table using its alternative hash function when it collides with another element in that hash table. This procedure carries on until a location becomes available or a predetermined number of iterations are reached, suggesting the possibility of a cycle that would necessitate enlarging the hash tables.

The fundamental principle of cuckoo hashing is that every element is assigned to one of the various places ascertained by the hash algorithms. If an element is being inserted and there is a collision, the existing element is removed and moved to a different place chosen by the other hash function. Once an empty slot is located or the maximum number of iterations is achieved, this eviction procedure may start a cascade of evictions.

  1. Hash Functions: When components collide, cuckoo hashing uses a variety of hash functions to discover the alternate places of the colliding elements. The effectiveness of the hashing method as a whole is significantly influenced by the calibre of these hash functions.
  2. Table Size and Load Factor: Two significant factors that affect the effectiveness of cuckoo hashing are the size of the hash tables and the load factor, which is the ratio of the number of elements to the total number of slots in the hash tables. A suitable trade-off must be maintained to minimize memory utilization and prevent excessive collisions.
  3. Cycle Detection and Resizing: Cuckoo hashing could get stuck in a cycle where elements are constantly being moved and removed, which would indicate that the hash tables need to be resized. Effective cycle detection systems, and resizing algorithms are crucial to maintain the stability of the hashing scheme and avoid performance loss.
  4. Analysis of Performance: The predicted number of relocations is a common way to describe the performance of cuckoo hashing. This number depends on a variety of variables, including the load factor, the number of hash functions, and the unique properties of the input data. It is common practice in theoretical analysis to compare the worst-case and average time complexity of certain operations, like search, insertion, and deletion.


Let's take an example to illustrate the cuckoo hashing in C++:


Search 50: Found
Search 100: Found
Search 30: Not found


  1. Header Files: The code contains the header files required for vector container data structures and input/output (iostream) operations.
  2. Hash Functions: Based on the supplied table size, two hash functions, hash_Function1 and hash_Function2, are defined to calculate the hash values for the given key.
  3. Cuckoo Hash Class: This class has the private members size, table1, and table2, and is responsible for managing the cuckoo hashing algorithm. The hash tables are initialized by the constructor with the given size, and empty slots are indicated by the value -1.
  4. Insertion Function: The insert function makes an effort to add a key to a hash table. If a slot in either table1 or table2 is open, the key is kept at the corresponding hash location. The function looks for an empty location or reaches a maximum number of iterations before attempting to rehash the current elements by switching them between the two tables if both slots are occupied.
  5. Search Function: This function looks up the existence of a given key in each of the hash tables. It uses the hash functions to calculate the hash values for the key and then compares it with the values stored at the calculated locations in Tables 1 and 2.
  6. Main Function: The Cuckoo_Hash class object is created with a size of 10 in the main function. Using the insert function, many elements are added to the hash table, and the search function is employed to determine whether certain keys are present. The search operations' results are printed to the console.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA