/* Progettare un programma C che legge in preordine da stdin un albero binario T etichettato con caratteri e stampa in inorder su sdtout l'albero ottenuto da T sostituendo tutte le vocali minuscole con il carattere *. */ #include #include struct nodo { struct nodo *Left; char Info; struct nodo *Right; }; typedef struct nodo NodoAlbero; typedef NodoAlbero *PtrNodo; PtrNodo leggiPreOrder(); void modificaEtichette(PtrNodo t); void stampaInOrder(PtrNodo t); int main() { PtrNodo MyTree; MyTree = leggiPreOrder(); modificaEtichette(MyTree); stampaInOrder(MyTree); return 0; } PtrNodo creaElemento() { return (PtrNodo)malloc(sizeof(NodoAlbero)); } PtrNodo leggiPreOrder() { PtrNodo nuovo; int numero_figli; char carattere; nuovo = creaElemento(); scanf("%d %c",&numero_figli, &carattere); nuovo->Info = carattere; if(numero_figli==2) { nuovo->Left = leggiPreOrder(); nuovo->Right = leggiPreOrder(); } else if(numero_figli==1) { nuovo->Left = leggiPreOrder(); nuovo->Right = NULL; } else { nuovo->Left = NULL; nuovo->Right = NULL; } return nuovo; } void modificaEtichette(PtrNodo t) { if(t!=NULL) { if(t->Info >= 'a' && t->Info <= 'z') t->Info = '*'; modificaEtichette(t->Left); modificaEtichette(t->Right); } } void stampaInOrder(PtrNodo t) { if(t!=NULL) { stampaInOrder(t->Left); printf("%c\n", t->Info); stampaInOrder(t->Right); } } -------------------------------------------------------------- /*Progettare un programma C che legge in preordine da stdin un albero binario T con etichette intere, un intero k e stampa su stdout e stampa in inorder su sdtout l'albero ottenuto da T eliminando tutti i sottoalberi con radice k.*/ #include #include struct nodo { struct nodo *Left; int Info; struct nodo *Right; }; typedef struct nodo NodoAlbero; typedef NodoAlbero *PtrNodo; PtrNodo creaElemento(), leggiPreOrder(), arrivaARadiceK(PtrNodo,int); void stampaInOrder(PtrNodo), eliminaAlbero(PtrNodo); int main() { PtrNodo MyTree = NULL; int k; MyTree = leggiPreOrder(); scanf("%d",&k); arrivaARadiceK(MyTree,k); stampaInOrder(MyTree); return 0; } PtrNodo creaElemento() { return (PtrNodo)malloc(sizeof(NodoAlbero)); } PtrNodo leggiPreOrder() { PtrNodo nuovo; int valore, numero_figli; nuovo = creaElemento(); scanf("%d%d",&numero_figli, &valore); nuovo->Info = valore; if(numero_figli==2) { nuovo->Left = leggiPreOrder(); nuovo->Right = leggiPreOrder(); } else if(numero_figli==1) { nuovo->Left = leggiPreOrder(); nuovo->Right = NULL; } else { nuovo->Left = NULL; nuovo->Right = NULL; } return nuovo; } PtrNodo arrivaARadiceK(PtrNodo t, int k) { if(t==NULL) return t; if(t->Info == k) { eliminaAlbero(t); return NULL; } t->Left = arrivaARadiceK(t->Left,k); t->Right = arrivaARadiceK(t->Right,k); return t; } void eliminaAlbero(PtrNodo t) { if(t!=NULL){ eliminaAlbero(t->Left); eliminaAlbero(t->Right); free(t); } } void stampaInOrder(PtrNodo t) { if(t!=NULL) { stampaInOrder(t->Left); printf("%d\n", t->Info); stampaInOrder(t->Right); } } **************************************************** /* Progettare un programma C che legga in preordine da stdin un albero binario di interi T e verifichi se e' vero che la radice di ogni sottoalbero sia l'elemento piu' grande del sottoalbero. */ #include #include struct nodotree { struct nodotree *Left; int Info; struct nodotree *Right; }; typedef struct nodotree NodoTree; typedef NodoTree *PtrNodo; PtrNodo creaElemento(void); PtrNodo leggiPreOrder(void); int verifica(PtrNodo t, int radice); int main() { PtrNodo t; t = leggiPreOrder(); if(verifica(t,t->Info+1)) printf("TRUE"); else printf("FALSE"); return 0; } PtrNodo creaElemento(void) { return (PtrNodo)malloc(sizeof(NodoTree)); } PtrNodo leggiPreOrder(void) { PtrNodo nuovo; int x,numfigli; scanf("%d %d", &numfigli, &x); nuovo = creaElemento(); if(nuovo==NULL) printf("memoria insufficiente"); else { nuovo->Info = x; if(numfigli==2) { nuovo->Left = leggiPreOrder(); nuovo->Right = leggiPreOrder(); } if(numfigli==1) { nuovo->Left = leggiPreOrder(); nuovo->Right = NULL; } if(numfigli==0) { nuovo->Left = nuovo->Right = NULL; } } return nuovo; } int verifica(PtrNodo t, int radice) { int a,b; if(t==NULL) return 1; a = verifica(t->Left, t->Info); b = verifica(t->Right, t->Info); if(a==1 && b==1 && t->Info < radice) return 1; else return 0; } ------------------------------------------------------- /* Progettare un programma C che legga in preordine da stdin un albero binario di interi T e verifichi se e' vero che la radice di ogni sottoalbero sia l'elemento piu' grande del sottoalbero. */ #include #include struct nodotree { struct nodotree *Left; int Info; struct nodotree *Right; }; typedef struct nodotree NodoTree; typedef NodoTree *PtrNodo; PtrNodo creaElemento(void); PtrNodo leggiPreOrder(void); int verifica(PtrNodo t, int radice); int main() { PtrNodo t; t = leggiPreOrder(); if(verifica(t,t->Info+1)) printf("TRUE"); else printf("FALSE"); return 0; } PtrNodo creaElemento(void) { return (PtrNodo)malloc(sizeof(NodoTree)); } PtrNodo leggiPreOrder(void) { PtrNodo nuovo; int x,numfigli; scanf("%d %d", &numfigli, &x); nuovo = creaElemento(); if(nuovo==NULL) printf("memoria insufficiente"); else { nuovo->Info = x; if(numfigli==2) { nuovo->Left = leggiPreOrder(); nuovo->Right = leggiPreOrder(); } if(numfigli==1) { nuovo->Left = leggiPreOrder(); nuovo->Right = NULL; } if(numfigli==0) { nuovo->Left = nuovo->Right = NULL; } } return nuovo; } int verifica(PtrNodo t, int radice) { int a,b; if(t==NULL) return 1; a = verifica(t->Left, t->Info); b = verifica(t->Right, t->Info); if(a==1 && b==1 && t->Info < radice) return 1; else return 0; } ********************************** /* Progettare un programma C che legge da standard input due interi x e y, due sequenze ordinate di rispettivamente x e y interi e stampa su standard output l'intersezione (ordinata) delle sequenze. */ #include #include void stampaIntersezione(int *s, int *p, int dim1, int dim2); void leggiSequenza(int *s, int dim); int main() { int x,y, *sequenza1, *sequenza2; scanf("%d%d",&x,&y); sequenza1 = (int*)malloc(sizeof(int)*x); sequenza2 = (int*)malloc(sizeof(int)*y); leggiSequenza(sequenza1,x); leggiSequenza(sequenza2,y); stampaIntersezione(sequenza1,sequenza2,x,y); } void leggiSequenza(int *s, int dim) { int i; for(i=0; i<=dim-1; i++) scanf("%d",&s[i]); } void stampaIntersezione(int *s, int *p, int dim1, int dim2) { int i,j,trovato=0; for(i=0; i<=dim1-1; i++) { j=0; trovato = 0; while(j<= dim2 - 1 && !trovato) { if(s[i] == p[j]) { printf("%d\n",s[i]); trovato = 1; } j++; } } } ********************************** /* Progettare un programma C che legge da stdin un albero binario di caratteri in preordine e lo stampa su stdout in postordine dopo aver trasformato i caratteri maiuscoli in minuscoli, i caratteri minuscoli in maiuscoli, le cifre pari nel carattere 0 e le cifre dispari nel carattere 1. */ #include #include struct nodotree { struct nodotree * Left; char Info; struct nodotree * Right; }; typedef struct nodotree NodoTree; typedef NodoTree * PtrTree; PtrTree creaElemento(void); PtrTree leggiPreOrdine(void); void modificaEtichette(PtrTree Tree); void stampaPostOrdine(PtrTree Tree); int main() { PtrTree MyTree = NULL; MyTree = leggiPreOrdine(); modificaEtichette(MyTree); stampaPostOrdine(MyTree); return 0; } PtrTree creaElemento(void) { return (PtrTree)malloc(sizeof(NodoTree)); } PtrTree leggiPreOrdine(void) { PtrTree Nuovo; char etichetta; int NumeroFigli; scanf("%d %c",&NumeroFigli,&etichetta); Nuovo = creaElemento(); Nuovo->Info=etichetta; if(NumeroFigli==2) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right=leggiPreOrdine(); } else if(NumeroFigli==1) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right = NULL; } else { Nuovo->Left=NULL; Nuovo->Right = NULL; } return Nuovo; } void modificaEtichette(PtrTree Tree) { if(Tree!=NULL) { if(isalpha(Tree->Info)) if(islower(Tree->Info)) Tree->Info = toupper(Tree->Info); else Tree->Info = tolower(Tree->Info); else if(isdigit(Tree->Info)) if(Tree->Info%2==0) Tree->Info = '0'; else Tree->Info = '1'; modificaEtichette(Tree->Left); modificaEtichette(Tree->Right); } } void stampaPostOrdine(PtrTree Tree) { if(Tree!=NULL) { stampaPostOrdine(Tree->Left); stampaPostOrdine(Tree->Right); printf("%c\n",Tree->Info); } } ********************************************* /* Progettare un programma C che legge da stdin un intero k ed un albero binario di interi in preordine, e stampa su sdtout il numero di nodi dell'albero appartenenti al livello k. La radice dell'albero si trova al livello 0. Un nodo il cui padre e' al livello h, appartiene al livello h+1. Quindi i figli della radice sono a livello 1, i figli dei figli della radice al livello 2 etc. Se il numero dei livelli dell'albero e' inferiore a k, oppure se l'albero e' vuoto, il programma stampera' in output 0. */ #include #include struct nodotree { struct nodotree * Left; int Info; struct nodotree * Right; }; typedef struct nodotree NodoTree; typedef NodoTree * PtrTree; PtrTree creaElemento(void); PtrTree leggiPreOrdine(void); void ContaFigli(PtrTree T, int livello, int *numero); int main() { PtrTree MyTree = NULL; int k,numero=0; scanf("%d",&k); MyTree = leggiPreOrdine(); ContaFigli(MyTree,k,&numero); printf("%d\n",numero); } PtrTree creaElemento(void) { return (PtrTree)malloc(sizeof(NodoTree)); } PtrTree leggiPreOrdine(void) { PtrTree Nuovo; int etichetta; int NumeroFigli; scanf("%d %d",&NumeroFigli,&etichetta); Nuovo = creaElemento(); Nuovo->Info=etichetta; if(NumeroFigli==2) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right=leggiPreOrdine(); } else if(NumeroFigli==1) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right = NULL; } else { Nuovo->Left=NULL; Nuovo->Right = NULL; } return Nuovo; } void ContaFigli(PtrTree T, int livello, int *numero) { if(T!=NULL) { if(livello == 0) (*numero)++; else { ContaFigli(T->Left,livello - 1, numero); ContaFigli(T->Right,livello-1, numero); } } } ************* ************* ************* /* Progettare un programma C che legge in preordine da stdin un albero binario T etichettato con caratteri e stampa in postorder su sdtout l'albero ottenuto da T sommando 1 a tutte le cifre pari e sottraendo 1 a tutte le cifre dispari. I caratteri che non rappresentano cifre restano inalterati. */ #include #include #include "tree.h" PtrNodo creaElemento(void), leggiPreOrder(void); void ModificaEtichette(PtrNodo); void stampaPostOrder(PtrNodo); int main() { PtrNodo MyTree; MyTree = leggiPreOrder(); ModificaEtichette(MyTree); stampaPostOrder(MyTree); } PtrNodo creaElemento(void) { return (PtrNodo)malloc(sizeof(NodoTree)); } PtrNodo leggiPreOrder(void) { PtrNodo nuovo; int x,numfigli; scanf("%d %c", &numfigli, &x); nuovo = creaElemento(); if(nuovo==NULL) printf("memoria insufficiente"); else { nuovo->Info = x; if(numfigli==2) { nuovo->Left = leggiPreOrder(); nuovo->Right = leggiPreOrder(); } if(numfigli==1) { nuovo->Left = leggiPreOrder(); nuovo->Right = NULL; } if(numfigli==0) { nuovo->Left = nuovo->Right = NULL; } } return nuovo; } void ModificaEtichette(PtrNodo t) { int cifra; if(t!=NULL) { if(isdigit(t->Info)) { cifra = t->Info - '0'; if(cifra%2==0) t->Info = t->Info + 1; else t->Info = t->Info - 1; } ModificaEtichette(t->Left); ModificaEtichette(t->Right); } } void stampaPostOrder(PtrNodo t) { if(t!=NULL) { stampaPostOrder(t->Left); stampaPostOrder(t->Right); printf("%c\n",t->Info); } } -------------------------------- /* Progettare un programma C che legge da stdin un albero binario di interi in preordine, e verifica se la somma dei valori pari e dei valori dispari contenuti nei nodi dell'albero sia o meno uguale. */ #include #include struct nodotree { struct nodotree * Left; int Info; struct nodotree * Right; }; typedef struct nodotree NodoTree; typedef NodoTree * PtrTree; PtrTree creaElemento(void); PtrTree leggiPreOrdine(void); void sommaValori(PtrTree T); int SommaPari = 0, SommaDispari = 0; int main() { PtrTree MyTree = NULL; MyTree = leggiPreOrdine(); sommaValori(MyTree); if(SommaPari == SommaDispari) printf("TRUE"); else printf("FALSE"); return 0; } PtrTree creaElemento(void) { return (PtrTree)malloc(sizeof(NodoTree)); } PtrTree leggiPreOrdine(void) { PtrTree Nuovo; int etichetta; int NumeroFigli; scanf("%d %d",&NumeroFigli,&etichetta); Nuovo = creaElemento(); Nuovo->Info=etichetta; if(NumeroFigli==2) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right=leggiPreOrdine(); } else if(NumeroFigli==1) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right = NULL; } else { Nuovo->Left=NULL; Nuovo->Right = NULL; } return Nuovo; } void sommaValori(PtrTree T) { if(T!=NULL) { if(T->Info%2 == 0) SommaPari = SommaPari + T->Info; else SommaDispari = SommaDispari + T->Info; sommaValori(T->Left); sommaValori(T->Right); } } ******************************** /* Progettare un programma C che legge da stdin un albero binario di interi in preordine, cancella i nodi che hanno un unico figlio dall'albero e stampa l'albero risultante in inorder. La cancellazione di un nodo x con un unico figlio y significa che il padre di x avra' come figlio al posto di x y. Se y e' ha a sua volta un unico figlio allora anche y sara' cancellato etc. */ #include #include struct nodotree { struct nodotree * Left; int Info; struct nodotree * Right; }; typedef struct nodotree NodoTree; typedef NodoTree * PtrTree; PtrTree creaElemento(void); PtrTree leggiPreOrdine(void); PtrTree cancellaNodi(PtrTree T); void stampaInOrdine(PtrTree T); int main() { PtrTree MyTree = NULL; MyTree = leggiPreOrdine(); MyTree = cancellaNodi(MyTree); stampaInOrdine(MyTree); return 0; } PtrTree creaElemento(void) { return (PtrTree)malloc(sizeof(NodoTree)); } PtrTree leggiPreOrdine(void) { PtrTree Nuovo; int etichetta; int NumeroFigli; scanf("%d %d",&NumeroFigli,&etichetta); Nuovo = creaElemento(); Nuovo->Info=etichetta; if(NumeroFigli==2) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right=leggiPreOrdine(); } else if(NumeroFigli==1) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right = NULL; } else { Nuovo->Left=NULL; Nuovo->Right = NULL; } return Nuovo; } PtrTree cancellaNodi(PtrTree T) { PtrTree figlio; if(T == NULL) return T; T->Left = cancellaNodi(T->Left); T->Right = cancellaNodi(T->Right); if(T->Left!=NULL&&T->Right==NULL) { figlio = T->Left; free(T); return figlio; } if(T->Left==NULL&&T->Right!=NULL) { figlio = T->Right; free(T); return figlio; } return T; } void stampaInOrdine(PtrTree T) { if(T!=NULL) { stampaInOrdine(T->Left); printf("%d\n",T->Info); stampaInOrdine(T->Right); } } **************************** /* Progettare un programma C che legge da stdin un albero binario in preordine e lo stampa su stdout in postordine moltiplicando per r i valori delle etichette dei nodi, dove r e' l'etichetta del padre del nodo. La radice dell'albero resta invariata. */ #include #include struct nodotree { struct nodotree * Left; int Info; struct nodotree * Right; }; typedef struct nodotree NodoTree; typedef NodoTree * PtrNodoTree; void modificaEtichette(PtrNodoTree Tree, int radice); void stampaPostOrdineR(PtrNodoTree Tree); PtrNodoTree leggiPreOrdine(void), creaElemento(void); void main() { PtrNodoTree MyTree; MyTree = leggiPreOrdine(); modificaEtichette(MyTree, 1); stampaPostOrdineR(MyTree); } PtrNodoTree creaElemento(void) { return (PtrNodoTree)malloc(sizeof(NodoTree)); } PtrNodoTree leggiPreOrdine(void) { PtrNodoTree Nuovo; int val; int NumeroFigli; scanf("%d %d",&NumeroFigli,&val); Nuovo = creaElemento(); Nuovo->Info=val; if(NumeroFigli==2) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right=leggiPreOrdine(); } else if(NumeroFigli==1) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right = NULL; } else { Nuovo->Left=NULL; Nuovo->Right = NULL; } return Nuovo; } void modificaEtichette(PtrNodoTree Tree, int radice) { if(Tree!=NULL) { modificaEtichette(Tree->Left, Tree->Info); modificaEtichette(Tree->Right, Tree->Info); Tree->Info = Tree->Info * radice; } } void stampaPostOrdineR(PtrNodoTree Tree) { if(Tree!=NULL) { stampaPostOrdineR(Tree->Left); stampaPostOrdineR(Tree->Right); printf("%d\n",Tree->Info); } } ********* ************ ************ /* Progettare un programma C che legge da stdin un albero binario in preordine e stampa in postordine su sdtout l'albero ottenuto sostituendo l'etichetta di ogni nodo r con il prodotto delle etichette del sottoalbero con radice r (r e' incluso nel prodotto). Le foglie restano invariate. */ #include #include struct nodotree { struct nodotree * Left; int Info; struct nodotree * Right; }; typedef struct nodotree NodoTree; typedef NodoTree * PtrNodoTree; void stampaPostOrdineR(PtrNodoTree PtrTree); int modificaEtichette(PtrNodoTree Tree); PtrNodoTree leggiPreOrdine(void), creaElemento(void); void main() { PtrNodoTree MyTree; MyTree = leggiPreOrdine(); modificaEtichette(MyTree); stampaPostOrdineR(MyTree); } PtrNodoTree creaElemento(void) { return (PtrNodoTree)malloc(sizeof(NodoTree)); } PtrNodoTree leggiPreOrdine(void) { PtrNodoTree Nuovo; int val; int NumeroFigli; scanf("%d %d",&NumeroFigli,&val); Nuovo = creaElemento(); Nuovo->Info=val; if(NumeroFigli==2) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right=leggiPreOrdine(); } else if(NumeroFigli==1) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right = NULL; } else { Nuovo->Left=NULL; Nuovo->Right = NULL; } return Nuovo; } int modificaEtichette(PtrNodoTree Tree) { int InfoFiglio1, InfoFiglio2; if(Tree==NULL) return 1; InfoFiglio1 = modificaEtichette(Tree->Left); InfoFiglio2 = modificaEtichette(Tree->Right); Tree->Info = Tree->Info * InfoFiglio1 * InfoFiglio2; return Tree->Info; } void stampaPostOrdineR(PtrNodoTree Tree) { if(Tree!=NULL) { stampaPostOrdineR(Tree->Left); stampaPostOrdineR(Tree->Right); printf("%d\n",Tree->Info); } } ***************************** /*Progettare un programma C che legge in preordine da stdin un albero binario T etichettato con caratteri e stampa in inorder su sdtout l'albero ottenuto da T sostituendo tutti i caratteri alfabetici minuscoli con i corrispondenti maiusoli e tutti i caratteri alfabetici maiuscoli con i corrispondenti minuscoli. */ #include #include struct nodotree { struct nodotree * Left; char Info; struct nodotree * Right; }; typedef struct nodotree NodoTree; typedef NodoTree * PtrNodoTree; void stampaInOrdine(PtrNodoTree PtrTree); PtrNodoTree leggiPreOrdine(void), creaElemento(void); void modificaEtichette(PtrNodoTree PtrTree); void main() { PtrNodoTree MyTree; MyTree = leggiPreOrdine(); modificaEtichette(MyTree); stampaInOrdine(MyTree); } PtrNodoTree creaElemento(void) { return (PtrNodoTree)malloc(sizeof(NodoTree)); } PtrNodoTree leggiPreOrdine(void) { PtrNodoTree Nuovo; char val; int NumeroFigli; scanf("%d %c",&NumeroFigli,&val); Nuovo = creaElemento(); Nuovo->Info=val; if(NumeroFigli==2) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right=leggiPreOrdine(); } else if(NumeroFigli==1) { Nuovo->Left=leggiPreOrdine(); Nuovo->Right = NULL; } else { Nuovo->Left=NULL; Nuovo->Right = NULL; } return Nuovo; } void modificaEtichette(PtrNodoTree Tree) { if(Tree!=NULL) { if(islower(Tree->Info)) Tree->Info = toupper(Tree->Info); else if(isupper(Tree->Info)) Tree->Info = tolower(Tree->Info); modificaEtichette(Tree->Left); modificaEtichette(Tree->Right); } } void stampaInOrdine(PtrNodoTree Tree) { if(Tree!=NULL) { stampaInOrdine(Tree->Left); printf("%c\n",Tree->Info); stampaInOrdine(Tree->Right); } } ************************* ************ ***************