File tree Expand file tree Collapse file tree 1 file changed +8
-0
lines changed
src/dataStructures/hashSet/openAddressing Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ public HashSet() {
51
51
/**
52
52
* Adds the specified element to this set if it is not already present
53
53
* If this set already contains the element, the call leaves the set unchanged and returns false.
54
+ * <p> If load factor (0.75) is exceeded, triggers a resize operation and double the current capacity.
54
55
*
55
56
* @param element the element to be added to this set
56
57
* @return true if this set did not already contain the specified
@@ -77,11 +78,18 @@ public boolean add(T element) {
77
78
*<p>
78
79
* Removed elements are replaced with a Tombstone instead of NULL. This is to prevent search from terminating earlier
79
80
* than expected when looking for an element.
81
+ * <p>
82
+ * If load factor falls below 0.25, trigger a resize and halve the current capacity.
80
83
*
81
84
* @param element the element to be removed from this set, if present
82
85
* @return true if this set contained the specified element
83
86
*/
84
87
public boolean remove (T element ) {
88
+ // If load factor falls below 0.25 and still above minimum size (16), shrink the hashset by half.
89
+ if (this .size () <= this .capacity () * 0.25 && this .capacity () / 2 >= INITIAL_CAPACITY ) {
90
+ resize (this .capacity () / 2 );
91
+ }
92
+
85
93
int bucketIndex = this .search (element );
86
94
if (bucketIndex == ELEMENT_NOT_FOUND ) {
87
95
return false ; // If the index returned by the probe function contains an empty bucket, then the element is not present in the set.
You can’t perform that action at this time.
0 commit comments