Solved

hash table method to find element and count number of probes

Posted on 2004-11-19
471 Views
I have a hashtable class where everything is working, except in my find method, I want to count the number of probes. It seems that if my desired element was not found, I should get number of probes equal to the table size.
heres the code:
public class myHash2
{
//data members
private hashNode[] space;
private int count;  //number of elements in table
private int probes = 0; //number of ties to insert element
private int findprobes = 0;  //number of tries to find element

//letters for the int keys to be converted to
String s = "abcdefghijklmnopqrstuvwxyz";
public myHash2(int n)     {
while(!isPrime(n))
{
n++;
}
space = new hashNode[n];
count = 0;
}

public static boolean isPrime(int n)
{
if(primeHelper(n) == 1)
return true;
else
return false;
}

public static int primeHelper(int n)
{
if(n < 0)
throw new IllegalArgumentException
("positive integer must be entered, " + n + "entered.");
int sum = 1;
for(int i = (n - 1); i >= 2; i--)
{
if((n % i) == 0)
{
sum += n;
}
}
return sum;
}

public int getProbes()
{
return probes;
}

public int getFindprobes()
{
return findprobes;
}

public int getCount()
{
return count;
}
public double find(int k)
{
int i = 0;
int findprobes = 0;
while(findprobes < space.length &&
space[rehash(k,i)] != null &&
space[rehash(k,i)].getKey() != k)
{
i++;
findprobes++;
}
if((i < space.length) && (space[rehash(k,i)] != null))
{
System.out.println("probed " + findprobes + " times, item " + s.charAt(k) + " found");
return (space[rehash(k,i)].getData());
}
else
System.out.println("probed " + findprobes + " times, item " + s.charAt(k) + " not found");
return 0;
}
public boolean fullTable()
{
return (space.length == count);
}
public void insert(int key, double data)
{
probes = 0;
if(fullTable())
{
System.out.println("table is full, cannot insert");
System.exit(0);
}
hashNode h = new hashNode(key, data);
int index = hash(key);
int attempt = 0;
for(int i = 0; i < space.length; i++)
{
if(space[index] == null)
{
space[index] = h;
attempt++;
probes++;
System.out.println("probed " + probes + " times to insert " + s.charAt(key));
break;
}
else
{
if(space[index].getKey() == key)
{
space[index].setData(data);
break;
}
index = rehash(key, attempt);
attempt++;
probes++;
System.out.println("probed " + probes + " times to insert " + s.charAt(key));
}
}
count++;
}
int hash(int key)
{
return (key % space.length);
}
int rehash(int key, int attempt)
{
findprobes++;
return ((hash(key) + (attempt * attempt)) % space.length);
}
}

heres the test program:
import java.util.*;
public class hashTest2
{
public static void main(String[] args)
{
Random r =new Random();
myHash2 m = new myHash2(15);

for(int i = 0; i < 15; i++)
{
int x = r.nextInt(25);
double  y = r.nextDouble() * 10;
m.insert(x, y);
}
System.out.println();
for(int i = 0; i < 15; i++)
{
int z = r.nextInt(25);
m.find(z);
}
}
}
my find operation works, but doesn't give me reasonable findprobes count.
where did I go wrong, I have tried a lot of things
thanks
0
Question by:DAJones

LVL 13

Expert Comment

i think the cause is that u r increminting findprobes inside rehash() method, and inside the find() method u r using rehash() 4 times and u increment findprobes a 5th time inside find()
hence I think u replicated the increament multiplied by fife times more than it suppose

count urself  how many times u use rehash inside find
0

Author Comment

I took out the findprobes++ in the find method. here is my output from the 2nd loop in test:
probed 0 times, item b found
probed 0 times, item c found
probed 0 times, item h found
probed 0 times, item o found
probed 0 times, item q found
probed 0 times, item m found
probed 0 times, item q found

>
0

LVL 13

Assisted Solution

here we go the same old mistake in ur find function:

public double find(int k)
{
int i = 0;
int findprobes = 0;   <---- // rename this value to another and replace in the whole function

or just:
>>int findprobes = 0;
replace with:
findprobes = 0;

ur choice
0

Author Comment

public double find(int k)
{
int i = 0;
findprobes = 0;
while(i < space.length &&
space[rehash(k,i)] != null &&
space[rehash(k,i)].getKey() != k)
{
i++;
}
if((i < space.length) && (space[rehash(k,i)] != null))
{
System.out.println("probed " + findprobes + " times, item " + s.charAt(k) + " found");
return (space[rehash(k,i)].getData());
}
else
System.out.println("probed " + findprobes + " times, item " + s.charAt(k) + " not found");
return 0;
}
out put:
probed 3 times, item p found
probed 3 times, item m found
probed 3 times, item m found
probed 3 times, item r found
probed 3 times, item g found
probed 3 times, item s found
probed 3 times, item p found
seems like I should get probed <tablesize> times if the itemisn't found

0

LVL 13

Expert Comment

no ur modification is wrong:
try remove findprobes++ from rehash and add it to the while loop inside find()
0

LVL 2

Accepted Solution

You need to change your while logic

if "space[rehash(k,i)] != null" evaluates to false, the program will stop looking.

try this:

while(i < space.length &&
(space[rehash(k,i)] == null ||
space[rehash(k,i)].getKey() != k))

You want to continue looking if
1.  There is nothing in space[<index>], or
2.  There is something and it is not k
0

LVL 2

Expert Comment

And "space[rehash(k,i)] != null" would return false if there is anything in "space[rehash(k,i)]"
0

LVL 2

Expert Comment

0

Featured Post

Suggested Solutions

countHi challenge 25 63
noX challenge 17 54
endX challenge 2 33
Unable to start eclipse ? 17 49
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …