Advertisement
ahmad_zizo

DELETE

May 28th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. void delete_id(node_id* tree, int id) {
  2. if (tree == NULL) {
  3. return;
  4. }
  5. if (id < tree->id)
  6. //delete node from left subtree
  7. delete_id(tree->left, id);
  8. else if (id > tree->id)
  9. //delete node from right subtree
  10. delete_id(tree->right, id);
  11.  
  12. else {
  13.  
  14. if (tree->left == NULL) {
  15. node_id* temp = tree->right;
  16. delete_name(tree, temp->name);
  17. printf("%d, %s has been deleted\n\n",temp->id,temp->name);
  18. free(tree);
  19. return;
  20. }
  21. else if (tree->right == NULL) {
  22. node_id* temp = tree->left;
  23. delete_name(tree, temp->name);
  24. printf("%d, %s has been deleted\n\n",temp->id,temp->name);
  25. free(tree);
  26. return;
  27. }
  28.  
  29. node_id* temp = min_value_node_id(tree->right);
  30.  
  31. tree->id = temp->id;
  32. strcpy(tree->name, temp->name);
  33. tree->right = delete_id(tree->right, temp->id);
  34. }
  35. return;
  36. }
  37.  
  38. void delete_name(node_id* tree, char* name) {
  39. if (tree == NULL) {
  40. return;
  41. }
  42.  
  43.  
  44. if (strcmp(name, tree->name) > 0)
  45. delete_name(tree->left, name);
  46. else if (strcmp(name, tree->name) < 0) {
  47. delete_name(tree->right, name);
  48. }
  49. else {
  50. if (tree->left == NULL) {
  51. node_id* temp = tree->right;
  52. delete_id(tree, temp->id);
  53. printf("%d, %s has been deleted\n\n",temp->id,temp->name);
  54. free(tree);
  55. return;
  56. }
  57. else if (tree->right == NULL) {
  58. node_id* temp = tree->left;
  59. delete_id(tree, temp->id);
  60. printf("%d, %s has been deleted\n\n",temp->id,temp->name);
  61. free(tree);
  62. return;
  63. }
  64. node_id* temp = min_value_node_id(tree->right);
  65. strcpy(tree->name, temp->name);
  66. tree->id = temp->id;
  67.  
  68. delete_name(tree->right, temp->name);
  69. }
  70.  
  71. return;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement