Guest User

Untitled

a guest
Dec 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. // Creates an empty binary tree
  2. template<class T> binary_tree<T>::binary_tree() {
  3.  
  4. }
  5.  
  6. // Cleans up memory before the tree's destruction
  7. template<class T> binary_tree<T>::~binary_tree() {
  8. destruct(root);
  9. }
  10.  
  11. // Inserts the value into the appropriate place in the tree.
  12. template<class T> void binary_tree<T>::insert(T val) {
  13. insert(val,root);
  14. }
  15.  
  16. // Helper function for insert that takes a node argument.
  17. template<class T> void binary_tree<T>::insert(T val, node* n) {
  18. if (n == 0) {
  19. root = new node(val,0,0);
  20. } else {
  21. if (val >= n->val) {
  22. if (n->right == 0) {
  23. n->right = new node(val,0,0);
  24. } else {
  25. insert(val,n->right);
  26. }
  27. } else {
  28. if (n->left == 0) {
  29. n->left = new node(val,0,0);
  30. } else {
  31. insert(val,n->left);
  32. }
  33. }
  34. }
  35. }
  36.  
  37. // Finds the v in the tree such that v == val. It's assumed that
  38. // for the generic type being used that if both v < val and v > val
  39. // are false, then v == val is true. Once v is found, it is returned.
  40. // If no matching value is found in the tree, not_found is thrown.
  41. template<class T> T binary_tree<T>::find(T val) const {
  42. if (find(val,root) == val) {
  43. return val;
  44. } else {
  45. throw not_found();
  46. }
  47. }
  48.  
  49. // Helper function for find that accepts a node argument.
  50. template<class T> T binary_tree<T>::find(T val, node* n) const {
  51. if (n == 0) {
  52. throw not_found();
  53. } else {
  54. if (val == n->val) {
  55. return val;
  56. } else {
  57. if (val < n->val) {
  58. return find(val,n->left);
  59. } else {
  60. return find(val,n->right);
  61. }
  62. }
  63. }
  64. }
  65.  
  66. // Removes a value from the tree. Works identially to find (and operates
  67. // with the same assumptions), except that it removes the value from the
  68. // tree in addition to returning it.
  69. template<class T> T binary_tree<T>::remove(T val) {
  70. return remove(val,root,root);
  71. }
  72.  
  73. // Helper function to remove that accepts a node argument.
  74. template<class T> T binary_tree<T>::remove(T val, node* n, node* parent) {
  75. if (val == parent->val) {
  76. n = parent->right;
  77. while (n->left != 0) {
  78. n = n->left;
  79. }
  80. n->left = parent->left;
  81. } else {
  82. if (val >= parent->val) {
  83. remove(val,parent->right,parent->right);
  84. } else {
  85. remove(val,parent->left,parent->left);
  86. }
  87. }
  88.  
  89. return val;
  90. }
  91.  
  92. // Helper function for the destructor that recursively deletes all
  93. // nodes in the tree.
  94. template<class T> void binary_tree<T>::destruct(node* n) {
  95. if (n != 0) {
  96. if (n->left != 0) {
  97. destruct(n->left);
  98. }
  99. if (n->right != 0) {
  100. destruct(n->right);
  101. }
  102. delete n;
  103. }
  104. }
Add Comment
Please, Sign In to add comment