Skip to content

Commit c263f00

Browse files
committed
Add detailed documentation on run-time of add and remove operations
1 parent f79beac commit c263f00

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/dataStructures/hashSet/openAddressing/HashSet.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public HashSet() {
5151
/**
5252
* Adds the specified element to this set if it is not already present
5353
* 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+
* <p>
55+
* If load factor (0.75) is exceeded, triggers a resize operation and double the current capacity.
56+
* It's important to note that resizing is not performed with every add operation but rather when the load
57+
* factor exceeds the threshold. Therefore, the amortized time complexity of adding elements remains O(1)
5558
*
5659
* @param element the element to be added to this set
5760
* @return true if this set did not already contain the specified
@@ -80,6 +83,9 @@ public boolean add(T element) {
8083
* than expected when looking for an element.
8184
* <p>
8285
* If load factor falls below 0.25, trigger a resize and halve the current capacity.
86+
* It's important to note that resizing is not performed with every remove operation but rather when the
87+
* load factor falls below a certain limit. Therefore, the amortized time complexity of removing elements
88+
* remains O(1)
8389
*
8490
* @param element the element to be removed from this set, if present
8591
* @return true if this set contained the specified element
@@ -275,6 +281,9 @@ private int capacity() {
275281
* If the load factor is exceeded, the capacity is increased by doubling it (possibly triggered after an add operation),
276282
* or if the load factor falls below 1/4 (arbitrary) of the capacity (and the capacity is larger than the minimum capacity), the
277283
* capacity is decreased by halving it (possibly triggered after a remove operation).
284+
* <p>
285+
* The resizing operation involves rehashing all existing elements into a new array with the updated capacity.
286+
* This process takes O(n) time, where n is the number of elements in the hash set.
278287
*/
279288
private void resize(int newCapacity) {
280289
// creates a temporary reference to the original bucket

0 commit comments

Comments
 (0)