-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlinear-struct-fr.tex
More file actions
408 lines (367 loc) · 10.9 KB
/
linear-struct-fr.tex
File metadata and controls
408 lines (367 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
\documentclass[12pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[frenchb]{babel}
\usepackage{listings}
\usepackage{tabu}
\usepackage{booktabs}
\beamertemplatenavigationsymbolsempty
\AtBeginSection[]
{
\begin{frame}
\frametitle{Table des matières}
\tableofcontents[currentsection]
\end{frame}
}
\newcommand{\gray}{\textcolor{gray}}
\newcommand{\bigoh}[1]{\mathcal{O}\left(#1\right)}
\newcommand{\constant}{\bigoh{1}}
\newcommand{\linear}{\bigoh{n}}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\lstset{ %
backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
basicstyle=\tiny, % the size of the fonts that are used for the code
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
commentstyle=\color{mygreen}, % comment style
deletekeywords={...}, % if you want to delete keywords from the given language
escapeinside={\%*}{*)}, % if you want to add LaTeX within your code
extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
frame=single, % adds a frame around the code
keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
keywordstyle=\color{blue}, % keyword style
language=C++, % the language of the code
morekeywords={*,...}, % if you want to add more keywords to the set
numbers=left, % where to put the line-numbers; possible values are (none, left, right)
numbersep=5pt, % how far the line-numbers are from the code
numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
showstringspaces=false, % underline spaces within strings only
showtabs=false, % show tabs within strings adding particular underscores
stepnumber=1, % the step between two line-numbers. If it's 1, each line will be numbered
stringstyle=\color{mymauve}, % string literal style
tabsize=2 % sets default tabsize to 2 spaces
}
\title{Structures de données linéaires}
\subtitle{Tableaux, vecteurs, listes chaînées}
\author{Training beOI}
\institute{\includegraphics[height=12em]{../share/beoi-logo}}
\begin{document}
\frame{\titlepage}
\section{Tableaux et variantes}
\begin{frame}[fragile]
\frametitle{Tableau}
\begin{lstlisting}
#define MAX_N 10000
int tab[MAX_N];
int main() {
tab[1234] = 100;
tab[1234]; // 100
tab[5678]; // 0
}
\end{lstlisting}
\begin{itemize}
\item Taille fixée à la compilation
\item Accès à un élément arbitraire: $\constant$
\item Astuce : en-dehors d'une fonction, initialisé à zéro
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Bitset}
C++ : \texttt{bitset} \\
Java : \texttt{BitSet}
\begin{lstlisting}
bitset<MAX_N> tab; // bool tab[MAX_N];
tab[1234] = true;
bitset<4> b1(string("1100")),
b2(string("0101"));
b1 | b2; // 1101
b1 & b2; // 0100
b1 >> 1; // 0110
\end{lstlisting}
\begin{itemize}
\item Comme un tableau de booléens
\item 8x plus compact
\item Opérations bit-à-bit 64x plus rapides
\item Voir manuel pour la liste des opérations
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Tableau dynamique: fonctionnement}
{\setlength{\parskip}{.9em}
Si plus de place, multiplier par 2
\def\arraystretch{1.3}
\begin{tabu} to .175\textwidth {|X[c]|X[c]|}
\hline
1 & \\
\hline
\end{tabu}
\hfill Capacité = 2
\begin{tabu} to .175\textwidth {|X[c]|X[c]|}
\hline
1 & 2 \\
\hline
\end{tabu}
\begin{tabu} to .35\textwidth {|X[c]|X[c]|X[c]|X[c]|}
\hline
1 & 2 & 3 & \\
\hline
\end{tabu}
\hfill Capacité = 4
\begin{tabu} to .35\textwidth {|X[c]|X[c]|X[c]|X[c]|}
\hline
1 & 2 & 3 & 4 \\
\hline
\end{tabu}
\begin{tabu} to .7\textwidth {|X[c]|X[c]|X[c]|X[c]|X[c]|X[c]|X[c]|X[c]|}
\hline
1 & 2 & 3 & 4 & 5 & & & \\
\hline
\end{tabu}
\hfill Capacité = 8} %\setlength
\end{frame}
\begin{frame}[fragile]
\frametitle{Tableau dynamique : en pratique}
C++ : \texttt{vector} \\
Java : \texttt{ArrayList<E>}
\begin{lstlisting}
vector<int> vec(8, -1); // initialize to -1
vec[5] += vec[2]; // -2
vec.push_back(5);
vec.push_back(19);
vec.pop_back();
vec.back(); // 5
\end{lstlisting}
\begin{itemize}
\item Taille augmente et diminue
\item Accès à un élément arbitraire: $\constant$
\item Ajout/suppression d'un élément \textbf{à la fin}: $\constant$
\item Ajout/suppression autre part: $\linear$
\end{itemize}
\end{frame}
\section{Listes chaînées}
\begin{frame}[fragile]
\frametitle{Liste chaînée: concept}
Des nœuds reliés par des liens (pointeurs)
\begin{figure}
\centering
\includegraphics[width=.8\textwidth]{img/singly-linked}
\end{figure}
\begin{itemize}
\item Chaque nœud sait où est le prochain
\item Les nœuds ne sont plus côte à côte
\end{itemize}
\begin{lstlisting}
struct Node {
int value;
Node *next; // link (pointer)
};
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Liste chaînée: parcours}
Commencer au premier nœud et suivre les liens
\begin{figure}
\centering
\includegraphics[width=.6\textwidth]{img/singly-linked}
\end{figure}
Dans le dernier nœud le lien vaut NULL:
\begin{lstlisting}
Node *cur = start; // always keep the first node!
while (cur != NULL) {
cur->value; // access value
cur = cur->next; // switch pointer to next
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Liste chaînée: ajout}
Seulement deux liens à changer
\begin{figure}
\centering
\includegraphics[width=.9\textwidth]{img/insert-node}
\end{figure}
\begin{lstlisting}
void insertAfter(Node *node, Node *new_node) {
new_node->next = node->next;
node->next = new_node;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Liste chaînée : suppression}
Changer le lien et supprimer
\begin{figure}
\centering
\includegraphics[width=.6\textwidth]{img/remove-node}
\end{figure}
\begin{lstlisting}
void removeAfter(Node *node) {
Node *toRemove = node->next;
node->next = node->next->next; // bypass
delete toRemove;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Liste chaînée : limitations}
\begin{figure}
\centering
\includegraphics[width=.8\textwidth]{img/singly-linked}
\end{figure}
Avec une liste (simplement) chaînée :
\begin{itemize}
\item Ajout/suppression au début: $\constant$
\item Ajout/suppression à une position \textbf{donnée}: $\constant$
\end{itemize}
Si on retient la fin aussi :
\begin{itemize}
\item Ajout à la fin: $\constant$
\item Suppression à la fin: pas possible, $\linear$
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Liste doublement chaînée}
Des liens dans les deux sens!
\begin{figure}
\centering
\includegraphics[width=.8\textwidth]{img/doubly-linked}
\end{figure}
\begin{itemize}
\item Parcours dans les deux sens
\item Suppression à la fin en $\constant$
\item Un peu plus lourd
\end{itemize}
\begin{lstlisting}
struct Node {
int value;
Node *prev, *next; // two pointers
};
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Listes chaînée: en pratique}
C++ : \texttt{list} \\
Java : \texttt{LinkedList<E>}
\begin{lstlisting}
list<int> l;
list<int>::iterator it;
l.push_back(3); // 3
it = l.begin(); // ^ points to 3
l.push_back(4); // 3 4
l.push_front(1); // 1 3 4
l.insert(it, 2); // 1 2 3 4 (inserts before 3)
l.pop_front(); // 2 3 4
l.pop_back(); // 2 3
\end{lstlisting}
\begin{itemize}
\item Les \texttt{list<>} sont doublement chaînées
\item Retenir les positions avec des \texttt{iterator}
\item Tout en $\constant$
\end{itemize}
\end{frame}
\section{File et pile}
\begin{frame}
\frametitle{File : concept}
\begin{itemize}
\item Faire la file dans un magasin
\item On ajoute à la fin, on enlève au début
\item Premier arrivé premier servi (First In First Out)
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=.6\textwidth]{img/queue}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{File : en pratique}
C++ : \texttt{queue} \\
Java : \texttt{Queue<E>}
\begin{itemize}
\item Ajouter à la fin, enlever au début $\Rightarrow$ liste chaînée
\item Tout en $\constant$
\end{itemize}
\begin{lstlisting}
queue<int> q;
q.push(1);
q.push(2);
q.front(); // 1
q.pop();
q.front(); // 2
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Pile : concept}
\begin{itemize}
\item Pile de crêpes
\item On ajoute au-dessus, on enlève au-dessus
\item Dernière cuite première mangée (Last In First Out)
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=.5\textwidth]{img/stack}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Pile : en pratique}
C++ : \texttt{stack} \\
Java : \texttt{Stack<E>}
\begin{itemize}
\item Ajouter et enlever à la fin $\Rightarrow$ liste chaînée \emph{ou vecteur}
\item Tout en $\constant$
\end{itemize}
\begin{lstlisting}
stack<int> q;
q.push(1);
q.push(2);
q.top(); // 2
q.pop();
q.top(); // 1
\end{lstlisting}
\end{frame}
\section{Choisir la bonne structure}
\begin{frame}
\frametitle{Choix: structures spéciales}
Structures pour besoins spéciaux:
\begin{itemize}
\item Ajoute d'un côté et on enlève de l'autre $\Rightarrow$ \textbf{file}
\item Enlève et ajoute d'un même côté $\Rightarrow$ \textbf{pile}
\item Booléens, opérations spéciales (et, ou, shift, ...) $\Rightarrow$ \textbf{bitset}
\end{itemize}
~
Sinon, voir slide suivante!
\end{frame}
\begin{frame}
\frametitle{Choix: tableaux, vecteurs, listes chaînées}
``Ajout'' = ajout ou suppression
\begin{center}
\begin{tabu}{l|cccc}
\toprule
Structure & Indexation & Ajout fin & Ajout milieu \\
\midrule
Tableau & $\constant$ & $\linear$ & $\linear$ \\
Vecteur & $\constant$ & $\constant$ & $\linear$ \\
Liste chaînée & $\linear$ & $\constant$ & $\constant$ \\
\bottomrule
\end{tabu}
\end{center}
\begin{itemize}
\item Ajout au milieu nécessaire (rare) $\Rightarrow$ \textbf{liste chaînée}
\item Taille maximale inconnue $\Rightarrow$ \textbf{vecteur}
\item Tous les autres cas $\Rightarrow$ \textbf{tableau} (plus rapide)
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Source des figures}
\begin{itemize}
\item \url{https://commons.wikimedia.org/wiki/File:Singly-linked-list.svg}
\item \url{https://commons.wikimedia.org/wiki/File:CPT-LinkedLists-addingnode.svg}
\item \url{https://en.wikipedia.org/wiki/File:CPT-LinkedLists-deletingnode.svg}
\item \url{https://en.wikipedia.org/wiki/File:Doubly-linked-list.svg}
\item \url{https://en.wikipedia.org/wiki/File:Data_Queue.svg}
\item \url{https://en.wikipedia.org/wiki/File:Data_stack.svg}
\end{itemize}
\end{frame}
\end{document}