0

I am trying to figure out how to convert an add method of a hash table class implemented with quadratic probing as a collision solution method to use coalesced hashing instead. I know that it has something to do with linked lists however because I am a newbie with hash tables I am not sure where to start.

Here is the add method in question,

public boolean add(AnyType x) {
    int currentPos = findPos(x);
    if (isActive(array, currentPos))
        return false;
    if (array[currentPos] == null)
        occupied++;
    array[currentPos] = new HashEntry(x, true);
    currentSize++;
    modCount++;
    if (occupied > array.length / 2)
        rehash();
    return true;
}

If anybody could show me how this method could be converted to use coalesced hashing I would greatly appreciate it.

1 Answers1

1

So, after I followed the wikipedia guideline and code here and I modified the code there to the following. I explain each portion with comments.

public boolean add(AnyType x) {
    // Finds the place that you want to insert into your table.
    // I believe the function contains the hash mapping
    int currentPos = findPos(x);
    // if the place is empty, you will insert your hash in there
    if (array[currentPos] == null) {
        array[currentPos] = new HashEntry(x, true);
    } else {
        // The place has been occupied (collision occured), so we will 
        // add it as a second item like a link list.
        // But, we do not want to allocate a new space and we use the 
        // empty spaces we have in our hash table instead.
        // That is the differece between coalesced hash and speparate chaining

        // the indexes are from 0 to HASH_TABLE_SIZE -1
        int cursor = HASH_TABLE_SIZE - 1; 
        /* Find the first empty bucket for the new entry */
        while ( cursor >= 0 && array[cursor] != NULL )
            --cursor;

        /* The table is full, terminate unsuccessfully */
        if ( cursor == -1 )
          return false;

        // Initial your new value in an empty bucket we found in the table
        array[cursor] = new HashEntry(x, true);

        /* Find the last node in the chain and point to it */
        // Here we need to find the tail of the link list in our hash table
        // We do not want to miss the link list that we made from the past,
        // therefore, we need to do the following
        struct node* it = array[currentPos];
        while ( it->next != NULL )
          it = it->next;
        // Now we assign the 
        it->next = array[cursor];       
    }
    return true;
}

I hope it helps.

Mahsa2
  • 412
  • 6
  • 15
  • This must be using C? The language I'm working with is Java. Although this looks right for C I don't know what to do about the `struct node`. Can you provide a Java example? –  Sep 10 '15 at 03:48
  • so, in java, instead of struct node*, you need to bring the type of array you have. e.g. it can be array of 'Node' objects that it has access to next. You can take a look at http://crunchify.com/how-to-implement-a-linkedlist-class-from-scratch-in-java/ , 'private class Node' to see how the type of array can be + how to change it->next with getNext(). – Mahsa2 Sep 10 '15 at 04:00
  • To add on to the previous comment: I don't think I have a linked list from the past as you stated. I only have the `array` that is of a class HashEntry. –  Sep 10 '15 at 04:01
  • So, each element in your table would be an instance of Node class. The link I send to you will help you how the node and link lists are working. – Mahsa2 Sep 10 '15 at 04:01
  • I have added a pastebin of my array's type (HashEntry). http://pastebin.com/CQPsqZMS It looks to be in the form of a linked list, just without access to the next element. I'm sorry for being very newbish, as you can see I'm having a difficult time understanding. –  Sep 10 '15 at 04:04
  • In order to implement coalesced hash table, you need to modify your `HashEntry` class to have the same functionalities as the 'Node' class I mentioned to you cause you need to have link list table for this hash. – Mahsa2 Sep 10 '15 at 04:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89214/discussion-between-nic-and-mahsa-mohammadkhani). –  Sep 10 '15 at 04:05