15. Symbol Tables
Total Page:16
File Type:pdf, Size:1020Kb
COMPUTER SCIENCE SEDGEWICK/WAYNE 15. Symbol Tables Section 4.4 http://introcs.cs.princeton.edu COMPUTER SCIENCE SEDGEWICK/WAYNE 15.Symbol Tables •APIs and clients •A design challenge •Binary search trees •Implementation •Analysis http://introcs.cs.princeton.edu FAQs about sorting and searching Hey, Alice. That whitelist filter with mergesort and binary search is working great. Right, but it's a pain sometimes. Why? We have to sort the whole list whenever we add new customers. Also, we want to process transactions and associate all sorts of information with our customers. Bottom line. Need a more flexible API. 3 Why are telephone books obsolete? Unsupported operations • Change the number associated with a given name. • Add a new name, associated with a given number. • Remove a givne name and associated number Observation. Mergesort + binary search has the same problem with add and remove. see Sorting and Searching lecture 4 Associative array abstraction Imagine using arrays whose indices are string values. phoneNumber["Alice"] = "(212) 123-4567" legal code in some programming phoneNumber["Bob"] = "(609) 987-6543" languages (not Java) phoneNumber["Carl"] = "(800) 888-8888" phoneNumber["Dave"] = "(888) 800-0800" phoneNumber["Eve"] = "(999) 999-9999" transactions["Alice"] = "Dec 12 12:01AM $111.11 Amazon, Dec 12 1:11 AM $989.99 Ebay" ... A fundamental abstraction • Use keys to access associated values. URL["128.112.136.11"] = "www.cs.princeton.edu" • Keys and values could be any type of data. URL["128.112.128.15"] = "www.princeton.edu" URL["130.132.143.21"] = "www.yale.edu" • Client code could not be simpler. URL["128.103.060.55"] = "www.harvard.edu" IPaddr["www.cs.princeton.edu"] = "128.112.136.11" IPaddr["www.princeton.edu"] = "128.112.128.15" Q. How to implement? IPaddr["www.yale.edu"] = "130.132.143.21" IPaddr["www.harvard.edu"] = "128.103.060.55" 5 Symbol table ADT A symbol table is an ADT whose values are sets of key-value pairs, with keys all different. Basic symbol-table operations key: word value: definition • Associate a given key with a given value. [If the key is not in the table, add it to the table.] [If the key is in the table, change its value.] • Return the value associated with a given key. • Test if a given key is in the table. • Iterate though the keys. key: number key: time+channel value: function value value: TV show Useful additional assumptions key: name value: phone number • Keys are comparable and iteration is in order. • No limit on number of key-value pairs. • All keys not in the table associate with null. key: term value: article 6 Benchmark example of symbol-table operations Application. Count frequency of occurrence of strings in StdIn. Keys. Strings from a sequence. Values. Integers. key it was the best of times it was the worst value 1 1 1 1 1 1 2 2 2 1 it 1 it 1 it 1 best 1 best 1 best 1 best 1 best 1 best 1 best 1 was 1 the 1 it 1 of 1 of 1 of 1 of 1 of 1 of 1 symbol-table was 1 the 1 it 1 it 1 it 2 it 2 it 2 it 2 contents was 1 the 1 the 1 the 1 the 1 the 2 the 2 after operation was 1 times 1 times 1 times 1 times 1 times 1 was 1 was 1 was 2 was 2 was 2 worst 1 change the value 7 Parameterized API for symbol tables Goal. Simple, safe, and clear client code for symbol tables holding any type of data. Java approach: Parameterized data types (generics) • Use placeholder type names for both keys and values. • Substitute concrete types for placeholder in clients. “implements compareTo()” public class ST<Key extends Comparable<Key>, Value> ST<Key, Value>() create a symbol table void put(Key key, Value val) associate key with val Symbol Table API Value get(Key key) return value associated with key, null if none boolean contains(Key key) is there a value associated with key? Iterable<Key> keys() all the keys in the table 8 Aside: Iteration (client code) Q. How to print the contents of a stack/queue? A. Use Java's foreach construct. Java foreach construct Stack<String> stack = new Stack<String>(); ... Enhanced for loop. for (String s : stack) • Useful for any collection. StdOut.println(s); ... • Iterate through each entry in the collection. • Order determined by implementation. • Substantially simplifies client code. public class Stack<Item> implements Iterable<Item> • Works when API "implements Iterable". Stack<Item>() create a stack of objects, all of type Item void push(Item item) add item to stack Item pop() remove and return item most recently pushed boolean isEmpty() is the stack empty ? int size() # of objects on the stack Performance specification. Constant-time per entry. 9 Aside: Iteration (implementation) Q. How to "implement Iterable"? public class Stack<Item> implements Iterable<Item> Stack<Item>() create a stack of objects, all of type Item void push(Item item) add item to stack A. We did it for Stack and Queue, Item pop() remove and return item most recently pushed so you don't have to. boolean isEmpty() is the stack empty ? int size() # of objects on the stack A. Implement an Iterator (see text pp. 588-89) Meets performance specification. Constant-time per entry. Bottom line. Use iteration in client code that uses collections. 10 Why ordered keys? Natural for many applications • Numeric types. • Strings. • Date and time. • Client-supplied types (Account numbers, ...). Enables useful API extensions • Provide the keys in sorted order. • Find the kth largest key. Enables efficient implementations • Mergesort. • Binary search. • BSTs (this lecture). thingsorganizedneatly.tumblr.com 11 Symbol table client example 1: Sort (with dedup) Goal. Sort lines on standard input (and remove duplicates). % more tale.txt it was the best of times • Key type. String (line on standard input). it was the worst of times it was the age of wisdom • Value type. (ignored). it was the age of foolishness it was the epoch of belief it was the epoch of incredulity it was the season of light it was the season of darkness it was the spring of hope it was the winter of despair public class Sort { % java Sort < tale.txt public static void main(String[] args) it was the age of foolishness { // Sort lines on StdIn it was the age of wisdom BST<String, Integer> st = new BST<String, Integer>(); it was the best of times it was the epoch of belief while (StdIn.hasNextLine()) it was the epoch of incredulity st.put(StdIn.readLine(), 0); it was the season of darkness for (String s : st.keys()) it was the season of light it was the spring of hope StdOut.println(s); it was the winter of despair } foreach it was the worst of times } construct 12 Symbol table client example 2: Frequency counter Goal. Compute frequencies of words on standard input. % more tale.txt it was the best of times • Key type. String (word on standard input). it was the worst of times it was the age of wisdom • Value type. Integer (frequency count). it was the age of foolishness it was the epoch of belief % java Freq < tale.txt | java Sort it was the epoch of incredulity 1 belief it was the season of light 1 best public class Freq it was the season of darkness 1 darkness it was the spring of hope { 1 despair it was the winter of despair public static void main(String[] args) 1 foolishness { // Frequency counter 1 hope 1 incredulity BST<String, Integer> st = new BST<String, Integer>(); 1 light while (!StdIn.isEmpty()) 1 spring { 1 winter 1 wisdom String key = StdIn.readString(); 1 worst if (st.contains(key)) st.put(key, st.get(key) + 1); 2 age else st.put(key, 1); 2 epoch 2 season } 2 times for (String s : st.keys()) 10 it 10 of StdOut.printf("%8d %s\n", st.get(s), s); 10 the } 10 was } 13 Symbol table client example 3: Index Goal. Print index to words on standard input. % more tale.txt • Key type. String (word on standard input). it was the best of times it was the worst of times • Value type. Queue<Integer> (indices where word occurs). it was the age of wisdom it was the age of foolishness it was the epoch of belief % java Index < tale.txt it was the epoch of incredulity public class Index age 15 21 it was the season of light { belief 29 it was the season of darkness best 3 public static void main(String[] args) it was the spring of hope darkness 47 { it was the winter of despair despair 59 BST<String, Queue<Integer>> st; epoch 27 33 st = new BST<String, Queue<Integer>>(); foolishness 23 int i = 0; hope 53 while (!StdIn.isEmpty()) incredulity 35 it 0 6 12 18 24 30 36 42 48 54 { light 41 String key = StdIn.readString(); of 4 10 16 22 28 34 40 46 52 58 if (!st.contains(key)) season 39 45 st.put(key, new Queue<Integer>()); spring 51 the 2 8 14 20 26 32 38 44 50 56 st.get(key).enqueue(i++); times 5 11 } was 1 7 13 19 25 31 37 43 49 55 for (String s : st.keys()) winter 57 StdOut.println(s + " " + st.get(s)); wisdom 17 worst 9 } } 14 Symbol-table applications application key value contacts name phone number, address Symbol tables credit card account number transaction details are ubiquitous file share name of song computer ID in today's computational dictionary word definition infrastructure.