fork download
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. int emptybit; //empty bit 1 means node is empty
  7. char colour;
  8. struct node* parent;
  9. struct node* left_child;
  10. struct node* right_child;
  11. };
  12. struct node* root_node;
  13. void visit(struct node* x)
  14. {
  15. if(x!=NULL){
  16. visit(x->left_child);
  17. if(x->parent==NULL){
  18. printf("%d %c NIL\n",x->data,x->colour);
  19. }
  20. else{
  21. printf("%d %c %d\n",x->data,x->colour,x->parent->data);
  22. }
  23. visit(x->right_child);
  24. }
  25. return;
  26. }
  27. // pre_fix_tour function will do the the tour of tree
  28. void pre_fix_tour(struct node* x)
  29. {
  30. if(x==NULL)
  31. printf("empty tree");
  32. else
  33. visit(x);
  34. return;
  35. }
  36.  
  37.  
  38. int main()
  39. {
  40. int i,n;
  41. int a[100];
  42. struct node RBtree;
  43. scanf("%d",&n);
  44. for(i=0;i<n;i++){
  45. scanf("%d",&a[i]);
  46. }
  47. // all the elements to be inserted in the tree are stored in array a
  48. RBtree.data=a[0];
  49. RBtree.emptybit=0;
  50. RBtree.colour= 'R';
  51. RBtree.parent=NULL;
  52. //declaration of RBtree
  53. root_node=&RBtree;
  54. root_node->colour='B';
  55. pre_fix_tour(root_node);
  56. return 0;
  57. }
Runtime error #stdin #stdout 0s 9432KB
stdin
5 6 7 8 59 9 
stdout
Standard output is empty