Language: C


void insert(int k){
    struct Node *y = NIL;
    struct Node *x = root;
    struct Node *z;

    z = (struct Node *)malloc(sizeof(struct Node));
    z->key = k;
    z->left = NIL;
    z->right = NIL;
    

    while(x!= NIL){
        y = x;
        if(z->key < x->key){
            x = x->left;
        } else{
            x = x->right;
        }
    }
    
    z->parent = y;
    if(y == NIL){
        root = z;
    } else{
        if( z->key < y->key){
            y->left = z;
        } else{
            y->right = z;
        }
    }

}


Compiler output:
main.c: In function 'insert':
main.c:3:22: error: 'NIL' undeclared (first use in this function)
    3 |     struct Node *y = NIL;
      |                      ^~~
main.c:3:22: note: each undeclared identifier is reported only once for each function it appears in
main.c:4:22: error: 'root' undeclared (first use in this function)
    4 |     struct Node *x = root;
      |                      ^~~~
main.c:7:24: error: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
    7 |     z = (struct Node *)malloc(sizeof(struct Node));
      |                        ^~~~~~
main.c:1:1: note: include '<stdlib.h>' or provide a declaration of 'malloc'
  +++ |+#include <stdlib.h>
    1 | 
main.c:7:24: warning: incompatible implicit declaration of built-in function 'malloc' [-Wbuiltin-declaration-mismatch]
    7 |     z = (struct Node *)malloc(sizeof(struct Node));
      |                        ^~~~~~
main.c:7:24: note: include '<stdlib.h>' or provide a declaration of 'malloc'
main.c:7:38: error: invalid application of 'sizeof' to incomplete type 'struct Node'
    7 |     z = (struct Node *)malloc(sizeof(struct Node));
      |                                      ^~~~~~
main.c:8:6: error: invalid use of undefined type 'struct Node'
    8 |     z->key = k;
      |      ^~
main.c:9:6: error: invalid use of undefined type 'struct Node'
    9 |     z->left = NIL;
      |      ^~
main.c:10:6: error: invalid use of undefined type 'struct Node'
   10 |     z->right = NIL;
      |      ^~
main.c:15:13: error: invalid use of undefined type 'struct Node'
   15 |         if(z->key < x->key){
      |             ^~
main.c:15:22: error: invalid use of undefined type 'struct Node'
   15 |         if(z->key < x->key){
      |                      ^~
main.c:16:18: error: invalid use of undefined type 'struct Node'
   16 |             x = x->left;
      |                  ^~
main.c:18:18: error: invalid use of undefined type 'struct Node'
   18 |             x = x->right;
      |                  ^~
main.c:22:6: error: invalid use of undefined type 'struct Node'
   22 |     z->parent = y;
      |      ^~
main.c:26:14: error: invalid use of undefined type 'struct Node'
   26 |         if( z->key < y->key){
      |              ^~
main.c:26:23: error: invalid use of undefined type 'struct Node'
   26 |         if( z->key < y->key){
      |                       ^~
main.c:27:14: error: invalid use of undefined type 'struct Node'
   27 |             y->left = z;
      |              ^~
main.c:29:14: error: invalid use of undefined type 'struct Node'
   29 |             y->right = z;
      |              ^~