06 - 1 Lecture Notes 6 1 CIS 121 Spring 2010 Data...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 2/5/10 Lecture Notes 6 1 CIS 121 Spring 2010 Data Structures and Introduction to Algorithms using JAVA Lecture Notes 6: Priority queues © 1999-2010 S.Kannan, V.Tannen, S.Guha & K.Daniilidis © 2006 M.Goodrich & R.Tamassia (with permission) 2/5/10 Lecture Notes 6 2 Sections 3.7, 4.8, and 6.9 Recommended reading: 2 2/5/10 Lecture Notes 6 3 Comparing objects --- abstractly A number of programs of interest perform comparisons between various elements… These elements can be numbers, strings, pairs, etc. In the spirit of generic programming and code reuse we would like the objects manipulated by these programs to be as general as possible. But just using the type Object is not enough… it does not make sense to compare two arbitrary objects. 2/5/10 Lecture Notes 6 4 Two design patterns: Comparator and Comparable There are two solutions to the problem---two design patterns (more about design patterns later) • One is to encapsulate the knowledge about which objects are comparable and how to compare them in an… object! Such an object implements the Comparator interface. Such an object is also an example of another, more general, design pattern, that of function object (the textbook also calls it functor ). • The other is to make the objects we compare implement themselves a comparison operation---via the interface Comparable . 3 2/5/10 Lecture Notes 6 5 The Comparator interface public interface Comparator <E> { // in java.util public int compare (E o1, E o2); // compare(o1,o2) is // negative if o1 is strictly less than o2 // zero if o1 is equal to o2 // positive if o1 strictly greater than o2 // throws ClassCastException if the arguments' // types prevent them from being compared } It’s used in by java.util by SortedSet, SortedMap, etc. 2/5/10 Lecture Notes 6 6 Comparator for lexicographic ordering of pairs class Pair<E1,E2> { E1 fst; E2 snd; } //constructor omitted class PairComp<E1,E2> implements Comparator<Pair<E1,E2>> { private Comparator<E1> fstComp; private Comparator<E2> sndComp; // to compare each component of a pair PairComp(Comparator<E1> c1, Comparator<E2> c2) { fstComp = c1; sndComp = c2; } public int compare(Pair<E1,E2> p1, Pair<E1,E2> p2) { int comp1 = fstComp.compare(p1.fst, p2.fst); if (comp1 != 0 ) return comp1; else return sndComp.compare(p1.snd, p2.snd); } } 3 2/5/10 Lecture Notes 6 5 The Comparator interface public interface Comparator <E> { // in java.util public int compare (E o1, E o2); // compare(o1,o2) is // negative if o1 is strictly less than o2 // zero if o1 is equal to o2 // positive if o1 strictly greater than o2 // throws ClassCastException if the arguments' // types prevent them from being compared } It’s used in by java.util by SortedSet, SortedMap, etc....
View Full Document

This note was uploaded on 02/19/2010 for the course CIS 121 taught by Professor Tannen during the Spring '09 term at UPenn.

Page1 / 12

06 - 1 Lecture Notes 6 1 CIS 121 Spring 2010 Data...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online