Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <random>
  4. #include <string>
  5. #include <functional>
  6. using namespace std;
  7.  
  8. enum Color { Black, Red };
  9. /*struct Data
  10. {
  11. public:
  12. int number;
  13. char key;
  14. public:
  15. Data() {};
  16. Data(int n, char k)
  17. {
  18. this->number = n;
  19. this->key = k;
  20. }
  21. };*/
  22. template<typename T>
  23. class RBTree;
  24.  
  25. template <typename T>
  26. class Node
  27. {
  28. friend class RBTree<T>;
  29. T data;
  30. Color color;
  31. Node* parent;
  32. Node* leftChild;
  33. Node* rightChild;
  34. public:
  35. Node()
  36. {
  37. this->parent = nullptr;
  38. this->leftChild = nullptr;
  39. this->rightChild = nullptr;
  40. //color = Red;
  41. }
  42. void gibColor(Node<T>*x)
  43. {
  44. if (x->color == Black)
  45. cout << "black";
  46. else
  47. cout << "red";
  48. }
  49. };
  50.  
  51. template <typename T>
  52. class RBTree
  53. {
  54. Node<T>* root;
  55. int size;
  56. public:
  57. RBTree()
  58. {
  59. root = nullptr;
  60. size = 1;
  61. }
  62.  
  63. void leftRotate(Node<T>*child, Node<T>*parent)
  64. {
  65. child = parent->rightChild;
  66. parent->rightChild = child->leftChild;
  67. if (child->leftChild != nullptr)
  68. {
  69. child->leftChild->parent = parent;
  70. }
  71.  
  72. child->parent = parent->parent;
  73. if (parent->parent == nullptr)
  74. {
  75. root = child;
  76. }
  77. else if (parent == parent->parent->leftChild)
  78. {
  79. parent->parent->leftChild = child;
  80. }
  81. else {
  82. parent->parent->rightChild = child;
  83. }
  84.  
  85. child->leftChild = parent;
  86. parent->parent = child;
  87. }
  88. void rightRotate(Node<T>*child, Node<T>*parent)
  89. {
  90. child = parent->leftChild;
  91. parent->leftChild = child->rightChild;
  92. if (child->rightChild != nullptr)
  93. {
  94. child->rightChild->parent = parent;
  95. }
  96. child->parent = parent->parent;
  97. if (parent->parent == nullptr)
  98. {
  99. root = child;
  100. }
  101. else if (parent == parent->parent->rightChild)
  102. {
  103. parent->parent->rightChild = child;
  104. }
  105. else
  106. {
  107. parent->parent->leftChild = child;
  108. }
  109.  
  110. child->rightChild = parent;
  111. parent->parent = child;
  112. }
  113. void fixViolation(Node<T>*n)
  114. {
  115. Node<T>*grandparent;
  116. Node<T>*parent;
  117. //Node<T>*uncle;
  118. parent = n->parent;
  119. while (parent != nullptr&& parent->color == Red)
  120. {
  121. Node<T>*uncle;
  122. grandparent = n->parent->parent;
  123. if (grandparent->leftChild == parent)
  124. {
  125. uncle = grandparent->rightChild;
  126. if (uncle != nullptr&&uncle->color == Red)
  127. {
  128. parent->color = Black;
  129. uncle->color = Black;
  130. grandparent->color = Red;
  131. n = grandparent;
  132. parent = n->parent;
  133. }
  134. else
  135. {
  136. if (parent->rightChild == n)
  137. {
  138. n = parent;
  139. leftRotate(n->parent, n);
  140. }
  141.  
  142. parent->color = Black;
  143. grandparent->color = Red;
  144. rightRotate(parent, grandparent);
  145. }
  146. }
  147. else
  148. {
  149. uncle = grandparent->leftChild;
  150. if (uncle != nullptr&&uncle->color == Red)
  151. {
  152. uncle->color = Black;
  153. parent->color = Black;
  154. grandparent->color = Red;
  155. n = grandparent;
  156. parent = n->parent;
  157. }
  158. else
  159. {
  160. if (parent->leftChild == n)
  161. {
  162. n = parent;
  163. rightRotate(n->parent, n);
  164. }
  165.  
  166. parent->color = Black;
  167. grandparent->color = Red;
  168. leftRotate(parent, grandparent);
  169. }
  170. }
  171. //root->color = Black;
  172. }
  173. root->color = Black;
  174. }
  175.  
  176.  
  177. void addElement(T el)
  178. {
  179. Node<T>*n = new Node<T>();
  180. n->data = el;
  181. n->leftChild = nullptr;
  182. n->rightChild = nullptr;
  183. n->color = Red;
  184. Node<T>*temp = this->root;
  185. Node<T>*y = nullptr;
  186. if (root == nullptr)
  187. {
  188. n->color = Black;
  189. root = n;
  190. }
  191. else
  192. {
  193. while (temp != nullptr)
  194. {
  195. y = temp;
  196. if (temp->data < n->data)
  197. {
  198. temp = temp->rightChild;
  199. }
  200. else
  201. {
  202. temp = temp->leftChild;
  203. }
  204. }
  205. n->parent = y;
  206. if (y->data == n->data)
  207. {
  208. cout << "Duplikaty won!" << endl;
  209. return;
  210. }
  211. if (y->data < n->data)
  212. {
  213. y->rightChild = n;
  214. size = size + 1;
  215. }
  216. else
  217. {
  218. y->leftChild = n;
  219. size = size + 1;
  220. }
  221. //InsertFixUp(n);
  222. fixViolation(n);
  223. }
  224.  
  225. }
  226. void print(Node<T>*x)
  227. {
  228.  
  229. //cout << "size: " << size << endl;
  230. if (x == nullptr)
  231. {
  232. return;
  233. }
  234. if (x->parent == nullptr)
  235. {
  236. cout << "(" << x->data << ")";
  237. cout << "[" << "kolor:";
  238. x->gibColor(x);
  239. cout << ", parent: NULL," << " l.child: ";
  240. if (x->leftChild == nullptr)
  241. {
  242. cout << "NIL";
  243. }
  244. else
  245. cout << x->leftChild->data;
  246. cout << ", r.child: ";
  247. if (x->rightChild == nullptr)
  248. {
  249. cout << "NIL";
  250. }
  251. else
  252. cout << x->rightChild->data;
  253. cout << "]";
  254. cout << "-root " << endl;
  255. //rodzic, l.dziecko, p.dziecko
  256.  
  257. }
  258. else if (x->parent->leftChild == x)
  259. {
  260. //int c = x->gibColor(x);
  261. cout << "(" << x->data << ")";
  262. cout << "[" << "kolor:";
  263. x->gibColor(x);
  264. cout << ", parent: " << x->parent->data << ", l.child: ";
  265. if (x->leftChild == nullptr)
  266. {
  267. cout << "NIL";
  268. }
  269. else
  270. cout << x->leftChild->data;
  271. cout << ", r.child: ";
  272. if (x->rightChild == nullptr)
  273. {
  274. cout << "NIL" << "]" << endl;
  275. }
  276. else
  277. cout << x->rightChild->data << "]" << endl;
  278. }
  279. else
  280. {
  281. cout << "(" << x->data << ")";
  282. cout << "[" << "kolor:";
  283. x->gibColor(x);
  284. cout << ", parent: " << x->parent->data << ", l.child: ";
  285. if (x->leftChild == nullptr)
  286. {
  287. cout << "NIL";
  288. }
  289. else
  290. cout << x->leftChild->data;
  291. cout << ", r.child: ";
  292. if (x->rightChild == nullptr)
  293. {
  294. cout << "NIL" << "]" << endl;;
  295. }
  296. else
  297. cout << x->rightChild->data << "]" << endl;
  298. }
  299. print(x->leftChild);
  300. print(x->rightChild);
  301.  
  302. }
  303. void printTree()
  304. {
  305.  
  306. if (root == nullptr)
  307. {
  308. cout << "Empty tree!" << endl;
  309.  
  310. }
  311. else
  312. print(root);
  313. }
  314. };
  315. int randomInt()
  316. {
  317. uniform_int_distribution<int> rozklad{ 0, 11000000 };
  318. default_random_engine generator{ 11 };
  319. function<int()> los{ bind(rozklad, generator) };
  320. int l = los();
  321. return l;
  322. }
  323.  
  324. int main()
  325. {
  326. RBTree<int>* d1 = new RBTree<int>();
  327. //d1->initialize(3);
  328. d1->addElement(55);
  329. d1->addElement(69);
  330. d1->addElement(62);
  331. d1->addElement(71);
  332. d1->addElement(70);
  333. d1->addElement(14);
  334. d1->printTree();
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement