Created
September 15, 2014 15:48
-
-
Save dlion/0463795cd5558dc5d747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct albero { | |
int valore; | |
struct albero *dx; | |
struct albero *sx; | |
} ABR; | |
ABR* insert(ABR* A, int value) | |
{ | |
if(A == NULL) | |
{ | |
A = (ABR*)malloc(sizeof(ABR)); | |
A->dx = NULL; | |
A->sx = NULL; | |
A->valore = value; | |
} | |
else | |
{ | |
if(value > A->valore) | |
A->dx = insert(A->dx, value); | |
else | |
A->sx = insert(A->sx, value); | |
} | |
return A; | |
} | |
void stampaAlbero(ABR *v) | |
{ | |
if(v != NULL) | |
{ | |
stampaAlbero(v->sx); | |
printf("%d ", v->valore); | |
stampaAlbero(v->dx); | |
} | |
} | |
int membership(ABR *X, int k) | |
{ | |
int result = 0; | |
if(X != NULL) | |
{ | |
if(k > X->valore) | |
result = membership(X->dx, k); | |
else if ( k == X->valore) | |
return 1; | |
else | |
result = membership(X->sx, k); | |
} | |
return result; | |
} | |
void adding(ABR *A, int *v, int *j) | |
{ | |
if(A == NULL) | |
return; | |
adding(A->sx, v, j); | |
v[*j] = A->valore; | |
(*j)++; | |
adding(A->dx, v, j); | |
} | |
ABR* unions(ABR *root, ABR *root1, int *arr1, int *j, int N) | |
{ | |
int i; | |
ABR *root3 = (ABR*)malloc(sizeof(ABR)); | |
adding(root, arr1, j); | |
adding(root1, arr1, j); | |
for(i=0; i < (N+N); i++) | |
root3 = insert(root3, arr1[i]); | |
return root3; | |
} | |
int main() | |
{ | |
ABR *root = NULL, | |
*root1 = NULL, | |
*root3 = NULL; | |
int i, value, N, j=0; | |
int *arr1; | |
printf("Grandezza alberi: "); | |
scanf("%d", &N); | |
arr1 = (int*)malloc((N+N)*sizeof(int)); | |
for(i=0; i < N; i++) | |
{ | |
printf("Valore %d: ", i); | |
scanf("%d", &value); | |
root = insert(root, value); | |
} | |
for(i=0; i < N; i++) | |
{ | |
printf("Valore %d: ", i); | |
scanf("%d", &value); | |
root1 = insert(root1, value); | |
} | |
printf("STAMPO A\n"); | |
stampaAlbero(root); | |
printf("\nSTAMPO B\n"); | |
stampaAlbero(root1); | |
printf("\nIl numero 5 è presente nell'albero1: %d\n", membership(root, 5)); | |
root3 = unions(root, root1, arr1, &j, N); | |
printf("STAMPO AUB\n"); | |
stampaAlbero(root3); | |
putchar('\n'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment