implement the method
public static
ChainingHashTable concat(DoubleHashing doubleHashing,
QuadProbingHashTable quadHashing){}
to concat DoubleHashing with QuadProbingHashTable
implement it in TestingChainingHashTable
public class
TestingChainingHashTable {
public static
void main(String arg[])
{
DoubleHashing doubleHashing = new
DoubleHashing(7);
QuadProbingHashTable quadHashing = new
QuadProbingHashTable(7);
}
public static
ChainingHashTable concat(DoubleHashing doubleHashing,
QuadProbingHashTable quadHashing){
}
}
public class
ChainingHashTable
{
private SortedList[] hashArray; // array
of lists
public int arraySize;
public ChainingHashTable(int
size) // constructor
{
arraySize = size;
hashArray = new SortedList[arraySize]; //
create array
for(int j=0; j<arraySize;
j++) // fill array
hashArray[j] = new SortedList();
// with lists
}
public double loadFactor()
{
double c=0;
for (int
i=0;i<arraySize;i++)
c=c+hashArray.size();
return c/arraySize;
}
public void rehash()
{
SortedList oldArray[]=hashArray;
arraySize*=2;
hashArray=new SortedList[arraySize];
for (int
i=0;i<arraySize;i++)
hashArray=new SortedList();
for(int
i=0;i<arraySize/2;i++)
{
Student t=oldArray.removeFirst();
while (t!=null)
{
insert(t);
t=oldArray.removeFirst();
}
}
}
public void displayTable()
{
for(int j=0; j<arraySize;
j++) // for each cell,
{
System.out.print(j + ".\n"); //
display cell number
hashArray[j].displayList(); // display list
System.out.println("");
}
}
public int
hashFunc(int key) // hash
function
{
return key % arraySize;
}
public void insert(Student
s)
{
int key = s.getId();
int hashVal = hashFunc(key); // hash the
key
hashArray[hashVal].insert(s); // insert at hashVal
} // end insert()
public void
delete(int key) // delete a
node
{
int hashVal = hashFunc(key); // hash the
key
hashArray[hashVal].delete(key); // delete the node
} // end delete()
public Student find(int
key)
{
int hashVal = hashFunc(key);
Student s = hashArray[hashVal].find(key);
return s;
}
}
public class DoubleHashing
{
private Student[] hashArray; //
array holds hash table
private int arraySize;
private Student defunct;
// for deleted items
public DoubleHashing(int size)
// constructor
{
arraySize = size;
hashArray = new Student[arraySize];
defunct = new Student(-1,"null",0); //
deleted item key is -1
}
public void displayTable()
{
System.out.println("Table: ");
for(int j=0; j<arraySize;
j++)
if(hashArray[j] != null)
System.out.println(hashArray[j]);
else
System.out.println("** ");
}
public boolean isFull()
{
for (int
i=0;i<arraySize;i++)
if(hashArray==null ||
hashArray==defunct)
return false;
return true;
}
public int
hashFunc(int key)
{
return key % arraySize; //
hash function
}
public int
hashFunc2(int key)
{
return 5-(key%5); // 5 is prime number less
than table size
}
public void insert(Student
s)
{
if (!isFull())
{
int key = s.getId(); //
extract key
int hashVal = hashFunc(key); // hash the
key
while(hashArray[hashVal] !=
null && hashArray[hashVal]!= defunct)
{
hashVal+=hashFunc2(key);
// go to next cell
hashVal %= arraySize; // wraparound if
necessary
}
hashArray[hashVal] = s; // insert item
}
}// end insert()
public Student delete(int
key) // delete a DataItem
{
int hashVal = hashFunc(key); // hash the
key
int start=hashVal;
boolean checkAll=false;
while(hashArray[hashVal] !=
null && !checkAll)
{
if(hashArray[hashVal].getId() == key)
{
Student temp = hashArray[hashVal]; // save item
hashArray[hashVal] = defunct; // delete
item
return temp;
// return item
}
hashVal+=hashFunc2(key);
// go to next cell
hashVal %= arraySize; // wraparound if
necessary
if (hashVal==start) // if we compare all and go
back to the same start index
checkAll=true;
}
return null;
// can't find
item
} // end delete()
public Student find(int
key) // find item with key
{
int hashVal = hashFunc(key); // hash the
key
int start=hashVal;
boolean checkAll=false;
while(hashArray[hashVal] !=
null && !checkAll) // until empty
cell,
{
// found the key?
if(hashArray[hashVal].getId() == key)
return hashArray[hashVal]; // yes,
return item
hashVal+=hashFunc2(key);
// go to next cell
hashVal %= arraySize; // wraparound if
necessary
if (hashVal==start) // if we compare all and go
back to the same start index
checkAll=true;
}
return null;
// can't find
item
}
}
public class Node{
private Student element;
private Node next;
public Node(Student e, Node n)
{
element=e;
next=n;
}
public Student getElement(){
return element;
}
public Node getNext() {
return next;
}
public void setNext(Node
n)
{
next=n;
}
}
public class
QuadProbingHashTable
{
private Student[] hashArray; //
array holds hash table
private int arraySize;
private Student defunct;
// for deleted items
public
QuadProbingHashTable(int size)
// constructor
{
arraySize = size;
hashArray = new Student[arraySize];
defunct = new Student(-1,"null",0); //
deleted item key is -1
}
public void displayTable()
{
System.out.println("Table: ");
for(int j=0; j<arraySize;
j++)
if(hashArray[j] != null)
System.out.println(hashArray[j]);
else
System.out.println("** ");
}
public boolean isFull()
{
for (int
i=0;i<arraySize;i++)
if(hashArray==null ||
hashArray==defunct)
return false;
return true;
}
public int
hashFunc(int key)
{
return key % arraySize; //
hash function
}
public void insert(Student
s)
{
if (!isFull())
{
int key = s.getId(); //
extract key
int hashVal = hashFunc(key); // hash the
key
int i=0;
while(hashArray[hashVal] !=
null && hashArray[hashVal]!= defunct)
{
i++;
hashVal=hashFunc(key)+i*i;
hashVal %= arraySize;
}
hashArray[hashVal] = s; // insert item
}
}// end insert()
public Student delete(int
key) // delete a DataItem
{
int hashVal = hashFunc(key); // hash the
key
int start=hashVal;
boolean checkAll=false;
int i=0;
while(hashArray[hashVal] !=
null && !checkAll)
{
if(hashArray[hashVal].getId() == key)
{
Student temp = hashArray[hashVal]; // save item
hashArray[hashVal] = defunct; // delete
item
return temp;
// return item
}
i++;
hashVal=hashFunc(key)+i*i;
// go to next cell
hashVal %= arraySize; // wraparound if
necessary
if (hashVal==start) // if we compare all and go
back to the same start index
checkAll=true;
}
return null;
// can't find
item
} // end delete()
public Student find(int
key) // find item with key
{
int hashVal = hashFunc(key); // hash the
key
int start=hashVal;
boolean checkAll=false;
int i=0;
while(hashArray[hashVal] !=
null && !checkAll) // until empty
cell,
{
// found the key?
if(hashArray[hashVal].getId() == key)
return hashArray[hashVal]; // yes,
return item
i++;
hashVal=hashFunc(key)+i*i;
// go to next cell
hashVal %= arraySize; // wraparound if
necessary
if (hashVal==start) // if we compare all and go
back to the same start index
checkAll=true;
}
return null;
// can't find
item
}
}
implement the method public static ChainingHashTable concat(DoubleHashing doubleHashing, QuadProbingHashTable quadHashin
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
implement the method public static ChainingHashTable concat(DoubleHashing doubleHashing, QuadProbingHashTable quadHashin
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!