fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node{
  5. int data;
  6. struct node *left,*right;
  7. };
  8.  
  9. struct node* insert(struct node* tree,int d)
  10. {
  11. if(tree==NULL)
  12. {
  13. struct node* temp=(struct node*)malloc(sizeof(struct node));
  14. temp->data=d;
  15. temp->right=temp->left=NULL;
  16. tree=temp;
  17. return tree;
  18. }
  19. if(d<tree->data)
  20. tree->left=insert(tree->left,d);
  21. else if(d>tree->data)
  22. tree->right=insert(tree->right,d);
  23. return tree;
  24. }
  25. struct node * minValueNode(struct node* node)
  26. {
  27. struct node* current = node;
  28.  
  29. /* loop down to find the leftmost leaf */
  30. while (current && current->left != NULL)
  31. current = current->left;
  32.  
  33. return current;
  34. }
  35.  
  36. /* Given a binary search tree and a key, this function deletes the key
  37.   and returns the new root */
  38. struct node* deleteNode(struct node* root, int key)
  39. {
  40. // base case
  41. if (root == NULL) return root;
  42.  
  43. // If the key to be deleted is smaller than the root's key,
  44. // then it lies in left subtree
  45. if (key < root->data)
  46. root->left = deleteNode(root->left, key);
  47.  
  48. // If the key to be deleted is greater than the root's key,
  49. // then it lies in right subtree
  50. else if (key > root->data)
  51. root->right = deleteNode(root->right, key);
  52.  
  53. // if key is same as root's key, then This is the node
  54. // to be deleted
  55. else
  56. {
  57. // node with only one child or no child
  58. if (root->left == NULL)
  59. {
  60. struct node *temp = root->right;
  61. free(root);
  62. return temp;
  63. }
  64. else if (root->right == NULL)
  65. {
  66. struct node *temp = root->left;
  67. free(root);
  68. return temp;
  69. }
  70.  
  71. // node with two children: Get the inorder successor (smallest
  72. // in the right subtree)
  73. struct node* temp = minValueNode(root->right);
  74.  
  75. // Copy the inorder successor's content to this node
  76. root->data = temp->data;
  77.  
  78. // Delete the inorder successor
  79. root->right = deleteNode(root->right, temp->data);
  80. }
  81. return root;
  82. }
  83.  
  84. void inorder(struct node *root)
  85. {
  86. if (root != NULL)
  87. {
  88. inorder(root->left);
  89. printf("%d \n", root->data);
  90. inorder(root->right);
  91. }
  92. }
  93.  
  94. int main(void)
  95. {
  96. struct node* tree=NULL;
  97. printf("enter the number of elements : ");
  98. int n;
  99. scanf("%d",&n);
  100. int i,d;
  101. for(i=0;i<n;i++)
  102. {
  103. printf("enter the value : ");
  104. scanf("%d",&d);
  105. tree=insert(tree,d);
  106. }
  107. inorder(tree);
  108. tree=deleteNode(tree,33);
  109. inorder(tree);
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
Success #stdin #stdout 0s 4324KB
stdin
5
11 22 33 44 55 

stdout
enter the number of elements : enter the value : enter the value : enter the value : enter the value : enter the value : 11 
22 
33 
44 
55 
11 
22 
44 
55