16_symbol_tables - 1 4.4 Symbol Tables 2 Symbol Table...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 4.4 Symbol Tables 2 Symbol Table Symbol table. Key-value pair abstraction. Insert a key with specified value. Given a key, search for the corresponding value. Ex. [DNS lookup] Insert URL with specified IP address. Given URL, find corresponding IP address. key value www.cs.princeton.edu URL IP address 128.112.136.11 www.princeton.edu 128.112.128.15 www.yale.edu 130.132.143.21 www.harvard.edu 128.103.060.55 www.simpsons.com 209.052.165.60 3 Symbol Table Applications Application Purpose Key Value phone book look up phone number name phone number bank process transaction account number transaction details file share find song to download name of song computer ID dictionary look up word word definition web search find relevant documents keyword list of documents genomics find markers DNA string known positions DNS find IP address given URL URL IP address reverse DNS find URL given IP address IP address URL book index find relevant pages keyword list of pages web cache download filename file contents compiler find properties of variable variable name value and type file system find file on disk filename location on disk routing table route Internet packets destination best route 4 Symbol Table API create an empty symbol table ST() public class ST<Key extends Comparable, Val> (symbol table data type) insert a key-value pair put(Key key, Val val) void return value associated with given key get(Key key) Val is the given key present? contains(Key key) boolean delete the key and associated value remove(Key key) void return an iterator over the keys iterator() Iterator<Key> public static void main ( String args ) { ST < String , String > st = new ST < String , String > (); st . put ( "www.cs.princeton.edu" , "128.112.136.11" ); st . put ( "www.princeton.edu" , "128.112.128.15" ); st . put ( "www.yale.edu" , "130.132.143.21" ); StdOut . println ( st . get ( "www.cs.princeton.edu" )); StdOut . println ( st . get ( "www.harvardsucks.com" )); StdOut . println ( st . get ( "www.yale.com" )); } st["www.yale.com"] = "209.052.165.60" st["www.yale.edu"] 128.112.136.11 null 130.132.143.21 2 5 Symbol Table Client: Frequency Counter Frequency counter. [e.g., web traffic analysis, linguistic analysis] Read in a key. If key is in symbol table, increment counter by one; If key is not in symbol table, insert it with count = 1. public class FrequencyCounter { public static void main ( String args ) { ST < String , Integer > st = new ST < String , Integer >(); while (! StdIn . isEmpty ()) { String key = StdIn . readString (); if ( st . contains ( key )) st . put ( key , st . get ( key ) + 1 ); else st . put ( key , 1 ); } for ( String s : st ) StdOut . println ( st . get ( s ) + " " + s ); } } calculate frequencies print results enhanced for loop value type key type 6 Datasets mobydick.txt File leipzig100k.txt Melville's Moby Dick Description 100K random sentences 210,028 Words 2,121,054 16,834 Distinct 144,256 leipzig200k.txt leipzig1m.txt 200K random sentences...
View Full Document

This note was uploaded on 09/21/2009 for the course COMPUTER computer 1 taught by Professor Abedauthman during the Fall '08 term at Aarhus Universitet.

Page1 / 16

16_symbol_tables - 1 4.4 Symbol Tables 2 Symbol Table...

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

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