Program crashes when inserting node into binary tree

I created the following library to insert, delete, search, and print nodes in a binary tree.

#include <stdlib.h>

struct NODE
{
    int code;
    char subject[20];
    struct NODE *left;
    struct NODE *right;
};


void InOrder(struct NODE *R)
{
    if (R==NULL)
    return;
    InOrder(R->left);
    printf("%d %s\n",R->code,R->subject);
    InOrder(R->right);
}

void PreOrder(struct NODE *R)
{
    if (R==NULL)
    return;
    printf("%d %s\n",R->code,R->subject);
    InOrder(R->left);
    InOrder(R->right);
}

void PostOrder(struct NODE *R)
{
    if (R==NULL)
    return;
    InOrder(R->left);
    InOrder(R->right);
    printf("%d %s\n",R->code,R->subject);
}

struct NODE *Search(struct NODE *R,int CODE,struct NODE **father)
{
    if(R==NULL)
    return NULL;
    if(R->code==CODE)
    {
        *father=R;
        return R;
    }
    if (CODE<R->code)
    return Search(R->left,CODE,father);
    else
    return Search(R->right,CODE,father);
}

struct NODE * CreateNode(struct NODE T)
{
    struct NODE *tmp;
    tmp=(struct NODE *)malloc(sizeof(T));
    *tmp=T;
    tmp->left=tmp->right=NULL;
    return tmp;
}

int Insert(struct NODE **R,struct NODE ND)
{
    struct NODE *cur,*fath=NULL;
    cur=Search(*R,ND.code,&fath);
    if (cur)
    return 0;
    cur=CreateNode(ND);
    if(fath==NULL)
    *R=cur;
    else
    if(fath->code>ND.code)
    fath->left=cur;
    else
    fath->right=cur;
    return 1;
}

struct NODE *MinOfMax (struct NODE *ND)
{
    struct NODE *tmp;
    if (ND==NULL)
    return NULL;
    if(ND->right==NULL)
    return NULL;
    tmp=ND->right;
    while(tmp->left!=NULL)
    tmp=tmp->left;
    return tmp;
}

struct NODE* Delete(struct NODE *R, int code)
{
    if (R==NULL) 
    return R;
    if (code<R->code)
    R->left=Delete(R->left,code);
    else if (code>R->code)
    R->right=Delete(R->right,code);
    else
    {
        if (R->left==NULL)
        {
            struct NODE *temp=R->right;
            free(R);
            return temp;
        }
        else if (R->right==NULL)
        {
            struct NODE *temp=R->left;
            free(R);
            return temp;
        }
        struct NODE *temp=MinOfMax(R->right);
        R->code=temp->code;
        R->right=Delete(R->right,temp->code);
    }
    return R;
}   

When I try to insert a node into a binary tree, the program will work. Here is my main one:

 int main(int argc,char* argv[])
{
    typedef struct NODE NODE;
    NODE *root=NULL;
    NODE tmp;
    Insert(&root,tmp);
    return 0;
}

I tried to assign static values ​​(for example, code = 100 and subject = "Physics"), but still the program will work. Should I malloc something, change something in my header file, or do something completely different? I was stuck here for several hours without finding any solution. Most insert functions assume that I only have one integer as data in a node, but I need to pass the entire node.

+4
2

. , -. , . node . , .

int main(int argc,char* argv[])
{
    typedef struct NODE NODE;
    NODE *root=NULL;
    NODE *tmp = malloc(sizeof(struct NODE));
    tmp->code = 1; /*Just a number*/
    strcpy(tmp->subject,"prova"); /*Put something in it*/
    Insert(&root,*tmp); /* Try to insert it*/
    PreOrder(root); /*Try to see if it has been inserted*/
    return 0;
}
+1

tmp node, node, main(). , -Wall.

, :

int Insert(struct NODE **R, struct NODE ND)
{
    struct NODE *cur,*fath=NULL;
    cur = Search(*R, ND.code, &fath); // ND.code is junk, since ND is uninitialized
    ...
    return 1;
}

, , .

root , NULL main().


, malloc? No.

+1

Source: https://habr.com/ru/post/1678066/


All Articles