Algoritmi e Strutture Dati Lezione 3 www.iet.unipi.it/a.virdis Antonio Virdis antonio.virdis@unipi.it 1
Sommario Alberi Binari di Ricerca Gestione Stringhe Progettazione Esercizi 2
3
4
Alberi Binari 10 5 9 11 6 8 0 5
Alberi Binari di Ricerca value <= > 6
bintree 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 struct Node int value; Node * left; Node * right; }; Node(int val): value(val), left(null), right(null) } class BinTree Node * root_; public: } BinTree() root_ = NULL; } Node * getroot() return root_; }
Insert 8 root 10 9 null 8
Insert 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 21 void insert( int val ) // inizializzo nuovo elemento // inizializzo variabili appoggio } // finchè non arrivo ad una foglia // aggiorno variabili // se <= // vado a sinistra // altrimenti // vado a destra } // se albero vuoto // aggiorno radice // decido se diventare figlio left o right INIZIALIZZAZIONE INDIVIDUO POSIZIONE INSERIMENTO 9
1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 21 void insert( int val ) Node * node = new Node(val); Node * pre = NULL; Node * post = root_; } while( post!= NULL ) pre = post; if( val <= post->value ) post = post->left; else post = post->right; } if( pre == NULL ) root_ = node; else if( val <= pre->value ) pre->left = node; else pre->right = node; Insert INIZIALIZZAZIONE INDIVIDUO POSIZIONE INSERIMENTO 10
min & MAX? 11
min & MAX? 12
Min/Max 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 Node * min() Node * temp = root_; while( temp->left!= NULL ) temp = temp->left; return temp; } Node * max() Node * temp = root_; while( temp->right!= NULL ) temp = temp->right; return temp; } 13
visite 1 2 3 10 5 9 11 inorder 6 8 0 14
In-Order 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 void inorder( Node * tree ) // se non sono in una foglia } } // visito verso left // stampo questo valore // visito verso right 15
In-Order 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 void inorder( Node * tree ) if(tree!=null) } } visitanodo(tree->left); cout << tree->value << "\t"; visitanodo (tree->right); 16
In-Order 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 void inorder( Node * tree ) if(tree!=null) } } inorder(tree->left); cout << tree->value << "\t"; inorder(tree->right); 1
Sort vs BinTree Albero alto: log(n) Inserimento: n * log(n) Sort/visita: n n + n l o g n ( n l o g n ) giusto? 18
Sort vs BinTree 10 Albero alto: log(n) Inserimento: n * log(n) Sort/visita: n 8 9 n + n l o g n ( n l o g n ) 6? 5 ( 2 n ) 4 19
Height 10 5 9 8 20
Altezza albero 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 int height( Node * tree ) int hleft; int hright; } if( tree == NULL ) return 0; hleft = height(tree->left); hright = height(tree->right); return 1 + max(hleft,hright); 21
Trova chiave Dato Un albero binario con valori distinti Un valore K Trovare Se il valore esiste 22
Search K=9 10 5 9 8 23
Search 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 bool search( Node * tree, int val ) if( tree == NULL ) return false; } bool found; if( tree->value == val ) return true; else if( val <= tree->value ) found = search( tree->left, val ); else found = search( tree->right, val ); return found; INDIVIDUO DIREZIONE 24
stringhe Creazione Concatenazione Compare Find aaa bbb ccc ddd ddd 25
Stringhe 1 2 3 4 5 6 8 9 10 11 12 13 14 #include <string> String parola = "liste"; String frase = "mi piacciono le liste"; String parola2 = "non "; String frase2 = parola2 + frase; frase.find(parola); // se fallisce -> string::npos parola.compare(parola2); http://www.cplusplus.com/reference/string/string/ 26
Esercizio Stringhe Input Una testo T formato da più parole Un insieme S di N parole Output: 1. Le parole di S contenute in T, ordinate per posizione in T (insieme R1) 2. Le parole di S non contenute in T, in ordine lessicografico (insieme R2) 2
Analisi Strutture Dati: Dove salvo T? Dove salvo S? Operazioni: Come ottengo gli elementi di 1? Come ottengo gli elementi di 2? Come ordino 1? Come ordino 2? 28
Analisi (2) string vector<string> T s1 s2 s3 s4 trovaint( ) R1 s2 s3 s1 s4 R2 compare1( ) compare2( ) s3 s2 s1 s4 29
Implementazione 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 // cerca stringhe di S dentro T void trovaint( ) } // implementa confronto per posizione bool compare1(string a, string b) } // implementa confronto lessicografico bool compare2(string a, string b) } int main() string T; vector <string> S, R1, R2; } // lettura T ed S trovaint( ); sort( R1.begin(), R1.end(), compare1 ) sort( R2.begin(), R2.end(), compare2 ) print(); 30
Gara Ad una gara partecipano N concorrenti Ogni concorrente e caratterizzato da: Un ID intero Un tempo di arrivo espresso in secondi Calcolare: Classifica K distacchi più ampi di utenti consecutivi 31
Analisi ID1, t1 ID2, t2 ID3, t3 ID4, t4 sort( ) struct int id; int tempo; } ID4, t4 ID1, t1 ID3, t3 ID2, t2 Stampa ID calcoladistacchi( ) t1-t4 t3-t1 t2-t3 sort() Stampa distacchi 32
Trovare altezza chiave K Input Una sequenza di N interi positivi Chiave K Output L altezza della chiave K dentro l albero (se esiste) 33
Search K=9 10 5 9 8 34
Altezza 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 18 19 20 int search( Node * tree, int val ) if( tree == NULL ) return 0; } int cont = 0; if( tree->value == val ) return 1; else if( val <= tree->value ) cont = search( tree->left, val ); else cont = search( tree->right, val ); if( cont!= 0 ) return cont+1; else return 0; SEARCH HEIGHT 35
Esercizi Esperimenti Sort vs Binary Tree Sort vs Min/Max Esercizi Visite pre- e post-order Esercizi di progettazione 36