Chapter 8: Dictionaries I

8.1 The Dictionary Abstract Data Type

8.1.1 The Dictionary ADT
Function
Input
Output
Description
size()
-
Integer
Return the number of items in D.
isEmpty()
-
Boolean
Test whether D is empty.
elements()
-
Iterator of objects (elements)
Returns the elements stored in D.
keys()
-
Iterator of objects (keys) Returns the keys stored in D.
find(k)
Object k (key) Position
If D contain an item with key equal to k, then return the position of such an item. If not, a null position is returned.
findAll(k)
Object k (key) Iterator of Positions Return an iterator of positions for all items whose key equals k.
insertItem(k,e)
Objects k (key) and e (element) -
Insert an item with element e and key k into D.
removeElement(k)
Object k (key)
-
Remove an item with key equal to k from D. An error condition occurs if D has no such item.
removeAllElements(k)
Object k (key) -
Remove the items with key equal to k from D.
Operation
Output
Dictionary
insertItem(5,A)
insertItem(7,B)
insertItem(2,C)
insertItem(8,D)
insertItem(2,E)
find(7)
find(4)
find(2)
findAll(2)
size()
removeElement(5)
removeElement(5)
removeAllElements(2)
find(2)
findAll(2)
-
-
-
-
-
p(B)
"null"
p(C) or p(E)
p(C),p(E)
5
-
"error"
-
"null"
"empty iterator"
{(5,A)}
{(5,A),(7,B)}
{(5,A),(7,B),(2,C)}
{(5,A),(7,B),(2,C),(8,D)}
{(5,A),(7,B),(2,C),(8,D),(2,E)}
{(5,A),(7,B),(2,C),(8,D),(2,E)}
{(5,A),(7,B),(2,C),(8,D),(2,E)}
{(5,A),(7,B),(2,C),(8,D),(2,E)}
{(5,A),(7,B),(2,C),(8,D),(2,E)}
{(5,A),(7,B),(2,C),(8,D),(2,E)}
{(7,B),(2,C),(8,D),(2,E)}
{(7,B),(2,C),(8,D),(2,E)}
{(7,B),(8,D)}
{(7,B),(8,D)}
{(7,B),(8,D)}
Operation
Input
Output
Description
element() -
Object (element) Return a reference to the element of the associated item.
key() -
Object (key) Return a constant reference to the key of the associated item.
isNull() -
Boolean
Determine if this is a null position.

8.1.2 Log Files

8.3 Ordered Dictionaries

In an ordered dictionary, we wish to perform the usual dictionary operations, but also maintain an order relation for the keys in our dictionary.

8.3.1 The Ordered Dictionary ADT

An ordered dictionary supports the following functions beyond those included in the general dictionary ADT (8.1.1):
8.3.2 Look-Up Tables
8.3.3 Binary Search

bsearch.cpp

Analysis of Binary Search

Comparing Simple Ordered Dictionary Implementations

Function
Log File
Look-Up Table
size(), isEmpty()
O(1)
O(1)
keys(), elements()
O(n)
O(n)
find(key)
O(n)
O(log n)
findAll(key)
Θ(n)
O(log n + s)
insertItem(key, element)
O(1)
O(n)
removeElement(key)
O(n)
O(n)
removeAllElements()
Θ(n) O(n)