mirror of
https://github.com/Rushilwiz/APCS.git
synced 2025-04-06 05:20:21 -04:00
270 lines
7.0 KiB
Java
270 lines
7.0 KiB
Java
// Name: B6-24
|
|
// Date: 3/4/2020
|
|
|
|
/*
|
|
Assignment: This hashing program results in collisions.
|
|
You are to implement three different collision schemes: linear
|
|
probing, rehashing, and chaining. Then implement a search
|
|
algorithm that is appropriate for each collision scheme.
|
|
*/
|
|
import java.util.*;
|
|
import javax.swing.*;
|
|
public class Hashing
|
|
{
|
|
public static void main(String[] args)
|
|
{
|
|
int arrayLength = Integer.parseInt(JOptionPane.showInputDialog(
|
|
"Hashing!\n"+
|
|
"Enter the size of the array: "));//20
|
|
|
|
int numItems = Integer.parseInt(JOptionPane.showInputDialog(
|
|
"Add n items: ")); //15
|
|
|
|
int scheme = Integer.parseInt(JOptionPane.showInputDialog(
|
|
"The Load Factor is " + (double)numItems/arrayLength +
|
|
"\nWhich collision scheme?\n"+
|
|
"1. Linear Probing\n" +
|
|
"2. Rehashing\n"+
|
|
"3. Chaining"));
|
|
Hashtable table = null;
|
|
switch( scheme )
|
|
{
|
|
case 1:
|
|
table = new HashtableLinearProbe(arrayLength);
|
|
break;
|
|
case 2:
|
|
table = new HashtableRehash(arrayLength);
|
|
break;
|
|
case 3:
|
|
table = new HashtableChaining(arrayLength);
|
|
break;
|
|
default: System.exit(0);
|
|
}
|
|
for(int i = 0; i < numItems; i++)
|
|
table.add("Item" + i);
|
|
int itemNumber = Integer.parseInt(JOptionPane.showInputDialog(
|
|
"Search for: Item0" + " to "+ "Item"+(numItems-1)));
|
|
while( itemNumber != -1 )
|
|
{
|
|
String key = "Item" + itemNumber;
|
|
int index = table.indexOf(key);
|
|
if( index >= 0) //found it
|
|
System.out.println(key + " found at index " + index);
|
|
else
|
|
System.out.println(key + " not found!");
|
|
itemNumber = Integer.parseInt(JOptionPane.showInputDialog(
|
|
"Search for: Item0" + " to "+ "Item"+(numItems-1)));
|
|
}
|
|
System.exit(0);
|
|
}
|
|
}
|
|
|
|
/*********************************************/
|
|
interface Hashtable
|
|
{
|
|
void add(Object obj);
|
|
int indexOf(Object obj);
|
|
}
|
|
/***************************************************/
|
|
|
|
class HashtableLinearProbe implements Hashtable
|
|
{
|
|
private Object[] array;
|
|
|
|
public HashtableLinearProbe(int size)//constructor
|
|
{
|
|
array = new Object[size];
|
|
}
|
|
|
|
public void add(Object obj)
|
|
{
|
|
int code = obj.hashCode();
|
|
int index = Math.abs(code % array.length);
|
|
if(array[index] == null) //empty
|
|
{
|
|
//insert it
|
|
array[index] = obj;
|
|
System.out.println(obj + "\t" + code + "\t" + index);
|
|
}
|
|
else //collision
|
|
{
|
|
System.out.println(obj + "\t" + code + "\tCollision at "+ index);
|
|
index = linearProbe(index);
|
|
array[index] = obj;
|
|
System.out.println(obj + "\t" + code + "\t" + index);
|
|
}
|
|
}
|
|
|
|
public int linearProbe(int index)
|
|
{
|
|
while (array[index] != null)
|
|
index++;
|
|
|
|
return index;
|
|
}
|
|
|
|
public int indexOf(Object obj)
|
|
{
|
|
|
|
int index = Math.abs(obj.hashCode() % array.length);
|
|
|
|
if (index < 0 || index >= array.length)
|
|
return -1;
|
|
|
|
while(array[index] != null)
|
|
{
|
|
if(array[index].equals(obj)) //found it
|
|
{
|
|
return index;
|
|
}
|
|
else //search for it in a linear probe manner
|
|
{
|
|
if (index < array.length - 1)
|
|
index++;
|
|
else
|
|
break;
|
|
|
|
System.out.println("Looking at index " + index);
|
|
}
|
|
}
|
|
//not found
|
|
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/*****************************************************/
|
|
class HashtableRehash implements Hashtable
|
|
{
|
|
private Object[] array;
|
|
private int constant;
|
|
|
|
public HashtableRehash(int size) //constructor
|
|
{
|
|
array = new Object[size];
|
|
|
|
constant = 2;
|
|
while (array.length % constant != 0 && !isPrime(constant))
|
|
constant++;
|
|
}
|
|
|
|
public void add(Object obj)
|
|
{
|
|
int code = obj.hashCode();
|
|
int index = Math.abs(code % array.length);
|
|
if(array[index] == null) //empty
|
|
{
|
|
//insert it
|
|
array[index] = obj;
|
|
System.out.println(obj + "\t" + code + "\t" + index);
|
|
}
|
|
else //collision
|
|
{
|
|
System.out.println(obj + "\t" + code + "\tCollision at "+ index);
|
|
index = rehash(index);
|
|
array[index] = obj;
|
|
System.out.println(obj + "\t" + code + "\t" + index);
|
|
}
|
|
}
|
|
|
|
public int rehash(int index)
|
|
{
|
|
while (index < array.length - constant && array[index] != null) {
|
|
index += constant;
|
|
}
|
|
|
|
if (array[index] != null) {
|
|
index = (index - array.length) + constant;
|
|
}
|
|
|
|
while (index < array.length - constant && array[index] != null) {
|
|
|
|
index += constant;
|
|
}
|
|
|
|
if (array[index] == null)
|
|
return index;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
private boolean isPrime (int n) {
|
|
if (n <= 1)
|
|
return false;
|
|
|
|
for (int i = 2; i < n; i++)
|
|
if (n % i == 0)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
public int indexOf(Object obj)
|
|
{
|
|
int index = Math.abs(obj.hashCode() % array.length);
|
|
|
|
if (index < 0 || index >= array.length)
|
|
return -1;
|
|
|
|
while(array[index] != null)
|
|
{
|
|
if(array[index].equals(obj)) //found it
|
|
{
|
|
return index;
|
|
}
|
|
else //search for it in a rehashing manner
|
|
{
|
|
if (index < array.length - constant)
|
|
index += constant;
|
|
else
|
|
break;
|
|
|
|
System.out.println("Looking at index " + index);
|
|
}
|
|
}
|
|
//not found
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/********************************************************/
|
|
class HashtableChaining implements Hashtable
|
|
{
|
|
private LinkedList[] array;
|
|
|
|
public HashtableChaining(int size)
|
|
{
|
|
//instantiate the array
|
|
array = new LinkedList[size];
|
|
|
|
//instantiate the LinkedLists
|
|
for (int i = 0; i < size; i++)
|
|
array[i] = new LinkedList();
|
|
}
|
|
public void add(Object obj)
|
|
{
|
|
int code = obj.hashCode();
|
|
int index = Math.abs(code % array.length);
|
|
array[index].addFirst(obj);
|
|
System.out.println(obj + "\t" + code + " " + " at " +index + ": "+ array[index]);
|
|
}
|
|
|
|
public int indexOf(Object obj)
|
|
{
|
|
int index = Math.abs(obj.hashCode() % array.length);
|
|
if( !array[index].isEmpty() )
|
|
{
|
|
if(array[index].contains(obj)) //found it
|
|
{
|
|
return index;
|
|
}
|
|
else //search for it in a chaining manner
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
//not found
|
|
return -1;
|
|
}
|
|
} |