From ed1273a35d255fb167b8a24e389e2faa28e68529 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 17 Aug 2020 19:22:32 -0400 Subject: [PATCH] Add dynamic hash table functionality --- .../HashMap/Hashing/HashMapLinearProbing.java | 50 +++++++++++++++---- .../HashMap/Hashing/MainLinearProbing.java | 6 +++ 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/DataStructures/HashMap/Hashing/HashMapLinearProbing.java b/DataStructures/HashMap/Hashing/HashMapLinearProbing.java index 5c3767187..4da123b80 100644 --- a/DataStructures/HashMap/Hashing/HashMapLinearProbing.java +++ b/DataStructures/HashMap/Hashing/HashMapLinearProbing.java @@ -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; } @@ -56,7 +62,7 @@ public class HashMapLinearProbing { } else { hash = 0; } - } + } } /** @@ -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++) { - if(buckets[hash].equals(wrappedInt)) { - buckets[hash] = AVAILABLE; - return hash; - } + 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 diff --git a/DataStructures/HashMap/Hashing/MainLinearProbing.java b/DataStructures/HashMap/Hashing/MainLinearProbing.java index 594ca41f5..c26435bdf 100644 --- a/DataStructures/HashMap/Hashing/MainLinearProbing.java +++ b/DataStructures/HashMap/Hashing/MainLinearProbing.java @@ -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,7 +47,12 @@ 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; + } } }