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 data)
  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. free(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. free(temp);
  63. }
  64. else if(root->right == NULL) {
  65. struct Node *temp = root;
  66. root = root->left;
  67. free(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.  
  80. void inorder(struct node *root)
  81. {
  82. if(root!=NULL)
  83. {
  84. inorder(root->left);
  85. printf("\t%d",root->data);
  86. inorder(root->right);
  87. }
  88. }
  89.  
  90. int findmin(struct node *root)
  91. {
  92. if((root==NULL)||(root->left==NULL))
  93. return root;
  94. else
  95. return findmin(root->left);
  96.  
  97. }
  98.  
  99. int main()
  100. {
  101. int ln,tn,s,l;
  102.  
  103. root=insert(root,23);
  104. root=insert(root,20);
  105. root=insert(root,25);
  106. root=insert(root,21);
  107. root=insert(root,19);
  108. root=insert(root,24);
  109. root=insert(root,27);
  110. root=insert(root,28);
  111. root=insert(root,26);
  112. root=insert(root,29);
  113. root=insert(root,22);
  114.  
  115. root=delete(root,27);
  116.  
  117. printf("\n");
  118. inorder(root);
  119.  
  120. printf("\n");
  121. return 0;
  122. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin

compilation info
prog.c: In function 'Delete':
prog.c:60:24: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
    struct Node *temp = root;
                        ^
prog.c:65:24: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
    struct Node *temp = root;
                        ^
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: In function 'findmin':
prog.c:93:10: warning: return makes integer from pointer without a cast [-Wint-conversion]
   return root;
          ^
prog.c: In function 'main':
prog.c:115:7: warning: implicit declaration of function 'delete' [-Wimplicit-function-declaration]
  root=delete(root,27);
       ^
prog.c:115:6: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  root=delete(root,27);
      ^
prog.c:101:14: warning: unused variable 'l' [-Wunused-variable]
  int ln,tn,s,l;
              ^
prog.c:101:12: warning: unused variable 's' [-Wunused-variable]
  int ln,tn,s,l;
            ^
prog.c:101:9: warning: unused variable 'tn' [-Wunused-variable]
  int ln,tn,s,l;
         ^
prog.c:101:6: warning: unused variable 'ln' [-Wunused-variable]
  int ln,tn,s,l;
      ^
stdout
Standard output is empty