Guest User

Untitled

a guest
Nov 27th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <utility>
  4. #include <cassert>
  5. #include <cstdlib>
  6. #include <string>
  7.  
  8.  
  9. template<typename D = std::chrono::microseconds>
  10. class Benchmark {
  11. public:
  12. Benchmark(bool printOnExit = false) : m_print(printOnExit) {
  13. start = std::chrono::high_resolution_clock::now();
  14. }
  15. typename D::rep elapsed() const {
  16. auto end = std::chrono::high_resolution_clock::now();
  17. auto result = std::chrono::duration_cast<D>(end-start);
  18. return result.count();
  19. }
  20. ~Benchmark() {
  21. auto result = elapsed();
  22. if (m_print)
  23. {
  24. std::cerr << "Czas: " << result << "\n";
  25. }
  26. }
  27. private:
  28. std::chrono::high_resolution_clock::time_point start;
  29. bool m_print = true;
  30. };
  31.  
  32. template<typename KeyType, typename ValueType>
  33. class Node{
  34. public:
  35. KeyType key;
  36. ValueType value;
  37. Node *left, *right;
  38.  
  39. Node(KeyType key, ValueType value) {
  40. this->key = key;
  41. this->value=value;
  42. left=right = NULL;
  43. }
  44.  
  45. ~Node() {
  46. if(left!=NULL)
  47. delete left;
  48. if(right!=NULL)
  49. delete right;
  50. }
  51. };
  52.  
  53. template<typename KeyType, typename ValueType>
  54. class TreeMap
  55. {
  56. public:
  57. using key_type = KeyType;
  58. using mapped_type = ValueType;
  59. using value_type = std::pair<key_type, mapped_type>;
  60. Node<KeyType, ValueType> *root;
  61. int treeSize;
  62.  
  63. TreeMap(){
  64. treeSize=0;
  65. root=NULL;
  66. }
  67. bool isEmpty()
  68. {
  69. if (this->root==NULL)
  70. return true;
  71. else
  72. {
  73. std::cout<<root->value;
  74. std::cout<<"\n";
  75. }
  76. return false;
  77.  
  78. }
  79.  
  80. // A utility function to right
  81. // rotate subtree rooted with y
  82. // See the diagram given above.
  83. Node<KeyType,ValueType> *rightRotate(Node<KeyType,ValueType> *x)
  84. {
  85. Node<KeyType,ValueType> *y = x->left;
  86. x->left = y->right;
  87. y->right = x;
  88. return y;
  89. }
  90.  
  91. // A utility function to left
  92. // rotate subtree rooted with x
  93. // See the diagram given above.
  94. Node<KeyType,ValueType> *leftRotate(Node<KeyType,ValueType> *x)
  95. {
  96. Node<KeyType,ValueType> *y = x->right;
  97. x->right = y->left;
  98. y->left = x;
  99. return y;
  100. }
  101.  
  102. Node<KeyType,ValueType> *splay(Node<KeyType,ValueType> *node, KeyType key){
  103. // Base cases: root is NULL or
  104. // key is present at root
  105. if (node == NULL || node->key == key)
  106. return node;
  107.  
  108. // Key lies in left subtree
  109. if (node->key > key)
  110. {
  111. // Key is not in tree, we are done
  112. if (node->left == NULL) return node;
  113.  
  114. // Zig-Zig (Left Left)
  115. if (node->left->key > key)
  116. {
  117. // First recursively bring the
  118. // key as root of left-left
  119. node->left->left = splay(node->left->left, key);
  120.  
  121. // Do first rotation for root,
  122. // second rotation is done after else
  123. node = rightRotate(node);
  124. }
  125. else if (node->left->key < key) // Zig-Zag (Left Right)
  126. {
  127. // First recursively bring
  128. // the key as root of left-right
  129. node->left->right = splay(node->left->right, key);
  130.  
  131. // Do first rotation for root->left
  132. if (node->left->right != NULL)
  133. node->left = leftRotate(node->left);
  134. }
  135.  
  136. // Do second rotation for root
  137. return (node->left == NULL)? node: rightRotate(node);
  138. }
  139. else // Key lies in right subtree
  140. {
  141. // Key is not in tree, we are done
  142. if (node->right == NULL) return node;
  143.  
  144. // Zig-Zag (Right Left)
  145. if (node->right->key > key)
  146. {
  147. // Bring the key as root of right-left
  148. node->right->left = splay(node->right->left, key);
  149.  
  150. // Do first rotation for root->right
  151. if (node->right->left != NULL)
  152. node->right = rightRotate(node->right);
  153. }
  154. else if (node->right->key < key)// Zag-Zag (Right Right)
  155. {
  156. // Bring the key as root of
  157. // right-right and do first rotation
  158. node->right->right = splay(node->right->right, key);
  159. node = leftRotate(node);
  160. }
  161.  
  162. // Do second rotation for root
  163. return (node->right == NULL)? node: leftRotate(node);
  164. }
  165. }
  166.  
  167.  
  168. Node<KeyType,ValueType> *insert(key_type key, mapped_type value){
  169. this->treeSize=this->treeSize+1;
  170. Node<KeyType,ValueType> *newnode=new Node<KeyType,ValueType>(key,value);
  171. if (this->root == NULL)
  172. {
  173. this->root=newnode;
  174. return newnode;
  175. }
  176. this->root=splay(this->root,value);
  177. if(this->root->key==key) return this->root;
  178.  
  179. // If root's key is greater, make
  180. // root as right child of newnode
  181. // and copy the left child of root to newnode
  182. if (this->root->key > key)
  183. {
  184.  
  185. newnode->right = this->root;
  186. newnode->left = this->root->left;
  187. this->root->left = NULL;
  188. }
  189. // If root's key is smaller, make
  190. // root as left child of newnode
  191. // and copy the right child of root to newnode
  192. else
  193. {
  194. newnode->left = this->root;
  195. newnode->right = this->root->right;
  196. this->root->right = NULL;
  197. }
  198. this->root=newnode;
  199. return newnode;
  200. }
  201.  
  202. /*!
  203. * dodaje wpis do slownika przez podanie pary klucz-wartosc
  204. */
  205. void insert(value_type key_value)
  206. {
  207. insert(key_value.first,key_value.second);
  208. }
  209.  
  210. /*!
  211. * zwraca referencje na wartosc dla podanego klucza
  212. *
  213. * jezeli elementu nie ma w slowniku, dodaje go
  214. */
  215. /*mapped_type& operator[](const key_type& key)
  216. {
  217. if(contains(key)==true)
  218. return root->value;
  219. insert(key)
  220. }*/
  221.  
  222. /*!
  223. * zwraca wartosc dla podanego klucza
  224. */
  225.  
  226. mapped_type value(key_type key)
  227. {
  228. if (contains(key)==false)
  229. {
  230. std::cout<<"Nie ma klucza dla "<<key<<"\n";
  231. return 0;
  232. }
  233. splay(root,key);
  234. return root->value;
  235. }
  236.  
  237. /*!
  238. * zwraca informacje, czy istnieje w slowniku podany klucz
  239. */
  240. bool contains(key_type key) {
  241. this->root=splay(this->root,key);
  242. if(root==NULL)
  243. return false;
  244. if (this->root->key==key)
  245. return true;
  246. return false;
  247. }
  248.  
  249. /*!
  250. * zwraca liczbe wpisow w slowniku
  251. */
  252.  
  253. int size(){
  254. return this->treeSize;
  255. }
  256.  
  257. mapped_type& operator[](const key_type& key)
  258. {
  259. if(contains(key)==true)
  260. {
  261. splay(this->root,key);
  262. return this->root->value;
  263. }
  264. //TODO: Dodac to zeby te asserty ostatnie sie wykonywaly, tylko nie mam pomyslu jak
  265.  
  266. }
  267.  
  268.  
  269. ~TreeMap() {
  270. delete root;
  271. }
  272. };
  273.  
  274.  
  275. int main()
  276. {
  277. TreeMap<int,int> dict;
  278.  
  279. // slownik jest pusty
  280. assert(dict.isEmpty() == true);
  281. assert(dict.size() == 0);
  282. assert(dict.contains(0) == false);
  283.  
  284. // dodanie elementu do slownika
  285. dict.insert(0, 1);
  286. assert(dict.isEmpty() == false);
  287. assert(dict.size() == 1);
  288. assert(dict.contains(0) == true);
  289. assert(dict.value(0) == 1);
  290.  
  291. // dodanie elementu do slownika jako pary
  292. dict.insert(std::pair<int,int>(1, 2));
  293. assert(dict.size() == 2);
  294. assert(dict.contains(1) == true);
  295. assert(dict.value(0) == 1);
  296. assert(dict.value(1) == 2);
  297.  
  298.  
  299. // operator []
  300. assert(dict[0] == 1);
  301. assert(dict[1] == 2);
  302. /*
  303. // operator [] tworzy nowy element
  304. dict[2] = 3;
  305. assert(dict.value(2) == 3);
  306. assert(dict.size() == 3);*/
  307.  
  308.  
  309. //nadpisanie wartosci dla istniejacego elementu
  310. dict.insert(2, 4);
  311. assert(dict.size() == 3);
  312. assert(dict.value(2) == 4);
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment