-1
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
#include <stdlib.h>

struct node
{
    char* data;
    struct node* left;
    struct node* right;
};
typedef struct node Node;
int count;

void initnode(Node* root)
{
    root->right = NULL;
    root->left = NULL;

}
void addtree(Node* root, char* input)
{
    char res = strcmp(input, root->data);
    printf("\nres %d\n", res);
    if(res < 0)
    {
        if(root->left == NULL)
        {
            Node *leaf = (Node *)malloc(sizeof(Node));
            initnode(leaf);
            root->left = leaf;
            //(root)->left->data = malloc(strlen(input) + 1);
            strcpy(leaf->data, input);
        }else
        {
            addtree(root->left, input);
        }
        return;
    }
    if(res > 0)
    {
        if(root->right == NULL)
        {
            Node *leaf = (Node *)malloc(sizeof(Node));
            initnode(leaf);
            root->right = leaf;
            //(root)->right->data = malloc(strlen(input) + 1);
            strcpy(leaf->data, input);
        }else
        {
            addtree(root->right, input);
        }
        return;
    }


}
void printtree(Node *root)
{
    if( root->left == NULL ) {
        printf("%s\n", root->data);
        if(root->right != NULL)
        {
            printtree( root->right);
        }

    }else
    {
        printtree(root->left);
    }
}
int main(void) {

    Node *root = NULL;
    char input[128], initial[128];
    int choice;
    root = (Node *)malloc(sizeof(Node));
    initnode(root);
    printf("Please enter the first and root word");
    scanf("%s", initial);
    strtok(initial, "\n" );
    strcpy(root->data, initial);



    while(choice != 0)
    {
        printf("Press 0 to exit\n");
        printf("Press 1 to enter new word\n");
        printf("Press 2 to print tree\n");
        scanf("%d", &choice);
        getchar();
        if(choice == 1)
        {
            printf("Please enter a string: ");
            fgets(input, sizeof input, stdin);
            strtok(input, "\n" );
            printf("You entered %s", input);
            addtree(root, input);
        }else if(choice == 2)
        {
            printtree(root);
        }
    }
    return 0;
}

Output:

Please enter the first and root wordHello
Press 0 to exit
Press 1 to enter new word
Press 2 to print tree
1
Please enter a string:Hi
 You entered Hi
res 1
Press 0 to exit
Press 1 to enter new word
Press 2 to print tree
2
Hi
Hi
Press 0 to exit
Press 1 to enter new word
Press 2 to print tree

Having issues making a Binary Tree that sorts words in C. I feel like my implementation is good, but cannot figure out where or why my words are overwriting each other. It should print Hello Hi and other words, but it just prints the most recently entered word. Any suggestions would be appreciated. I do not think it is my addtree function as the words are added, but maybe its that way I am passing 'root'?

Maxwell D. Dorliea
  • 1,086
  • 1
  • 8
  • 20
JohnM Wall
  • 15
  • 4
  • Have you tried running your code line-by-line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Jul 24 '22 at 17:43
  • 1
    `strcpy(leaf->data, input)` in your add is blasting `input` into an indeterminate target pointer `leaf->data`, thereby invoking *undefined behavior*. It happens in two different locations within the `addtree` function, and once in `main`. – WhozCraig Jul 24 '22 at 17:47
  • To elaborate on the previous comment (which was written by someone else): You must first allocate memory for the destination string (for example using `malloc`) and the first argument passed to `strcpy` must point to that allocated memory. The function `strcpy` will not allocate memory for you. If the function [`strdup`](https://en.cppreference.com/w/c/string/byte/strdup) is available to you, then you may want to consider using that function instead of `strcpy`, as `strdup` will allocate the necessary amount of memory for you. – Andreas Wenzel Jul 24 '22 at 17:58
  • Aside: `while(choice != 0)` uses an uninitialised variable. – Weather Vane Jul 24 '22 at 18:19
  • The malloc on the string seemed to fix some issues thanks to those who suggested still working on printing more then two words, but I think it is an error with my printtree – JohnM Wall Jul 24 '22 at 18:28

1 Answers1

2

Well the first major problem of this code was, it gives a segmentation fault everytime it tries to copy the strings to the leaf or root of this binary tree. So I added a malloc statement to the addtree function for both of the if statements like;

        leaf->data = (char *)malloc(sizeof(char));
        //(root)->right->data = malloc(strlen(input) + 1);
        strcpy(leaf->data, input);
...

and added to the main function like;

    root->data = (char *)malloc(sizeof(char));
    strcpy(root->data, new_initial);

And the second major problem was, my compiler gave a segmentation fault again for the while loop in the main function, because the condition was choice != 0 before there was a choice input from the user, so I added the following statement before the while loop;

    scanf("%d", &choice);

to let the code start the loop with a value other than zero. Or you can modify it just as you like. Here's the output:

Please enter the first and root wordhello
1
Press 0 to exit
Press 1 to enter new word
Press 2 to print tree
1
Please enter a string: hi
You entered hi
res 1
Press 0 to exit
Press 1 to enter new word
Press 2 to print tree
2
hello
hi
Press 0 to exit
Press 1 to enter new word
Press 2 to print tree