|
| HOMEPAGE | INDICE FORUM | REGOLAMENTO | ::. | NEI PREFERITI | .:: | RSS Forum | RSS News | NEWS web | NEWS software | |
| PUBBLICITA' | | | ARTICOLI | WIN XP | VISTA | WIN 7 | REGISTRI | SOFTWARE | MANUALI | RECENSIONI | LINUX | HUMOR | HARDWARE | DOWNLOAD | | | CERCA nel FORUM » | |
![]() |
#1 |
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
[C] Esercizio d'esame..... help, please!!
Ho un problema con questi esercizi d'esame (che alla fine sono molto simili tra loro quindi se riesco a risolvere il problema con uno, l'ho risolto anche negli altri). Allora vi posto il testo di un esercizio. Progettare un programma C che legge in preordine da stdin un albero binario etichettato con stringhe sui nodi e lo stampa su stdout in inorder. Questo è l'input che devo dargli (nella prima colonna c'è 1 se esiste il sotto albero sinistro e 0 altrimenti, nella seconda colonna c'è 1 se esiste il sotto albero destro e 0 altrimenti e poi nella terza c'è proprio il valore del nodo) 1 1 asdf 1 0 qwerty 0 0 zxd12 1 1 a723 0 1 tiny 0 0 y67u 1 1 a345 0 0 y5rn[ 0 0 zrbu L'output che devo produrre è della stessa forma dell'input: 0 0 zxd12 1 0 qwerty 1 1 asdf 0 1 tiny 0 0 y67u 1 1 a723 0 0 y5rn[ 1 1 a345 0 0 zrbu Questo è invece il codice che ho scritto: Codice:
#include <stdio.h> struct node{ char *string; struct node *left; struct node *right; }; typedef struct node NODE; typedef NODE *TREE; TREE LeggiAlbero(){ TREE t=(TREE)malloc(sizeof(NODE)); int left, right; char *data; scanf("%d %d %s", &left, &right, data); t->string=data; if(left) t->left=LeggiAlbero(); if(right) t->right=LeggiAlbero(); return t; } void StampaAlbero(TREE t){ if(t==NULL) return; StampaAlbero(t->left); printf("%d %d %s", (t->left != NULL), (t->right!=NULL), t->string); StampaAlbero(t->right); } int main(void){ TREE a=(TREE)malloc(sizeof(NODE)); a=LeggiAlbero(); StampaAlbero(a); return 0; } ![]() |
![]() |
![]() |
![]() |
#2 |
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Possibile che nessuno mi sappia aiutare? é importante
![]() |
![]() |
![]() |
![]() |
#3 |
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Finalmente ci sono riuscito......
La funzione ReadTree andava scritta così: Codice:
TREE ReadTree() { TREE t = (TREE)malloc(sizeof(NODE)); int fHasLeft, fHasRight; int nData; scanf("%d %d %d", &fHasLeft, &fHasRight, &nData); t->nData = nData; t->pLeft = NULL; /* Mancavano queste t->pRight = NULL; due righe */ if (fHasLeft) { t->pLeft = ReadTree(); } if (fHasRight) { t->pRight = ReadTree(); } return t; } ![]() |
![]() |
![]() |
![]() |
Utenti attualmente attivi che stanno leggendo questa discussione: 1 (0 utenti e 1 ospiti) | |
|
|
![]() |
||||
Discussione | Autore discussione | Forum | Risposte | Ultimo messaggio |
[C++] esercizio urgente file binario | antony88 | Programmazione | 0 | 23-06-2008 21.31.07 |
[C++] esercizio file binari | antony88 | Programmazione | 8 | 08-05-2008 18.37.08 |
Esercizio matematico... | handyman | Chiacchiere in libertà | 33 | 23-04-2004 23.20.48 |
Che ve ne pare? E per la tesina d'esame. | Lu(a | Chiacchiere in libertà | 19 | 24-03-2004 19.41.50 |
[Java] Esercizio Telefonino | Alhazred | Programmazione | 9 | 06-03-2004 18.33.02 |