Question: why am I getting error on: temporary = hash _ table [ second _ index ] ; and how to fix it ? package spiderman;

why am I getting error on: temporary=hash_table[second_index]; and how to fix it?
package spiderman;
public class creatingHT {
//to create the hash table, you need to first perform the hash function
//Hash Function:
public static int hashCode( ClusterNode temporary, int size){
int num_dime = temporary.getDimen_num();
int i = num_dime % size;
return i;
}
//ReHash Function:
//to rehash, you need two tables: one which is prev and one which is current
private static void reHash( ClusterNode[] prev, ClusterNode[] current ){
int new_size = current.length; //you need a new size inorder to re-hash your hash table.
for ( int a =0; a < prev.length; a++){//go through the LL of ClusterNode
ClusterNode prev_pointer = prev[a]; //pointer
while ( prev_pointer != null ){
ClusterNode current_pointer = prev_pointer;
prev_pointer = prev_pointer.getNext(); //go to the next node
int b = hashCode(current_pointer, new_size); //creating new index which uses the Hash Function, we can use this and create a new hash table.
ClusterNode temporary = current[b];
current[b]= current_pointer;
current[b].setNext(temporary);
}
}
}
public static ClusterNode[] createHashTable(String in){
//create a tracker to check how many nodes there are in a Hash Table
int tracker =0;
StdIn.setFile(in); // the in is the dimension.in which we are taking in
/*Step 1:
* DimensionInputFile name is passed through the command line as args[0]
* Read from the DimensionsInputFile with the format:
*1. The first line with three numbers:
* i. a (int): number of dimensions in the graph
* ii. b (int): the initial size of the cluster table prior to rehashing
* iii. c (double): the capacity(threshold) used to rehash the cluster table */
int a = StdIn.readInt();
int b = StdIn.readInt();
double c = StdIn.readDouble();
StdIn.readLine();
ClusterNode[] hash_table = new ClusterNode[b];
while (!StdIn.isEmpty()){
if (tracker >= c){
ClusterNode[] current = new ClusterNode[b*2];
b = b*2;
c = c*2;
//call the rehash method to rehash everything copied from the current to the new table.
reHash(hash_table, current);
hash_table = current;
}
int num_dime = StdIn.readInt();
int canon_events = StdIn.readInt();
int dimen_weight = StdIn.readInt();
ClusterNode Node = new ClusterNode(num_dime, canon_events, dimen_weight, null);
int i = hashCode(Node, b);
ClusterNode temporary = hash_table[i];
hash_table[i]= Node;
hash_table[i].setNext(temporary);
tracker = tracker++;
System.out.println();
StdIn.readLine();
}
int length = hash_table.length;
for ( int indexes =0; indexes < hash_table.length; indexes++){
//adding the first two nodes of the hash table and adding it at the end of the table which we have.
//you need two indexes to do this. one for each node.
int first_index = indexes -1;
int second_index = indexes -2;
if ( first_index <0){//you need to check for num's <0.
first_index = length + first_index;
}
if ( second_index <0){
second_index = length + second_index;
}
ClusterNode temporary = hash_table[first_index];
ClusterNode first_node = new ClusterNode(temporary.getDimen_num(), temporary.getCanon_events(), temporary.getDimen_weight(), null );
temporary = hash_table[second_index];
ClusterNode second_node = new ClusterNode(temporary.getDimen_num(), temporary.getCanon_events(), temporary.getDimen_weight(), null );
//to add the nodes, you have to traverse through the LL in the hash_table.
ClusterNode pointer = hash_table[indexes];
while( pointer.getNext()!= null ){//check if the list is not empty
pointer = pointer.getNext();
}
//go to the end of the LL
pointer.setNext(first_node);
pointer = pointer.getNext();
pointer.setNext(second_node);
}
return hash_table;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!