Advertisement
Cinestra

treemm.h (Attempt 1)

Jun 20th, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. #ifndef TREEMULTIMAP_INCLUDED
  2. #define TREEMULTIMAP_INCLUDED
  3.  
  4. template <typename KeyType, typename ValueType>
  5. class TreeMultimap
  6. {
  7. public:
  8.  
  9. // When you search for a particular key in the multimap, you receive an iterator object as the result
  10. // The iterator object can then be used to obtain all values that were associated with the searched-for key
  11. class Iterator
  12. {
  13. public:
  14. Iterator(std::vector<ValueType>* values = nullptr)
  15. {
  16. m_values = values;
  17. m_index = 0;
  18. }
  19.  
  20. bool is_valid() const
  21. {
  22. if (m_values == nullptr || m_index >= m_values->size())
  23. return false;
  24.  
  25. return true;
  26. }
  27.  
  28. ValueType& get_value() const
  29. {
  30. // Ask about throw and catch later
  31. if (is_valid())
  32. // *m_values is the dereferenced vector being pointed to by m_values
  33. return (*m_values)[m_index];
  34. }
  35.  
  36. void advance()
  37. {
  38. if (is_valid())
  39. m_index++;
  40. }
  41.  
  42. private:
  43. std::vector<ValueType>* m_values;
  44. int m_index;
  45. };
  46.  
  47. TreeMultimap()
  48. {
  49. m_root = nullptr;
  50. }
  51.  
  52. ~TreeMultimap()
  53. {
  54. // Postfix deletion so that we simply delete all the leaves first
  55.  
  56. delete_tree_multimap(m_root);
  57.  
  58. }
  59.  
  60. void insert(const KeyType& key, const ValueType& value)
  61. {
  62. KeyType key_copy = key;
  63. ValueType value_copy = value;
  64.  
  65. // First insertion
  66. if (m_root == nullptr)
  67. {
  68. m_root = new Node(key_copy);
  69. m_root->values.push_back(value_copy);
  70. return;
  71. }
  72.  
  73. Node* curr = m_root;
  74. for (;;)
  75. {
  76. // If the desired to add node has the same key value as the one being evaluated
  77. if (key_copy == curr->node_Key)
  78. {
  79. curr->values.push_back(value_copy);
  80. return;
  81. }
  82.  
  83. // Desired to add node is less than the one being evaluated
  84. else if (key_copy < curr->node_Key)
  85. {
  86. // If the node being evaluated has a left branch, simply move to the left node to evaluate
  87. if (curr->left != nullptr)
  88. // This now makes us go back to the beginning of the for loop
  89. curr = curr->left;
  90.  
  91. // If it doesn't, then we can add this node to the left branch
  92. else
  93. {
  94. curr->left = new Node(key_copy);
  95. curr->left->values.push_back(value_copy);
  96. return;
  97. }
  98. }
  99.  
  100. // Desired to add node is more than the one being evaluaed
  101. else
  102. {
  103. // Same concept as above
  104. if (curr->right != nullptr)
  105. curr = curr->right;
  106.  
  107. else
  108. {
  109. curr->right = new Node(key_copy);
  110. curr->left->values.push_back(value_copy);
  111. return;
  112. }
  113. }
  114. }
  115. }
  116.  
  117. Iterator find(const KeyType& key) const
  118. {
  119. Node* curr = m_root;
  120. while (curr != nullptr)
  121. {
  122. if (key == curr->node_Key)
  123. return Iterator(&(curr->values));
  124. else if (key < curr->node_Key)
  125. curr = curr->left;
  126. else
  127. curr = curr->right;
  128. }
  129.  
  130. return Iterator(); // Replace this line with correct code.
  131. }
  132.  
  133. private:
  134. struct Node
  135. {
  136. Node(const KeyType& Key)
  137. {
  138. node_Key = Key;
  139. left = nullptr;
  140. right = nullptr;
  141. }
  142. KeyType node_Key;
  143. std::vector<ValueType> values;
  144. Node *left, *right;
  145. };
  146.  
  147. Node *m_root;
  148.  
  149. void delete_tree_multimap(Node* curr)
  150. {
  151. // Post-order deletion so we delete all leaves first
  152.  
  153. if (curr == nullptr)
  154. return;
  155. delete_tree_multimap(curr->left);
  156. delete_tree_multimap(curr->right);
  157. delete curr;
  158. }
  159. };
  160.  
  161. #endif // TREEMULTIMAP_INCLUDED
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement