12-map-grammar - CSE143 Lecture12 MapsandGrammars...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 12 Maps and Grammars slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Exercise Write a program to count the occurrences of each word in a  text file (e.g.  Moby Dick  or  Green Eggs and Ham ). Allow the user to type a word and report how many times that  word appeared in the book. Report all words that appeared in the book at least 500 times, in  alphabetical order. How will we store the data to solve this problem?
Background image of page 2
3 The Map ADT map : Holds a set of unique  keys  and a collection of  values where each key is associated with one value. a.k.a. "dictionary", "associative array", "hash" basic map operations: put ( key value  ): Adds a  mapping from a key to a value. get ( key  ): Retrieves the value mapped to the key. remove ( key  ): Removes the given key and its reference to the mapped value. myMap.get("Juliet")  returns  "Capulet"
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Maps and tallying a map can be thought of as generalization of a tallying array the "index" (key) doesn't have to be an  int recall previous tallying examples from CSE 142 count digits:  22092310907 // (M)cCain, (O)bama, (I)ndependent count votes: "MOOOOOOMMMMMOOOOOOMOMMIMOMMIMOMMIO" inde x 0 1 2 3 4 5 6 7 8 9 valu e 3 1 3 0 0 0 0 1 0 2 key "M" "O" "I" value 16 14 3 "O" "I" 16 3 14 keys values
Background image of page 4
5 Map  implementation in Java, maps are represented by  Map  interface in  java.util Map  is implemented by the  HashMap  and  TreeMap  classes HashMap : implemented using an array called a "hash table"; extremely fast:  O(1)  ; keys are stored in unpredictable order TreeMap : implemented as a linked "binary tree" structure; very fast:  O(log N)
Background image of page 5

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

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

This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 18

12-map-grammar - CSE143 Lecture12 MapsandGrammars...

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

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