Advertisement
vkichukova

Untitled

Jan 30th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. template <class T>
  12. class CompleteBinTree
  13. {
  14. private:
  15. T* values;
  16. int current;
  17. int capacity;
  18.  
  19. void resize();
  20. void clean();
  21. void copy(CompleteBinTree<T> const&);
  22. public:
  23. CompleteBinTree();
  24. ~CompleteBinTree();
  25. CompleteBinTree(CompleteBinTree<T> const&);
  26. CompleteBinTree& operator=(CompleteBinTree<T> const&);
  27.  
  28. void addElement(T const& x);
  29. int leftChildIndex(int index) const;
  30. int rightChildIndex(int index) const;
  31. bool empty() const;
  32. void create();
  33. void print() const;
  34. void printBFS();
  35. void printDFS(); // TO DO
  36. };
  37.  
  38. template <class T>
  39. CompleteBinTree<T>::CompleteBinTree()
  40. {
  41. capacity = 16;
  42. current = 0;
  43. values = new T[capacity];
  44. }
  45.  
  46. template <class T>
  47. CompleteBinTree<T>::~CompleteBinTree()
  48. {
  49. clean();
  50. }
  51.  
  52. template <class T>
  53. CompleteBinTree<T>::CompleteBinTree(CompleteBinTree<T> const& other)
  54. {
  55. copy(other);
  56. }
  57.  
  58. template <class T>
  59. CompleteBinTree<T>& CompleteBinTree<T>::operator=(CompleteBinTree<T> const& other)
  60. {
  61. if (this != &other)
  62. {
  63. clean();
  64. copy(other);
  65. }
  66. return *this;
  67. }
  68.  
  69. template <class T>
  70. void CompleteBinTree<T>::resize()
  71. {
  72. T* helper = new T[capacity];
  73. for (int i = 1; i <= current; i++)
  74. helper[i] = values[i];
  75.  
  76. clean();
  77.  
  78. capacity *= 2;
  79. values = new T[capacity];
  80. for (int i = 1; i <= current; i++)
  81. values[i] = helper[i];
  82. delete[] helper;
  83. }
  84.  
  85. template <class T>
  86. void CompleteBinTree<T>::copy(CompleteBinTree<T> const& other)
  87. {
  88. capacity = other.capacity;
  89. current = other.current;
  90. values = new T[capacity];
  91. for (int i = 1; i <= current; i++)
  92. values[i] = other.values[i];
  93. }
  94.  
  95. template <class T>
  96. void CompleteBinTree<T>::clean()
  97. {
  98. delete[] values;
  99. }
  100.  
  101. template <class T>
  102. void CompleteBinTree<T>::addElement(T const& x)
  103. {
  104. if (current == capacity)
  105. resize();
  106. values[++current] = x;
  107. }
  108.  
  109. template <class T>
  110. int CompleteBinTree<T>::leftChildIndex(int index) const
  111. {
  112. if (2*index <= current)
  113. return 2*index;
  114. return -1;
  115. }
  116.  
  117. template <class T>
  118. int CompleteBinTree<T>::rightChildIndex(int index) const
  119. {
  120. if (2*index + 1 <= current)
  121. return 2*index + 1;
  122. return -1;
  123. }
  124.  
  125. template <class T>
  126. bool CompleteBinTree<T>::empty() const
  127. {
  128. return current == 0;
  129. }
  130.  
  131. template <class T>
  132. void CompleteBinTree<T>::create()
  133. {
  134. char c;
  135. do
  136. {
  137. int x;
  138. cout << "Enter element: ";
  139. cin >> x;
  140. addElement(x);
  141. cout << "Next element y/n? ";
  142. cin >> c;
  143. } while (c == 'y');
  144. }
  145.  
  146. template <class T>
  147. void CompleteBinTree<T>::printBFS()
  148. {
  149. for (int i = 1; i <= current; i++)
  150. cout << values[i] << " ";
  151. cout << endl;
  152. }
  153.  
  154.  
  155. bool inArray(int arr[], int n, int element)
  156. {
  157. for(int i = 0; i < n; i++)
  158. if(arr[i] == element)
  159. return true;
  160. return false;
  161. }
  162.  
  163. template<class T>
  164. void myerase(vector<T>& vec, int index)
  165. {
  166. for(unsigned i = index+1; i < vec.size(); i++)
  167. vec[i-1] = vec[i];
  168. vec.pop_back();
  169. }
  170.  
  171. template<class T>
  172. void fillTree(vector<T> leaves, CompleteBinTree<T>& tree)
  173. {
  174. int size = leaves.size();
  175. int usedPositions[size] = {0};
  176. for(int i = 0; i < size; i++)
  177. usedPositions[i] = -1;
  178.  
  179. int i = 0;
  180. srand(time(NULL));
  181. while(!leaves.empty())
  182. {
  183. int position = rand() % leaves.size();
  184. //cout << position << endl;
  185. if(!inArray(usedPositions, size, position))
  186. {
  187. usedPositions[++i] = position;
  188. cout << usedPositions[i] << " ";
  189.  
  190. tree.addElement(leaves[position]);
  191. myerase(leaves, position);
  192. }
  193. }
  194. }
  195.  
  196. int main()
  197. {
  198. vector<int> vec;
  199.  
  200. for(int i = 0; i < 10; i++)
  201. vec.push_back(i);
  202.  
  203. CompleteBinTree<int> tree; fillTree(vec, tree);
  204. tree.printBFS();
  205. return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement