Advertisement
ahmad_zizo

Untitled

Jun 18th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. node* delete(node* tree, int x)
  2. {
  3. if (tree == NULL)
  4. {
  5. return tree;
  6. }
  7. if (x < tree->data)
  8. //delete node from left subtree
  9. tree->left = delete(tree->left, x);
  10. else if (x > tree->data)
  11. //delete node from right subtree
  12. tree->right = delete(tree->right, x);
  13.  
  14. else
  15. {
  16.  
  17. if (tree->left == NULL)
  18. {
  19. node* temp = tree->right;
  20. free(tree);
  21. return temp;
  22. }
  23. else if (tree->right == NULL)
  24. {
  25. node* temp = tree->left;
  26. free(tree);
  27. return temp;
  28. }
  29.  
  30. node* temp = min_value_node(tree->right);
  31.  
  32. tree->data = temp->data;
  33. tree->right = delete(tree->right, temp->data);
  34. }
  35. return tree;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement