30.05.2013 Views

Appunti per il modulo di algoritmi e strutture dati - Sezione di ...

Appunti per il modulo di algoritmi e strutture dati - Sezione di ...

Appunti per il modulo di algoritmi e strutture dati - Sezione di ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Proposizione 6.2<br />

Un albero binario quasi b<strong>il</strong>anciato non vuoto <strong>di</strong> livello k ha un numero <strong>di</strong> no<strong>di</strong> compreso fra 2 k e (2 (k+1) −1)<br />

e un numero <strong>di</strong> foglie compreso fra 2 (k−1) e 2 k .<br />

Proposizione 6.3<br />

In un albero binario pienamente binario <strong>il</strong> numero <strong>di</strong> no<strong>di</strong> interni è uguale al numero <strong>di</strong> foglie meno 1.<br />

6.2 Esempi <strong>di</strong> programmi su alberi binari<br />

Essendo gli alberi binari una struttura <strong>dati</strong> ricorsiva, analogamente alle liste, è conveniente progettare i<br />

programmi che lavorano su alberi binari facendo corrispondere <strong>il</strong> più possib<strong>il</strong>e la struttura del programma<br />

ricorsivo a quella della definizione, come è stato fatto <strong>per</strong> le liste: i casi non ricorsivi comprenderanno<br />

l’albero binario vuoto, gli altri un nodo con due sottoalberi. Le seguenti funzioni contano rispettivamente<br />

<strong>il</strong> numero <strong>di</strong> no<strong>di</strong> e <strong>il</strong> numero <strong>di</strong> foglie in unalbero binario:<br />

int nodes ( Node * tree ) {<br />

if (! tree ) return 0;<br />

return 1+ nodes (tree -> left )+ nodes (tree -> right );<br />

}<br />

int leaves ( Node * tree ) {<br />

if (! tree ) return 0;<br />

if ( !tree -> left && !tree -> right ) return 1;<br />

return leaves (tree -> left )+ leaves (tree -> right );<br />

}<br />

La funzione seguente cerca un’etichetta in un albero binario e restituisce <strong>il</strong> puntatore al nodo che la<br />

contiene:<br />

Node * findNode ( LabelType n, Node * tree ) {<br />

if (! tree ) return NULL ;<br />

if (tree -> label == n) return tree ;<br />

Node * a= findNode (n, tree -> left );<br />

if (a) return a;<br />

else return findNode (n, tree -> right );<br />

}<br />

La funzione seguente cancella un albero binario restituendo un puntatore nullo:<br />

void delTree ( Node * & tree ) {<br />

if ( tree ) {<br />

delTree (tree -> left );<br />

delTree (tree -> right );<br />

delete tree ; tree = NULL ;<br />

}<br />

}<br />

La funzione seguente inserisce un nuovo nodo in un albero binario. La funzione restituisce 1 se<br />

l’inserimento ha avuto successo e 0 altrimenti. La funzione inserisce una foglia con etichetta son come<br />

figlio del nodo con etichetta father, in modo che sia <strong>il</strong> figlio sinistro o destro a seconda che c=’l’ o c=’r’;<br />

se father non compare nell’albero o ha gia’ un figlio in quella posizione, non mo<strong>di</strong>fica l’albero. Se l’albero<br />

e’ vuoto, inserisce son come ra<strong>di</strong>ce.<br />

32

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

Saved successfully!

Ooh no, something went wrong!