I have problem with printing the binary tree.After i put the numbers it is just closing.Can't find the problem,somehow it ignores the function called "printukas".

#include <stdio.h>
#include <stdlib.h>
struct tree
{
       int data;
       struct tree *rajt;
       struct tree *left;
};
typedef struct tree node;
                                                          
int main()
{ 
    init();       
 
}              
int insert(node *nju,node *root)
{

    if(nju->data > root->data)
    {
                 if(root->rajt=NULL)
                 root->rajt=nju;
                 else
                 insert(nju,root->rajt);
    }
    if(nju->data < root->data)
    {
                 if(root->left=NULL)
                 root->left=nju;
                 else
                 insert(nju,root->left);             
    }
    printukas();
}             
    
int printukas(struct tree *node)
{
    if( node != NULL)
    { 
    printukas(node->left);
    printukas(node->rajt);
    printf("%d\n",node->data);
    }
    system("PAUSE");    
    return 0;
} 
int init()
{
      int num,n,i; 
      node *root=NULL;
      node *nju=NULL;
 
      printf("How many elements do you want?");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
      printf("Enter elements for a tree:\n");
      scanf("%d",&num);
      }
              
              nju=malloc(sizeof(node));
              nju->data=num;
              nju->left = NULL;
              nju->rajt = NULL;
 
              if(root == NULL)
              root=nju;
              else
              {
              insert(nju,root);
      }        
}

Compare line 33 and 36, you call printukas with no parameters but it expects 1. This is why you should provide prototypes for your functions before calling them. I expect you were getting and ignoring compiler warnings. Don't ignore compiler warnings they have been printed for a reason work out what it is and fix it.

When you call printukas without a parameter the parameter its expecting will get a random value, this probably wont be 0 (NULL) so the function will try to access the memory it is points to but since it is random it probably doesn't point to valid memory and your program crashes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.