44st - 4.4 Symbol Tables Introduction to Programming in...

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: 4.4 Symbol Tables Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2008 * * 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 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.edu" )); } st["www.yale.com"] = "209.052.165.60" st["www.yale.edu"] 128.112.136.11 null 130.132.143.21 6 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 Freq { 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 (stay tuned) value type key type 7 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 1M random sentences 4,238,435 21,191,455 215,515 534,580 Linguistic analysis. Compute word frequencies in a piece of text....
View Full Document

Page1 / 10

44st - 4.4 Symbol Tables Introduction to Programming in...

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