22.01.2015 Views

Université de Nice-Sophia Antipolis POLYTECH CiP2 2010–2011 ...

Université de Nice-Sophia Antipolis POLYTECH CiP2 2010–2011 ...

Université de Nice-Sophia Antipolis POLYTECH CiP2 2010–2011 ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Université <strong>de</strong> <strong>Nice</strong>-<strong>Sophia</strong> <strong>Antipolis</strong><br />

<strong>POLYTECH</strong><br />

<strong>CiP2</strong> 2010–2011<br />

Examen <strong>de</strong> Algorithmique et Programmation<br />

Durée : 1h30 minutes<br />

Aucun document autorisé Note : la qualité <strong>de</strong>s commentaires, avec notamment la présence<br />

d’affirmations significatives, ainsi que les noms donnés aux variables, l’emploi à<br />

bon escient <strong>de</strong>s majuscules et la bonne in<strong>de</strong>ntation rentreront pour une part importante<br />

dans l’appréciation du travail.<br />

◮ 1. Écrivez la définition (ensembles, fonctions et axiomes) du type abstraitPile comme<br />

vu en TD.<br />

Question sur 3 points<br />

Ensembles :<br />

Pile utiliseE et booléen<br />

pilevi<strong>de</strong>∈Pile<br />

Fonctions :<br />

Axiomes :<br />

empiler : Pile×E → Pile<br />

dépiler : Pile → Pile<br />

sommet : Pile → E<br />

est-vi<strong>de</strong> : Pile → booléen<br />

1. ∀p∈Pile,∀e∈E<br />

2. est-vi<strong>de</strong>(pilevi<strong>de</strong>)=vrai<br />

3. est-vi<strong>de</strong>(empiler(p,e))=faux<br />

4. dépiler(empiler(p,e))= p<br />

5. sommet(empiler(p,e))=e<br />

6. ∄ p, p=dépiler(pilevi<strong>de</strong>)<br />

7. ∄e, e=sommet(pilevi<strong>de</strong>)<br />

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .<br />

◮ 2. Une classe ListeChaînée implémente les fonctions longueur, ième, supprimer et<br />

insérer d’une interface Liste comme vue en TD. Par héritage <strong>de</strong> ListeChaînée,<br />

écrivez la classe PileChaînée qui implémente une interface Pile.<br />

1


Question sur 3 points<br />

p u b l i c c l a s s PileChaînée extends ListeChaînée <br />

implements Pile <br />

{<br />

/**<br />

* Rôle : construit une pile vi<strong>de</strong>. Les éléments<br />

* <strong>de</strong> la pile pourront être <strong>de</strong> types différents<br />

*/<br />

p u b l i c PileChaînée () {<br />

super ();<br />

}<br />

/**<br />

* Rôle : teste si la pile est vi<strong>de</strong> ou non<br />

*<br />

* @return un boolean<br />

*/<br />

p u b l i c boolean estVi<strong>de</strong> () {<br />

return longueur () == 0;<br />

}<br />

/**<br />

* Rôle : retourne le sommet <strong>de</strong> la pile<br />

* @return un T<br />

* @exception PileVi<strong>de</strong>Exception si la pile<br />

* est vi<strong>de</strong><br />

*/<br />

p u b l i c T sommet () throws PileVi<strong>de</strong>Exception {<br />

i f ( estVi<strong>de</strong> ())<br />

throw new PileVi<strong>de</strong>Exception ();<br />

return ième (1);<br />

}<br />

/**<br />

* Rôle : empile un objet en sommet <strong>de</strong> pile<br />

* @param o T, est l’objet à empiler<br />

*/<br />

p u b l i c void empiler (T e) {<br />

insérer (1 ,e);<br />

}<br />

/**<br />

* Rôle : enlève l’élément en sommet <strong>de</strong> pile<br />

* @exception PileVi<strong>de</strong>Exception si la pile<br />

* est vi<strong>de</strong><br />

*/<br />

p u b l i c void dépiler () throws PileVi<strong>de</strong>Exception {<br />

i f ( estVi<strong>de</strong> ())<br />

throw new PileVi<strong>de</strong>Exception ();<br />

supprimer (1);<br />

}<br />

}<br />

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .<br />

◮ 3. La classe ArbreBinaireChaîné définit l’implémentation d’arbres binaires comme<br />

vus en TD. Deux arbres binaires a et b sont miroirs s’ils possè<strong>de</strong>nt la même racine, si le<br />

sous-arbre gauche <strong>de</strong> a est le miroir du sous-arbre droit <strong>de</strong> b et si le sous-arbre droit <strong>de</strong><br />

2


a est le miroir du sous-arbre gauche <strong>de</strong> b. Ajoutez à la classe ArbreBinaireChaîné<br />

la métho<strong>de</strong> créerMiroir qui retourne l’arbre miroir <strong>de</strong> l’arbre courant this.<br />

Question sur 3 points<br />

/**<br />

* Rôle : retourne un arbre binaire miroir <strong>de</strong> this<br />

*/<br />

p u b l i c ArbreBinaire créerMiroir () throws ArbreVi<strong>de</strong>Exception<br />

{<br />

return t h i s . estVi<strong>de</strong> () arbreVi<strong>de</strong> :<br />

// this non vi<strong>de</strong><br />

new ArbreBinaireChaîné ( t h i s . valeur () ,<br />

t h i s . créerMiroir ( t h i s . sad ()) , t h i s . créerMiroir ( t h i s . sag ()));<br />

}<br />

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .<br />

◮ 4. Un arbre binaire complet est un arbre dont les nœuds, qui ne sont pas <strong>de</strong>s feuilles,<br />

possè<strong>de</strong>nt toujours <strong>de</strong>ux sous-arbres. Écrivez la métho<strong>de</strong> estComplet qui retourne un<br />

booléen : vrai si l’arbre binaire courant est complet, et faux sinon.<br />

Question sur 4 points<br />

/**<br />

* Rôle : retourne vrai si l’arbre courant (this) est complet<br />

* et faux sinon. Note un arbre vi<strong>de</strong> est complet.<br />

*/<br />

p u b l i c boolean estComplet () throws ArbreVi<strong>de</strong>Exception {<br />

return estVi<strong>de</strong> () true : estComplet ( t h i s );<br />

}<br />

/**<br />

* Rôle : retourne vrai si l’arbre a complet<br />

* et faux sinon<br />

* Antécé<strong>de</strong>nt : a non vi<strong>de</strong><br />

*/<br />

p r i v a t e boolean estComplet ( ArbreBinaire a)<br />

throws ArbreVi<strong>de</strong>Exception<br />

{<br />

}<br />

assert !a. estVi<strong>de</strong> ();<br />

i f (a. sag (). estVi<strong>de</strong> () && a. sad (). estVi<strong>de</strong> ()) return true ;<br />

return a. sag (). estVi<strong>de</strong> () || a. sad (). estVi<strong>de</strong> () <br />

// un <strong>de</strong>s <strong>de</strong>ux arbres est vi<strong>de</strong> et pas l’autre<br />

f a l s e :<br />

// les 2 sous-arbres ne sont pas vi<strong>de</strong>s => poursuivre le test<br />

a. sag (). estComplet () && a. sad (). estComplet ();<br />

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .<br />

◮ 5. Quelle est les complexités théoriques moyennes, en termes <strong>de</strong> comparaisons et <strong>de</strong><br />

transferts, du tri par insertion séquentielle vu en TD Expliquez.<br />

3


Question sur 2 points<br />

Pour ce tri, il y a n−1 étapes, et dans le cas moyen, à chaque étape, si les clés<br />

sont réparties <strong>de</strong> façon équiprobable, il y a<br />

2 i comparaisons. Le nombre moyen <strong>de</strong><br />

comparaisons est donc ∑ n i=2 2 i = 1 4 (n2 + n−2), La complexité moyenne en terme <strong>de</strong><br />

comparaisons <strong>de</strong> ce tri est doncO(n 2 ).<br />

À chaque étape, le nombre <strong>de</strong> déplacements est égal au nombre <strong>de</strong> comparaisons<br />

plus un. En moyenne, il est <strong>de</strong> 1 4 (n2 + n+2). La complexité moyenne en terme <strong>de</strong><br />

transferts <strong>de</strong> ce tri est aussiO(n 2 )<br />

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .<br />

◮ 6. Écrivez la métho<strong>de</strong> TriDicho qui trie une liste générique par insertion avec recherche<br />

dichotomique du rang d’insertion.<br />

Question sur 4 points<br />

/**<br />

* Rôle : trie par insertion dichotomique la liste courante (this)<br />

* Antécé<strong>de</strong>nt : c comparateur <strong>de</strong>s clés <strong>de</strong>s éléments <strong>de</strong> la liste courante<br />

*/<br />

p u b l i c void triDicho ( Comparateur c) {<br />

f o r ( i n t i =2; i clé(ième(milieu))<br />

gauche = milieu +1;<br />

}<br />

// gauche est le rang d’insertion<br />

// décaler tous les éléments <strong>de</strong>puis ce rang jusqu’à i-1<br />

f o r ( i n t j=i -1; j >= gauche ; j - -)<br />

affecter (j+1 , ième (j ));<br />

// mettre l’élément clé au rang gauche<br />

affecter ( gauche ,x);<br />

}<br />

}<br />

}<br />

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .<br />

◮ 7. Modifiez la métho<strong>de</strong> <strong>de</strong> tri rapi<strong>de</strong> (quicksort) afin qu’elle retourne la valeur médiane<br />

d’une liste générique. On rappelle que la valeur médiane divise une liste en <strong>de</strong>ux souslistes<br />

<strong>de</strong> même taille telles que tous les éléments <strong>de</strong> la première sont inférieurs ou égaux<br />

à la médiane, et tous éléments <strong>de</strong> la secon<strong>de</strong> lui sont supérieurs ou égaux.<br />

4


Question sur 3 points<br />

/**<br />

* Rôle : recherche <strong>de</strong> la médiane <strong>de</strong> la liste courante (this)<br />

* La métho<strong>de</strong> est basée sur l’algorithme du tri rapi<strong>de</strong><br />

* Antécé<strong>de</strong>nt : c comparateur <strong>de</strong>s clés <strong>de</strong>s éléments<br />

* <strong>de</strong> la liste courante<br />

*/<br />

p u b l i c Élément médiane ( Comparateur c) {<br />

return médiane ( t h i s , c, (1+ t h i s . longueur ())/2 , 1, t h i s . longueur ());<br />

}<br />

p r i v a t e Élément médiane ( Liste < Élément > l, Comparateur c,<br />

i n t k, i n t gauche , i n t droite ) {<br />

i n t i= gauche , j= droite ;<br />

C pivot = l. ième (k). clé ();<br />

do {<br />

while (c. inférieur (l. ième (i). clé () , pivot )) i ++;<br />

while (c. supérieur (l. ième (j). clé () , pivot )) j - -;<br />

i f (i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!