Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 479
  • Last Modified:

hash table method to find element and count number of probes

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
DAJones
Asked:
DAJones
  • 3
  • 3
  • 2
2 Solutions
 
petmagdyCommented:
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
 
DAJonesAuthor Commented:
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 e not found
probed 0 times, item c found
probed 0 times, item x not found
probed 0 times, item h found
probed 0 times, item i not found
probed 0 times, item t not found
probed 0 times, item o found
probed 0 times, item n not found
probed 0 times, item j not found
probed 0 times, item q found
probed 0 times, item j not found
probed 0 times, item m found
probed 0 times, item q found
probed 0 times, item i not found

0
 
petmagdyCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DAJonesAuthor Commented:
I made these changes:
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 2 times, item d not found
probed 3 times, item p found
probed 2 times, item k not found
probed 2 times, item e not found
probed 3 times, item m found
probed 2 times, item v not found
probed 3 times, item m found
probed 2 times, item u not found
probed 2 times, item k not found
probed 3 times, item r found
probed 2 times, item j not found
probed 3 times, item g found
probed 6 times, item f not 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
 
petmagdyCommented:
no ur modification is wrong:
try remove findprobes++ from rehash and add it to the while loop inside find()
0
 
tdisessaCommented:
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
 
tdisessaCommented:
And "space[rehash(k,i)] != null" would return false if there is anything in "space[rehash(k,i)]"
0
 
tdisessaCommented:
Glad I could help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now