fork(1) download
  1. #include <stdio.h>
  2. #include<malloc.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node *left,*right;
  8. };
  9.  
  10. struct node *root=NULL;
  11.  
  12. int findmin(struct node *root);
  13.  
  14. struct node *insert(struct node *root,int val)
  15. {
  16. struct node *par=NULL,*cur,*nn;
  17. nn=(struct node*)malloc(sizeof(struct node*));
  18. nn->data=val;
  19. nn->left=nn->right=NULL;
  20.  
  21. if(root==NULL)
  22. {
  23. root=nn;
  24. return root;
  25. }
  26.  
  27. cur=root;
  28.  
  29. while(cur!=NULL)
  30. {
  31. par=cur;
  32. if(val<cur->data)
  33. cur=cur->left;
  34. else
  35. cur=cur->right;
  36. }
  37.  
  38. if(val<par->data)
  39. par->left=nn;
  40. else
  41. par->right=nn;
  42.  
  43. return root;
  44. }
  45.  
  46. struct node *Delete(struct node *root,int val)
  47. {
  48. if(root == NULL) return root;
  49. else if(data < root->data) root->left = Delete(root->left,data);
  50. else if (data > root->data) root->right = Delete(root->right,data);
  51. // Wohoo... I found you, Get ready to be deleted
  52. else {
  53. // Case 1: No child
  54. if(root->left == NULL && root->right == NULL) {
  55. delete root;
  56. root = NULL;
  57. }
  58. //Case 2: One child
  59. else if(root->left == NULL) {
  60. struct Node *temp = root;
  61. root = root->right;
  62. delete temp;
  63. }
  64. else if(root->right == NULL) {
  65. struct Node *temp = root;
  66. root = root->left;
  67. delete temp;
  68. }
  69. // case 3: 2 children
  70. else {
  71. struct Node *temp = FindMin(root->right);
  72. root->data = temp->data;
  73. root->right = Delete(root->right,temp->data);
  74. }
  75. }
  76. return root;
  77. }
  78.  
  79. else
  80. {
  81. ptr=findmin(cur->right);
  82. cur->data=ptr->data;
  83. cur=delete(cur->right,ptr->data);
  84. if(par->left==cur)
  85. par->left=ptr;
  86. else
  87. par->right=ptr;
  88. free(cur);
  89. return root;
  90.  
  91. }
  92.  
  93.  
  94. }
  95.  
  96. void inorder(struct node *root)
  97. {
  98. if(root!=NULL)
  99. {
  100. inorder(root->left);
  101. printf("\t%d",root->data);
  102. inorder(root->right);
  103. }
  104. }
  105.  
  106. int findmin(struct node *root)
  107. {
  108. if((root==NULL)||(root->left==NULL))
  109. return root;
  110. else
  111. return findmin(root->left);
  112.  
  113. }
  114.  
  115. int main()
  116. {
  117. int ln,tn,s,l;
  118.  
  119. root=insert(root,23);
  120. root=insert(root,20);
  121. root=insert(root,25);
  122. root=insert(root,21);
  123. root=insert(root,19);
  124. root=insert(root,24);
  125. root=insert(root,27);
  126. root=insert(root,28);
  127. root=insert(root,26);
  128. root=insert(root,29);
  129. root=insert(root,22);
  130.  
  131. root=delete(root,27);
  132.  
  133. printf("\n");
  134. inorder(root);
  135.  
  136. printf("\n");
  137. return 0;
  138. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin

compilation info
prog.c: In function 'Delete':
prog.c:49:10: error: 'data' undeclared (first use in this function)
  else if(data < root->data) root->left = Delete(root->left,data);
          ^
prog.c:49:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:55:4: error: unknown type name 'delete'
    delete root;
    ^
prog.c:56:9: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
    root = NULL;
         ^
prog.c:55:11: warning: variable 'root' set but not used [-Wunused-but-set-variable]
    delete root;
           ^
prog.c:60:24: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
    struct Node *temp = root;
                        ^
prog.c:62:4: error: unknown type name 'delete'
    delete temp;
    ^
prog.c:62:11: error: conflicting types for 'temp'
    delete temp;
           ^
prog.c:60:17: note: previous definition of 'temp' was here
    struct Node *temp = root;
                 ^
prog.c:62:11: warning: unused variable 'temp' [-Wunused-variable]
    delete temp;
           ^
prog.c:65:24: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
    struct Node *temp = root;
                        ^
prog.c:67:4: error: unknown type name 'delete'
    delete temp;
    ^
prog.c:67:11: error: conflicting types for 'temp'
    delete temp;
           ^
prog.c:65:17: note: previous definition of 'temp' was here
    struct Node *temp = root;
                 ^
prog.c:67:11: warning: unused variable 'temp' [-Wunused-variable]
    delete temp;
           ^
prog.c:71:24: warning: implicit declaration of function 'FindMin' [-Wimplicit-function-declaration]
    struct Node *temp = FindMin(root->right);
                        ^
prog.c:71:24: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
prog.c:72:21: error: dereferencing pointer to incomplete type 'struct Node'
    root->data = temp->data;
                     ^
prog.c: At top level:
prog.c:79:2: error: expected identifier or '(' before 'else'
  else
  ^
prog.c:94:1: error: expected identifier or '(' before '}' token
 }
 ^
prog.c: In function 'findmin':
prog.c:109:10: warning: return makes integer from pointer without a cast [-Wint-conversion]
   return root;
          ^
prog.c: In function 'main':
prog.c:131:7: warning: implicit declaration of function 'delete' [-Wimplicit-function-declaration]
  root=delete(root,27);
       ^
prog.c:131:6: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  root=delete(root,27);
      ^
prog.c:117:14: warning: unused variable 'l' [-Wunused-variable]
  int ln,tn,s,l;
              ^
prog.c:117:12: warning: unused variable 's' [-Wunused-variable]
  int ln,tn,s,l;
            ^
prog.c:117:9: warning: unused variable 'tn' [-Wunused-variable]
  int ln,tn,s,l;
         ^
prog.c:117:6: warning: unused variable 'ln' [-Wunused-variable]
  int ln,tn,s,l;
      ^
stdout
Standard output is empty