View
214
Download
1
Tags:
Embed Size (px)
Citation preview
Ordered ContainersOrdered Containers
Cmput 115 - Lecture 21
Department of Computing Science
University of Alberta©Duane Szafron 2000
Some code in this lecture is based on code from the book:Java Structures by Duane A. Bailey or the companion structure package
Revised 2/25/00
©Duane Szafron 2000
2
About This LectureAbout This Lecture
In this lecture we will learn about Ordered containers.
An ordered container is a container where the order of the elements depends not on the order they are added, but rather on comparisons of the elements that are added.
©Duane Szafron 2000
3
OutlineOutline
Ordered Containers OrderedStructure Interface OrderedStructure Example OrderedVector class OrderedList class
©Duane Szafron 2000
4
Ordered ContainersOrdered Containers An ordered container is a container whose elements are
ordered by comparing them with each other.
This requires a binary operation to be defined that applies to any pair of elements that can be added to the container.
In Java, we use the compareTo(Object) method from the Comparable Interface.
As each element is added to the container it immediately goes to the proper location in the container based on comparing it with all other elements that are in the container.
©Duane Szafron 2000
5
OrderedStructure HierarchyOrderedStructure Hierarchy
The structure package adds the OrderedStructure interface below the Collection interface.
Store
Collection
List OrderedStructure
©Duane Szafron 2000
6
Structure Interface - StoreStructure Interface - Store
public interface Store {public int size();//post: returns the number of elements contained in // the store.
public boolean isEmpty();// post: returns the true iff store is empty.
public void clear();// post: clears the store so that it contains no // elements.
}
code based on Bailey pg. 18
©Duane Szafron 2000
7code based on Bailey pg. 19
Structure Interface - CollectionStructure Interface - Collectionpublic interface Collection extends Store {
public boolean contains(Object anObject);// pre: anObject is non-null// post: returns true iff the collection contains the object
public void add(Object anObject);// pre: anObject is non-null// post: the object is added to the collection. // Replacement policy is not specified
public Object remove(Object anObject);// pre: anObject is non-null// post: removes object “equal” to anObject and returns it,// otherwise returns nil
public Iterator elements();// post: return an iterator for traversing the collection }
©Duane Szafron 2000
8
Structure Interface - OrderedStructureStructure Interface - OrderedStructure
public interface OrderedStructure extends Collection {}
code based on Bailey pg. 173
The unusual thing about the OrderedStructure interface is that it does not add any new methods to those provided by Collection.
However, any class that implements this interface must ensure that when elements are added, they go to the correct location.
In essence, it changes the post condition of the add method in Collection:
// post: the object is added to the collection. The // replacement policy is not specified
©Duane Szafron 2000
9
OrderedStructure ExampleOrderedStructure Example
public static void main (String[ ] args) {
OrderedStructure container;RandomInt generator;int index;Iterator iterator;
container = new OrderedVector();generator = new RandomInt(1);for (index = 0; index < 100; index++) {
container.add(new Integer(generator.next(100)))};iterator = container.elements();while(iterator.hasMoreElements())
System.out.print(iterator.nextElement() + ‘ ‘);} code based on Bailey pg. 158
1 1 1 2 2 3 3 3 44 5 7 7 7 8 9 9 1013 13 14 14 15 17...
©Duane Szafron 2000
10
OrderedVector - OrderedVector - State and ConstructorState and Constructor
class OrderedVector implements OrderedStructure {
protected Vector data;
public OrderedVector(){// post: intitalizes the OrderedVector to have 0 elements
this.data = new Vector();}
code based on Bailey pg. 173
©Duane Szafron 2000
11
OrderedVector - OrderedVector - Store InterfaceStore Interface
/* Interface Store Methods */public int size() {//post: returns the number of elements contained in the store.
return this.data.size();}
public boolean isEmpty() {// post: returns the true iff store is empty.
return this.size() == 0;}
public void clear();// post: clears the store so that it contains no elements.
this.data.clear(); }code based on Bailey pg. 178
©Duane Szafron 2000
12
OrderedVector - OrderedVector - contains(Object)contains(Object)
/* Interface Collection Methods */
public boolean contains(Object anObject) {// pre: anObject is non-null// post: returns true iff the collection contains the object
int index;
index = this.indexOf ((Comparable) anObject);return (index < this.size()) &&
(this.data.elementAt(index).equals(anObject));}
code based on Bailey pg. 176
©Duane Szafron 2000
13
OrderedVector - OrderedVector - add(Object)add(Object)
public void add(Object anObject);// pre: anObject is non-null// post: the object is added to the collection at the// appropriate position based on comparing it to the// other elements.
int index;
index = this.indexOf ((Comparable) anObject);this.data.insertElementAt(anObject, index);
}
code based on Bailey pg. 176
©Duane Szafron 2000
14
OrderedVector - OrderedVector - remove(Object)remove(Object)
public Object remove(Object anObject);// pre: anObject is non-null// post: removes object “equal” to anObject and returns it,// otherwise returns nil
int index;Object result;
index = this.indexOf((Comparable) anObject));if (index < this.size()) && (this.data.elementAt(index).equals(anObject)) { result = this.data.elementAt(index); //finds this.data.removeElementAt(index); //removes return result;}return null; } code based on Bailey pg. 177
©Duane Szafron 2000
15
OrderedVector - OrderedVector - elements()elements()
public Iterator elements();// post: return an iterator for traversing the collection
return this.data.elements();}
code based on Bailey pg. 177
©Duane Szafron 2000
16
The Search ProblemThe Search Problem
To complete this class, we need to solve the search problem for a sorted container.
Given a container, find the index of a particular element, called the key.
If it is not there, find the index where it should be.
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
30
?
©Duane Szafron 2000
17
Binary Search AlgorithmBinary Search Algorithm
L HM middle = (low + high) / 2
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
L HM
low = middle + 1
middle = (low + high) / 2
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
30
middle = (low + high) / 2
high = middle - 1
HML
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
©Duane Szafron 2000
18
Element not found 1Element not found 1
L HM middle = (low + high) / 2
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
35
middle = (low + high) / 2
high = middle - 1
HML
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
L HM
low = middle + 1
middle = (low + high) / 2
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
©Duane Szafron 2000
19
Element not found 2Element not found 2
H
low < high
middle = (low + high) / 2LM
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
35
low = middle + 1
L HM
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
©Duane Szafron 2000
20
Element past end 1Element past end 1
L HMmiddle = (low + high) / 2
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
90
middle = (low + high) / 2
HML
10 25 30 50 55 60 70 75 80
0 1 2 3 4 5 6 7 8 9
low = middle + 1
L HM
low = middle + 1
middle = (low + high) / 2
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
©Duane Szafron 2000
21
Element past end 2Element past end 2
H
low < high
middle = (low + high) / 2 LM
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
90
low = middle + 1
L HM
10 25 30 50 55 60 70 75 800 1 2 3 4 5 6 7 8 9
©Duane Szafron 2000
22
OrderedVector - OrderedVector - indexOf(Object) 1indexOf(Object) 1
/* Protected Methods */ protected int indexOf(Comparable anObject) {
// pre: anObject is non-null// post: returns index of object in the collection or where// it should be placed if it is not in the collection
Comparable midObject;int low;int high;int middle;int comparison;
low = 0;high = this.data.size();middle = (low + high) / 2;
code based on Bailey pg. 174
©Duane Szafron 2000
23
OrderedVector - OrderedVector - indexOf(Object) 2indexOf(Object) 2
while (low < high) { midObject =
(Comparable) this.data.elementAt(middle); comparison = midObject.compareTo(anObject); if (comparison) < 0)
low = middle + 1; else if (comparison > 0)
high = middle - 1; else
return middle; middle = (low + high) / 2;}return low;
}code based on Bailey pg. 174
©Duane Szafron 2000
24
Time Complexity of OrderedVectorTime Complexity of OrderedVector
The indexOf(Object) method does O(log(n)) comparisons to find the index.
This means that it takes O(log(n)) comparisons for the add(Object), remove(Object) and contains(Object) methods.
However, it also requires O(n) assignments to move elements in methods add(Object) and remove(Object).
In Java, for most objects, the compareTo(Comparable) method is slower than assignment so the time complexity of add(Object) and remove(Object) is O(log(n)).
©Duane Szafron 2000
25
OrderedListOrderedList
We can also implement the OrderedStructure Interface using a linked list in a class called OrderedList.
However, we do not simply bind an instance variable to a linked list object like a SinglyLinkedList since we require access to the middle of the list to put added elements in the correct location.
Therefore we use SinglyLinkedListElements and link them together manually.
©Duane Szafron 2000
26
OrderedList - OrderedList - difference from difference from OrderedVectorOrderedVector
The important difference between OrderedList and OrderedVector is that the internal implementation of OrderedVector has access to the indexes of the underlying Vector elements.
– This allows us to find the index of a particular element so that it can be found, added, or removed.
– It also allows us to do a binary search since we can divide the search list in half using the indexes.
©Duane Szafron 2000
27
OrderedList - Sequential SearchOrderedList - Sequential Search
In OrderedList, we create an analog of the indexOf(Object) method called previousOf(Object) which returns the node before the node containing the object, or the node before the node where the object should be inserted.
Unfortunately, we must do a sequential search instead of a binary search.
However, we can stop early if we encounter an element that is larger than the one we are looking for.
©Duane Szafron 2000
28
OrderedList - OrderedList - State and ConstructorState and Constructor
class OrderedList implements OrderedStructure {
protected SinglyLinkedListElement head;protected int count;
public OrderedList(){// post: intitalizes the OrderedList to have 0 elements
this.clear();}
code based on Bailey pg. 180
©Duane Szafron 2000
29
OrderedList - OrderedList - Store InterfaceStore Interface
/* Interface Store Methods */public int size() {//post: returns the number of elements in the store.
return this.count;}
public boolean isEmpty() {// post: returns the true iff store is empty.
return this.size() == 0;}
public void clear();// post: clears the store so that it contains no elements.
this.head = null;this.count = 0; }
code based on Bailey pg. 180
©Duane Szafron 2000
30
OrderedList - OrderedList - contains(Object)contains(Object)
/* Interface Collection Methods */ public boolean contains(Object anObject) {
// pre: anObject is non-null// post: returns true iff the collection contains the object
SinglyLinkedListElement previous;SinglyLinkedListElement current;previous = this.previousOf((Comparable)
anObject);if (previous == null) // no previous element, first node
current = this.head;else current = previous.next();if (current == null) return false;else return current.value().equals(anObject); }
code based on Bailey pg. 180
©Duane Szafron 2000
31
OrderedList - OrderedList - add(Object)add(Object)
public void add(Object anObject);// pre: anObject is non-null// post: the object is added at the appropriate position // based on comparing it to the other elements.
SinglyLinkedListElement previous;SinglyLinkedListElement current;Comparable comparable;previous = this.previousOf((Comparable)
anObject);if (previous == null) // no previous element, first node
this.head =new SinglyLinkedListElement(anObject, this.head);
else previous.setNext(new SinglyLinkedListElement(anObject, previous.next()));
this.count++;}
code based on Bailey pg. 181
©Duane Szafron 2000
32
OrderedList - OrderedList - remove(Object) 1remove(Object) 1
public Object remove(Object anObject);// pre: anObject is non-null// post: removes object “equal” to anObject and returns it,// otherwise returns null
SinglyLinkedListElement previous;SinglyLinkedListElement current;Comparable comparable;previous = this.previousOf((Comparable) anObject);if (previous == null) // no previous element, first node
current = this.head;else current = previous.next();if ((current == null) || !current.value().equals(anObject)) return null;
code based on Bailey pg. 182
©Duane Szafron 2000
33
OrderedList - OrderedList - remove(Object) 2remove(Object) 2
if (previous == null) // no previous element, first node
this.head = current.next();else previous.setNext(current.next());this.count--;
}
code based on Bailey pg. 182
©Duane Szafron 2000
34
OrderedList - OrderedList - elements()elements()
public Iterator elements();// post: return an iterator for traversing the collection
return new SinglyLinkedListIterator(this.head);}
code based on Bailey pg. 182
©Duane Szafron 2000
35
OrderedList - OrderedList - previousOf(Object) 1previousOf(Object) 1
/* Protected Methods */ protected SinglyLinkedListElement previousOf(Object
anObject) {// pre: anObject is non-null// post: returns the node before the node that contains the// given object, if the object is in the collection or the// node before where it should be placed if it is not in the collection
SinglyLinkedListElement cursor;SinglyLinkedListElement previous;Comparable key;
cursor = this.head;previous = null;key = (Comparable) anObject;
code based on Bailey pg. 181
©Duane Szafron 2000
36
OrderedList - OrderedList - previousOf(Object) 2previousOf(Object) 2
while ((cursor != null) && (((Comparable) cursor.value()).compareTo(key) < 0)) {
previous = cursor; cursor = cursor.next();}return previous;
}
code based on Bailey pg. 181
©Duane Szafron 2000
37
Some Principles from the TextbookSome Principles from the Textbook
16. Declare parameters of overriding methods with the most general types possible. (e.g., equals)
17. Avoid multiple casts of the same object by assigning the value to a temporary variable.
18. Consider your code from different points of view.
principles from Bailey ch. 9