Guest User

Untitled

a guest
Jan 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long int
  3.  
  4. #define fi freopen("in.txt", "r", stdin)
  5. #define fo freopen("out.txt", "w", stdout)
  6. #define m0(a) memset(a , 0 , sizeof(a))
  7. #define m1(a) memset(a , -1 , sizeof(a))
  8.  
  9. #define Red 1
  10. #define Black 2
  11.  
  12. #define NilValue -9999999999
  13.  
  14. using namespace std ;
  15.  
  16. class Node
  17. {
  18. public:
  19. Node* Parent ;
  20. Node* rightChild ;
  21. Node* leftChild ;
  22. ll Value ;
  23. ll Color ;
  24.  
  25. Node()
  26. {
  27. Parent = rightChild = leftChild = 0 ;
  28. Value = 0 ;
  29. Color = Black ;
  30. }
  31.  
  32. Node(ll a)
  33. {
  34. Parent = rightChild = leftChild = 0 ;
  35. Value = a;
  36. Color = Black ;
  37. }
  38. };
  39.  
  40. Node* Nil ;
  41.  
  42.  
  43. class RedBlackTree
  44. {
  45. public:
  46. Node* Root ;
  47.  
  48. RedBlackTree()
  49. {
  50. Root = Nil ;
  51. }
  52.  
  53. void leftRotate(Node *Curr);
  54. void rightRotate(Node *Curr);
  55.  
  56. void Insert(Node *newVal);
  57. void insertFixUp(Node *newVal);
  58.  
  59. Node* RBmin(Node *root);
  60. Node* RBmax(Node *root);
  61. Node* Search(ll value);
  62. void AscPrint(Node* n);
  63.  
  64. void RBTransplant(Node *u, Node *v);
  65. void RBdelete(Node *toDeleteValue);
  66. void RBdeleteFixUp(Node *x);
  67. };
  68.  
  69. Node* RedBlackTree::RBmax(Node *root)
  70. {
  71. Node *x, *y ;
  72. y = Nil;
  73. x = root;
  74.  
  75. while(x != Nil)
  76. {
  77. y = x ;
  78. x = x->rightChild;
  79. }
  80. return y;
  81. }
  82.  
  83. Node* RedBlackTree::RBmin(Node *root)
  84. {
  85. Node *x, *y ;
  86. y = Nil;
  87. x = root;
  88.  
  89. while(x != Nil)
  90. {
  91. y = x ;
  92. x = x->leftChild ;
  93. }
  94. return y;
  95. }
  96.  
  97.  
  98. Node* RedBlackTree::Search(ll value)
  99. {
  100. Node *x, *y ;
  101. y = Nil;
  102. x = Root;
  103.  
  104. while(x != Nil)
  105. {
  106. y = x ;
  107.  
  108. if(value > x->Value)
  109. {
  110. x = x->rightChild;
  111. }
  112. else if(value < x->Value)
  113. {
  114. x = x->leftChild;
  115. }
  116. else
  117. {
  118. return x ;
  119. }
  120. }
  121. return Nil;
  122. }
  123.  
  124.  
  125.  
  126. void RedBlackTree::AscPrint(Node* n)
  127. {
  128. if(n == Nil) return ;
  129. AscPrint(n->leftChild);
  130. cout << n->Value << " " ;
  131. AscPrint(n->rightChild);
  132. return ;
  133. }
  134.  
  135.  
  136. void RedBlackTree::RBdelete(Node *toDeleteValue)
  137. {
  138. Node *y, *x ;
  139. y = toDeleteValue ;
  140. ll originial_Color_Of_Y ;
  141. originial_Color_Of_Y = y->Color ;
  142.  
  143. if(toDeleteValue->leftChild == Nil)
  144. {
  145. x = toDeleteValue->rightChild;
  146. RBTransplant(toDeleteValue,toDeleteValue->rightChild);
  147. }
  148. else if(toDeleteValue->rightChild == Nil)
  149. {
  150. x = toDeleteValue->leftChild;
  151. RBTransplant(toDeleteValue,toDeleteValue->leftChild);
  152. }
  153. else
  154. {
  155. y = RBmin(toDeleteValue->rightChild);
  156. originial_Color_Of_Y = y->Color ;
  157. x = y->rightChild ;
  158.  
  159. if(y->Parent == toDeleteValue)
  160. {
  161. x->Parent = y ;
  162. }
  163. else
  164. {
  165. RBTransplant(y, y->rightChild);
  166. y->rightChild = toDeleteValue->rightChild;
  167. y->rightChild->Parent = y ;
  168. }
  169. RBTransplant(toDeleteValue, y);
  170. y->leftChild = toDeleteValue->leftChild;
  171. y->leftChild->Parent = y ;
  172. y->Color = toDeleteValue->Color;
  173. }
  174.  
  175. if(originial_Color_Of_Y == Black)
  176. {
  177. RBdeleteFixUp(x);
  178. }
  179. }
  180.  
  181.  
  182. void RedBlackTree::RBdeleteFixUp(Node *x)
  183. {
  184. Node *w ;
  185.  
  186. while(x != Root && x->Color == Black)
  187. {
  188. if(x == x->Parent->leftChild)
  189. {
  190. w = x->Parent->rightChild;
  191.  
  192. if(w->Color == Red)
  193. {
  194. w->Color = Black ;
  195. x->Parent->Color = Red;
  196. leftRotate(x->Parent);
  197. w = x->Parent->rightChild ;
  198. }
  199.  
  200. if(w->leftChild->Color == Black && w->rightChild->Color == Black)
  201. {
  202. w->Color = Red ;
  203. x = x->Parent ;
  204. }
  205. else
  206. {
  207. if(w->rightChild->Color == Black)
  208. {
  209. w->leftChild->Color = Black ;
  210. w->Color = Red ;
  211. rightRotate(w);
  212. w = x->Parent->rightChild;
  213. }
  214. w->Color = x->Parent->Color;
  215. x->Parent->Color = Black ;
  216. w->rightChild->Color = Black ;
  217. leftRotate(x->Parent);
  218. x = Root ;
  219. }
  220.  
  221. }
  222. else /// x == x->Parent->rightChild
  223. {
  224. cout << "hi" << endl ;
  225. w = x->Parent->leftChild;
  226.  
  227. if(w->Color == Red)
  228. {
  229. w->Color = Black ;
  230. x->Parent->Color = Red;
  231. rightRotate(x->Parent);
  232. w = x->Parent->leftChild ;
  233. }
  234. if(w->rightChild->Color == Black && w->leftChild->Color == Black)
  235. {
  236. w->Color = Red ;
  237. x = x->Parent ;
  238. }
  239. else
  240. {
  241. if(w->leftChild->Color == Black)
  242. {
  243. w->rightChild->Color = Black ;
  244. w->Color = Red ;
  245. leftRotate(w);
  246. w = x->Parent->leftChild;
  247. }
  248. w->Color = x->Parent->Color;
  249. x->Parent->Color = Black ;
  250. w->leftChild->Color = Black ;
  251. rightRotate(x->Parent);
  252. x = Root ;
  253. }
  254.  
  255.  
  256. }
  257.  
  258. }
  259. x->Color = Black;
  260.  
  261. return ;
  262. }
  263.  
  264.  
  265. void RedBlackTree::RBTransplant(Node *u, Node *v)
  266. {
  267. if(u->Parent == Nil)
  268. {
  269. Root = v ;
  270. Root->Parent = Nil ;
  271. }
  272. else
  273. {
  274. if(u == u->Parent->leftChild)
  275. {
  276. u->Parent->leftChild = v ;
  277. }
  278. else
  279. {
  280. u->Parent->rightChild = v;
  281. }
  282. }
  283.  
  284. v->Parent = u->Parent ;
  285. return ;
  286. }
  287.  
  288. void RedBlackTree::leftRotate(Node *Curr)
  289. {
  290. Node *currRightChild ;
  291.  
  292. currRightChild = Curr->rightChild;
  293.  
  294.  
  295. Curr->rightChild = currRightChild->leftChild ;
  296. if(currRightChild->leftChild != Nil)
  297. {
  298. currRightChild->leftChild->Parent = Curr ;
  299. }
  300.  
  301. currRightChild->Parent = Curr->Parent ;
  302. if(Curr->Parent == Nil)
  303. {
  304. Root = currRightChild ;
  305. }
  306. else
  307. {
  308. if(Curr == Curr->Parent->leftChild)
  309. {
  310. Curr->Parent->leftChild = currRightChild ;
  311. }
  312. else if(Curr == Curr->Parent->rightChild)
  313. {
  314. Curr->Parent->rightChild = currRightChild ;
  315. }
  316. }
  317.  
  318. currRightChild->leftChild = Curr ;
  319. Curr->Parent = currRightChild ;
  320.  
  321. return ;
  322. }
  323.  
  324. void RedBlackTree::rightRotate(Node *Curr)
  325. {
  326. Node *currLeftChild ;
  327.  
  328. currLeftChild = Curr->leftChild;
  329.  
  330.  
  331. Curr->leftChild = currLeftChild->rightChild ;
  332. if(currLeftChild->rightChild != Nil)
  333. {
  334. currLeftChild->rightChild->Parent = Curr ;
  335. }
  336.  
  337. currLeftChild->Parent = Curr->Parent ;
  338. if(Curr->Parent == Nil)
  339. {
  340. Root = currLeftChild ;
  341. }
  342. else
  343. {
  344. if(Curr == Curr->Parent->leftChild)
  345. {
  346. Curr->Parent->leftChild = currLeftChild ;
  347. }
  348. else if(Curr == Curr->Parent->rightChild)
  349. {
  350. Curr->Parent->rightChild = currLeftChild ;
  351. }
  352. }
  353.  
  354. currLeftChild->rightChild = Curr ;
  355. Curr->Parent = currLeftChild ;
  356.  
  357. return ;
  358. }
  359.  
  360.  
  361. void RedBlackTree::Insert(Node *newVal)
  362. {
  363. Node *y, *x ;
  364. y = Nil ;
  365. x = Root ;
  366.  
  367. while(x != Nil)
  368. {
  369. y = x ;
  370. if(newVal->Value >= x->Value)
  371. {
  372. x = x->rightChild ;
  373. }
  374. else
  375. {
  376. x = x->leftChild ;
  377. }
  378. }
  379.  
  380. newVal->Parent = y ;
  381.  
  382. if(y == Nil)
  383. {
  384. Root = newVal ;
  385. Root->Parent = Nil ;
  386. }
  387. else
  388. {
  389. if(newVal->Value >= y->Value)
  390. {
  391. y->rightChild = newVal ;
  392. }
  393. else
  394. {
  395. y->leftChild = newVal ;
  396. }
  397. }
  398.  
  399. newVal->rightChild = Nil;
  400. newVal->leftChild = Nil ;
  401. newVal->Color = Red ;
  402.  
  403. insertFixUp(newVal);
  404. return;
  405. }
  406.  
  407.  
  408. void RedBlackTree::insertFixUp(Node *newVal)
  409. {
  410. Node *uncleNode, *fatherNode;
  411.  
  412. while(newVal->Parent->Color == Red)
  413. {
  414. if(newVal->Parent == newVal->Parent->Parent->leftChild)
  415. {
  416. uncleNode = newVal->Parent->Parent->rightChild;
  417. fatherNode = newVal->Parent ;
  418.  
  419. if(uncleNode->Color == Red)
  420. {
  421. uncleNode->Color = Black;
  422. fatherNode->Color = Black ;
  423. fatherNode->Parent->Color = Red ;
  424. newVal = fatherNode->Parent ;
  425. }
  426. else
  427. {
  428. if(newVal == fatherNode->rightChild)
  429. {
  430. newVal = newVal->Parent ;
  431. leftRotate(newVal);
  432. }
  433. else if(newVal == fatherNode->leftChild)
  434. {
  435. fatherNode->Color = Black ;
  436. fatherNode->Parent->Color = Red ;
  437. rightRotate(fatherNode->Parent);
  438. }
  439. }
  440. }
  441. else if(newVal->Parent == newVal->Parent->Parent->rightChild)
  442. {
  443. uncleNode = newVal->Parent->Parent->leftChild;
  444. fatherNode = newVal->Parent ;
  445.  
  446. if(uncleNode->Color == Red)
  447. {
  448. fatherNode->Color = Black ;
  449. uncleNode->Color = Black ;
  450. fatherNode->Parent->Color = Red ;
  451. newVal = fatherNode->Parent ;
  452. }
  453. else
  454. {
  455. if(newVal == fatherNode->leftChild)
  456. {
  457. newVal = newVal->Parent ;
  458. rightRotate(newVal) ;
  459. }
  460. else if(newVal == fatherNode->rightChild)
  461. {
  462. fatherNode->Color = Black;
  463. fatherNode->Parent->Color = Red ;
  464. leftRotate(fatherNode->Parent);
  465. }
  466. }
  467. }
  468. }
  469.  
  470. Root->Color = Black;
  471. return ;
  472. }
  473.  
  474.  
  475. int main()
  476. {
  477. Nil = new Node(NilValue);
  478.  
  479.  
  480. /// ***** Do your Code Here***** ///
  481.  
  482. RedBlackTree t ;
  483. ll a ;
  484.  
  485. while(1)
  486. {
  487. ll c ;
  488. cout << "1. Insert \n2. Delete" << endl ;
  489. cin >> c >> a;
  490. Node *n ;
  491. n = new Node(a);
  492. if(c == 1)
  493. {
  494. t.Insert(n);
  495. }
  496. else if(c==2)
  497. {
  498. Node* p ;
  499. if(t.Root != Nil)
  500. {
  501. t.RBdelete(t.Search(a));
  502. }
  503. else
  504. {
  505. cout << "No item left"<<endl ;
  506. }
  507. }
  508. cout << "Success" << endl << endl ;
  509.  
  510. cout << "Asc Print: " ;
  511. t.AscPrint(t.Root);
  512. cout << endl ;
  513.  
  514. cout << "Min: " << t.RBmin(t.Root)->Value << endl ;
  515. cout << "Max: " << t.RBmax(t.Root)->Value << endl ;
  516. }
  517.  
  518. return 0 ;
  519. }
Add Comment
Please, Sign In to add comment