X Tutup
Skip to content

Commit ed1273a

Browse files
author
Ray
committed
Add dynamic hash table functionality
1 parent ee11fca commit ed1273a

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

DataStructures/HashMap/Hashing/HashMapLinearProbing.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package DataStructures.HashMap.Hashing;
22

3+
import java.util.*;
4+
35
/**
46
* This class is an implementation of a hash table using linear probing
5-
*
7+
* It uses a dynamic array to lengthen the size of the hash table when
8+
* load factor > .7
69
*/
710
public class HashMapLinearProbing {
8-
private int hsize;
9-
private Integer[] buckets;
11+
private int hsize; //size of the hash table
12+
private Integer[] buckets; //array representing the table
1013
private Integer AVAILABLE;
14+
private int size; //amount of elements in the hash table
1115

1216
/**
1317
* Constructor initializes buckets array, hsize, and creates dummy object for AVAILABLE
1418
* @param hsize the desired size of the hash map
1519
*/
1620
public HashMapLinearProbing(int hsize) {
17-
buckets = new Integer[hsize];
21+
this.buckets = new Integer[hsize];
1822
this.hsize = hsize;
19-
AVAILABLE = new Integer(Integer.MIN_VALUE);
23+
this.AVAILABLE = new Integer(Integer.MIN_VALUE);
24+
this.size = 0;
2025
}
2126

2227
/**
@@ -48,6 +53,7 @@ public void insertHash(int key) {
4853
for (int i = 0;i < hsize; i++) {
4954
if(buckets[hash] == null || buckets[hash] == AVAILABLE) {
5055
buckets[hash] = wrappedInt;
56+
size++;
5157
return;
5258
}
5359

@@ -56,7 +62,7 @@ public void insertHash(int key) {
5662
} else {
5763
hash = 0;
5864
}
59-
}
65+
}
6066
}
6167

6268
/**
@@ -75,6 +81,7 @@ public void deleteHash(int key) {
7581
for(int i = 0;i < hsize; i++) {
7682
if(buckets[hash] != null && buckets[hash].equals(wrappedInt)) {
7783
buckets[hash] = AVAILABLE;
84+
size--;
7885
return;
7986
}
8087

@@ -116,10 +123,12 @@ public int findHash(int key) {
116123
}
117124

118125
for(int i = 0;i < hsize; i++) {
119-
if(buckets[hash].equals(wrappedInt)) {
120-
buckets[hash] = AVAILABLE;
121-
return hash;
122-
}
126+
try {
127+
if(buckets[hash].equals(wrappedInt)) {
128+
buckets[hash] = AVAILABLE;
129+
return hash;
130+
}
131+
} catch (Exception E) {}
123132

124133
if(hash + 1 < hsize) {
125134
hash++;
@@ -131,6 +140,27 @@ public int findHash(int key) {
131140
return -1;
132141
}
133142

143+
private void lengthenTable() {
144+
buckets = Arrays.copyOf(buckets, hsize * 2);
145+
hsize *= 2;
146+
System.out.println("Table size is now: " + hsize);
147+
}
148+
149+
/**
150+
* Checks the load factor of the hash table
151+
* if greater than .7, automatically lengthens table
152+
* to prevent further collisions
153+
*/
154+
public void checkLoadFactor() {
155+
double factor = (double) size / hsize;
156+
if(factor > .7) {
157+
System.out.println("Load factor is " + factor + ", lengthening table");
158+
lengthenTable();
159+
} else {
160+
System.out.println("Load factor is " + factor);
161+
}
162+
}
163+
134164
/**
135165
* isFull returns true if the hash map is full and false if not full
136166
* @return boolean is Empty

DataStructures/HashMap/Hashing/MainLinearProbing.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static void main(String[] args) {
1717
System.out.println("3. Print Table");
1818
System.out.println("4. Exit");
1919
System.out.println("5. Search and print key index");
20+
System.out.println("6. Check load factor");
2021

2122
choice = In.nextInt();
2223

@@ -46,7 +47,12 @@ public static void main(String[] args) {
4647
System.out.println("Enter the Key to find and print: ");
4748
key = In.nextInt();
4849
System.out.println("Key: "+ key + " is at index: "+ h.findHash(key));
50+
break;
4951
}
52+
case 6: {
53+
h.checkLoadFactor();
54+
break;
55+
}
5056
}
5157

5258
}

0 commit comments

Comments
 (0)
X Tutup