Hi everyone!
I'm having some troubles with the definition of a function.
Here there is the exercise:
"Given two Binary Search Trees, T1 and T2, which contain float values, write a function that create a new Binary Search Tree, which has in his nodes the common elements among T1 and T2".
This were my thoughts:
-I create a function which put in an array the elements of a Binary Search Tree (I called it "visitcopy");
-I create two arrays, A1 and A2, for the values of my T1 and T2;
-I create a doublepointer and a pointer to a structured data "struct btree" (this is my new Tree);
-With a for cycle, I make a comparison between the two arrays and everytime I find a common element, I add a node in my new Binary Search Tree.
Here there is my code:
Code:
void newcommon (struct btree *p, struct btree *punt, int first, int second){
//first and second are the number of nodes of the first and second tree
float *A;
A= (float*)calloc(first, sizeof(float));
int *k;
int u=-1;
k=&u;
visitcopy(p, A, k);
float *B;
B=(float*)calloc(second, sizeof(float));
visitcopy(punt, B, k);
struct btree **doublepointer;
struct btree *pointer;
doublepointer= &pointer;
init(doublepointer);
int i,j;
for (i=0;i<first;i++){
for (j=0;j<second;j++){
if (A[i]==B[j]){
insert_inorder(doublepointer, A[i]);
printf ("\n\n new node created in the new tree %f\n\n", A[i]);
}
}
}
}
void visitcopy (struct btree * ptr, float *E, int *i) {
if (ptr != NULL) {
visitcopy(ptr->left_ptr, E, (int)(*i)++);
E[(*i)]=(ptr->value);
printf ("\n\n\n the value number %d of the array is %f \n\n\n",(*i), E[(*i)]);
visitcopy(ptr->right_ptr, E, (int)(*i)++);
}
}
From the debugging, I think that the main problem is in visitcopy, but there are also one or more mistake in the main function.
Can anyone help me please?