0% found this document useful (0 votes)
27 views5 pages

Java Hashing with Linear Probing

The document describes two Java programs for hashing techniques: one using Modulo Division with linear probing for collision resolution, and the other using Digit-Extraction with the same collision resolution method. Both programs initialize a hash table, insert keys while handling collisions, and display the final state of the hash table. The conclusion states that the hashing techniques were implemented successfully.

Uploaded by

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

Java Hashing with Linear Probing

The document describes two Java programs for hashing techniques: one using Modulo Division with linear probing for collision resolution, and the other using Digit-Extraction with the same collision resolution method. Both programs initialize a hash table, insert keys while handling collisions, and display the final state of the hash table. The conclusion states that the hashing techniques were implemented successfully.

Uploaded by

jiteshkadam2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical 2 – Advanced Data Structures Lab

1. Write a java program to hash various keys using Modulo Division hashing methods
and use linear probe for collision resolution.
Program:
[Link]
package mypack;
import [Link];
public class HashTable {
private static final int SIZE = 10;
private int[] hashTable;
// Constructor to initialize hash table
public HashTable() {
hashTable = new int[SIZE];
[Link](hashTable, -1); // -1 indicates an empty slot for probing
}
// Insert key using Linear Probing
public void insertLinearProbing(int key) {
int index = key % SIZE;
while (hashTable[index] != -1) {
[Link]("\nCollision found at address " + index + " for " + key);
[Link]("Searching next empty slot using linear probing!");
index = (index + 1) % SIZE; // Find the next available slot
}
[Link]("\nNo collision at address " + index + " for " + key);
hashTable[index] = key;
}
// Display the hash table for probing methods
public void displayProbing() {
[Link]("\n\n");
for (int i = 0; i < SIZE; i++) {
if (hashTable[i] != -1) {
[Link](" Index " + i + " : " + hashTable[i]);
} else {
[Link](" Index " + i + " : NULL");
}
}
}
public static void main(String[] args) {
int[] keys = {25, 36, 47, 55, 63, 75, 88, 92}; // keys to insert
HashTable ht = new HashTable();
[Link]("\nHashing using Linear Probing:\n");
for (int key : keys) {
[Link](key);
}
[Link]();
}
}
Output:
2. Write a java program to hash various keys using Digit-Extraction hashing methods
and use linear probe for collision resolution.
Program:
[Link]:
package mypack;
import [Link];
public class HashTableDigitExt {
private static final int SIZE = 10; // size of the hash table
private int[] hashTable; // Array to store the hash table
// Constructor to initialize hash table
public HashTableDigitExt() {
hashTable = new int[SIZE];
[Link](hashTable, -1); // -1 indicates an empty slot for probing
}
// Insert key into hash table
public void insert(int key, int digitPosition) {
int numDigits = (int) Math.log10(key) + 1; // Count total number of digits in the key
if (digitPosition > numDigits || digitPosition <= 0) {
[Link](" Invalid digit position!");
return;
}
// Extract the digit at the desired position and assign to index (key/10^(numDigits-
digitPosition))%10
int index = (key / (int) [Link](10, numDigits - digitPosition)) % 10;
while (hashTable[index] != -1) {
[Link]("\nCollision found at address " + index + " for " + key);
[Link]("Searching next empty slot using linear probing!");
index = (index + 1) % SIZE; // Find the next available slot
}
[Link]("\nNo collision at address " + index + " for " + key);
hashTable[index] = key;
}
// Display the hash table
public void display() {
for (int i = 0; i < SIZE; i++) {
if (hashTable[i] != -1) {
[Link](" Index " + i + " : " + hashTable[i]);
} else {
[Link](" Index " + i + " : NULL");
}
}
}
public static void main(String[] args) {
int[] keys = {12345, 67890, 13579, 24680, 98765, 43210, 56789, 10234}; // keys to
insert
int n = [Link];
int digitPosition = 3;
[Link]("Keys: ");
for (int key : keys) {
[Link](key+" " );
}
[Link]();
HashTableDigitExt ht = new HashTableDigitExt();
for (int key : keys) {
[Link](key, digitPosition);
}
[Link]("\n\nHash Table using Digit Extraction Hashing (place of digit is " +
digitPosition + " )\n");
[Link]();
}
}
Output:

Conclusion: Hashing techniques implemented successfully.

You might also like