

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


























The interface describes the operations, but says nothing at all about how they are implemented
public interface Stack { public void push(Object x); public Object pop(); public Object peek(); public boolean isEmpty(); public void clear(); }
Operations: void insert(Object element); boolean contains(Object element); void remove(Object element); boolean isEmpty(); void clear(); for(Object o: mySet) { ... }
Wide use within other algorithms
A “set” with duplicates is sometimes called a multiset or bag
Operations: void insert(Object key, Object value); void update(Object key, Object value); Object find(Object key); void remove(Object key); boolean isEmpty(); void clear();
Symbol tables Wide use within other algorithms
From interface to implementation
class ArrayStack implements Stack { private Object[] array; //Array that holds the Stack private int index = 0; //First empty slot in Stack public ArrayStack(int maxSize) { array = new Object[maxSize]; } public void push(Object x) { array[index++] = x; } public Object pop() { return array[--index]; } public Object peek() { return array[index-1]; } public boolean isEmpty() { return index == 0; } public void clear() { index = 0; } } max- 1 3 2 1 0 4 index O(1) worst- case time for each operation Question: What can go wrong?
Goal: Design a Dictionary (aka Map )
n is the number of items currently held in the dictionary
U = all legal identifiers
h converts each letter to a number, then compute a function of these numbers Best hash functions are highly random This is connected to cryptography We’ll return to this in a few minutes
Analysis for Hashing with Chaining
Analysis of Table Doubling, Cont’d
Worst-case insertion time of O(n) is definitely achieved (but rarely) Thus, not appropriate for time critical operations