|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Apr 2007
Città: Torino
Messaggi: 95
|
[C] ricerca in albero binario
Devo implementare una funzione tree find_name(tree T, char *vname) che cerca nell’albero T un nodo con campo name uguale a vname; se lo trova, ritorna il puntatore a tale nodo, altrimenti ritorna NULL.
L' albero deve avere questa struttura Codice:
typedef struct _node *tree;
struct _node {
char *name;
double val;
tree left, right;
};
Codice:
tree find_name(tree T, char *name){
if(T == NULL)
return NULL;
if((T->name)==name)
return T;
else return (strcmp(name,(T)->name) < 0) ? find_name(T->left,name):find_name(T->right,name);
}
|
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2782
|
Quote:
Siccome nell'if fai un return l'else non è necessario. |
|
|
|
|
|
|
#3 |
|
Member
Iscritto dal: Apr 2007
Città: Torino
Messaggi: 95
|
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:01.




















