Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. #ifndef __PROJ_THREE_AVL_HPP
  2. #define __PROJ_THREE_AVL_HPP
  3.  
  4. #include "runtimeexcept.hpp"
  5. #include <string>
  6. #include <vector>
  7.  
  8. class ElementNotFoundException : public RuntimeException
  9. {
  10. public:
  11. ElementNotFoundException(const std::string & err) : RuntimeException(err) {}
  12. };
  13.  
  14. template<typename Key, typename Value>
  15. class Node{
  16. public:
  17. Key key;
  18. Value value;
  19. Node<Key, Value>* left;
  20. Node<Key, Value>* right;
  21. };
  22.  
  23. template<typename Key, typename Value>
  24. class MyAVLTree{
  25. private:
  26. Node<Key, Value>* avl;
  27. int total_size = 0;
  28. // fill in private member data here
  29. // If you need to declare private functions, do so here too.
  30.  
  31. public:
  32. MyAVLTree();
  33.  
  34. // In general, a copy constructor and assignment operator
  35. // are good things to have.
  36. // For this quarter, I am not requiring these.
  37. // MyAVLTree(const MyAVLTree & st);
  38. // MyAVLTree & operator=(const MyAVLTree & st);
  39.  
  40.  
  41. // The destructor is, however, required.
  42. ~MyAVLTree(){
  43. // TODO
  44. }
  45.  
  46. // size() returns the number of distinct keys in the tree.
  47. size_t size() const noexcept;
  48.  
  49. // isEmpty() returns true if and only if the tree has no values in it.
  50. bool isEmpty() const noexcept;
  51.  
  52. // contains() returns true if and only if there
  53. // is a (key, value) pair in the tree
  54. // that has the given key as its key.
  55. bool contains(const Key & k) const;
  56.  
  57. // find returns the value associated with the given key
  58. // If !contains(k), this will throw an ElementNotFoundException
  59. // There needs to be a version for const and non-const MyAVLTrees.
  60. Value & find(const Key & k);
  61. const Value & find(const Key & k) const;
  62.  
  63. // Inserts the given key-value pair into
  64. // the tree and performs the AVL re-balance
  65. // operation, as described in lecture.
  66. // If the key already exists in the tree,
  67. // you may do as you please (no test cases in
  68. // the grading script will deal with this situation)
  69. void insert(const Key & k, const Value & v);
  70.  
  71. // in general, a "remove" function would be here
  72. // It's a little trickier with an AVL tree
  73. // and I am not requiring it for this quarter's ICS 46.
  74. // You should still read about the remove operation
  75. // in your textbook.
  76.  
  77. // The following three functions all return
  78. // the set of keys in the tree as a standard vector.
  79. // Each returns them in a different order.
  80. std::vector<Key> inOrder() const;
  81. std::vector<Key> preOrder() const;
  82. std::vector<Key> postOrder() const;
  83. };
  84.  
  85.  
  86. template<typename Key, typename Value>
  87. MyAVLTree<Key,Value>::MyAVLTree(){
  88. avl = nullptr; // Initialize AVL tree
  89. }
  90.  
  91. template<typename Key, typename Value>
  92. size_t MyAVLTree<Key, Value>::size() const noexcept{
  93. return total_size; // stub
  94. }
  95.  
  96. template<typename Key, typename Value>
  97. bool MyAVLTree<Key, Value>::isEmpty() const noexcept{
  98. if(avl == nullptr){
  99. return true;
  100. }
  101. return false;
  102. }
  103.  
  104.  
  105. template<typename Key, typename Value>
  106. bool MyAVLTree<Key, Value>::contains(const Key &k) const{
  107. Node<Key, Value>* newNode = avl;
  108. do
  109. {
  110. if(newNode == nullptr)
  111. {
  112. return false;
  113. }
  114. else if(newNode->key == k)
  115. {
  116. return true;
  117. }
  118. else if(k < newNode->key)
  119. {
  120. newNode = newNode->left;
  121.  
  122. }
  123. else
  124. {
  125. newNode = newNode->right;
  126. }
  127. }while(newNode != nullptr);
  128.  
  129. return false;
  130. }
  131.  
  132.  
  133. template<typename Key, typename Value>
  134. Value & MyAVLTree<Key, Value>::find(const Key & k){
  135. Value v;
  136. return v; // not only a stub, but a terrible idea.
  137. }
  138.  
  139. template<typename Key, typename Value>
  140. const Value & MyAVLTree<Key, Value>::find(const Key & k) const{
  141.  
  142. Value v;
  143. return v; // not only a stub, but a terrible idea.
  144. }
  145.  
  146. template<typename Key, typename Value>
  147. void MyAVLTree<Key, Value>::insert(const Key & k, const Value & v){
  148. // Create new node with values
  149. Node<Key, Value>* newNode = new Node<Key, Value>;
  150. newNode->key = k;
  151. newNode->value = v;
  152. newNode->left = nullptr;
  153. newNode->right = nullptr;
  154. // Assign new node to AVL tree if the tree is empty
  155. if(avl == nullptr){
  156. avl = newNode;
  157. total_size++;
  158. }
  159. else{
  160. Node<Key, Value>* travelNode = avl;
  161. do{
  162. if(k == travelNode->key) //If the current key is correct
  163. break;
  164. else if(k > travelNode->key){
  165. if(travelNode->right == nullptr)
  166. {
  167. travelNode->right = newNode;
  168. total_size++;
  169. }
  170. else
  171. {
  172. travelNode = travelNode->right;
  173. }
  174.  
  175.  
  176. }
  177. else{
  178. if(travelNode->left == nullptr)
  179. {
  180. travelNode->left = newNode;
  181. total_size++;
  182. }
  183. else
  184. {
  185. travelNode = travelNode->left;
  186. }
  187. }
  188.  
  189. }while(travelNode != nullptr);
  190. }
  191. }
  192.  
  193.  
  194.  
  195. template<typename Key, typename Value>
  196. std::vector<Key> MyAVLTree<Key, Value>::inOrder() const{
  197. std::vector<Key> foo;
  198. return foo;
  199. }
  200.  
  201.  
  202. template<typename Key, typename Value>
  203. std::vector<Key> MyAVLTree<Key, Value>::preOrder() const{
  204. std::vector<Key> foo;
  205. return foo;
  206. }
  207.  
  208.  
  209. template<typename Key, typename Value>
  210. std::vector<Key> MyAVLTree<Key, Value>::postOrder() const{
  211. std::vector<Key> foo;
  212. return foo;
  213. }
  214.  
  215. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement