# What Is Closed Addressing in Data Structure?

//

Scott Campbell

What Is Closed Addressing in Data Structure?

Data structures are essential for organizing and managing data efficiently. One commonly used technique in data structures is closed addressing, also known as open hashing or chaining. Closed addressing is a method that handles collisions that occur when multiple keys are hashed to the same index of a hash table.

## Understanding Hash Tables

Before diving into closed addressing, let’s quickly revisit hash tables. A hash table is a data structure that allows for efficient retrieval and storage of key-value pairs. It utilizes a hash function to compute an index where the value will be stored.

The ideal scenario is when each key hashes to a unique index in the hash table. However, collisions can occur when two or more keys produce the same index. This is where closed addressing comes into play.

In closed addressing, collisions are resolved by creating a linked list at each index of the hash table. This linked list contains all the key-value pairs that collided at that index.

To illustrate this concept, consider the following example: we have a hash table with an array size of 10. Two keys, “apple” and “banana,” both produce an index of 3 when hashed using our hash function.

• Step 1: Hash function calculates the index for key “apple” as 3.
• Step 2: “apple” is inserted at index 3 of the hash table.
• Step 3: Hash function calculates the index for key “banana” as also 3 (collision).
• Step 4: Rather than overwriting the value at index 3, “banana” is added to the linked list at index 3.

This way, both “apple” and “banana” can coexist in the hash table without any data loss. When searching for a key, the hash function is used to calculate the index, and then a linear search is performed on the linked list at that index.

## Benefits and Drawbacks of Closed Addressing

• It guarantees no data loss as all collisions are handled by chaining.
• It allows for efficient insertion and deletion operations since linked lists can be easily modified.
• It supports a dynamic number of elements as the size of the hash table can be adjusted accordingly.

However, closed addressing also has some drawbacks:

• The extra memory required for storing linked lists can significantly increase space complexity.
• The performance of searching for a key increases if many collisions occur, as it requires traversing the linked list.

## Conclusion

Closed addressing, or chaining, is a powerful technique used in data structures to handle collisions in hash tables. By creating linked lists at each index where collisions occur, it ensures that no data is lost and enables efficient retrieval and storage operations. While closed addressing has its advantages and drawbacks, understanding its principles is vital for designing efficient data structures.