Consider the following class HashSet
, a set of elements implemented using a hash table with separate chaining.
Assume that the class is implemented in the following way:
public class HashSet implements Set {
private Node[] elements;
private int size;
...
private class Node {
private E data;
private Node next;
...
}
}
Add a method to this class named removeInRange
that accepts a minimum and maximum value and removes from the set any elements that are between the given minimum and maximum, inclusive.
For example, if a hash set in a variable set
stores [31, 12, 22, 45, 6, 28, 18, 59]
, after calling set.removeInRange(10, 30);
, the set should store [31, 45, 6, 59]
.
You should assume that the element type E
is comparable and cast it to type Comparable
so that you can determine which elements are in the given range.
You should not create any arrays or temporary data structures.
This method should run in O(N) time, where N is the number of total elements stored in the set.
(NOTE: To be compatible with Practice-It and avoid conflicting with Java's java.util.HashSet
, our HashSet
is renamed HashSet2
here.)