05 - Dictionaries
05 - Dictionaries
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
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
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;
...
...
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;