Advertisement
ahmad_zizo

DELETE

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