-1
typedef struct T{ //Main struct of the nodes
     char *value;    //String view like a pointer                                      
     struct T *T_l, *T_r;  //Pointers left and right                                                                                  
}*tree, dim; 

tree mergetree(char *el, tree t1, tree t2){ // Merging sub-btree

     tree t0 = (tree)malloc(sizeof(dim));// Create a struct for merge the sub-btree
     t0->T_l = t1;
     t0->T_r = t2;
     t0->value = el;
     return(t0);
}
tree createleaf(char *el){ //New leaf calling the mergetree function
     return mergetree(el, NULL, NULL);                  

}
int isvoidtree(tree t){ // Checking if the tree is void or not
     return (t == NULL);                                 

}

char *root(tree t){ //Return value of the node

    return t->value;
}

tree leftchild(tree t){ // Return pointer of the node

     return t->T_l;
}
tree rightchild(tree t){ // Return pointer of the node

     return t->T_r;
}

tree insert(char *el, tree t){ //Insert the new element calling specific functions

    if(isvoidtree(t))                                  

        return createleaf(el);
   if (strcmp(root(t), el)>=0)      //Left side                           

       return mergetree(root(t), insert(el, leftchild(t)), rightchild(t)); 
    if (strcmp(root(t),el)<0)  //Right side
             return mergetree(root(t), leftchild(t), insert(el, rightchild(t)));  
    else return t;
}

void showtree(tree t){ //Show recursively the root of all sub-btree
    int i;
    if (isvoidtree(t) == false){ // if the tree is not null the start of recursive calls start
            showtree(leftchild(t));
            printf("%s\n", root(t));
            showtree(rightchild(t));
    }
}

The main function contains initialise like tree structure, variable interactions.

int main(int argc, char** argv) {
    int N,i;  
    char el[20];
    tree btree = NULL;  //init btree
    printf("Size:\n");
    scanf("%d",&N);
    for(i=0;i<N;i++){
    printf("Insert name:\n");
    scanf("%s",el);

    btree = insert(el,btree);}

    showtree(btree); //Output btree

    return (EXIT_SUCCESS);
}

Hello. Above is my code. I have a problem with the concatenation of the trees left and right. Output I always get the last node added iterated N times how big the tree. The problem seems to be in the string. Thanks in advance guys.

Jilz
  • 31
  • 5
  • Do not typedef pointers, unless you use them for opaque data types. This shadows their meaning and is a source of confusion for the reader. And use "speaking" names. For tree **node** `Node` might be a good choice. – too honest for this site Aug 28 '15 at 18:13

1 Answers1

0

You read each name into the same array; thus, every element of the tree point to the same string, which holds the last name entered. Just as you allocate space for each new node (but fail to free those you no longer use, alas), you need to allocate space for each name.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101