Language: C

#include <stdio.h>
#include <stdlib.h>
 
typedef struct node_t {
    int key;
    struct node_t *parent;
    struct node_t *left;
    struct node_t *right;
} node;
 
node* Node(int);
void print(node *);
void inorder(node *);
void preorder(node *);
void insert(int, node *);
node* find(int, node *);
int delete(int, node *);
void deleteNode(node *, node *);
void free_tree(node *);
 
int main(void)
{
    int i, m, k;
    char com[10];
    node *tree_root;
     
    tree_root = newNode(0);
     
    scanf("%d",&m);
    for (i=0; i<m; i++) {
        scanf("%s",com);
        if(com[0] == 'p') {
            print(tree_root->left);
        } else {
            scanf("%d",&k);
            if(com[0] == 'i') {
                insert(k, tree_root);
            } else if(com[0] == 'f') {
                if(find(k, tree_root) != NULL) {
                    printf("yes\n");
                } else {
                    printf("no\n");
                }
            } else if(com[0] == 'd') {
                if(delete(k, tree_root) == 0) printf("delete error!\n");
            }
        }
    }
     
    free_tree(tree_root);
     
    return 0;
}
 
node* Node(int key)
{
    node *tmp;
     
    tmp = (node *)malloc(sizeof(node));
    if (tmp == NULL) exit(0);
     
    tmp.key = key;
    tmp.parent = NULL;
    tmp.left = NULL;
    tmp.right = NULL;
     
    return tmp;
}
 
void print(node *root)
{
    inorder(root);
    printf("\n");
    preorder(root);
    printf("\n");
}
 
void inorder(node *node)
{
     
    if(node.left != NULL) inorder(node.left);
    printf(" %d",node.key);
    if(node.right != NULL) inorder(node.right);
}
 
void preorder(node *node)
{
    printf(" %d",node.key);
    if(node.left != NULL) preorder(node.left);
    if(node.right != NULL) preorder(node.right);
}
 
void insert(int k, node *tree_root)
{
    node *x, *y, *z;
     
    z = newNode(k);
    y = tree_root;
    x = tree_root.left;
    while (x != NULL) {
        y = x;
        if(z.key < x.key) {
            x = x.left;
        } else {
            x = x.right;
        }
    }
    z.parent = y;
    if(y == tree_root || z.key < y.key) {
        y.left = z;
    } else {
        y.right = z;
    }
}
 
node* find(int k, node *tree_root) 
{
    node *x;
     
    x = tree_root.left;
    while (x.key != k) {
        if(x.key > k) {
            if(x.left == NULL) return NULL;
            x = x.left;
        } else {
            if(x.right == NULL) return NULL;
            x = x.right;
        }
    }
    return x;
}
 
int delete(int k, node *tree_root)
{
    node *x;
     
    x = find(k, tree_root);
    if(x==NULL) return 0;
     
    deleteNode(x, tree_root);
    return 1;
}
 
void deleteNode(node *x, node *tree_root)
{
    int isLeft;
    node *y;
     
    if(x == tree_root.left || x.key < x.parent.key) isLeft = 1;
     else isLeft = 0;
     
    if(x.left==NULL && x.right==NULL) {
        if(isLeft) {
            x.parent.left = NULL;
        } else {
            x.parent.right = NULL;
        }
        free(x);
    } else if(x.right==NULL) {
        if(isLeft) {
            x.parent.left = x.left;
        } else {
            x.parent.right = x.left;
        }
        x.left.parent = x.parent;
        free(x);
    } else if(x.left==NULL) {
        if(isLeft) {
            x.parent.left = x.right;
        } else {
            x.parent.right = x.right;
        }
        x.right.parent = x.parent;
        free(x);
    } else {
        y = x.right;
        while(y.left!=NULL) {
            y = y.left;
        }
        x.key = y.key;
        deleteNode(y, tree_root);
    }
}
 
void free_tree(node *root)
{
    if(root.left != NULL) free_tree(root.left);
    if(root.right != NULL) free_tree(root.right);
    free(root);
}

Compiler output:
main.c: In function 'main':
main.c:27:17: error: implicit declaration of function 'newNode'; did you mean 'Node'? [-Wimplicit-function-declaration]
   27 |     tree_root = newNode(0);
      |                 ^~~~~~~
      |                 Node
main.c:27:15: error: assignment to 'node *' {aka 'struct node_t *'} from 'int' makes pointer from integer without a cast [-Wint-conversion]
   27 |     tree_root = newNode(0);
      |               ^
main.c: In function 'Node':
main.c:62:8: error: 'tmp' is a pointer; did you mean to use '->'?
   62 |     tmp.key = key;
      |        ^
      |        ->
main.c:63:8: error: 'tmp' is a pointer; did you mean to use '->'?
   63 |     tmp.parent = NULL;
      |        ^
      |        ->
main.c:64:8: error: 'tmp' is a pointer; did you mean to use '->'?
   64 |     tmp.left = NULL;
      |        ^
      |        ->
main.c:65:8: error: 'tmp' is a pointer; did you mean to use '->'?
   65 |     tmp.right = NULL;
      |        ^
      |        ->
main.c: In function 'inorder':
main.c:81:12: error: 'node' is a pointer; did you mean to use '->'?
   81 |     if(node.left != NULL) inorder(node.left);
      |            ^
      |            ->
main.c:81:39: error: 'node' is a pointer; did you mean to use '->'?
   81 |     if(node.left != NULL) inorder(node.left);
      |                                       ^
      |                                       ->
main.c:82:22: error: 'node' is a pointer; did you mean to use '->'?
   82 |     printf(" %d",node.key);
      |                      ^
      |                      ->
main.c:83:12: error: 'node' is a pointer; did you mean to use '->'?
   83 |     if(node.right != NULL) inorder(node.right);
      |            ^
      |            ->
main.c:83:40: error: 'node' is a pointer; did you mean to use '->'?
   83 |     if(node.right != NULL) inorder(node.right);
      |                                        ^
      |                                        ->
main.c: In function 'preorder':
main.c:88:22: error: 'node' is a pointer; did you mean to use '->'?
   88 |     printf(" %d",node.key);
      |                      ^
      |                      ->
main.c:89:12: error: 'node' is a pointer; did you mean to use '->'?
   89 |     if(node.left != NULL) preorder(node.left);
      |            ^
      |            ->
main.c:89:40: error: 'node' is a pointer; did you mean to use '->'?
   89 |     if(node.left != NULL) preorder(node.left);
      |                                        ^
      |                                        ->
main.c:90:12: error: 'node' is a pointer; did you mean to use '->'?
   90 |     if(node.right != NULL) preorder(node.right);
      |            ^
      |            ->
main.c:90:41: error: 'node' is a pointer; did you mean to use '->'?
   90 |     if(node.right != NULL) preorder(node.right);
      |                                         ^
      |                                         ->
main.c: In function 'insert':
main.c:97:7: error: assignment to 'node *' {aka 'struct node_t *'} from 'int' makes pointer from integer without a cast [-Wint-conversion]
   97 |     z = newNode(k);
      |       ^
main.c:99:18: error: 'tree_root' is a pointer; did you mean to use '->'?
   99 |     x = tree_root.left;
      |                  ^
      |                  ->
main.c:102:13: error: 'z' is a pointer; did you mean to use '->'?
  102 |         if(z.key < x.key) {
      |             ^
      |             ->
main.c:102:21: error: 'x' is a pointer; did you mean to use '->'?
  102 |         if(z.key < x.key) {
      |                     ^
      |                     ->
main.c:103:18: error: 'x' is a pointer; did you mean to use '->'?
  103 |             x = x.left;
      |                  ^
      |                  ->
main.c:105:18: error: 'x' is a pointer; did you mean to use '->'?
  105 |             x = x.right;
      |                  ^
      |                  ->
main.c:108:6: error: 'z' is a pointer; did you mean to use '->'?
  108 |     z.parent = y;
      |      ^
      |      ->
main.c:109:27: error: 'z' is a pointer; did you mean to use '->'?
  109 |     if(y == tree_root || z.key < y.key) {
      |                           ^
      |                           ->
main.c:109:35: error: 'y' is a pointer; did you mean to use '->'?
  109 |     if(y == tree_root || z.key < y.key) {
      |                                   ^
      |                                   ->
main.c:110:10: error: 'y' is a pointer; did you mean to use '->'?
  110 |         y.left = z;
      |          ^
      |          ->
main.c:112:10: error: 'y' is a pointer; did you mean to use '->'?
  112 |         y.right = z;
      |          ^
      |          ->
main.c: In function 'find':
main.c:120:18: error: 'tree_root' is a pointer; did you mean to use '->'?
  120 |     x = tree_root.left;
      |                  ^
      |                  ->
main.c:121:13: error: 'x' is a pointer; did you mean to use '->'?
  121 |     while (x.key != k) {
      |             ^
      |             ->
main.c:122:13: error: 'x' is a pointer; did you mean to use '->'?
  122 |         if(x.key > k) {
      |             ^
      |             ->
main.c:123:17: error: 'x' is a pointer; did you mean to use '->'?
  123 |             if(x.left == NULL) return NULL;
      |                 ^
      |                 ->
main.c:124:18: error: 'x' is a pointer; did you mean to use '->'?
  124 |             x = x.left;
      |                  ^
      |                  ->
main.c:126:17: error: 'x' is a pointer; did you mean to use '->'?
  126 |             if(x.right == NULL) return NULL;
      |                 ^
      |                 ->
main.c:127:18: error: 'x' is a pointer; did you mean to use '->'?
  127 |             x = x.right;
      |                  ^
      |                  ->
main.c: In function 'deleteNode':
main.c:149:22: error: 'tree_root' is a pointer; did you mean to use '->'?
  149 |     if(x == tree_root.left || x.key < x.parent.key) isLeft = 1;
      |                      ^
      |                      ->
main.c:149:32: error: 'x' is a pointer; did you mean to use '->'?
  149 |     if(x == tree_root.left || x.key < x.parent.key) isLeft = 1;
      |                                ^
      |                                ->
main.c:149:40: error: 'x' is a pointer; did you mean to use '->'?
  149 |     if(x == tree_root.left || x.key < x.parent.key) isLeft = 1;
      |                                        ^
      |                                        ->
main.c:152:9: error: 'x' is a pointer; did you mean to use '->'?
  152 |     if(x.left==NULL && x.right==NULL) {
      |         ^
      |         ->
main.c:152:25: error: 'x' is a pointer; did you mean to use '->'?
  152 |     if(x.left==NULL && x.right==NULL) {
      |                         ^
      |                         ->
main.c:154:14: error: 'x' is a pointer; did you mean to use '->'?
  154 |             x.parent.left = NULL;
      |              ^
      |              ->
main.c:156:14: error: 'x' is a pointer; did you mean to use '->'?
  156 |             x.parent.right = NULL;
      |              ^
      |              ->
main.c:159:16: error: 'x' is a pointer; did you mean to use '->'?
  159 |     } else if(x.right==NULL) {
      |                ^
      |                ->
main.c:161:14: error: 'x' is a pointer; did you mean to use '->'?
  161 |             x.parent.left = x.left;
      |              ^
      |              ->
main.c:161:30: error: 'x' is a pointer; did you mean to use '->'?
  161 |             x.parent.left = x.left;
      |                              ^
      |                              ->
main.c:163:14: error: 'x' is a pointer; did you mean to use '->'?
  163 |             x.parent.right = x.left;
      |              ^
      |              ->
main.c:163:31: error: 'x' is a pointer; did you mean to use '->'?
  163 |             x.parent.right = x.left;
      |                               ^
      |                               ->
main.c:165:10: error: 'x' is a pointer; did you mean to use '->'?
  165 |         x.left.parent = x.parent;
      |          ^
      |          ->
main.c:165:26: error: 'x' is a pointer; did you mean to use '->'?
  165 |         x.left.parent = x.parent;
      |                          ^
      |                          ->
main.c:167:16: error: 'x' is a pointer; did you mean to use '->'?
  167 |     } else if(x.left==NULL) {
      |                ^
      |                ->
main.c:169:14: error: 'x' is a pointer; did you mean to use '->'?
  169 |             x.parent.left = x.right;
      |              ^
      |              ->
main.c:169:30: error: 'x' is a pointer; did you mean to use '->'?
  169 |             x.parent.left = x.right;
      |                              ^
      |                              ->
main.c:171:14: error: 'x' is a pointer; did you mean to use '->'?
  171 |             x.parent.right = x.right;
      |              ^
      |              ->
main.c:171:31: error: 'x' is a pointer; did you mean to use '->'?
  171 |             x.parent.right = x.right;
      |                               ^
      |                               ->
main.c:173:10: error: 'x' is a pointer; did you mean to use '->'?
  173 |         x.right.parent = x.parent;
      |          ^
      |          ->
main.c:173:27: error: 'x' is a pointer; did you mean to use '->'?
  173 |         x.right.parent = x.parent;
      |                           ^
      |                           ->
main.c:176:14: error: 'x' is a pointer; did you mean to use '->'?
  176 |         y = x.right;
      |              ^
      |              ->
main.c:177:16: error: 'y' is a pointer; did you mean to use '->'?
  177 |         while(y.left!=NULL) {
      |                ^
      |                ->
main.c:178:18: error: 'y' is a pointer; did you mean to use '->'?
  178 |             y = y.left;
      |                  ^
      |                  ->
main.c:180:10: error: 'x' is a pointer; did you mean to use '->'?
  180 |         x.key = y.key;
      |          ^
      |          ->
main.c:180:18: error: 'y' is a pointer; did you mean to use '->'?
  180 |         x.key = y.key;