• Non ci sono risultati.

PROGRAMMAZIONE  2   3a.  Verso  Java

N/A
N/A
Protected

Academic year: 2022

Condividi "PROGRAMMAZIONE  2   3a.  Verso  Java"

Copied!
20
0
0

Testo completo

(1)

PROGRAMMAZIONE  2   3a.  Verso  Java  

AA  2014-­‐2015  

1  

(2)

Una  implementazione  in  C  degli  

alberi  binari  di  ricerca  

(3)

3  

un  albero  binario  di  ricerca  

typedef  struct  _nodo  {    int  key;  

 struct  _nodo  *le?;  

 struct  _nodo  *right;  

}  nodo;  

10  

4   15  

4  

(4)

4  

un  albero  binario  di  ricerca  

10  

4   15  

4  

6  

6  

(5)

5  

Inserimento  iteraFvo  

10  

4   15  

4  

nodo*  inserisci(nodo  *t,  int  key)  {  

 nodo*  new  =  (nodo  *)  malloc(sizeof(nodo));  

 new-­‐>key  =  key;  

 new-­‐>right  =  new-­‐>le?  =  NULL;  

 if  (t  ==  NULL)  {  return  new;  }      nodo*  parent;  

 nodo*  current  =  t;    

 while  (current  !=  NULL)  {      parent  =  current;  

   if  (current-­‐>key  <  key)  current  =  current-­‐>right;  

   else  current  =  current-­‐>le?;  

 }  

 if  (parent-­‐>key  <  key)  parent-­‐>right  =  new;  

 else  parent-­‐>le?  =  new;  

 return  t;  

}  

(6)

6  

Inserimento  ricorsivo  

10  

4   15  

4  

nodo*  inserisci(nodo  *t,  int  key)  {      if  (t  ==  NULL)  {  

       nodo*  new  =  (nodo  *)  malloc(sizeof(nodo));  

       new-­‐>key  =  key;  

       new-­‐>le?  =  NULL;  

         new-­‐>right  =  NULL;  

         return  new;            

 }        

   if  (t-­‐>key  <  key)    

     t-­‐>right  =  inserisci(t-­‐>right,  key);  

   else    

     t-­‐>le?  =  inserisci(t-­‐>le?,  key);  

   return  t;  

}  

(7)

7  

ricerca  iteraFva  

10  

4   15  

4  

int  cerca(albero  t,  int  key)  {    int  depth  =  0;    

 struct  Nodo  *current  =  t;    

 while  (current  !=  NULL)  {  

   if  (key  ==  current-­‐>key)  return  depth;  

   if  (current-­‐>key  <  key)    

     current  =  current-­‐>right;  

   else    

     current  =  current-­‐>le?;  

   depth++;  

 }      return  -­‐1;  

}  

(8)

8  

ricerca  ricorsiva  

10  

4   15  

4  

int  cerca(albero  t,  int  key)  {    if(  t  ==  NULL)  return  -­‐1;  

 if  (t-­‐>key  ==  key)  return  0;  

 int  found  =  -­‐1;    

 if(  t-­‐>key  <  key)    

   found  =  cerca(t-­‐>right,key);  

   else    

   found  =  cerca(t-­‐>le?,key);  

     

 if  (found  >=  0)  return  1+found;  

   else  return  -­‐1;  

}  

(9)

scenario:  ABR  modulo  condiviso  

ABR

Programmatori  

condividono  

la  stru[ura  ABR  

(10)

:  

G_node  =  inserisci(t,  2);  

:  

G_node  >key    =  18;  

:      

7  

3   75   2  

7  

3   75  

Goofy’s  code  

(11)

:  

G_node  =  inserisci(t,  2);  

:  

G_node  >key    =  18;  

:      

7  

3   75   18  

Goofy’s  code  

7  

3   75  

2   Viene  distruPa  

l’invariante  di  

rappresentazione  

Come  mai?  

(12)

12  

invarianF  e  rappresentazione    

10  

4   15  

18  

Invariante:  (nodo  -­‐>le1)  -­‐>key    <nodo-­‐>key  <  (nodo  -­‐>right)-­‐>key Invariante:  (nodo  -­‐>le1)  -­‐>key    <nodo-­‐>key  <  (nodo  -­‐>right)-­‐>key Invariante:  (nodo  -­‐>le1)  -­‐>key    <nodo-­‐>key  <  (nodo  -­‐>right)-­‐>key Invariante:  (nodo  -­‐>le1)  -­‐>key    <nodo-­‐>key  <  (nodo  -­‐>right)-­‐>key Invariante:  (nodo  -­‐>le1)  -­‐>key    <nodo-­‐>key  <  (nodo  -­‐>right)-­‐>key          

( nodo  -­‐>le*)-­‐>key    <  nodo-­‐>key    &&  

nodo-­‐>key  <  (nodo  -­‐>right)-­‐>key  

typedef  struct  _nodo  {    int  key;  

 struct  _nodo  *le?;  

 struct  _nodo  *right;  

}  nodo;  

PUBBLICA:  visibile  a  tuV  

RAPPRESENTAZIONE  

(13)

13  

Scenario  2:  ABR  e  dizionario  

  ABR  per  implementare  un  dizionario  

o   l’estensione  richiede  di  avere  una  chiave   per  effe[uare  la  ricerca  e  una  stringa  per   codificare  l ’ informazione  

  Riuso  del  codice:  uFlizziamo  il  vecchio   modulo  aggiungendo  le  opportune  

modifiche  per  realizzare  il  dizionario  

(14)

14  

Scenario  2:  ABR  e  dizionario  

typedef  struct  _nodo  {    int  key;  

 string  info;  

 struct  _nodo  *le?;  

 struct  _nodo  *right;  

}  nodo;  

L’invariante  è  ora  una  proprietà  delle  chiavi  

(15)

15  

ricerca…  

nodo*  inserisci(nodo  *t,  int  key,  string  info)  {      if  (t  ==  NULL)  {  

   nodo*  new  =  (nodo  *)  malloc(sizeof(nodo));  

   new-­‐>key  =  key;  

   new-­‐>info  =  info;  

   new-­‐>le?  =  NULL;  

       new-­‐>right  =  NULL;  

       return  new;            

 }        

   if  (t-­‐>key  <  key)    

     t-­‐>right  =  inserisci(t-­‐>right,  key,  info);  

   else    

     t-­‐>le?  =  inserisci(t-­‐>le?,  key,  info);  

   return  t;  

}  

 

   

(16)

16  

valutazione  della  soluzione  

  Non  abbiamo  strumenF  linguisFci   (ovvero  previsF  nel  linguaggio)  per  

estendere  il  codice  alle  nuove  esigenze   Cut&Paste  Reuse    

o   codice  debole  

o   difficile  da  mantenere  

o   difficile  evitare  errori  

(17)

17  

sull’astrazione  

  “AbstracFon  arises  from  a  recogniFon  of  

similariFes  between  certain  objects,  situaFons,   or  processes  in  the  real  world,  and  the  decision   to  concentrate  upon  those  similariFes  and  to   ignore  for  the  Fme  being  the  differences.”  

  Astrazione:  separare  le  funzionalità  offerte  dalla   loro  implementazione  

[Tony  Hoare]  

(18)

18  

funzionalità  vs.  implementazione  

(19)

19  

encapsulaFon  

  “EncapsulaFon  is  the  process  of  compartmentalizing   the  elements  of  an  abstracFon  that  consFtute  its  

structure  and  behavior;  encapsulaFon  serves  to  

separate  the  contractual  interface  of  an  abstracFon  

and  its  implementaFon.”   [Grady  Booch]  

(20)

20  

sull’erediterietà  

Passare  dal  Cut&Paste  reuse  a  un  metodo  

supportato  da  strumenF  linguisFci  nel  quale  una   nuova  funzionalità  è  o[enuta  estendendo  

esplicitamente  del  codice  già  implementato     La  nuova  implementazione  estende  la  vecchia  

con  funzionalità  aggiunFve  ma  conservando  

quelle  esistenF  

Riferimenti

Documenti correlati

successo delle applet Java: programmi Java eseguibili dentro al browser Web (la JVM installata come plug-in del browser) Con il tempo altre tecnologie soppiantano Java nell’ambito

•Se un nuovo tipo di eccezione estende la classe Exception, l’eccezione è checked (eccezioni che si riferiscono a condizioni recuperabili e che quindi possono essere gestite

Eccezioni unchecked: il metodo non deve necessariamente prevedere il codice di gestione. ArrayExceptionExampleException in

–  come i valori del &gt;po astra&lt;o sono implementa&gt; in termini di altri &gt;pi. •  &gt;pi primi&gt;vi o

•  Questo tipi di eccezioni a run-time sono denominate unchecked exception. •  Domanda: è possibile prevedere meccanisni linguistici che permettono di

•  Se un nuovo 9po di eccezione estende la classe Excep9on, l’eccezione è checked. •  Se un nuovo 9po di eccezione estende la classe Run9meExcep9on, l’eccezione

– possiamo astrarre dalla specifica computazione descritta nel corpo della procedura, associando a ogni procedura una specifica (la semantica “intesa” della procedura). –

Anche se Type2 è un soSo-;po di Type3, se i due ;pi sono diversi allora Type1&lt;Type2&gt; non è un soSo-;po di Type1&lt;Type3&gt;?. Formalmente: la nozione di soSo-;po usata in