0% found this document useful (0 votes)
27 views

Double Hashing

Uploaded by

eman063895
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Double Hashing

Uploaded by

eman063895
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <vector>

class HashTable {

private:

int tableSize;

int count;

std::vector<int> table;

public:

HashTable(int size) {

tableSize = size;

count = 0;

table.resize(tableSize, -1);

int hash(int key) {

return key % tableSize;

int hash2(int key) {

// Choose a second hash function that returns a value relatively prime to tableSize

// For example, if tableSize is prime, hash2(key) = prime - (key % prime)

return 7 - (key % 7); // A simple example

}
void insertDoubleHashing(int key) {

int index = hash(key); // Compute initial hash index

int step = hash2(key); // Compute step size using second hash function

int i = 0;

while (table[(index + i * step) % tableSize] != -1) { // Continue probing until an empty slot is found

if (table[(index + i * step) % tableSize] == key) // If key is already present, return

return;

i++; // Increment probe number

table[(index + i * step) % tableSize] = key; // Insert key into the table

count++; // Increment count of elements

void deleteDoubleHashing(int key) {

int index = hash(key); // Compute initial hash index

int step = hash2(key); // Compute step size using second hash function

int i = 0;

while (table[(index + i * step) % tableSize] != key) { // Continue probing until key is found or an
empty slot is reached

if (table[(index + i * step) % tableSize] == -1) { // If empty slot is reached, key is not present

std::cout << "Key " << key << " not found." << std::endl;

return;

i++; // Increment probe number

}
table[(index + i * step) % tableSize] = -1; // Mark the slot as empty

count--; // Decrement count of elements

std::cout << "Key " << key << " deleted." << std::endl;

bool searchDoubleHashing(int key) {

int index = hash(key); // Compute initial hash index

int step = hash2(key); // Compute step size using second hash function

int i = 0;

while (table[(index + i * step) % tableSize] != key) { // Continue probing until key is found or an
empty slot is reached

if (table[(index + i * step) % tableSize] == -1) // If empty slot is reached, key is not present

return false;

i++; // Increment probe number

return true; // Key found

void printTable() {

for (int i = 0; i < tableSize; i++) {

std::cout << "Index " << i << ": " << table[i] << std::endl;

};

int main() {
HashTable ht(10);

ht.insertDoubleHashing(4322);

ht.insertDoubleHashing(1334);

ht.insertDoubleHashing(1471);

ht.insertDoubleHashing(9679);

ht.insertDoubleHashing(1989);

ht.insertDoubleHashing(6171);

ht.insertDoubleHashing(6173);

ht.insertDoubleHashing(4199);

ht.printTable();

std::cout << "Searching for key 1334: " << (ht.searchDoubleHashing(1334) ? "Found" : "Not Found") <<
std::endl;

std::cout << "Searching for key 1234: " << (ht.searchDoubleHashing(1234) ? "Found" : "Not Found") <<
std::endl;

ht.deleteDoubleHashing(1334);

ht.deleteDoubleHashing(1234);

ht.printTable();

return 0;

You might also like