csse221: software dev. honors day 14 announcements announcements pass in homework 5 now pass in...
Post on 21-Dec-2015
219 views
TRANSCRIPT
CSSE221: Software Dev. Honors CSSE221: Software Dev. Honors Day 14Day 14 AnnouncementsAnnouncements
Pass in Homework 5 nowPass in Homework 5 now Questions on Questions on Cars, Trucks, TrainsCars, Trucks, Trains?? Homework 6 and Markov posted soon: Homework 6 and Markov posted soon:
do team sign-up for Markov.do team sign-up for Markov.
This week: MarkovThis week: Markov
Monday:Monday: Stacks and QueuesStacks and Queues Sets, Maps, and PriorityQueuesSets, Maps, and PriorityQueues
Tuesday:Tuesday: Some pros and cons of various data Some pros and cons of various data
structuresstructures Brief example of file I/OBrief example of file I/O Introduction to Introduction to MarkovMarkov, a cool statistical , a cool statistical
text program with lots of data structurestext program with lots of data structures Thursday:Thursday:
Exam 1Exam 1
StackStack LIFO (last-in-first-out).LIFO (last-in-first-out). Push [addBack] Push [addBack] pop [removeBack/“topAndPop”] pop [removeBack/“topAndPop”] peek [get(back--); ]peek [get(back--); ] Many applications, such as the Many applications, such as the
implementation of (recursive) implementation of (recursive) method calls and “undo” functions.method calls and “undo” functions.
push, pop, and peek are all constant-time O(1) operations.
Can be implemented using an ArrayList (last element in ArrayList is top of Stack) or a LinkedList (first element in LinkedList is top of stack). Which is better?
QueueQueue Operations: offer [enqueue], poll [dequeue], peek.Operations: offer [enqueue], poll [dequeue], peek. FIFO (first-in-first-out).FIFO (first-in-first-out). Applications include simulations and operating systems.Applications include simulations and operating systems.
offer, poll, and peek are all constant-time operations.
Can be implemented using an ArrayList (treat positions as a circle) or a LinkedList (first element in LinkedList is front of queue, last element is rear of Queue). More details next slide.
Array Array implementatimplementat
ion of a ion of a queuequeue
What if we run out of room?
LinkedList LinkedList implementation of a implementation of a
queuequeue
DemoDemo
A tale of two interfaces: A tale of two interfaces: Set<E>…Set<E>…
A collection with A collection with no duplicatesno duplicates If If obj1obj1 and and obj2obj2 are both in the set, are both in the set,
then then obj1.equals(obj2)obj1.equals(obj2) returns returns falsefalse..
Subinterface: SortedSetSubinterface: SortedSet Not quite a mathematical set (no Not quite a mathematical set (no
intersection or union methods)intersection or union methods)
“Bob”, “Flo”, “Gary”, “Lisa”, “Marie”
……and Map<K,V>and Map<K,V> An object that maps keys to An object that maps keys to
values. Duplicates? values. Duplicates? A map cannot contain A map cannot contain
duplicate keys; each key duplicate keys; each key can map to at most one can map to at most one value. value. V get(Object key)V get(Object key)
Multiple keys Multiple keys can can have the have the same value same value
Other operations: Other operations: put(K key, V value) put(K key, V value) containsKey(Object kcontainsKey(Object k
ey) ey) containsValue(Object containsValue(Object
value)value) V remove(Object key)V remove(Object key)
123456789, 987654321, 102934857
“Bill Smith”, “Darla Clive”
NO
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(123456789, “”Bill Smith”);map.put(987654321, “Darla Clive”);
Keys
Values
OK
TreeSets and TreeMapsTreeSets and TreeMaps
……are are java.utiljava.util implementations of implementations of SortedSetSortedSet and and MapMap..
Ordered elements. Ordered elements. In a tree, average time is O(log n), In a tree, average time is O(log n),
and with complex algorithms, worst case and with complex algorithms, worst case can also be O(log N)can also be O(log N)
Also support taking ordered subsets Also support taking ordered subsets from head, tail, or interior of set or from head, tail, or interior of set or mapmap
More details in CSSE230…More details in CSSE230…
HashSets and HashMapsHashSets and HashMaps
……are are java.utiljava.util implementations of implementations of SetSet (not (not SortedSetSortedSet) and ) and MapMap..
Average time for lookup, insertion, or Average time for lookup, insertion, or deletion is deletion is O(1)O(1).. but worst case is O(N).but worst case is O(N). A quick view of how it works:A quick view of how it works:
hashCode function maps object to an integer, which hashCode function maps object to an integer, which is used to find an index into an array.is used to find an index into an array.
Collision resolution.Collision resolution. Fast search, but Fast search, but unorderedunordered..
Need to implement Need to implement .equals().equals() and and .hashCode().hashCode()
More details in CSSE230…More details in CSSE230…
PriorityQueue classPriorityQueue class
Similar to a queue, but each item has an Similar to a queue, but each item has an associated priority. associated priority.
Only the item with minimum priority is Only the item with minimum priority is accessible. accessible.
Allows an object to “cut” in line if lower priorityAllows an object to “cut” in line if lower priority Elements need to be comparableElements need to be comparable Operations: Operations:
findMin(peek) findMin(peek) (O(log n))(O(log n)) deleteMin(poll)deleteMin(poll) (O(log n)) (O(log n)) insert(offer) insert(offer) (O(1))(O(1))
Underlying data structure: a binary heap Underlying data structure: a binary heap (another shameless plug for CSSE230)(another shameless plug for CSSE230)
Speaking of CSSE230Speaking of CSSE230
Given the choice, you should take Given the choice, you should take CSSE230 CSSE230 after after this Winter. Why? this Winter. Why?
Less overlap with 221 and you’ll do a Less overlap with 221 and you’ll do a cool projectcool project
DemoDemo