Symbol Tables 1 / 15

Similar documents
3.1 Symbol Tables. API sequential search binary search ordered operations


ELEMENTARY SEARCH ALGORITHMS

Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE

4.1 Symbol Tables. API sequential search binary search ordered operations. Symbol tables

Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE

Priority Queues 1 / 15

cs2010: algorithms and data structures

Basic Data Structures 1 / 24

Searching Applications 1 / 18

ELEMENTARY SEARCH ALGORITHMS


ELEMENTARY SEARCH ALGORITHMS

ELEMENTARY SEARCH ALGORITHMS

Basic Data Structures

Outline. 1 Hashing. 2 Separate-Chaining Symbol Table 2 / 13

Analysis of Algorithms 1 / 18

! Insert a key with specified value. ! Given a key, search for the corresponding value. ! Insert URL with specified IP address.

3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion. Algorithms ROBERT SEDGEWICK KEVIN WAYNE.

Symbol Table. IP address

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations iteration deletion (see book or videos) ROBERT SEDGEWICK KEVIN WAYNE

Elementary Symbol Tables

BINARY SEARCH TREES TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Binary Search Tree (BST) Binary search trees

BINARY SEARCH TREES BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING


4.4 Symbol Tables. Symbol Table. Symbol Table Applications. Symbol Table API

Lecture 13: Binary Search Trees

Algorithms. Algorithms 3.2 BINARY SEARCH TREES. BSTs ordered operations deletion ROBERT SEDGEWICK KEVIN WAYNE.

Sorting Applications 1 / 9

4.4 Symbol Tables. Symbol Table. Symbol Table Applications. Symbol Table API

4.4 Symbol Tables and BSTs

Lecture 14: Binary Search Trees (2)

CMSC 132, Object-Oriented Programming II Summer Lecture 13

Heaps. Heaps. A heap is a complete binary tree.

4.2 Binary Search Trees

CS2012 Programming Techniques II

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

JVM (java) compiler. A Java program is either a library of static methods (functions) or a data type definition


COMPUTER SCIENCE. 15. Symbol Tables. Section 4.4.

Introduction to Computing II (ITI 1121) Final Examination

CSC Java Programming, Fall Java Data Types and Control Constructs

4.4 Symbol Tables. Symbol Table. Symbol Table API. Symbol Table Applications

Collections, Maps and Generics

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Project 2 (Deques and Randomized Queues)

Examination Questions Midterm 1

CSE 2123: Collections: Priority Queues. Jeremy Morris

Your data structure should implement all operations in logarithmic time (or better) as a function of the size of the queue.

CE 221 Data Structures and Algorithms

CS.15.A.SymbolTables.API. Alice

CS210 Study Guide Swami Iyer. 1 Programming Model (Algorithms 1.1) 2. 2 Data Abstraction (Algorithms 1.2) 4

Priority Queues. 04/10/03 Lecture 22 1

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

COMPUTER SCIENCE. Computer Science. 13. Symbol Tables. Computer Science. An Interdisciplinary Approach. Section 4.4.

Algorithms. Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations

CS171 Midterm Exam. October 29, Name:

Final Exam May 21, 2003

protected BinaryNode root; } 02/17/04 Lecture 11 1

COS 226 Algorithms and Data Structures Fall Midterm

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

COS 226 Algorithms and Data Structures Fall Midterm Solutions

CMSC 132: Object-Oriented Programming II. Stack and Queue

COS 226 Algorithms and Data Structures Spring Midterm

Dictionaries. Chapter 19. Copyright 2012 by Pearson Education, Inc. All rights reserved

Data Structures in Java

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

Points off Total off Net Score. CS 314 Final Exam Spring 2016

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

CmpSci 187: Programming with Data Structures Spring 2015

Pace University. Fundamental Concepts of CS121 1

Algorithms. Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

CSE 214 Computer Science II Heaps and Priority Queues

Priority Queues 3/19/14

PIC 20A Collections and Data Structures

Inner Classes. CMSC 433 Programming Language Technologies and Paradigms Spring Example: The Queue Class. Example: The Queue Class (cont d)

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS Introduction to Data Structures Week 1 Thursday

COMP 250 Midterm #2 March 11 th 2013

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers)

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Priority Queues (Heaps)

CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017

CS 3 Introduction to Software Engineering. 5: Iterators

Fun facts about recursion

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

cs2010: algorithms and data structures

data structures and algorithms lecture 6

CS 61B Spring 2017 Guerrilla Section 3 Solutions

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

PROGRAMMING FUNDAMENTALS

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

Transcription:

Symbol Tables 1 / 15

Outline 1 What is a Symbol Table? 2 API 3 Sample Clients 4 Implementations 5 Performance Characteristics 2 / 15

What is a Symbol Table? A symbol table is a data structure for key-value pairs that supports two operations: insert (put) a new pair into the table and search (get) the value associated with a given key Applications application purpose key value dictionary find definition word definition book index find relevant pages term list of page numbers file share find song to download name of song computer ID web search find relevant web pages keyword list of page names compiler find type and value variable name type and value 3 / 15

What is a Symbol Table? Conventions No duplicate keys are allowed; when a client puts a key-value pair into a table already containing that key (and an associated value), the new value replaces the old one Keys must not be null No key can be associated with the value null; otherwise the key is deleted Deleting a key involves removing the key (and the associated value) from the table immediately 4 / 15

API Basic symbol table method int size() boolean isempty() boolean contains(key key) Value get(key key) void put(key key, Value val) void delete(key key) Iterable<Key> keys() description number of key-value pairs in the table is the table empty? is there a value paired with key? value paired with key, or null put key-value pair into the table (remove key from table if value is null) remove key (and its value) from table all the keys in the table 5 / 15

API Ordered symbol table method int size() boolean isempty() boolean contains(key key) Value get(key key) int rank(key key) void put(key key, Value val) void delete(key key) void deletemin() void deletemax() Key min() Key max() Key select(int k) Key floor(key key) Key ceiling(key key) int size(key lo, Key hi) Iterable<Key> keys(key lo, Key hi) Iterable<Key> keys() description number of key-value pairs in the table is the table empty? is there a value paired with key? value paired with key, or null number of keys less than key put key-value pair into the table (remove key from table if value is null) remove key (and its value) from table delete smallest key delete largest key smallest key largest key key of rank k largest key less than or equal to key smallest key greater than or equal to key number of keys in [lo, hi] keys in [lo, hi], in sorted order all keys, in sorted order 6 / 15

Sample Clients Test client package edu. princeton.cs. algs4 ; public class SequentialSearchST { public static void main ( String [] args ) { SequentialSearchST < String, Integer > st = new SequentialSearchST < String, Integer >(); for ( int i = 0;! StdIn. isempty (); i ++) { String key = StdIn. readstring (); st.put (key, i); for ( String s : st. keys ()) { StdOut. println (s + " " + st.get (s )); $ java edu. princeton.cs. algs4. SequentialSearchST S E A R C H E X A M P L E <ctrl -d> L 11 P 10 M 9 X 7 H 5 C 4 R 3 A 8 E 12 S 0 7 / 15

Sample Clients Performance client package edu. princeton.cs. algs4 ; public class FrequencyCounter { public static void main ( String [] args ) { int distinct = 0, words = 0; int minlen = Integer. parseint ( args [0]); BinarySearchST < String, Integer > st = new BinarySearchST < String, Integer >(); while (! StdIn. isempty ()) { String key = StdIn. readstring (); if ( key. length () < minlen ) { continue ; words ++; if (st. contains ( key )) { st.put (key, st.get ( key ) + 1); else { st.put (key, 1); distinct ++; String max = ""; st.put (max, 0); for ( String word : st. keys ()) { if (st.get ( word ) > st.get ( max )) { max = word ; StdOut. println ( max + " " + st.get ( max )); StdOut. println (" distinct = " + distinct ); StdOut. println (" words = " + words ); $ java edu. princeton.cs. algs4. FrequencyCounter 8 < tale. txt business 122 distinct = 5126 words = 14346 8 / 15

Implementations Linked list based implementation of the basic symbol table API package edu. princeton.cs. algs4 ; public class SequentialSearchST < Key, Value > { private int N; private Node first ; private class Node { private Key key ; private Value val ; private Node next ; public Node ( Key key, Value val, Node next ) { this. key = key ; this. val = val ; this. next = next ; public SequentialSearchST () { public int size () { return N; public boolean isempty () { return size () == 0; public boolean contains ( Key key ) { return get ( key )!= null ; public Value get ( Key key ) { for ( Node x = first ; x!= null ; x = x. next ) { if ( key. equals (x. key )) { return x. val ; return null ; 9 / 15

Implementations public void put ( Key key, Value val ) { if ( val == null ) { delete ( key ); return ; for ( Node x = first ; x!= null ; x = x. next ) { if ( key. equals (x. key )) { x. val = val ; return ; first = new Node ( key, val, first ); N ++; public void delete ( Key key ) { first = delete ( first, key ); private Node delete ( Node x, Key key ) { if ( x == null ) { return null ; if ( key. equals (x. key )) { N - -; return x. next ; x. next = delete (x.next, key ); return x; public Iterable < Key > keys () { Queue < Key > queue = new Queue < Key >(); for ( Node x = first ; x!= null ; x = x. next ) { queue. enqueue (x. key ); return queue ; 10 / 15

Implementations Array based implementation of the ordered symbol table API package edu. princeton.cs. algs4 ; import java. util. NoSuchElementException ; public class BinarySearchST < Key extends Comparable < Key >, Value > { private static final int INIT_CAPACITY = 2; private Key [] keys ; private Value [] vals ; private int N = 0; public Binar ysearchs T () { this ( INIT_CAPACITY ); public Binar ysearchs T ( int capacity ) { keys = ( Key []) new Comparable [ capacity ]; vals = ( Value []) new Object [ capacity ]; private void resize ( int capacity ) { Key [] tempk = ( Key []) new Comparable [ capacity ]; Value [] tempv = ( Value []) new Object [ capacity ]; for ( int i = 0; i < N; i ++) { tempk [i] = keys [i]; tempv [i] = vals [i]; vals = tempv ; keys = tempk ; public boolean contains ( Key key ) { return get ( key )!= null ; public int size () { return N; public boolean isempty () { return size () == 0; 11 / 15

Implementations public Value get ( Key key ) { if ( isempty ()) { return null ; int i = rank ( key ); if (i < N && keys [i]. compareto ( key ) == 0) { return vals [i]; return null ; public int rank ( Key key ) { int lo = 0, hi = N - 1; while (lo <= hi) { int m = lo + ( hi - lo) / 2; int cmp = key. compareto ( keys [m ]); if ( cmp < 0) { hi = m - 1; else if ( cmp > 0) { lo = m + 1; else { return m; return lo; public void put ( Key key, Value val ) { if ( val == null ) { delete ( key ); return ; int i = rank ( key ); if (i < N && keys [i]. compareto ( key ) == 0) { vals [i] = val ; return ; if ( N == keys. length ) { resize (2 * keys. length ); for ( int j = N; j > i; j - -) { keys [j] = keys [j - 1]; vals [j] = vals [j - 1]; keys [i] = key ; vals [i] = val ; N ++; 12 / 15

Implementations public void delete ( Key key ) { if ( isempty ()) { return ; int i = rank ( key ); if (i == N keys [i]. compareto ( key )!= 0) { return ; for ( int j = i; j < N - 1; j ++) { keys [j] = keys [j + 1]; vals [j] = vals [j + 1]; N - -; keys [N] = null ; vals [N] = null ; if ( N > 0 && N == keys. length / 4) { resize ( keys. length / 2); public void deletemin () { if ( isempty ()) { throw new NoSuchElementException (); delete ( min ()); public void deletemax () { if ( isempty ()) { throw new NoSuchElementException (); delete ( max ()); public Key min () { if ( isempty ()) { return null ; return keys [0]; public Key max () { if ( isempty ()) { return null ; return keys [ N - 1]; 13 / 15

Implementations public Key floor ( Key key ) { int i = rank ( key ); if (i < N && key. compareto ( keys [i]) == 0) { return keys [i]; if ( i == 0) { return null ; return keys [ i - 1]; public Key ceiling ( Key key ) { int i = rank ( key ); if (i == N) { return null ; return keys [i]; public int size ( Key lo, Key hi) { if ( lo. compareto ( hi) > 0) { return 0; if ( contains (hi )) { return rank (hi) - rank (lo) + 1; return rank (hi) - rank (lo ); public Iterable <Key > keys () { return keys ( min (), max ()); public Iterable < Key > keys ( Key lo, Key hi) { Queue < Key > queue = new Queue < Key >(); if ( lo == null && hi == null ) { return queue ; if (lo == null ) { throw new NullPointerException (); if (hi == null ) { throw new NullPointerException (); if ( lo. compareto ( hi) > 0) { return queue ; for ( int i = rank (lo ); i < rank (hi ); i ++) { queue. enqueue ( keys [i ]); if ( contains (hi )) { queue. enqueue ( keys [ rank (hi )]); return queue ; 14 / 15

Performance Characteristics When studying symbol table implementations, we count comparisons, and in (rare) cases where comparisons are not in the inner loop, we count array accesses Cost summary for symbol table implementations operation unordered linked list ordered array search (worst case) N lg N insert (worst case) N 2N search (average case) N/2 lg N insert (average case) N N efficiently supports ordered operations? no yes 15 / 15