Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Open Addressing Collision Handling technique in Hashing

Open Addressing Collision Handling technique in Hashing | DSA in C++ - Software Development PDF Download

What is Open Addressing?

Open addressing is a collision handling technique in hashing where when a collision occurs, the new key is inserted in the next available empty slot within the hash table itself, rather than using separate data structures like linked lists. The goal is to find an empty slot by applying certain probing techniques.

Collision Handling Methods


Linear Probing

In linear probing, if a collision occurs, the algorithm checks the next slot in the hash table and continues this process until an empty slot is found. The probing sequence is linear.

Quadratic Probing

Quadratic probing uses a quadratic function to probe the next slot when a collision occurs. The probing sequence follows a quadratic equation, incrementing the step size at each iteration.

Double Hashing

Double hashing utilizes two hash functions to determine the next slot in case of a collision. It calculates the step size based on the second hash function and continues probing until an empty slot is found.

Implementation in C++


Hash Table Class Structure

class HashTable {

    int size;

    int* table;

public:

    HashTable(int size);

    void insert(int key);

    int search(int key);

};

Insertion Operation

void HashTable::insert(int key) {

    int index = key % size;

    while (table[index] != -1) {

        index = (index + 1) % size;

    }

    table[index] = key;

}

Searching Operation

int HashTable::search(int key) {

    int index = key % size;

    int originalIndex = index;

    while (table[index] != key) {

        if (table[index] == -1)

            return -1;  // Key not found

        index = (index + 1) % size;

        if (index == originalIndex)

            return -1;  // Key not found after full table traversal

    }

    return index;  // Key found at index

}

This doc is part of
153 videos|115 docs|24 tests
Join course for free

Example Codes and Output

Download the notes
Open Addressing Collision Handling technique in Hashing
Download as PDF
Download as PDF

Linear Probing Example

HashTable linearTable(10);

linearTable.insert(5);

linearTable.insert(15);

linearTable.insert(25);

linearTable.insert(35);

linearTable.insert(45);

linearTable.insert(55);

Output:

Hash Table: [5, 15, 25, 35, 45, 55, -1, -1, -1, -1]

Quadratic Probing Example

HashTable quadraticTable(10);

quadraticTable.insert(5);

quadraticTable.insert(15);

quadraticTable.insert(25);

quadraticTable.insert(35);

quadraticTable.insert(45);

quadraticTable.insert(55);

Output:

Hash Table: [5, 15, 25, -1, 35, 45, 55, -1, -1, -1]

Double Hashing Example

HashTable doubleHashingTable(10);

doubleHashingTable.insert(5);

doubleHashingTable.insert(15);

doubleHashingTable.insert(25);

doubleHashingTable.insert(35);

doubleHashingTable.insert(45);

doubleHashingTable.insert(55);

Output:

Hash Table: [5, 15, 25, 35, 45, 55, -1, -1, -1, -1]

Sample Problems and Solutions

Problem 1: Implement a hash table using linear probing and perform insertion and searching operations.

Problem 2: Implement a hash table using quadratic probing and perform insertion and searching operations.

Problem 3: Implement a hash table using double hashing and perform insertion and searching operations.

The solutions to the sample problems can be found in the example codes and outputs provided above.

Take a Practice Test
Test yourself on topics from Software Development exam
Practice Now
Practice Now

Conclusion

Open addressing collision handling technique, such as linear probing, quadratic probing, and double hashing, allows efficient handling of collisions in hash tables. By understanding these methods and their implementations in C++, beginners can grasp the concept of open addressing and apply it to solve real-world problems efficiently.

The document Open Addressing Collision Handling technique in Hashing | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
Are you preparing for Software Development Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Software Development exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
153 videos|115 docs|24 tests

Up next

153 videos|115 docs|24 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Related Searches

Free

,

Previous Year Questions with Solutions

,

Semester Notes

,

Open Addressing Collision Handling technique in Hashing | DSA in C++ - Software Development

,

Extra Questions

,

study material

,

past year papers

,

Objective type Questions

,

Summary

,

ppt

,

Open Addressing Collision Handling technique in Hashing | DSA in C++ - Software Development

,

practice quizzes

,

mock tests for examination

,

shortcuts and tricks

,

Exam

,

Open Addressing Collision Handling technique in Hashing | DSA in C++ - Software Development

,

pdf

,

video lectures

,

Sample Paper

,

Important questions

,

MCQs

,

Viva Questions

;