Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int max3(int, int, int);
- struct tree_node {
- int dato;
- struct tree_node *sx;
- struct tree_node *dx;
- } nodo;
- typedef struct tree_node *Tree;
- int ricerca(Tree t, int k) {
- if (t==NULL)
- return 0;
- else
- return (t->dato==k) || (ricerca(t->sx, k)) || (ricerca(t->dx, k));
- }
- int get_max(Tree t) {
- int max_sx, max_dx;
- /*Caso base: albero vuoto*/
- if (t==NULL)
- return -1;
- max_sx = get_max(t->sx);
- max_dx = get_max(t->dx);
- return max3(max_sx, max_dx, t->dato);
- }
- int max3(int a, int b, int c) {
- if (a > b)
- if (c > a) return c;
- else return a;
- else
- if (c > b) return c;
- else return b;
- }
- int uguali(Tree t1, Tree t2) {
- if (t1==NULL && t2==NULL)
- return 1;
- else
- if (t1==NULL || t2== NULL)
- return 0;
- return ((t1->dato==t2->dato) && uguali(t1->sx, t2->sx) && uguali(t1->dx, t2->dx));
- }
- Tree creaAlbero() {
- Tree t = (Tree)malloc(sizeof(nodo));
- t->sx = NULL;
- t->dx = NULL;
- char c;
- printf("Inserisci il dato del nodo: ");
- scanf(" %d", &t->dato);
- printf("Vuoi continuare a inserire altri dati? [Y/N] ");
- scanf(" %c", &c);
- if (c!='Y')
- return t;
- else {
- t->sx = creaAlbero();
- t->dx = creaAlbero();
- return t; //devi aggiungere questa linea così che la tua funzione possa sempre terminare
- }
- }
- int main() {
- Tree t = creaAlbero();
- printf("Nell'albero è pr4esente il valore 4? ");
- if (ricerca(t, 4)!=0)
- printf("TRUE\n");
- else
- printf("FALSE\n");
- printf("Il valore più alto inserito nell'albero e' %d\n", get_max(t));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement