COMP 103 2015-T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.

Slides:



Advertisements
Similar presentations
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Advertisements

COMP 103 Linked Stack and Linked Queue.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Stacks.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
TCSS 342, Winter 2005 Lecture Notes
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
COMP 103 Iterators and Iterable. RECAP  Maps and Queues TODAY  Queue Methods  Iterator and Iterable 2 RECAP-TODAY.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
COMP 103 Hashing 2013-T2 Lecture 28 Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,
Computer Science 209 Software Development Inheritance and Composition.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Some Collections: BAGS, SETS, and STACKS
Recap: Solution of Last Lecture Activity
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
Software Development Inheritance and Composition
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
COMP 103 Maps, Stacks Thomas Kuehne 2016-T2 Lecture 5
Building Java Programs
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
"He's off the map!" - Eternal Sunshine of the Spotless Mind
Stacks Chapter 5.
Introduction to Stacks
Introduction to Stacks
Stacks.
CHAPTER 3: Collections—Stacks
Presentation transcript:

COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus Frean,

RECAP So far we’ve looked at these types of Collection  Bag, List, Set, Queue and Map TODAY  Stack (the last of the Collection types we will look at)  Collections of collections of... RECAP-TODAY 2

Maps  When declaring and constructing, we must specify two types:  Type of the key, and type of the value private Map phoneBook; : phoneBook = new HashMap ();  Operations:  get(key) → returns value associated with key (or null)  put(key, value) → sets the value associated with key (and returns the old value, if any)  remove(key) → removes the key and associated value (and returns the old value, if any)  containsKey(key) → boolean  size() 3

Iterating through a Map  How do you iterate through a Map ? (e.g,. to print it out)  A Map isn’t just a collection of single items! ⇒ could iterate through the collection of keys ⇒ could iterate through the collection of values ⇒ could iterate through the collection of key-value pairs  Java Collection library’s Map allows all of the above!  keySet()→ Set of all keys for (String name : phonebook.keySet()) {….  values()→ Collection of all values for (Integer num : phonebook.values()) {….  entrySet()→ Set of all Map.Entry’s for (Map.Entry entry : phonebook.entrySet()) { … entry.getKey() … … entry.getValue()… 4 Why Collection of values, not Set of values like the other two ? Type for key  value pair

Example of using Map  Find the highest frequency word in a file ⇒ must count frequency of every word i.e., need to associate a count (int) with each word (String) ⇒ use a Map of “word  count” pairs  Two Steps:  construct the counts of each word:countWords(file) → map  find the highest count:maxCountWord(map) → word UI.println( maxCountWord( countWords(file) ) ); 5

Example of using Map (similar to Assign#2) // Construct histogram of counts of all words in a file public Map countWords(Scanner sc) { // construct new map // for each word in file // if word is in the map, increment its count // else, put it in map with a count of 1 // return map } // Find word in histogram with highest count public String maxCountWord(Map counts) { // for each word in map // if word has higher count than current maximum count, record it // return last recorded maximum count word } 6 “pseudocode” Design the step- by-step solution (algorithm) before you code “pseudocode” Design the step- by-step solution (algorithm) before you code

Example of using Map // Construct histogram of counts of all words in a file public Map countWords(Scanner scan){ Map counts = new HashMap (); while (scan.hasNext()) { String word = scan.next(); if ( counts.containsKey(word) ) counts.put(word, counts.get(word)+1); else counts.put(word, 1); } return counts; } 7

Example of using Map (faster) // Construct histogram of counts of all words in a file public Map countWords(Scanner scan){ Map counts = new HashMap (); while (scan.hasNext()) { String word = scan.next(); Integer frequency = counts.get(word); counts.put(word, frequency == null ? 1 : frequency + 1); } return counts; } 8

Iterating through Map: entrySet public String maxCountWord(Map counts) { String maxWord = null; int maxCount = -1; for (Map.Entry entry : counts.entrySet() ) { if (entry.getValue() > maxCount) { maxCount = entry.getValue(); maxWord = entry.getKey(); } } return maxWord; } “public” ⇒ 1 “Bob” ⇒ 2 “Alice” ⇒ 1 “eavesdrops” ⇒ 5 “private” ⇒ 1 Map.Entry - getKey() - getValue() 9 Map.Entry - getKey() - getValue()

Stacks  Based on the “first in, last out” principle (cf. Queues…)  A special kind of List:  Constrained access: add, get, and remove only from one end  We should have a Stack interface, and different implementations of it (ArrayStack, LinkedStack, etc.), but...  In Java’s Collections library, it is a class that implements List (actually, it extends Vector)  So you make one like this: Stack myNums = new Stack (); 10

Stacks  Stacks have extra operations: push(value), pop(), peek()  push(value): Put value on top of stack  pop():Removes and returns top of stack  peek():Returns top of stack, without removing  plus the other List operations… ( oops! ) 11

Stacks example  Reversing the items from a file:  read and push onto a stack  pop them off the stack public void reverseNums(Scanner sc){ Stack myNums = new Stack (); while (sc.hasNextInt()) myNums.push(sc.nextInt()) while (! myNums.isEmpty()) UI.print(myNums.pop() + “\n”); } 12

Applications of Stacks  Programs that deal with programs  e.g., program execution,  e.g., expression execution, (6 + 4) * ((10 * √ 64) – ( √ 81/ 3))  working on subtasks, returning to previous task.  Processing files of structured (nested) data.  e.g., reading files of with structured markup (HTML, XML,…)  Undo in editors 13

Stack for evaluating expressions (6 + 4) * ((10 * √ 64) – ( √ 81/ 3)) ⇒ ( (6, 4)+, ((10, (64) √ ) *, ((81) √, 3)/ )- )* ⇒ √ * 81 √ 3 / - * Algorithm: if operand:push on stack if operator:pop arguments from stack compute value push on stack if done:pop answer from stack Postfix order (also known as “Reverse Polish notation”) 14

Summary of these recent collection types  Queue  First in, first out (FIFO)  Always remove from one end  Ordinary queue: add at the opposed end  Priority queue: add anywhere, according to priority  Stack  First in, last out (FILO)  [Java: No Stack interface, just a class, named “Stack”, with lax control]  Map  Key  Value pairs  Three ways to iterate:over set of Keys, over collection of Values, over set of Key  Value pairs  Key  Value pairs represented using Map.Entry 15

Collections of collections? Yes! A collection is an object. Often really useful, for instance,  a List of Sets  a Map, from Strings into Lists (of...) 16