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

ITC120-HashingAlgorithm-SourceCode

The document presents a Java implementation of a Chaining Hash Table, which includes methods for inserting, searching, and displaying keys. It also features a demo class that showcases the insertion and display of keys using different hashing techniques, including Linear Probing, Quadratic Probing, and Double Hashing. The hash table is initialized with a specified size and uses linked lists to handle collisions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ITC120-HashingAlgorithm-SourceCode

The document presents a Java implementation of a Chaining Hash Table, which includes methods for inserting, searching, and displaying keys. It also features a demo class that showcases the insertion and display of keys using different hashing techniques, including Linear Probing, Quadratic Probing, and Double Hashing. The hash table is initialized with a specified size and uses linked lists to handle collisions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

ArrayList;
import java.util.LinkedList;

interface HashTable {
void insert(int key);

boolean search(int key);

void display();
}

// Chaining
class ChainingHashTable implements HashTable {
private final ArrayList<LinkedList<Integer>> table;
private final int size;

public ChainingHashTable(int size) {


this.size = size;
table = new ArrayList<>();
for (int i = 0; i < size; i++) {
table.add(new LinkedList<>());
}
}

private int hash(int key) {


return key % size;
}

public void insert(int key) {


int index = hash(key);
table.get(index).add(key);
}

public boolean search(int key) {


int index = hash(key);
return table.get(index).contains(key);
}

public void display() {


for (int i = 0; i < size; i++) {
System.out.print(i + ": " + table.get(i));
System.out.println();
}
}
}
// Demo class
public class HashingDemo {
public static void main(String[] args) {
int size = 10;
int[] keys = {23, 12, 45, 36, 15, 22};

System.out.println("Chaining:");
ChainingHashTable chainingTable = new ChainingHashTable(size);
for (int key : keys) chainingTable.insert(key);
chainingTable.display();

System.out.println("\nLinear Probing:");
LinearProbingHashTable linearTable = new
LinearProbingHashTable(size);
for (int key : keys) linearTable.insert(key);
linearTable.display();

System.out.println("\nQuadratic Probing:");
QuadraticProbingHashTable quadraticTable = new
QuadraticProbingHashTable(size);
for (int key : keys) quadraticTable.insert(key);
quadraticTable.display();

System.out.println("\nDouble Hashing:");
DoubleHashingHashTable doubleHashingTable = new
DoubleHashingHashTable(size);
for (int key : keys) doubleHashingTable.insert(key);
doubleHashingTable.display();
}
}

You might also like