1

I wrote a C program to input elements of a binary search tree and display its InOrder, PostOrder and PreOrder traversals.

#include<stdio.h>
#include<stdlib.h>
struct tnode
{
    int data;
    struct tnode *leftc;
    struct tnode *rightc;
};
int main()
{
    char ans='N';
    struct tnode *new_node,*root;
    //  struct tnode *get_node();
    root=NULL;
    do{
        // new_node=get_node();
        printf("\nEnter the Element");
        scanf("%d",&new_node->data);
        if(root==NULL)
            root=new_node;
        else
            insert(root,new_node);
        printf("\nDo you want to enter a new element?(y/n)");
        scanf("%c",&ans);
    }while(ans == 'y');
    printf("Inorder traversal:the elements in the tree are");
    inorder(root);
    printf("\nPreorder traversal:the elements in the tree are");
    preorder(root);
    printf("Postorder traversal:the elements in the tree are");
    postorder(root);
    return 0;
}
void insert(struct tnode ** tree,int num)
{
    struct tnode *temp = NULL;
    if(!(*tree))
    {
        temp=(struct tnode *)malloc(sizeof (struct tnode));
        temp->leftc=temp->rightc=NULL;
        temp->data=num;
        *tree=temp;
        return;
    }
    if(num < (*tree)->data)
    {
        insert(&(*tree)->leftc,num);
    }
    else if(num > (*tree)->data)
    {
        insert(&(*tree)->rightc,num);
    }
}
void preorder(struct tnode * s)
{
    if(s)
    {
        printf("%d\n",s->data);
        preorder(s->leftc);
        preorder(s->rightc);
    }
}
void inorder(struct tnode * s)
{
    if(s)
    {
        inorder(s->leftc);
        printf("%d\n",s->data);
        inorder(s->rightc);
    }
}
void postorder(struct tnode * s)
{
    if(s)
    {
        postorder(s->leftc);
        postorder(s->rightc);
        printf("%d\n",s->data);
    }
}

I am getting these warning messages:

warning: implicit declaration of functionS,
conflicting types OF FUNCTIONS,
new_node’ may be used uninitialized in this function

I can't understand the errors.Can you please help me fix these?

VatsalSura
  • 922
  • 1
  • 11
  • 26
user-6589801
  • 119
  • 1
  • 14
  • 3
    Please indent the code, otherwise I'm not looking at it. – Sourav Ghosh Jul 14 '16 at 14:04
  • 2
    Didn't look at the code, but regarding the first warning: In C you must declare functions before you use therm. In fact you must declare *everything* before you use it. As for the other, do you initialize the `new_node` variable before you use it? – Some programmer dude Jul 14 '16 at 14:05

2 Answers2

3

In C in order to use function you need to declare em before main function like in your case you should write :

void insert(struct tnode ** tree,int num);
//all declarations of other functions here . 

//btw you can declare em without the names of variables like this :

void insert(struct tnode ** , int );

also just try to google Binary Search Tree in C . There are many websites that shows exactly the answer you are looking for and plenty of websites that got tutorials that explain everything around it.

P.S if you don't want to declare the functions before the main function , you can just put the ready functions that you have upper of a main and main function should be at bottom at end.

FabienAndre
  • 4,100
  • 20
  • 37
Mark Davydov
  • 177
  • 1
  • 14
2
  • You have to declare or define functions to use before you use them.
  • Your usage of insert() is wrong.
  • Using value of uninitialized variable having automatic storage duration, which is indeterminate, invokes undefined behavior. In this case, you don't have to use the struct to read the data for new node.
  • What you don't expect may be read into ans. Add a space before format specifier%c to have scanf() skip whitespace chracter before reading a character.
  • You should use one of standard signatures of main(). In C, int main() and int main(void) have different meanings.
  • You should format your code properly.
  • They say you shouldn't cast the result of malloc() in C.

Try this:

#include<stdio.h>
#include<stdlib.h>
struct tnode
{
    int data;
    struct tnode *leftc;
    struct tnode *rightc;
};
/* declare functions */
void insert(struct tnode ** tree,int num);
void preorder(struct tnode * s);
void inorder(struct tnode * s);
void postorder(struct tnode * s);
/* use one of the standard forms of main() */
int main(void)
{
    char ans='N';
    struct tnode *root;
    int new_node_data;
    //  struct tnode *get_node();
    root=NULL;
    do{
        // new_node=get_node();
        printf("\nEnter the Element");
        scanf("%d",&new_node_data); /* do not dereference indeterminate pointer */
        insert(&root,new_node_data); /* pass correct data */
        printf("\nDo you want to enter a new element?(y/n)");
        scanf(" %c",&ans); /* add a space before %c to have scanf() skip whitespace characters */
    }while(ans == 'y');
    printf("Inorder traversal:the elements in the tree are");
    inorder(root);
    printf("\nPreorder traversal:the elements in the tree are");
    preorder(root);
    printf("Postorder traversal:the elements in the tree are");
    postorder(root);
    return 0;
}
void insert(struct tnode ** tree,int num)
{
    struct tnode *temp = NULL;
    if(!(*tree))
    {
        temp=malloc(sizeof (struct tnode));
        temp->leftc=temp->rightc=NULL;
        temp->data=num;
        *tree=temp;
        return;
    }
    if(num < (*tree)->data)
    {
        insert(&(*tree)->leftc,num);
    }
    else if(num > (*tree)->data)
    {
        insert(&(*tree)->rightc,num);
    }
}
void preorder(struct tnode * s)
{
    if(s)
    {
        printf("%d\n",s->data);
        preorder(s->leftc);
        preorder(s->rightc);
    }
}
void inorder(struct tnode * s)
{
    if(s)
    {
        inorder(s->leftc);
        printf("%d\n",s->data);
        inorder(s->rightc);
    }
}
void postorder(struct tnode * s)
{
    if(s)
    {
        postorder(s->leftc);
        postorder(s->rightc);
        printf("%d\n",s->data);
    }
}
Community
  • 1
  • 1
MikeCAT
  • 61,086
  • 10
  • 41
  • 58