1 of 34

Lecture 19:

Symbol Tables

CS 136: Spring 2024

Katie Keith

2 of 34

Record on Zoom

3 of 34

  • Lab 5 due Monday (today) or Tuesday (tomorrow)
  • Lab 6 this week: No starter code!
    • Recommended to allocate a bit more time for this lab
  • Optional Midterm Corrections due Monday April 14 (today) at 10pm. Submit on Gradescope.
  • TA eval form (Due April 18) : https://forms.gle/sbqCGVLAFnhUQ4i39

📣 Announcements

4 of 34

📚Readings

  • Sedgewick and Wayne. Algorithms. Section 3.1.

5 of 34

  • Symbol tables (ADT) overview
  • Symbol Table Data Structure #1: Linked List
  • Symbol Table Data Structure #2: Two arrays (sorted)

🎯 Today’s Learning Objectives

6 of 34

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

7 of 34

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

8 of 34

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!

9 of 34

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

10 of 34

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

11 of 34

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

12 of 34

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

13 of 34

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

14 of 34

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

15 of 34

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

16 of 34

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!)

17 of 34

  • Symbol tables (ADT) overview
  • Symbol Table Data Structure #1: Linked List
  • Symbol Table Data Structure #2: Two arrays (sorted)

âś…

🎯 Today’s Learning Objectives

18 of 34

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

19 of 34

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

20 of 34

Design Sketch: LinkedList for SymbolTable

  • Each node of a Linked List will contain both a key and a value
  • put
    • First, scan through the list to check if the key already exists
    • If a key is found, replace the value.
    • If the key is not found, insert a node with the new key and value at the beginning of the list (elements in the list are unordered).
  • get
    • Scan through the list and return if the key exists

21 of 34

LinkedListST.java

đź’»

22 of 34

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

23 of 34

LinkedListST.java

get()

main()

đź’»

24 of 34

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)

25 of 34

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)

26 of 34

  • Symbol tables (ADT) overview
  • Symbol Table Data Structure #1: Linked List
  • Symbol Table Data Structure #2: Two arrays (sorted)

âś…

âś…

🎯 Today’s Learning Objectives

27 of 34

Design Sketch: Two arrays (sorted) for SymbolTables

  • Use two “parallel” arrays, one for keys and one for values
  • Keep the keys in sorted order
  • Helper function rank: returns number of keys smaller than a given key
  • put
    • Call rank. If the key exists, move all larger keys over one position to make room
  • get
    • Call rank and return the value if the key exists

We’ll start with get, then return to put

28 of 34

TwoArraysST.java

đź’»

29 of 34

Search algorithms

*Note: Our book refers to “linear search” as “sequential search”

30 of 34

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

31 of 34

LinkedListST.java

put()

main()

đź’»

32 of 34

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)

33 of 34

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?

34 of 34

  • Symbol tables (ADT) overview
  • Symbol Table Data Structure #1: Linked List
  • Symbol Table Data Structure #2: Two arrays (sorted)

âś…

âś…

âś…

🎯 Today’s Learning Objectives