fork download
  1. int insert(int n, struct node *start)
  2. {
  3. //if node doesnt exist then add it to the tree otherwise increase amount of keys
  4.  
  5. //if tree is empty add root
  6. if (root == NULL)
  7. {
  8. root = (struct node*)malloc(sizeof *root);
  9. root->value = n;
  10. root->keys_amount=0;
  11. root->left = NULL;
  12. root->right = NULL;
  13. root->up = NULL;
  14.  
  15. }
  16. else if(search(root,n)!=NULL)
  17. {
  18. struct node *tmp = search(root, n);
  19. tmp->keys_amount += 1;
  20. return 0;
  21. }
  22. //if value is lower than root val then go to left son
  23. else if(n < start->value)
  24. {
  25. //if left son exist then apply function insert for it
  26. if(start->left != NULL)
  27. {
  28. insert(n,start->left);
  29. }
  30. //if it doesnt exist then create
  31. else
  32. {
  33. struct node *new = (struct node*)malloc(sizeof *root);
  34. new->value = n;
  35. new->keys_amount=0;
  36. new->left = NULL;
  37. new->right = NULL;
  38. new->up = start;
  39. start->left=new;
  40. correct_tree(new);
  41. }
  42. }
  43. //if new value is higher than root
  44. else
  45. {
  46. //if right son exist then apply function for it
  47. if(start->right!=NULL)
  48. {
  49. insert(n,start->right);
  50. }
  51. //if it doesnt exist create new one
  52. else
  53. {
  54. struct node *new = (struct node*)malloc(sizeof *root);
  55. new->value = n;
  56. new->keys_amount=0;
  57. new->left = NULL;
  58. new->right = NULL;
  59. new->up = start;
  60. start->right=new;
  61. correct_tree(new);
  62. }
  63. }
  64. return 0;
  65. }
  66.  
  67. //////////////////////////////////////////////////////////////////
  68.  
  69. void correct_tree(struct node *start)
  70. {
  71. struct node *tmp = (struct node*)malloc(sizeof *root);
  72. start->color = 'R';
  73. while((start != root) && (start->up->color == 'R'))
  74. {
  75. if(start->up == start->up->up->left)
  76. {
  77. tmp = start->up->up->right; //uncle of start for tmp
  78. if(tmp->color == 'R') //case 1
  79. {
  80. start->up->color = 'B';
  81. tmp->color = 'B';
  82. start->up->up->color='R';
  83. start = start->up->up;
  84. continue;
  85. }
  86. if(start == start->up->right) //case 2
  87. {
  88. start = start->up;
  89. rot_L(start);
  90. }
  91. start->up->color = 'B'; //case3
  92. start->up->up->color = 'R';
  93. rot_R(start->up->up);
  94. break;
  95. }
  96. else //mirror cases
  97. {
  98. tmp = start->up->up->left;
  99. if(tmp->color == 'R') //case 1
  100. {
  101. start->up->color = 'B';
  102. tmp->color = 'B';
  103. start->up->up->color='R';
  104. start = start->up->up;
  105. continue;
  106. }
  107. if(start == start->up->left) //case 2
  108. {
  109. start = start->up;
  110. rot_R(start);
  111. }
  112. start->up->color = 'B'; //case3
  113. start->up->up->color = 'R';
  114. rot_L(start->up->up);
  115. break;
  116. }
  117. }
  118. root->color = 'B';
  119. }
  120.  
  121. //////////////////////////////////////////////////////////////
  122.  
  123. void rot_L(struct node *start)
  124. {
  125. struct node *tmp = (struct node*)malloc(sizeof *root);
  126. struct node *tmp2 = (struct node*)malloc(sizeof *root);
  127. tmp = start->right;
  128. if(tmp != NULL)
  129. {
  130. tmp2 = start->up;
  131. start->right = tmp->left;
  132. if(start->right != NULL)
  133. start->right->up = start;
  134. tmp->left = start;
  135. tmp->up = tmp2;
  136. start->up = tmp;
  137. if(tmp2 != NULL)
  138. {
  139. if(tmp2->left == start)
  140. tmp2->left = tmp;
  141. else
  142. tmp2->right = tmp;
  143. }
  144. else root = tmp;
  145. }
  146.  
  147. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:26: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
 int insert(int n, struct node *start)
                          ^~~~
prog.c: In function ‘insert’:
prog.c:6:9: error: ‘root’ undeclared (first use in this function)
     if (root == NULL)
         ^~~~
prog.c:6:9: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:17: error: ‘NULL’ undeclared (first use in this function)
     if (root == NULL)
                 ^~~~
prog.c:8:30: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
         root = (struct node*)malloc(sizeof *root);
                              ^~~~~~
prog.c:8:30: warning: incompatible implicit declaration of built-in function ‘malloc’
prog.c:8:30: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
prog.c:16:13: warning: implicit declaration of function ‘search’ [-Wimplicit-function-declaration]
     else if(search(root,n)!=NULL)
             ^~~~~~
prog.c:19:12: error: dereferencing pointer to incomplete type ‘struct node’
         tmp->keys_amount += 1;
            ^~
prog.c:33:38: warning: incompatible implicit declaration of built-in function ‘malloc’
     struct node *new = (struct node*)malloc(sizeof *root);
                                      ^~~~~~
prog.c:33:38: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
prog.c:40:5: warning: implicit declaration of function ‘correct_tree’ [-Wimplicit-function-declaration]
     correct_tree(new);
     ^~~~~~~~~~~~
prog.c:54:38: warning: incompatible implicit declaration of built-in function ‘malloc’
     struct node *new = (struct node*)malloc(sizeof *root);
                                      ^~~~~~
prog.c:54:38: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
prog.c: At top level:
prog.c:69:26: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
 void correct_tree(struct node *start)
                          ^~~~
prog.c:69:6: warning: conflicting types for ‘correct_tree’
 void correct_tree(struct node *start)
      ^~~~~~~~~~~~
prog.c:40:5: note: previous implicit declaration of ‘correct_tree’ was here
     correct_tree(new);
     ^~~~~~~~~~~~
prog.c: In function ‘correct_tree’:
prog.c:71:38: warning: incompatible implicit declaration of built-in function ‘malloc’
     struct node *tmp = (struct node*)malloc(sizeof *root);
                                      ^~~~~~
prog.c:71:38: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
prog.c:71:53: error: ‘root’ undeclared (first use in this function)
     struct node *tmp = (struct node*)malloc(sizeof *root);
                                                     ^~~~
prog.c:72:10: error: dereferencing pointer to incomplete type ‘struct node’
     start->color = 'R';
          ^~
prog.c:89:17: warning: implicit declaration of function ‘rot_L’ [-Wimplicit-function-declaration]
                 rot_L(start);
                 ^~~~~
prog.c:93:13: warning: implicit declaration of function ‘rot_R’ [-Wimplicit-function-declaration]
             rot_R(start->up->up);
             ^~~~~
prog.c: At top level:
prog.c:123:19: warning: ‘struct node’ declared inside parameter list will not be visible outside of this definition or declaration
 void rot_L(struct node *start)
                   ^~~~
prog.c:123:6: warning: conflicting types for ‘rot_L’
 void rot_L(struct node *start)
      ^~~~~
prog.c:89:17: note: previous implicit declaration of ‘rot_L’ was here
                 rot_L(start);
                 ^~~~~
prog.c: In function ‘rot_L’:
prog.c:125:38: warning: incompatible implicit declaration of built-in function ‘malloc’
     struct node *tmp = (struct node*)malloc(sizeof *root);
                                      ^~~~~~
prog.c:125:38: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
prog.c:125:53: error: ‘root’ undeclared (first use in this function)
     struct node *tmp = (struct node*)malloc(sizeof *root);
                                                     ^~~~
prog.c:127:16: error: dereferencing pointer to incomplete type ‘struct node’
     tmp = start->right;
                ^~
prog.c:128:15: error: ‘NULL’ undeclared (first use in this function)
     if(tmp != NULL)
               ^~~~
stdout
Standard output is empty