Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Node
  6. {
  7. public:
  8. Node *left,*right,*parent;
  9. int val;
  10. char color='B';
  11. int low,high,mx;
  12.  
  13. void moveDown(Node *nParent)
  14. {
  15. if (parent != NULL)
  16. {
  17. if (isOnLeft())
  18. {
  19. parent->left = nParent;
  20. }
  21. else
  22. {
  23. parent->right = nParent;
  24. }
  25. }
  26. nParent->parent = parent;
  27. parent = nParent;
  28. }
  29.  
  30. bool isOnLeft()
  31. {
  32. return this == parent->left;
  33. }
  34.  
  35. void SwapColor(Node*node)
  36. {
  37. if (node->color=='R')
  38. node->color='B';
  39. else
  40. node->color='R';
  41. }
  42.  
  43. };
  44. class RBT
  45. {
  46.  
  47. public:
  48. Node *root= new Node;
  49. void RightRotate(Node *x)
  50. {
  51. // new parent will be node's left child
  52. Node *nParent = x->left;
  53.  
  54. // update root if current node is root
  55. if (x == root)
  56. root = nParent;
  57.  
  58. x->moveDown(nParent);
  59.  
  60. // connect x with new parent's right element
  61. x->left = nParent->right;
  62. // connect new parent's right element with node
  63. // if it is not null
  64. if (nParent->right != NULL)
  65. nParent->right->parent = x;
  66.  
  67. // connect new parent with x
  68. nParent->right = x;
  69. }
  70.  
  71.  
  72. void LeftRotate(Node *x)
  73. {
  74. // new parent will be node's right child
  75. Node *nParent = x->right;
  76.  
  77. // update root if current node is root
  78. if (x == root)
  79. root = nParent;
  80.  
  81. x->moveDown(nParent);
  82.  
  83. // connect x with new parent's left element
  84. x->right = nParent->left;
  85. // connect new parent's left element with node
  86. // if it is not null
  87. if (nParent->left != NULL)
  88. nParent->left->parent = x;
  89.  
  90. // connect new parent with x
  91. nParent->left = x;
  92. }
  93. void inorder(Node *root)
  94. {
  95. if (root == NULL)
  96. return;
  97.  
  98. inorder(root->left);
  99.  
  100. cout << "[" << root->low << ", " << root->high << "]"
  101. << " max = " << root->mx << endl;
  102.  
  103. inorder(root->right);
  104. }
  105. void preorder(Node *root)
  106. {
  107. if (root == NULL)
  108. return;
  109.  
  110. cout << "[ " << root->low << " " << root->high << " ]"
  111. << " max = " << root->mx << endl;
  112. inorder(root->left);
  113. inorder(root->right);
  114. }
  115.  
  116.  
  117. Node* add(Node* node, int low,int high)
  118. {
  119. cout<<"hi from add func\n";
  120. int l = root->low;
  121.  
  122. if (root == NULL)
  123. {
  124. cout<<"root added\n";
  125. return root; //bfkar a5alii myrturnesh
  126. }
  127.  
  128. else if (low<l)
  129. {root->left = add(root->left, low,high);
  130. root->left->parent = root; //not sure
  131. }
  132. else if (low>l)
  133. {
  134. root->right = add(root->right, low,high);
  135. root->right->parent = root; //not sure
  136. }
  137.  
  138.  
  139. else{
  140. cout<<"sth wrong in adding\n";
  141. return node;
  142. }
  143.  
  144. if (root->mx < high) //ancesstor fixation
  145. root->mx = high;
  146.  
  147. cout<<"node added succ\n";
  148.  
  149. return root;
  150. }
  151.  
  152. void Insert(int high,int low)
  153. {
  154. cout<<"hi from insert func\n";
  155. if(root==NULL)
  156. {
  157. root->mx=high;
  158. root->low=low;
  159. root->high=high;
  160. root->left = root->right = NULL;
  161. add(root,high,low);
  162. }
  163. /*return;
  164. if(root==NULL)
  165. {
  166. add(root,high,low);
  167. root->color='B';
  168. cout<<"Tree of only Root fixed successfuly\n";
  169. } */
  170. else
  171. {
  172. Node *x=new Node;
  173. add(x,high,low);
  174. x->color='R';
  175.  
  176. Node *uncle=new Node;
  177.  
  178. while(x!=root && x->parent->color!='R')
  179. {
  180. if(x->parent==x->parent->parent->left) //law baba el ebn el as3'ar
  181. {
  182. uncle=x->parent->parent->right;
  183.  
  184. if (uncle->color=='R')
  185. {
  186. x->parent->color = 'B';
  187. uncle->color = 'B';
  188. x->parent->parent->color = 'R';
  189. x = x->parent->parent;
  190.  
  191. }
  192. else //law 3amy eswed
  193. {
  194. //left left case
  195. // (p is left child of g and x is left child of p)
  196. if(x->parent==x->parent->parent->left && x==x->parent->left)
  197. {
  198. RightRotate(x->parent->parent);
  199. x->SwapColor(x->parent->parent);
  200. x->SwapColor(x->parent);
  201.  
  202. }
  203.  
  204. //left right case
  205. else if(x->parent==x->parent->parent->left && x==x->parent->right)
  206. {
  207. LeftRotate(x->parent);
  208. RightRotate(x->parent->parent);
  209. x->SwapColor(x->parent->parent);
  210. x->SwapColor(x->parent);
  211.  
  212. }
  213. //right right
  214. else if (x->parent==x->parent->parent->right && x==x->parent->right)
  215. {
  216. LeftRotate(x->parent->parent);
  217. x->SwapColor(x->parent->parent);
  218. x->SwapColor(x->parent);
  219.  
  220. }
  221. else
  222. {
  223. RightRotate(x->parent);
  224. LeftRotate(x->parent->parent);
  225. x->SwapColor(x->parent->parent);
  226. x->SwapColor(x->parent);
  227.  
  228. }
  229.  
  230. }
  231.  
  232.  
  233. }
  234.  
  235.  
  236.  
  237. }
  238. cout<<"tree fixed\n";
  239. }
  240.  
  241. }
  242.  
  243. };
  244.  
  245.  
  246.  
  247.  
  248. int main()
  249. {
  250. RBT MyTree;
  251. int n=0;
  252. cout<<"how many nodes\n";
  253. cin>>n;
  254. cout << "insert inputs" << endl;
  255. int x,y;
  256. for(int i=0;i<n;i++)
  257. {
  258. cin>>x;
  259. cin>>y;
  260. MyTree.Insert(x,y);
  261.  
  262. }
  263. cout<<"preorder\n";
  264. MyTree.preorder(MyTree.root);
  265. cout<<"\n";
  266. cout<<"inorder\n";
  267. MyTree.inorder(MyTree.root);
  268. return 0;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement