Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSSE221: Software Dev. Honors Day 14 Announcements Announcements Pass in Homework 5 now Pass in Homework 5 now Questions on Cars, Trucks, Trains? Questions.

Similar presentations


Presentation on theme: "CSSE221: Software Dev. Honors Day 14 Announcements Announcements Pass in Homework 5 now Pass in Homework 5 now Questions on Cars, Trucks, Trains? Questions."— Presentation transcript:

1 CSSE221: Software Dev. Honors Day 14 Announcements Announcements Pass in Homework 5 now Pass in Homework 5 now Questions on Cars, Trucks, Trains? Questions on Cars, 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.

2 This week: Markov Monday: Monday: Stacks and Queues Stacks and Queues Sets, Maps, and PriorityQueues Sets, Maps, and PriorityQueues Tuesday: Tuesday: Some pros and cons of various data structures Some pros and cons of various data structures Brief example of file I/O Brief example of file I/O Introduction to Markov, a cool statistical text program with lots of data structures Introduction to Markov, a cool statistical text program with lots of data structures Thursday: Thursday: Exam 1 Exam 1

3 Stack 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 implementation of (recursive) method calls and “undo” functions. Many applications, such as the implementation of (recursive) 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?

4 Queue 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.

5 Array implementation of a queue What if we run out of room?

6 LinkedList implementation of a queue

7 Demo

8 A tale of two interfaces: Set … A collection with no duplicates A collection with no duplicates If obj1 and obj2 are both in the set, then obj1.equals(obj2) returns false. If obj1 and obj2 are both in the set, then obj1.equals(obj2) returns false. Subinterface: SortedSet Subinterface: SortedSet Not quite a mathematical set (no intersection or union methods) Not quite a mathematical set (no intersection or union methods) “Bob”, “Flo”, “Gary”, “Lisa”, “Marie”

9 …and Map …and Map An object that maps keys to values. Duplicates? A map cannot contain duplicate keys; each key can map to at most one value. V get(Object key) Multiple keys can have the same value Other operations: put(K key, V value) containsKey(Object key) containsValue(Object value) V remove(Object key) 123456789, 987654321, 102934857 “Bill Smith”, “Darla Clive” NO HashMap map = new HashMap (); map.put(123456789, “”Bill Smith”); map.put(987654321, “Darla Clive”); Keys Values OK

10 TreeSets and TreeMaps …are java.util implementations of SortedSet and Map. 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 can also be O(log N) and with complex algorithms, worst case can also be O(log N) Also support taking ordered subsets from head, tail, or interior of set or map Also support taking ordered subsets from head, tail, or interior of set or map More details in CSSE230… More details in CSSE230…

11 HashSets and HashMaps …are java.util implementations of Set (not SortedSet) and Map. Average time for lookup, insertion, or deletion is O(1). Average time for lookup, insertion, or deletion is 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 is used to find an index into an array. hashCode function maps object to an integer, which is used to find an index into an array. Collision resolution. Collision resolution. Fast search, but unordered. Fast search, but unordered. Need to implement.equals() and.hashCode() Need to implement.equals() and.hashCode() More details in CSSE230… More details in CSSE230…

12 PriorityQueue class Similar to a queue, but each item has an associated priority. Similar to a queue, but each item has an associated priority. Only the item with minimum priority is accessible. Only the item with minimum priority is accessible. Allows an object to “cut” in line if lower priority Allows an object to “cut” in line if lower priority Elements need to be comparable Elements need to be comparable Operations: Operations: findMin(peek) (O(log n)) findMin(peek) (O(log n)) deleteMin(poll) (O(log n)) deleteMin(poll) (O(log n)) insert(offer) (O(1)) insert(offer) (O(1)) Underlying data structure: a binary heap (another shameless plug for CSSE230) Underlying data structure: a binary heap (another shameless plug for CSSE230)

13 Speaking of CSSE230 Given the choice, you should take CSSE230 after this Winter. Why? Given the choice, you should take CSSE230 after this Winter. Why? Less overlap with 221 and you’ll do a cool project Less overlap with 221 and you’ll do a cool project

14 Demo


Download ppt "CSSE221: Software Dev. Honors Day 14 Announcements Announcements Pass in Homework 5 now Pass in Homework 5 now Questions on Cars, Trucks, Trains? Questions."

Similar presentations


Ads by Google