Lecture 06: Sets, Binary Search, Recursion
The USet ADT
- The ADTs we’ve used in the past were based on Lists. A Set is more limited. There are no repeat items
- Idea: Store a collection of distinct elements
- S = { x1, x2, … xn }. The order of items does not matter. Ex: a Set of primary colors {“red”, “yellow”, “blue”} is the same as {“blue”, “red”, “yellow”}. The strings are not literally equal, but these Sets represent the same collection of items.
- Equality vs semantic equivalence
- Strict equality: Integer val1 = new Integer(2);
- Integer val2 = new Integer(2);
- What does val1 == val2 return? False, because they store different references. Var1 == var2 returns true if and only if var1 and var2 refer to the same object instance.
- Semantic equivalence: Two instances are semantically equivalent if they represent the same value.
- To test semantic equivalence, use .equals() method, which every class has (inherited for the Object class), and override it to act differently than ==.
- var1.equals(var2) returns true when the two instances represent the same value in a sense defined for the class.
- Want: If we added var1 to a set S, then asked if var2 is contained in S, we would expect the result to be “yes.” S does contain an Integer whose value is 2
- All the elements in a Set are semantically different from each other - you cannot have var1 and var2 in the same set, because they both reference 2
- USet ADT (SimpleUSet interface)
- State: a set of elements
- Operations:
- find(y): Is there an element equivalent to y in the set? If so, return the address. (Not the value - the address) Otherwise, return null.
- add(y): if there is some Xi satisfying y.equals(xi), return false. Otherwise, update state to {x1, …., xn, y} and return true.
- remove(y): if there is some xi satisfying y.equals(xi), update state to {x1, x2, … , x(i-1), x(i+1, …. , xn} and return xi. Otherwise, return null
- Implementing it
- LinkedList (for assignment 2)
- Array
- Object[ ] contents
- Find(y): Loop through array, checking if each element is equal to y: for i = 0 to n - 1, if (y.equals(xi)), return xi. If not, return null
- Add(y): if (find(y) != null); return false; resize if necessary; set contents[n] = y
- Remove(y): if find xi with y.equals(xi). apply list remove method to remove xi. return xi. otherwise return null
- This is awful unless your set is… Sorted! (so we don’t have to look through every single element)
- Sorted Sets
- We now consider sets of elements that can be compared according to a “natural order” on the elements. That is, there is a notion of “less than” (formally a binary relation), denoted < such that given any two elements x, y, then precisely one of the following holds: x < y, y > x, x = y (semantic equivalence)
- Binary search: pick an interval to look for elements, look at midpoint of the interval, and compare y to that interval value. If it’s greater, find the point between the midpoint and the last element (and compare that); if it’s lower, the point between the midpoint and the first element. Repeat!!