implement method public void removeCollision(int key) {} to remove collision public class QuadProbingHashTable { pri

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

implement method public void removeCollision(int key) {} to remove collision public class QuadProbingHashTable { pri

Post by answerhappygod »

implement method
public void removeCollision(int key) {}
to remove collision
public class QuadProbingHashTable
{
private Student[] hashArray; //
array holds hash table
private int arraySize;
private Student defunct;
// for deleted items
//
-------------------------------------------------------------
public void removeCollision(int key)
{

}

//
-------------------------------------------------------------
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
}
//
-------------------------------------------------------------
}
public class Student {
private int id;
private String name;
private double GPA;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
public Student(int id, String name, double gPA)
{
super();
this.id = id;
this.name = name;
GPA = gPA;
}
public String toString()
{
return "ID: "+ id+ ", Name:
"+name;
}
}
public class Test {
public static void
main(String arg[])
{

QuadProbingHashTable t2=new QuadProbingHashTable(7);//table
size

t2.insert(new Student(17,"ali",2));
//3

t2.insert(new Student(14,"ahmed",2));
//0

t2.insert(new Student(28,"khaled",2));
//1

t2.insert(new Student(7,"khaled",2));
//4

t2.displayTable();

}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply