package lab08;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
class LinearProbeHashTable extends HashTable {
public LinearProbeHashTable(){ super(); }
int hashFunction(int hashKey, int probe){
int hashValue = (hashKey + probe) % numBuckets;
return hashValue;
}
String getDescription(){ return "Linear Probing"; }
}
class QuadraticProbeHashTable extends HashTable{
int c1, c2;
QuadraticProbeHashTable() {
super();
c1 = 191747; c2 = 89989;
}
int hashFunction(int hashKey, int probe){
int hashValue = 0;
return hashValue;
}
String getDescription(){ return "Quadratic Probing"; }
}
class DoubleHashingTable extends HashTable{
DoubleHashingTable() {
super();
}
int hashFunction(int hashKey, int probe){
int hashValue = 0;
return hashValue;
}
String getDescription(){ return "Double Hashing"; }
}
public abstract class HashTable {
String [] hashBuckets;
int numBuckets;
int num_collisions, num_operations;
public HashTable(){
numBuckets = 6203;
hashBuckets = new String[numBuckets];
num_collisions = 0;
num_operations = 0;
}
static int getHashKey(String data){
int hashKey = 0;
for(int i = 0; i < data.length(); i++)
hashKey += data.charAt(i);
return hashKey;
}
abstract int hashFunction(int hashKey, int probe);
void insertData(int dataHashKey, String newData) throws Exception {
num_operations++;
int probe = 1;
}
void paint(Graphics g, int left, int top, int right, int bottom){
top = top + 30;
g.setColor(Color.BLACK); g.setFont(new Font("Verdana",Font.BOLD,15));
g.drawString( getDescription()+" ouccupied buckets,", left, top - 5 );
g.setColor(Color.RED);
g.drawString(""+num_collisions+" collisions for "+num_operations+" insertions",
left + 350, top - 5);
int nrows = 40;
int ncols = (int)Math.ceil(1.0*numBuckets/nrows);
int cellw = Math.min((right-left)/ncols, (bottom-top)/nrows);
int counter = 0;
int y = top;
for(int i = 0; i < nrows && counter < numBuckets; i++, y += cellw){
int x = left;
for(int j = 0; j < ncols && counter < numBuckets; j++, x += cellw){
if( hashBuckets[counter++] != null )
g.setColor(Color.ORANGE);
else
g.setColor(Color.WHITE);
g.fillRect(x, y, cellw, cellw);
}
}
}
abstract String getDescription();
}
|