04 Structures
04 Structures
1. Introduction
2. Pile
4. Liste
5. Vecteur
6. File à priorité
Dans ce cours :
Principalement des ensembles dynamiques (dynamic sets), amenés à
croı̂tre, se rétrécir et à changer au cours du temps.
Les objets de ces ensembles comportent des attributs.
Un de ces attributs est une clé qui permet d’identifier l’objet, les
autres attributs sont la plupart du temps non pertinents pour
l’implémentation de la structure.
Certains ensembles supposent qu’il existe un ordre total entre les
clés.
Pop(S)
Stack-Empty(S)
Push(S, x)
11 ifif S.top
Stack-Empty(S)
== 0
1 if S.top == S.length 2 error “underflow”
2 error “overflow” 2 return true
else return
33 else S.top = S.top 1
false
3 S.top = S.top + 1 4 return S[top(S) + 1]
4 S[S.top] = x
ComplexitéPush(S,
en temps x) et en espace : O(1) Pop(S)
(Inconvénient : L’espace occupé
1 S.top = S.top + 1 ne dépend pas du nombre d’objets)
1 if Stack-Emp
2 S[S.top] = x 2 error “und
3 else S.top = S
Structures de données élémentaires 171
10.2 Linked lists 237
Rappel : liste simplement et doublement liée
prev key next
(a) L:head 9 16 4 1
(b) L:head 25 9 16 4 1
Structure de données composée d’une séquence d’éléments de liste.
(c)
Chaque élément x de25 la liste est9 composé16:
L:head 1
I d’un contenu utile x.data de type arbitraire (par exemple une clé),
I Figure 10.3 x.next
d’un pointeur (a) A doubly linked
vers list L representing
l’élément the dynamic
suivant dansset la 9; 16g. Each element in
séquence
f1; 4;
I the list is an object with attributes for the key and pointers (shown by arrows) to the next and previous
Doublement liée : d’une pointeur x.prev vers l’élément précédent
objects. The next attribute of the tail and the pre! attribute of the head are NIL , indicated by a diagonal
dans laslash. The attribute L: head points to the head. (b) Following the execution of L IST-I NSERT.L; x/,
séquence
where x: key D 25, the linked list has a new object with key 25 as the new head. This new object
Soit L unepoints
listeto the
liée
old head with key 9. (c) The result of the subsequent call L IST-D ELETE.L; x/, where x
points to the object with key 4.
I L.head pointe vers le premier élément de la liste
I Doublement
elements.liée : L.tail
In the pointe
remainder of this vers le we
section, dernier
assumeélément dewith
that the lists la which
liste we
Le dernier are working are
élément unsorted un
possède and doubly linked.x.next vide (noté NIL)
pointeur
Searching
Doublement liée : aLe
linked list
premier élément possède un pointeur x.prev
vide The procedure L IST-S EARCH .L; k/ finds the first element with key k in list L
by a simple linear search, returning a pointer to this element. If no object with
key k appears in the list, then the procedure returns NIL. For the linked list in
Figure 10.3(a), the call L IST-S EARCH .L; 4/ returns a pointer to the third element,
Structures de données élémentaires 172
ecution of L IST-I NSERT.L; x/,
heImplémentation
new head. This newd’uneobject pile à l’aide d’une liste liée
L IST-D ELETE.L; x/, where x
S est une liste simplement liée (S.head pointe vers le premier
à l’aide d’une liste liée
élément de la liste)
ément dewith
at the lists la which
liste we Stack-Empty(S)
liée (S.head pointe
9 vers le14premier 1 if S. head = = NIL
de (noté NIL) 2 return true
3 else return false
pointeur x.prev
ack-Empty(S)
4
Pop(S)
ment with key
if S.head k in list L
== NIL 1 if Stack-Empty(S)
ement. If no object with
Push(S,
return x)
true 2 error “underflow”
(b) Q 13 25 3 4 5 6 15 7 68 99 8
10 4
11 17
12
(b) Q 3 Q:tail
5 D 3 Q:head15D 76 9 8 4 17
1
2 3 D
Q:tail 4 3 5 Q:head
6 7 8 D 97 10 11 12
(c) Q 3 Q:tail
5 D3 15 6 D97 8 4 17
Q:head
1 2 3 4 5 6 7 8 9 10 11 12
Dequeue(Q) ! 15
(c) Q 13 25 3 4 5 6 15
7 68 99 810 411 17
12
Q:tail D 3 Q:head D 8
(c) Q 3 5 15 6 9 8 4 17
Q:tail D 3 Q:head D 8
Figure 10.2 A queue implemented using an array QŒ1 : : 12!. Queue elements appear o
lightly shaded D 3 (a) TheQ:head
positions.
Q:tail queue hasD5 8elements, in locations QŒ7 : : 11!. (b) The con
of the queue after the calls E NQUEUE.Q; 17/, E NQUEUE.Q; 3/, and E NQUEUE.Q; 5/
Figure 10.2 ofA the
configuration queue implemented
queue using
after the call an array
D EQUEUE .Q/QŒ1returns theQueue
: : 12!. elements
key value ap
15 forme
lightly shaded
Structures de données élémentaires positions. (a) The queue has 5 elements, in locations QŒ7 : : 11!. (b)
176 T
Enqueue et Dequeue
Enqueue(Q,x) Dequeue(Q)
1 Q[Q.tail] = x 1 x = Q[Q.head]
2 if Q.tail == Q.length 2 if Q.head == Q.length
3 Q.tail = 1 3 Q.head = 1
4 else Q.tail = Q.tail + 1 4 else Q.head = Q.head + 1
5 return x
remove-first(Q) remove-last(Q)
1 if (Q.size == 0) 1 if (Q.size == 0)
2 error 2 error
3 x = Q.head.next 3 x = Q.tail.prev
4 Q.head.next = Q.head.next.next 4 Q.tail.prev = Q.tail.prev .prev
5 Q.head.next.prev = Q.head 5 Q.tail.prev .next = Q.head
6 Q.size = Q.size 1 6 Q.size = Q.size 1
7 return x 7 return x
c0 + 2c0 + . . . + 2k 1
c0 = (2k 1)c0 .
Remove-At-Rank :
I O(n) pour une opération individuelle, où n est le nombre de
composantes du vecteur
I O(n2 ) pour n opérations de retrait en début de vecteur
I O(n) pour n opérations de retrait en fin de vecteur
Remarque : Un tableau circulaire permettrait d’améliorer l’efficacité
des opérations d’ajout et de retrait en début de vecteur.
Heap-Maximum(A) Heap-Extract-Max(A)
1 return A[1] 1 if A.heap-size < 1
2 error “heap underflow”
3 max = A[1]
4 A[1] = A[A.heap-size]
5 A.heap-size = A.heap-size 1
6 Max-heapify(A, 1) // reconstruit le tas
7 return max
8 7 9 3 16 14
8 9 10
2 4 1
Heap property
Structures de données élémentaires 191
Implémentation à l’aide d’un tas : insertion
Heap-Increase-Key(A, i, key )
1 if key < A[i]
2 error “new key is smaller than current key”
3 A[i] = key
4 while i > 1 and A[Parent(i)] < A[i]
5 swap(A[i], A[Parent(i)])
6 i = Parent(i)
Heap-Insert(A, key )
1 A.heap-size = A.heap-size + 1
2 A[A.heap-size] = 1
3 Heap-Increase-Key(A, A.heap-size, key )