0% found this document useful (0 votes)
10 views34 pages

05 - Dictionaries

introduzione ai Dizionari

Uploaded by

Sora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views34 pages

05 - Dictionaries

introduzione ai Dizionari

Uploaded by

Sora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Algoritmi e

Programmazione
DICTIONARIES
Goal
This lecture aims at presenting how dictionaries
are usually implemented.
Prerequisites
Lectures:
◦ Introduction to ADT
◦ Lists
◦ Sorting Algorithms
Outline
Introduction
Dictionaries as ADT
Implementations of Dictionaries
Outline
Introduction
Dictionaries as ADT
Implementations of Dictionaries
Introduction
The problem to be faced is to check if an element belong to a certain set, i.e., the search for an
element within a set
Introduction
Among the fields of each element x of the set, a key field can be identified, the content of which
characterizes the element itself:

x.data
x
x.key

An order relationship can be defined on the keys (partial, total or lexicographical)


Solution
Special ADTs are leveraged, namely:
◦ Priority queues: in the event that the search for a particular element is required, namely the one with
the minimum (or maximum) key
◦ Dictionaries: in the cases in which the search for any element of the set is required

In the cases in which do not exist keys that characterize the elements of the set, it is possible to
look at different ADT, whose discussion is beyond the scope of this course.
Priority queue
A priority queue is an abstract data-type similar to a
regular queue or stack data structure in which each
element additionally has a priority associated with it.
In a priority queue, an element with high priority is
served before an element with low priority.
The priority queues must support such operations:
◦ insert(element e, key k)
◦ findMin()
◦ removeMin()
◦ findMax()
◦ removeMax()
insert(x, Q)
This operation inserts element x in the queue Q according to its priority defined by x.key.
Returns:
◦ OK if the operation completed successfully
◦ ERROR otherwise
findMin(Q)
This operation returns the element in the queue with the lowest priority, without deleting
removeMin(Q)
This operation removes the element from the queue with the lowest priority.
findMax(Q)
This operation returns the highest priority queue element without deleting it
removeMax(Q)
This operation returns the element in the queue with the highest priority.
Outline
Introduction
Dictionaries as ADT
Implementations of Dictionaries
Dictionaries
They are generally classified according to:
◦ operations supported by the dictionary
◦ uniqueness of the key elements
Dictionaries
They are generally classified according to:
◦ operations supported by the dictionary
◦ uniqueness of the key elements
static: if insertion or deletion
of elements are not allowed
dynamic: if both deletion and
insertion are allowed
semi-dynamic: if insertion is
allowed, but not deletion
Dictionaries
They are generally classified according to:
◦ operations supported by the dictionary
◦ uniqueness of the key elements

unique keys: each element


has a different key from all the
others
multiple keys: elements can
be added by the same key
Dictionaries as ADT
The following operations are defined for the ADT dictionary:
◦ insert(x, D)
◦ delete(key, D)
◦ search(key, D)
◦ next(key, D)
◦ min(D)
insert(x, D)
Verifying if the key of the element x (x.key) is unique, then adds the element x to dictionary D,
Returns:
◦ OK if the operation completed successfully
◦ ERROR otherwise
delete(key, D)
Deletes from the dictionary D the element with the key provide as input
Returns:
◦ OK if the operation completed successfully
◦ ERROR otherwise
search(key, D)
Search in the dictionary D the record having the same key provided as input
Returns:
◦ the record x characterized by the searched key
◦ ERROR if the element does not exit
next(key, D)
Search in D for the element with the smallest x.key > key
Returns:
◦ The element with the smallest key among the x.keys > key
◦ ERROR if the element does not exist

It makes easy the sequential scanning of the elements of the set


min(D)
Returns the smallest keys memorized in D
Outline
Introduction
Dictionaries as ADT
Implementations of Dictionaries
Implementations of Dictionaries
Different solutions are used:
◦ Vectors
◦ Lists
◦ Hash Tables
◦ Trees
Implementations of Dictionaries
Size

Small array array


array
lists lists

Large trees trees


trees
hash table hash table

Static Semi-dynamic Dynamic Typologies


Implementations of Dictionaries
There are two main problems to be faced in the implementation of dictionaries:
◦ the choice of the most suitable data structure
◦ identification of the most suitable research method

The two aspects are strictly correlated, and they depend on:
◦ the research to be carried out
◦ the typology of the set of elements.
Implementations using array
Definition
/* defining an item of the dictionary */
typedef struct dict_el{
char* key;
char* value;
}dict_el;

...

/* defining the dictionary */


dict_el dictionary[SIZE];
Implementations using array
Insert
int insert (dict_el *dict, char* newkey, char* newval){
int keyExist = 0;

/* Check for unique key*/


for (int i = 0; i<SIZE; i++){
if(dict[i].key==newkey){
return (-1); // The key is not unique
}
}

...
Implementations using array
Insert
/* Search for the first free position */
for (int i = 0; i<SIZE; i++){
if(dict[i].key==NULL){
dict[i].key = newkey;
dict[i].value = newval;
return (0);
}
}
return (-1); // The dictionary is full
}
Implementations using array
Delete
int delete (dict_el *dict, char* keyToRemove){
for (int i = 0; i<SIZE; i++){
if(dict[i].key==keyToRemove){
dict[i].key = NULL;
dict[i].value = NULL;
return (0);
}
}
return (-1);
}
Implementations using array
Search
dict_el search(dict_el*dict, char *key){
dict_el elem;
elem.key=NULL;
elem.value=NULL;

for (int i = 0; i<SIZE; i++){


if(dict[i].key==key){
elem = dict[i];
return elem;
}
}
return elem;
}
Riferimenti
Queste slide sono una rielaborazione del materiale realizzato dal prof. P. Prinetto per il corso di
Algoritmo e Programmazione A.A. 2020/2021 presso il Politecnico di Torino.

You might also like