Advertisement
Sanlover

Untitled

Dec 2nd, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. void
  2. Tree::addMethod(Node*& node, const int& value)
  3. {
  4. if (node == nullptr)
  5. node = new Node(value);
  6. else if (value > node->value)
  7. addMethod(node->right, value);
  8. else
  9. addMethod(node->left, value);
  10. }
  11.  
  12. void
  13. Tree::deleteMethod(Node* node)
  14. {
  15. if (node->left != nullptr)
  16. deleteMethod(node->left);
  17.  
  18. if (node->right != nullptr)
  19. deleteMethod(node->right);
  20. delete node;
  21. }
  22.  
  23. void Tree::eraseMethod(const int& value)
  24. {
  25. }
  26.  
  27. int
  28. Tree::heightMethod(Node* node)
  29. {
  30. if (node == 0)
  31. return 0;
  32. int left, right;
  33. if (node->left != NULL)
  34. left = heightMethod(node->left);
  35. else
  36. left = -1;
  37. if (node->right != NULL)
  38. right = heightMethod(node->right);
  39. else
  40. right = -1;
  41. int max = left > right ? left : right;
  42. return max + 1;
  43. }
  44.  
  45. bool Tree::empty()
  46. {
  47. return head == nullptr;
  48. }
  49.  
  50. void Tree::clear()
  51. {
  52. deleteMethod(head);
  53. head = nullptr;
  54. }
  55.  
  56. int Tree::height()
  57. {
  58. return heightMethod(head);
  59. }
  60.  
  61. void Tree::add(const int& value)
  62. {
  63. addMethod(head, value);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement