Merge pull request #1422 from rbshealy/master
Add dynamic hash table functionality
This commit is contained in:
commit
b7a05621e5
@ -1,22 +1,27 @@
|
|||||||
package DataStructures.HashMap.Hashing;
|
package DataStructures.HashMap.Hashing;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is an implementation of a hash table using linear probing
|
* 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 {
|
public class HashMapLinearProbing {
|
||||||
private int hsize;
|
private int hsize; //size of the hash table
|
||||||
private Integer[] buckets;
|
private Integer[] buckets; //array representing the table
|
||||||
private Integer AVAILABLE;
|
private Integer AVAILABLE;
|
||||||
|
private int size; //amount of elements in the hash table
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor initializes buckets array, hsize, and creates dummy object for AVAILABLE
|
* Constructor initializes buckets array, hsize, and creates dummy object for AVAILABLE
|
||||||
* @param hsize the desired size of the hash map
|
* @param hsize the desired size of the hash map
|
||||||
*/
|
*/
|
||||||
public HashMapLinearProbing(int hsize) {
|
public HashMapLinearProbing(int hsize) {
|
||||||
buckets = new Integer[hsize];
|
this.buckets = new Integer[hsize];
|
||||||
this.hsize = 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++) {
|
for (int i = 0;i < hsize; i++) {
|
||||||
if(buckets[hash] == null || buckets[hash] == AVAILABLE) {
|
if(buckets[hash] == null || buckets[hash] == AVAILABLE) {
|
||||||
buckets[hash] = wrappedInt;
|
buckets[hash] = wrappedInt;
|
||||||
|
size++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,6 +81,7 @@ public class HashMapLinearProbing {
|
|||||||
for(int i = 0;i < hsize; i++) {
|
for(int i = 0;i < hsize; i++) {
|
||||||
if(buckets[hash] != null && buckets[hash].equals(wrappedInt)) {
|
if(buckets[hash] != null && buckets[hash].equals(wrappedInt)) {
|
||||||
buckets[hash] = AVAILABLE;
|
buckets[hash] = AVAILABLE;
|
||||||
|
size--;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,10 +123,12 @@ public class HashMapLinearProbing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0;i < hsize; i++) {
|
for(int i = 0;i < hsize; i++) {
|
||||||
|
try {
|
||||||
if(buckets[hash].equals(wrappedInt)) {
|
if(buckets[hash].equals(wrappedInt)) {
|
||||||
buckets[hash] = AVAILABLE;
|
buckets[hash] = AVAILABLE;
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
} catch (Exception E) {}
|
||||||
|
|
||||||
if(hash + 1 < hsize) {
|
if(hash + 1 < hsize) {
|
||||||
hash++;
|
hash++;
|
||||||
@ -131,6 +140,27 @@ public class HashMapLinearProbing {
|
|||||||
return -1;
|
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
|
* isFull returns true if the hash map is full and false if not full
|
||||||
* @return boolean is Empty
|
* @return boolean is Empty
|
||||||
|
@ -17,6 +17,7 @@ public class MainLinearProbing {
|
|||||||
System.out.println("3. Print Table");
|
System.out.println("3. Print Table");
|
||||||
System.out.println("4. Exit");
|
System.out.println("4. Exit");
|
||||||
System.out.println("5. Search and print key index");
|
System.out.println("5. Search and print key index");
|
||||||
|
System.out.println("6. Check load factor");
|
||||||
|
|
||||||
choice = In.nextInt();
|
choice = In.nextInt();
|
||||||
|
|
||||||
@ -46,6 +47,11 @@ public class MainLinearProbing {
|
|||||||
System.out.println("Enter the Key to find and print: ");
|
System.out.println("Enter the Key to find and print: ");
|
||||||
key = In.nextInt();
|
key = In.nextInt();
|
||||||
System.out.println("Key: "+ key + " is at index: "+ h.findHash(key));
|
System.out.println("Key: "+ key + " is at index: "+ h.findHash(key));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 6: {
|
||||||
|
h.checkLoadFactor();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user