fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6. struct node *left;
  7. char color;
  8. struct node *right;
  9. struct node *p;
  10. int data;
  11. }node;
  12. node *root = NULL;
  13.  
  14. struct node *newNode(int key)
  15. {
  16. struct node *temp = (struct node *)malloc(sizeof(struct node));
  17. temp->data = key;
  18. temp->left = temp->right = NULL;
  19. temp->color='R';
  20. return temp;
  21. }
  22.  
  23. void left_rotate( node *x ) {
  24. node *y;
  25. y = x->right;
  26. x->right = y->left;
  27. if ( y->left != NULL )
  28. y->left->p = x;
  29. y->p = x->p;
  30.  
  31. if ( x->p == NULL ) root = y;
  32. else
  33. if ( x == (x->p)->left )
  34. x->p->left = y;
  35. else
  36.  
  37. x->p->right = y;
  38. y->left = x;
  39. x->p = y;
  40. return;
  41. }
  42.  
  43. void right_rotate( node *x ) {
  44. node *y;
  45. y = x->left;
  46. x->left = y->right;
  47. if ( y->right != NULL )
  48. y->right->p = x;
  49. y->p = x->p;
  50. if ( x->p == NULL ) T->root = y;
  51. else
  52. if ( x == (x->p)->right )
  53. x->p->right = y;
  54. else
  55. x->p->left = y;
  56. y->right = x;
  57. x->p = y;
  58. return;
  59. }
  60.  
  61.  
  62.  
  63. void rbinsert (int val) {
  64. node *z=newNode(val);
  65. node *x = root;
  66. node *y;
  67. while (x ≠ NULL)
  68. {
  69. y = x;
  70. if (z->data < x->data)
  71. x = x->left;
  72. else x = x->right;
  73. }
  74.  
  75. z->p = y;
  76. if (y == NULL)
  77. root = z;
  78. else if (z->data < y->data)
  79. y->left = z;
  80. else y->right = z;
  81. z->left = NULL;
  82. z->right = NULL;
  83. z->color = 'R';
  84. rb_fix(z) ;
  85. return;
  86. }
  87.  
  88.  
  89. void rb_fix(int val)
  90. {
  91. node *z=newNode(val);
  92. while (z->p->color = 'R')
  93. {
  94. if (z->p == z->p->p->left)
  95. {
  96. y=z->p->p->right;
  97. if(y->color=='R')
  98. {
  99. z->p->color='B';
  100. y->color='B';
  101. z->p->p->color='R';
  102. z=z->p->p;
  103. }
  104. else
  105. {
  106. if (z==z->p->right)
  107. {
  108. z=z->p;
  109. left_rotate(z);
  110. }
  111. z->p->color='B';
  112. z->p->p->color='R';
  113. right_rotate(z->p->p);
  114. }
  115. else
  116. {
  117. y=z->p->p->left;
  118. if(y->color=='R')
  119. {
  120. z->p->color='B';
  121. y->color='B';
  122. z->p->p->color='R';
  123. z=z->p->p;
  124. }
  125. else
  126. {
  127. if (z==z->p->left)
  128. {
  129. z=z->p;
  130. right_rotate(z);
  131. }
  132. z->p->color='B';
  133. z->p->p->color='R';
  134. left_rotate(z->p->p);
  135. }
  136. }
  137. }
  138. root->color='B';
  139. }
  140.  
  141. void prefixtour(node *p)
  142. {
  143. if (p==NULL)
  144. {
  145. printf("empty tree");
  146. return ;
  147. }
  148.  
  149. else
  150. visit(p);
  151. return ;
  152. }
  153.  
  154. void visit( node *x)
  155. {
  156. if(x!=NULL)
  157. {
  158.  
  159. visit(x->left);
  160. if(x->p==NULL)
  161. printf( "%d %c NIL\n",x->data,x->color);
  162. else
  163. printf( "%d %c %d\n",x->data,x->color,x->p->data);
  164. visit(x->right);
  165. }
  166. return;
  167. }
  168.  
  169. int main()
  170. {
  171. int n;
  172. scanf("%d",&n);
  173. int i,key;
  174. for(i=0;i<n;i++)
  175. {
  176. scanf("%d",&key);
  177. rb_insert(key);
  178. }
  179. prefixtour(root);
  180. return 0;
  181.  
  182. }
  183.  
  184.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘right_rotate’:
prog.c:50:25: error: ‘T’ undeclared (first use in this function)
     if ( x->p == NULL ) T->root = y;
                         ^
prog.c:50:25: note: each undeclared identifier is reported only once for each function it appears in
prog.c: In function ‘rbinsert’:
prog.c:67:11: error: stray ‘\342’ in program
  while (x ≠ NULL)
           ^
prog.c:67:12: error: stray ‘\211’ in program
  while (x ≠ NULL)
            ^
prog.c:67:13: error: stray ‘\240’ in program
  while (x ≠ NULL)
             ^
prog.c:67:9: error: called object ‘x’ is not a function or function pointer
  while (x ≠ NULL)
         ^
prog.c:65:11: note: declared here
     node *x = root;
           ^
prog.c:84:2: warning: implicit declaration of function ‘rb_fix’ [-Wimplicit-function-declaration]
  rb_fix(z) ;
  ^~~~~~
prog.c: At top level:
prog.c:89:7: warning: conflicting types for ‘rb_fix’
  void rb_fix(int val)
       ^~~~~~
prog.c:84:2: note: previous implicit declaration of ‘rb_fix’ was here
  rb_fix(z) ;
  ^~~~~~
prog.c: In function ‘rb_fix’:
prog.c:92:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   while (z->p->color = 'R')
   ^~~~~
prog.c:96:9: error: ‘y’ undeclared (first use in this function)
         y=z->p->p->right;
         ^
prog.c:115:9: error: expected ‘}’ before ‘else’
         else
         ^~~~
prog.c: In function ‘prefixtour’:
prog.c:150:3: warning: implicit declaration of function ‘visit’ [-Wimplicit-function-declaration]
   visit(p);
   ^~~~~
prog.c: At top level:
prog.c:154:6: warning: conflicting types for ‘visit’
 void visit( node *x)
      ^~~~~
prog.c:150:3: note: previous implicit declaration of ‘visit’ was here
   visit(p);
   ^~~~~
prog.c: In function ‘main’:
prog.c:177:6: warning: implicit declaration of function ‘rb_insert’ [-Wimplicit-function-declaration]
      rb_insert(key);
      ^~~~~~~~~
stdout
Standard output is empty