Advertisement
ASabry

Untitled

Dec 6th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7. int data{};
  8. node* left = nullptr;
  9. node* right = nullptr;
  10. node* parent = nullptr;
  11. string color;
  12. int Rank;
  13. };
  14. class RB_TREE
  15. {
  16.  
  17. node* root;
  18.  
  19. public:
  20. RB_TREE() : root(nullptr) {}
  21.  
  22. node* GetRoot()
  23. {
  24. return root;
  25. }
  26.  
  27. void InsertNode(int stuff)
  28. {
  29. if(root == nullptr)
  30. {
  31. root = new node();
  32. root->data = stuff;
  33. root->parent = nullptr;
  34. root->color = "BLACk";
  35. //root->Rank = 1;
  36. cout << "Element inserted.\n";
  37. }
  38. else
  39. {
  40. node *Root = GetRoot();
  41. node* newnode = new node();
  42. newnode->data = stuff;
  43.  
  44. while(Root != nullptr)
  45. {
  46. if(Root->data > stuff)
  47. {
  48. if(Root->left == nullptr)
  49. {
  50. Root->left = newnode;
  51. newnode->color = "RED";
  52. newnode->parent = Root;
  53. // newnode->Rank = 1;
  54. // node* temp = newnode->parent;
  55. // while(temp != nullptr)
  56. // {
  57. // temp->Rank ++;
  58. // temp = temp->parent;
  59. // }
  60. cout << "Element inserted.\n";
  61. break;
  62. }
  63. else
  64. {
  65. Root = Root->left;
  66. }
  67. }
  68. else
  69. {
  70. if(Root->right == nullptr)
  71. {
  72. Root->right = newnode;
  73. newnode->color = "RED";
  74. newnode->parent = Root;
  75. // newnode->Rank = 1;
  76. // node* temp = newnode->parent;
  77. // while(temp != nullptr)
  78. // {
  79. // temp->Rank ++;
  80. // temp = temp->parent;
  81. // }
  82. cout << "Element inserted.\n";
  83. break;
  84. }
  85. else
  86. {
  87. Root = Root->right;
  88. }
  89. }
  90. }
  91. RB_Insert_Fixup(newnode);
  92.  
  93. }
  94. }
  95.  
  96.  
  97. void RB_Insert_Fixup(node* z)
  98. {
  99. while(z->parent->color == "RED")
  100. {
  101. node* grandparent = z->parent->parent;
  102. node* uncle = GetRoot();
  103. if(z->parent == grandparent->left)
  104. {
  105. if(grandparent->right)
  106. {
  107. uncle = grandparent->right;
  108. }
  109. if(uncle->color == "RED")
  110. {
  111. z->parent->color = "BLACK";
  112. uncle->color = "BLACK";
  113. grandparent->color = "RED";
  114. if(grandparent->data != root->data)
  115. {
  116. z = grandparent;
  117. }
  118. else
  119. {
  120. break;
  121. }
  122. }
  123. else if(z == grandparent->left->right)
  124. {
  125. LeftRotate(z->parent);
  126. }
  127. else
  128. {
  129. z->parent->color = "BLACK";
  130. grandparent->color = "RED";
  131. RightRotate(grandparent);
  132. if(grandparent->data != root->data)
  133. {
  134. z = grandparent;
  135. }
  136. else
  137. {
  138. break;
  139. }
  140. }
  141. }
  142. else
  143. {
  144. if(grandparent->left)
  145. {
  146. uncle = grandparent->left;
  147. }
  148. if(uncle->color == "RED")
  149. {
  150. z->parent->color = "BLACK";
  151. uncle->color = "BLACK";
  152. grandparent->color = "RED";
  153. if(grandparent->data != root->data)
  154. {
  155. z = grandparent;
  156. }
  157. else
  158. {
  159. break;
  160. }
  161. }
  162. else if(z == grandparent->right->left)
  163. {
  164. RightRotate(z->parent);
  165. }
  166. else
  167. {
  168. z->parent->color = "BLACK";
  169. grandparent->color = "RED";
  170. LeftRotate(grandparent);
  171. if(grandparent->data != root->data)
  172. {
  173. z = grandparent;
  174. }
  175. else
  176. {
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. root->color = "BLACK";
  183. }
  184. node* TreeSearch(int stuff)
  185. {
  186. node* temp = GetRoot();
  187. if(temp == nullptr)
  188. {
  189. return nullptr;
  190. }
  191.  
  192. while(temp)
  193. {
  194. if(stuff == temp->data)
  195. {
  196. return temp;
  197. }
  198. else if(stuff < temp->data)
  199. {
  200. temp = temp->left;
  201. }
  202. else
  203. {
  204. temp = temp->right;
  205. }
  206. }
  207. return nullptr;
  208. }
  209.  
  210. void LeftRotate(node* x)
  211. {
  212. node* nw_node = new node();
  213. if(x->right->left)
  214. {
  215. nw_node->right = x->right->left;
  216. }
  217. nw_node->left = x->left;
  218. nw_node->data = x->data;
  219. nw_node->color = x->color;
  220. x->data = x->right->data;
  221.  
  222. x->left = nw_node;
  223. if(nw_node->left)
  224. {
  225. nw_node->left->parent = nw_node;
  226. }
  227. if(nw_node->right)
  228. {
  229. nw_node->right->parent = nw_node;
  230. }
  231. nw_node->parent = x;
  232.  
  233. if(x->right->right)
  234. {
  235. x->right = x->right->right;
  236. }
  237. else
  238. {
  239. x->right = nullptr;
  240. }
  241.  
  242. if(x->right)
  243. {
  244. x->right->parent = x;
  245. }
  246. }
  247.  
  248. void RightRotate(node* x)
  249. {
  250. node* nw_node = new node();
  251. if(x->left->right)
  252. {
  253. nw_node->left = x->left->right;
  254. }
  255. nw_node->right = x->right;
  256. nw_node->data = x->data;
  257. nw_node->color = x->color;
  258.  
  259. x->data = x->left->data;
  260. x->color = x->left->color;
  261.  
  262. x->right = nw_node;
  263. if(nw_node->left)
  264. {
  265. nw_node->left->parent = nw_node;
  266. }
  267. if(nw_node->right)
  268. {
  269. nw_node->right->parent = nw_node;
  270. }
  271. nw_node->parent = x;
  272.  
  273. if(x->left->left)
  274. {
  275. x->left = x->left->left;
  276. }
  277. else
  278. {
  279. x->left = nullptr;
  280. }
  281.  
  282. if(x->left)
  283. {
  284. x->left->parent = x;
  285. }
  286. }
  287.  
  288. void ReRank(node* temp)
  289. {
  290. if(!temp)
  291. {
  292. return;
  293. }
  294.  
  295. ReRank(temp->left);
  296.  
  297. ReRank(temp->right);
  298.  
  299. if(temp->left == NULL && temp->right == NULL)
  300. temp->Rank = 1;
  301. else if (temp->left == NULL)
  302. temp->Rank = temp->right->Rank + 1;
  303. else if(temp->right == NULL)
  304. temp->Rank = temp->left->Rank + 1;
  305. else
  306. temp->Rank = temp->left->Rank + temp->right->Rank + 1;
  307.  
  308. }
  309. int Select(node* temp,int i)
  310. {
  311.  
  312. if(i==temp->Rank)
  313. return temp->data;
  314. if(i< temp->Rank)
  315. Select(temp->left,i);
  316. else if(i> temp->Rank)
  317. {
  318. i = i - temp->Rank;
  319. Select(temp->right,i);
  320. }
  321.  
  322. }
  323. // void ReRank(node* p)
  324. // {
  325. // if(!p)
  326. // return
  327. //
  328. // int sl=0 ,sr=0;
  329. // if(p->left)
  330. // sl=ReRank(p->left);
  331. // if(p->right)
  332. // sr=ReRank(p->right)
  333. // }
  334. void InorderTraversal(node* temp)
  335. {
  336.  
  337.  
  338. if(!temp)
  339. {
  340. return;
  341. }
  342.  
  343.  
  344. InorderTraversal(temp->left);
  345. cout << "--> " << temp->data << "<" << temp->color << ">" << "< " << temp->Rank << " >"<<endl;
  346. InorderTraversal(temp->right);
  347. }
  348. };
  349. int main()
  350. {
  351. RB_TREE demo;
  352. int input,k;
  353. cin>>k;
  354. while(true){
  355. switch(k)
  356. {
  357.  
  358. case 1:
  359. cout<<"Enter The Number To Insert"<<endl;
  360. cin>>input;
  361. demo.InsertNode(input);
  362. cout<<endl;
  363. break;
  364. case 2:
  365. demo.ReRank(demo.GetRoot());
  366. demo.InorderTraversal(demo.GetRoot());
  367. cout<<demo.Select(demo.GetRoot(),1);
  368. cout<<endl;
  369. break;
  370. }
  371. //cout<<"Review The Tree"<<endl;
  372.  
  373. }
  374. return 0;
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement