E' un po' difficile attraversare tutti i nodi di un albero binario con un while inquanto ad ogni ciclo i nodi aumentano esponenzialmente!
L'unico modo sarebbe trasformare l'albero in una lista per poi attraversare linearmente la lista:
Attento però che questa funzione non è molto efficente...codice:#include <malloc.h> typedef struct LElement_s { double value; struct LElement_s *next; } LElement; typedef LElement * List; List treeToList(NODO *a) { if(a == NULL) return NULL; // Lista vuota return concat(treeToList(NODO->left), concat(toList(a->value), treeToList(NODO->right)); } List toList(double value) { List temp = (List)malloc(sizeof(LElement)); temp->value = value; temp->next = NULL; return temp; } List concat(List a, List b) { if(a == NULL) return b; if(b == NULL) return a; List temp = a; while(temp->next != NULL) temp = temp->next; temp->next = b; return a; }
NB: Bisogna poi rilasciare la memoria degli elementi allocati!