Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 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 << "czarny";
  46. else
  47. cout << "czerowny";
  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. grandparent = n->parent->parent;
  122. if (grandparent->leftChild == parent)
  123. {
  124. uncle = grandparent->rightChild;
  125. if (uncle != nullptr&&uncle->color == Red)
  126. {
  127. parent->color = Black;
  128. uncle->color = Black;
  129. grandparent->color = Red;
  130. n = grandparent;
  131. parent = n->parent;
  132. }
  133. else
  134. {
  135. if (parent->rightChild == n)
  136. {
  137. n = parent;
  138. leftRotate(n->parent, n);
  139. }
  140.  
  141. parent->color = Black;
  142. grandparent->color = Red;
  143. rightRotate(parent, grandparent);
  144. }
  145. }
  146. else
  147. {
  148. uncle = grandparent->leftChild;
  149. if (uncle != nullptr&&uncle->color == Red)
  150. {
  151. uncle->color = Black;
  152. parent->color = Black;
  153. grandparent->color = Red;
  154. n = grandparent;
  155. parent = n->parent;
  156. }
  157. else
  158. {
  159. if (parent->leftChild == n)
  160. {
  161. n = parent;
  162. rightRotate(n->parent, n);
  163. }
  164.  
  165. parent->color = Black;
  166. grandparent->color = Red;
  167. leftRotate(parent, grandparent);
  168. }
  169. }
  170. //root->color = Black;
  171. }
  172. root->color = Black;
  173. }
  174. /*void InsertFixUp(Node<T>* node)
  175. {
  176. Node<T>*parent;
  177. parent = node->parent;
  178. while (node !=root && parent->color == Red)
  179. {
  180. Node<T>*gparent = parent->parent;
  181. if (gparent->leftChild == parent)
  182. {
  183. Node<T>*uncle = gparent->rightChild;
  184. if (uncle != nullptr && uncle->color == Red)
  185. {
  186. parent->color = Black;
  187. uncle->color = Black;
  188. gparent->color = Red;
  189. node = gparent;
  190. parent = node->parent;
  191. }
  192. else
  193. {
  194. if (parent->rightChild == node)
  195. {
  196. leftRotate(root, parent);
  197.  
  198. }
  199. rightRotate(root, gparent);
  200. gparent->color = Red;
  201. parent->color = Black;
  202. break;
  203. }
  204. }
  205. else
  206. {
  207. Node<T>*uncle = gparent->leftChild;
  208. if (uncle != nullptr && uncle->color == Red)
  209. {
  210. gparent->color = Red;
  211. parent->color = Black;
  212. uncle->color = Black;
  213.  
  214. node = gparent;
  215. parent = node->parent;
  216. }
  217. else
  218. {
  219. if (parent->leftChild == node)
  220. {
  221. rightRotate(root, parent);
  222.  
  223. }
  224. leftRotate(root, gparent);
  225. parent->color = Black;
  226. gparent->color = Red;
  227. break;
  228. }
  229. }
  230. }
  231. root->color = Black;
  232. }*/
  233. /*void fixViolation(Node<T> *t)
  234. {
  235. Node<T> *u=new Node<T>();
  236. Node<T> *g = new Node<T>();
  237. if (root == t)
  238. {
  239. t->color = Black;
  240. return;
  241. }
  242. while (t->parent != nullptr && t->parent->color == Red)
  243. {
  244. Node<T> *g = t->parent->parent;
  245. if (g->leftChild == t->parent)
  246. {
  247. if (g->rightChild != nullptr)
  248. {
  249. u = g->rightChild;
  250. if (u->color == Red)
  251. {
  252. t->parent->color = Black;
  253. u->color = Black;
  254. g->color = Red;
  255. t = g;
  256. t->parent = t->parent;
  257. }
  258. }
  259. else
  260. {
  261. if (t->parent->rightChild == t)
  262. {
  263. t = t->parent;
  264. leftRotate(t,t->parent->parent);
  265. }
  266. t->parent->color = Black;
  267. g->color = Red;
  268. rightRotate(t->parent,g);
  269. }
  270. }
  271. else
  272. {
  273. if (g->leftChild != nullptr)
  274. {
  275. u = g->leftChild;
  276. if (u->color == Red)
  277. {
  278. t->parent->color = Black;
  279. u->color = Black;
  280. g->color = Red;
  281. t = g;
  282. t->parent = t->parent;
  283. }
  284. }
  285. else
  286. {
  287. if (t->parent->leftChild == t)
  288. {
  289. t = t->parent;
  290. rightRotate(t,t->parent->parent);
  291. }
  292. t->parent->color = Black;
  293. g->color = Red;
  294. leftRotate(t->parent,g);
  295. }
  296. }
  297. root->color = Black;
  298. }
  299. }*/
  300.  
  301. void addElement(T el)
  302. {
  303. Node<T>*n = new Node<T>();
  304. n->data = el;
  305. n->leftChild = nullptr;
  306. n->rightChild = nullptr;
  307. n->color = Red;
  308. Node<T>*temp = this->root;
  309. Node<T>*y = nullptr;
  310. if (root == nullptr)
  311. {
  312. n->color = Black;
  313. root = n;
  314. }
  315. else
  316. {
  317. while (temp != nullptr)
  318. {
  319. y = temp;
  320. if (temp->data < n->data)
  321. {
  322. temp = temp->rightChild;
  323. }
  324. else
  325. {
  326. temp = temp->leftChild;
  327. }
  328. }
  329. n->parent = y;
  330. if (y->data == n->data)
  331. {
  332. cout << "Duplikaty won!" << endl;
  333. return;
  334. }
  335. if (y->data < n->data)
  336. {
  337. y->rightChild = n;
  338. size = size + 1;
  339. }
  340. else
  341. {
  342. y->leftChild = n;
  343. size = size + 1;
  344. }
  345. //InsertFixUp(n);
  346. fixViolation(n);
  347. }
  348.  
  349. }
  350. void print(Node<T>*x)
  351. {
  352.  
  353. //cout << "size: " << size << endl;
  354. if (x == nullptr)
  355. {
  356. return;
  357. }
  358. if (x->parent == nullptr)
  359. {
  360. cout << "(" << x->data << ")";
  361. cout << "[" << "kolor:";
  362. x->gibColor(x);
  363. cout << ", rodzic: NULL," << " l.dziecko: ";
  364. if (x->leftChild == nullptr)
  365. {
  366. cout << "NIL";
  367. }
  368. else
  369. cout << x->leftChild->data;
  370. cout << ", p. dziecko: ";
  371. if (x->rightChild == nullptr)
  372. {
  373. cout << "NIL";
  374. }
  375. else
  376. cout << x->rightChild->data;
  377. cout << "]";
  378. cout << "-korzen " << endl;
  379. //rodzic, l.dziecko, p.dziecko
  380.  
  381. }
  382. else if (x->parent->leftChild == x)
  383. {
  384. //int c = x->gibColor(x);
  385. cout << "(" << x->data << ")";
  386. cout << "[" << "kolor:";
  387. x->gibColor(x);
  388. cout << ", rodzic: " << x->parent->data << ", l.dziecko: ";
  389. if (x->leftChild == nullptr)
  390. {
  391. cout << "NIL";
  392. }
  393. else
  394. cout << x->leftChild->data;
  395. cout << ", p. dziecko: ";
  396. if (x->rightChild == nullptr)
  397. {
  398. cout << "NIL" << "]" << endl;
  399. }
  400. else
  401. cout << x->rightChild->data << "]" << endl;
  402. }
  403. else
  404. {
  405. cout << "(" << x->data << ")";
  406. cout << "[" << "kolor:";
  407. x->gibColor(x);
  408. cout << ", rodzic: " << x->parent->data << ", l.dziecko: ";
  409. if (x->leftChild == nullptr)
  410. {
  411. cout << "NIL";
  412. }
  413. else
  414. cout << x->leftChild->data;
  415. cout << ", p. dziecko: ";
  416. if (x->rightChild == nullptr)
  417. {
  418. cout << "NIL" << "]" << endl;;
  419. }
  420. else
  421. cout << x->rightChild->data << "]" << endl;
  422. }
  423. print(x->leftChild);
  424. print(x->rightChild);
  425.  
  426. }
  427. void printTree()
  428. {
  429.  
  430. if (root == nullptr)
  431. {
  432. cout << "Puste drzewo!" << endl;
  433.  
  434. }
  435. else
  436. print(root);
  437. }
  438. //wyszukiwanie elementu
  439. //przejście preorder
  440. //przejście inorder
  441. //czyszczenie drzewa
  442. //wyznaczenie wysokości
  443. //rotacje jako funkcje naprawcze dodawania
  444. };
  445. int randomInt()
  446. {
  447. uniform_int_distribution<int> rozklad{ 0, 11000000 };
  448. default_random_engine generator{ 11 };
  449. function<int()> los{ bind(rozklad, generator) };
  450. int l = los();
  451. return l;
  452. }
  453.  
  454. int main()
  455. {
  456. RBTree<int>* d1 = new RBTree<int>();
  457. //d1->initialize(3);
  458. d1->addElement(35);
  459. d1->addElement(67);
  460. d1->addElement(69);
  461. d1->addElement(15);
  462. d1->addElement(30);
  463. d1->addElement(300);
  464. d1->addElement(123);
  465. /*d1->addElement(5);
  466. d1->addElement(10);
  467. d1->addElement(1);
  468. d1->addElement(8);
  469. d1->addElement(6);
  470. d1->addElement(7);
  471. d1->addElement(9);*/
  472. d1->printTree();
  473.  
  474. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement