Merge pull request #1422 from rbshealy/master

Add dynamic hash table functionality
This commit is contained in:
Stepfen Shawn 2020-08-26 09:30:49 +08:00 committed by GitHub
commit b7a05621e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 10 deletions

View File

@ -1,22 +1,27 @@
package DataStructures.HashMap.Hashing;
import java.util.*;
/**
* This class is an implementation of a hash table using linear probing
*
* It uses a dynamic array to lengthen the size of the hash table when
* load factor > .7
*/
public class HashMapLinearProbing {
private int hsize;
private Integer[] buckets;
private int hsize; //size of the hash table
private Integer[] buckets; //array representing the table
private Integer AVAILABLE;
private int size; //amount of elements in the hash table
/**
* Constructor initializes buckets array, hsize, and creates dummy object for AVAILABLE
* @param hsize the desired size of the hash map
*/
public HashMapLinearProbing(int hsize) {
buckets = new Integer[hsize];
this.buckets = new Integer[hsize];
this.hsize = hsize;
AVAILABLE = new Integer(Integer.MIN_VALUE);
this.AVAILABLE = new Integer(Integer.MIN_VALUE);
this.size = 0;
}
/**
@ -48,6 +53,7 @@ public class HashMapLinearProbing {
for (int i = 0;i < hsize; i++) {
if(buckets[hash] == null || buckets[hash] == AVAILABLE) {
buckets[hash] = wrappedInt;
size++;
return;
}
@ -75,6 +81,7 @@ public class HashMapLinearProbing {
for(int i = 0;i < hsize; i++) {
if(buckets[hash] != null && buckets[hash].equals(wrappedInt)) {
buckets[hash] = AVAILABLE;
size--;
return;
}
@ -116,10 +123,12 @@ public class HashMapLinearProbing {
}
for(int i = 0;i < hsize; i++) {
try {
if(buckets[hash].equals(wrappedInt)) {
buckets[hash] = AVAILABLE;
return hash;
}
} catch (Exception E) {}
if(hash + 1 < hsize) {
hash++;
@ -131,6 +140,27 @@ public class HashMapLinearProbing {
return -1;
}
private void lengthenTable() {
buckets = Arrays.copyOf(buckets, hsize * 2);
hsize *= 2;
System.out.println("Table size is now: " + hsize);
}
/**
* Checks the load factor of the hash table
* if greater than .7, automatically lengthens table
* to prevent further collisions
*/
public void checkLoadFactor() {
double factor = (double) size / hsize;
if(factor > .7) {
System.out.println("Load factor is " + factor + ", lengthening table");
lengthenTable();
} else {
System.out.println("Load factor is " + factor);
}
}
/**
* isFull returns true if the hash map is full and false if not full
* @return boolean is Empty

View File

@ -17,6 +17,7 @@ public class MainLinearProbing {
System.out.println("3. Print Table");
System.out.println("4. Exit");
System.out.println("5. Search and print key index");
System.out.println("6. Check load factor");
choice = In.nextInt();
@ -46,6 +47,11 @@ public class MainLinearProbing {
System.out.println("Enter the Key to find and print: ");
key = In.nextInt();
System.out.println("Key: "+ key + " is at index: "+ h.findHash(key));
break;
}
case 6: {
h.checkLoadFactor();
break;
}
}