-2

What's wrong in my code? I successfully created a tree and everything is working fine except printInorder function and print statement in void main. When I try to print with %s its skipping the entire line.

Can you please help me in resolving this? I even tried fflush but it doesn't help me in my case and I don't try any scanf statement in my code.

struct Tree
{
    char data[10];
    struct Tree *left;
    struct Tree *right;
} * root;

struct Tree *createNode(char data[])
{
    struct Tree *node = malloc(sizeof(struct Tree));
    strcpy(node->data, data);
    node->left = node->right = NULL;
    insertTree(node, root, data);
}
void insertTree(struct Tree *currentNode, struct Tree *temp, char data[])
{
    char fletter=data[0];
    if (fletter=='a'||fletter=='e'||fletter=='o'||fletter=='i'||fletter=='u')
    {
        if (root == NULL)
        {
            root = currentNode;
            printf("inserted at root");
        }
        else if (temp->left == NULL)
        {
            temp->left = currentNode;
            printf("data inserted successfully at left");
        }
        else
        {
            insertTree(currentNode, temp->left,data);
        }
    }

    else
    {
         if (root == NULL)
        {
            root = currentNode;
            printf("inserted at root");
        }
        else if (temp->right == NULL)
        {
            temp->right = currentNode;
            printf("data inserted successfully at right");
        }
        else
        {
            insertTree(currentNode, temp->right,data);
        }
    }
}
void printInorder(struct Tree *tmp) 
{ 
     if (tmp == NULL) 
          return; 
     printInorder(tmp->left); 
     printf("%s ",tmp->data);   
     printInorder(tmp->right); 
} 
  

void main()
{
    struct Tree *root = createNode("saish");
    struct Tree *d1 = createNode("ansha");
    struct Tree *d2 = createNode("shish");
printf("%s",root->data);
    printInorder(root);
}
Priyanshul Govil
  • 518
  • 5
  • 20
sathish
  • 23
  • 1
  • 4
  • c or c++ ? Please pick one. `void main` is not valid C++, not sure about C (and thats not the only difference between the two) – 463035818_is_not_an_ai Feb 03 '21 at 11:15
  • 3
    You have two *different* `root` variables. The `root` in `main` is not the same as the `root` that is used in the other functions. And your `createNode` function doesn't return anything even though it is defined to do so. – kaylum Feb 03 '21 at 11:16
  • 1
    What does it print? What do you expect it to print? – Shawn Feb 03 '21 at 11:16
  • 5
    @largest_prime_is_463035818 [What should main() return in C and C++? - Stack Overflow](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – MikeCAT Feb 03 '21 at 11:17
  • its c and both root has same pointer – sathish Feb 03 '21 at 11:18
  • @MikeCAT cppref says for C "/* another implementation-defined signature */ (since C99)" (while for C++ it must return `int`). I didn't find it in the linked question wether `void` is fine in C (well, I already know that it is implementation defined) – 463035818_is_not_an_ai Feb 03 '21 at 11:19
  • 3
    *both root has same pointer*. What do you mean? The `struct Tree *root` in `main` is not the same variable as the global `root` which is used in the other functions. The one in `main` has an indeterminate value because `createNode` doesn't return anything. – kaylum Feb 03 '21 at 11:20
  • 2
    `createNode` is not returning anything as it should when updated with new nodes – IrAM Feb 03 '21 at 11:21
  • @kaylum Thanks for helping me, Your answer works for me. Thanks for other guys too – sathish Feb 03 '21 at 11:21
  • 2
    If your compiler did not shout at you for "not returning anything from a non-void function" you should turn up warnings. For GCC/clang you can do this via `-Wextra -Wall` – Gerhardh Feb 03 '21 at 11:23
  • You must **never, ever** use `fflush` with `stdin`. [You should read about it](https://stackoverflow.com/questions/2979209/using-fflushstdin). – Priyanshul Govil Feb 03 '21 at 11:41

1 Answers1

-1

createNode function doesn't return anything even though it is defined to do so.

void insertTree(struct Tree *currentNode, struct Tree *temp, char data[])
{
    char fletter=data[0];
    if (fletter=='a'||fletter=='e'||fletter=='o'||fletter=='i'||fletter=='u')
    {
        if (root == NULL)
        {
            root = currentNode;
            printf("inserted at root");
        }
        else if (temp->left == NULL)
        {
            temp->left = currentNode;
            printf("data inserted successfully at left");
        }
        else
        {
            insertTree(currentNode, temp->left,data);
return node;

        }
    }
sathish
  • 23
  • 1
  • 4