Lecture 19:
Symbol Tables
CS 136: Spring 2024
Katie Keith
Record on Zoom
📣 Announcements
📚Readings
🎯 Today’s Learning Objectives
Symbol Table
A symbol table is an ADT where we save information (a value) that we can later search for and retrieve by specifying a key. Keys are unique (no duplicates).
Keys | Values |
"USA" | 40 |
"China" | 40 |
"Japan" | 20 |
"Saint Lucia" | 1 |
Running Example:
Record the number of gold medals each country won in the 2024 Olympics
Julien Alfred Wins Saint Lucia's First Gold Medal in the 100 M Dash
Python Dictionaries are Symbol Tables
gold_medals = {
"USA": 40,
"China": 40,
"Japan": 20,
"Saint Lucia": 1,
}
CS 134: How do we use dictionaries?
CS 136: How do we implement dictionaries “under the hood”?
Key of a dictionary
Value of a dictionary
Other Applications of Symbol Tables
Source: S&W Sec 3.1
Application | Keys | Values |
Dictionary | Word | Definition |
Compiler | Variable Name | Type and Value |
Bank Account | Account Number | Amount in Account |
Domain Name Service (DNS) | Domain name (e.g., “williams.edu”) | IP Address (e.g., 152.78.150.150) |
Lab 6!
Multiple data structures can implement the same ADT
ADT
Data Structures
List
Finite number of elements
(same element may occur more than once)
Stack
Last in first out (LIFO) operations
Symbol Table
Associates a key with a value
Graph
A set of vertices, pairs of which are connected by edges
Queue
First in first out (FIFO) operations
Multiple data structures can implement the same ADT
Singly Linked Lists
Data Structures
Arrays
int[] arr = {1, 2, 3, 4, 5};
List
Finite number of elements
(same element may occur more than once)
Doubly Linked Lists
Stack
Last in first out (LIFO) operations
Symbol Table
Associates a key with a value
Graph
A set of vertices, pairs of which are connected by edges
Queue
First in first out (FIFO) operations
ADT
Multiple data structures can implement the same ADT
Binary Search Tree
Data Structures
Linked List
Hash Tables
List
Finite number of elements
(same element may occur more than once)
Two arrays (sorted)
Stack
Last in first out (LIFO) operations
Symbol Table
Associates a key with a value
Graph
A set of vertices, pairs of which are connected by edges
Queue
First in first out (FIFO) operations
String[] keys;
int[] values;
Today!
Today!
Weds: Overview
Lab 6: Implement!
Fri
ADT
Symbol Table Interface
import java.util.Comparator;
public interface SymbolTable<Key, Value>{
/* Returns true if the table is empty */
public abstract boolean isEmpty();
/* Returns the number of key-value pairs */
public abstract int size();
/* Inserts the key-value pair into the table
* The comparator is used to compare keys
* Convention: If key already exists, overwrites with new val */
public abstract void put(Key key, Value val, Comparator<Key> comparator);
/* “Search”!
* Returns the value paired with the key
* Returns null if the key is not in the table */
public abstract Value get(Key key, Comparator<Key> comparator);
}
Generic types
Symbol Table Interface
import java.util.Comparator;
public interface SymbolTable<Key, Value>{
/* Returns true if the table is empty */
public abstract boolean isEmpty();
/* Returns the number of key-value pairs */
public abstract int size();
/* Inserts the key-value pair into the table
* The comparator is used to compare keys
* Convention: If key already exists, overwrites with new val */
public abstract void put(Key key, Value val, Comparator<Key> comparator);
/* “Search”!
* Returns the value paired with the key
* Returns null if the key is not in the table */
public abstract Value get(Key key, Comparator<Key> comparator);
}
Generic types
Symbol Table Interface
import java.util.Comparator;
public interface SymbolTable<Key, Value>{
/* Returns true if the table is empty */
public abstract boolean isEmpty();
/* Returns the number of key-value pairs */
public abstract int size();
/* Inserts the key-value pair into the table
* The comparator is used to compare keys
* Convention: If key already exists, overwrites with new val */
public abstract void put(Key key, Value val, Comparator<Key> comparator);
/* “Search”!
* Returns the value paired with the key
* Returns null if the key is not in the table */
public abstract Value get(Key key, Comparator<Key> comparator);
}
Generic types
Preview: important for data structures with sorted keys
Symbol Table Interface
import java.util.Comparator;
public interface SymbolTable<Key, Value>{
/* Returns true if the table is empty */
public abstract boolean isEmpty();
/* Returns the number of key-value pairs */
public abstract int size();
/* Inserts the key-value pair into the table
* The comparator is used to compare keys
* Convention: If key already exists, overwrites with new val */
public abstract void put(Key key, Value val, Comparator<Key> comparator);
/* "Search"!
* Returns the value paired with the key
* Returns null if the key is not in the table */
public abstract Value get(Key key, Comparator<Key> comparator);
}
Generic types
Python Dictionaries are Symbol Tables
gold_medals = {
"USA": 40,
"China": 40,
"Japan": 20,
"Saint Lucia": 1,
}
gold_medals["New Zealand"] = 10
print(gold_medals["USA"])
put operation
get operation (search!)
âś…
🎯 Today’s Learning Objectives
Review: Linked Lists (of Strings)
private Node first;
private class Node {
private String data;
private Node next;
public Node() {
this.data =null ;
this.next = null;
}
}
"USA"
"China"
"Japan"
null
first
data
next
for(Node x = first; x != null; x = x.next){
// Do something
}
Loop to traverse a Linked List
Linked List to implement a Symbol Table
To implement a Symbol Table with a Linked List, each Node has three instance variables: one that stores the key, one that stores the value and one that stores a reference to the next node.
"USA"
40
"China"
40
"Japan"
20
null
key
val
next
first
Design Sketch: LinkedList for SymbolTable
LinkedListST.java
đź’»
There are two major errors in the put method below. What are they and how would we fix them?
public void put(Key key, Value val, Comparator<Key> comparator){
if (key == null){return;} //Bad key
for (Node x = first; x != null; x = x.next) {
if (key == x.key) {
x.val = val;
return;
}
}
first = new Node();
first.key = key;
first.val = val;
numPairs++;
}
đź’ˇThink-pair-share
LinkedListST.java
get()
main()
đź’»
Analysis of Symbol Table’s Data Structures
Data Structure | put() | get() |
(Unordered) Linked List | | |
Two ordered arrays (with binary search) | | |
Binary Search Tree | | |
Hash Table (with separate chaining) | | |
Hash table (with linear probing) | | |
Analysis of Symbol Table’s Data Structures
Data Structure | put() | get() |
(Unordered) Linked List | O(n) | O(n) |
Two ordered arrays (with binary search) | | |
Binary Search Tree | | |
Hash Table (with separate chaining) | | |
Hash table (with linear probing) | | |
âś…
âś…
🎯 Today’s Learning Objectives
Design Sketch: Two arrays (sorted) for SymbolTables
We’ll start with get, then return to put
TwoArraysST.java
đź’»
Search algorithms
*Note: Our book refers to “linear search” as “sequential search”
Finish the rank method below. Note, this uses binary search.
// Returns the num. of keys strictly less than target
private int rank(Key target, Comparator<Key> comp) {
if (target == null){ return -1;} // invalid target
int lo = 0;
int hi = numPairs-1;
while ( ) {
int mid = lo + (hi - lo) / 2;
int cmp = comp.compare( , keys[ ]);
if (cmp < 0) hi = ;
else if (cmp > 0) lo = ;
else return ;
}
return lo;
}
đź’ˇThink-pair-share
LinkedListST.java
put()
main()
đź’»
Analysis of Symbol Table’s Data Structures
Data Structure | put() | get() |
(Unordered) Linked List | O(n) | O(n) |
Two ordered arrays (with binary search) | | |
Binary Search Tree | | |
Hash Table (with separate chaining) | | |
Hash table (with linear probing) | | |
Analysis of Symbol Table’s Data Structures
Data Structure | put() | get() |
(Unordered) Linked List | O(n) | O(n) |
Two ordered arrays (with binary search) | O(n) | O(log n) |
Binary Search Tree | | |
Hash Table (with separate chaining) | | |
Hash table (with linear probing) | | |
Preview for Weds: Can we do better for put?
âś…
âś…
âś…
🎯 Today’s Learning Objectives