Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. // Search tree
  6.  
  7. struct node_s {
  8. int elem;
  9. node_s *left;
  10. node_s *right;
  11. node_s (int k) {elem = k; left = right = 0;}
  12. };
  13.  
  14. node_s *ins_st(node_s *t, int k) {
  15. if (t==NULL)
  16. return new node_s(k);
  17. if (k < t->elem)
  18. t->left = ins_st (t->left, k);
  19. else if (k < t->elem)
  20. t->right = ins_st (t->right, k);
  21. return t;
  22. }
  23.  
  24. node_s *findmin_s (node_s *t) {
  25. return t->left ? findmin_s (t->left) : t;
  26. }
  27.  
  28. node_s *removemin_s (node_s *t){
  29. if(t->left == NULL)
  30. return t->right;
  31. t->left = removemin_s (t->left);
  32. return t;
  33. }
  34.  
  35. node_s *remove_s (node_s *t, int k) {
  36. if(t == NULL) return 0;
  37. if (k < t->elem)
  38. t->left = remove_s (t->left, k);
  39. else if (k > t->elem)
  40. t->right = remove_s (t->right, k);
  41. else // k == t->elem
  42. {
  43. node_s *q = t->left;
  44. node_s *r = t->right;
  45. delete t;
  46. if (r == NULL) return q;
  47. node_s *min = findmin_s(r);
  48. min->right = removemin_s(r);
  49. min->left = q;
  50. return min;
  51. }
  52. return t;
  53. }
  54.  
  55.  
  56. // other
  57.  
  58.  
  59. struct node {
  60. int elem;
  61. unsigned char height;
  62. char color;
  63. node* left = nullptr;
  64. node* right = nullptr;
  65. node* parent = nullptr;
  66. node(int k, char c) {elem = k; left = right = 0; height = 1; color = c;} // r - red; b - black
  67. };
  68.  
  69. node *root(NULL);
  70.  
  71. unsigned char height(node *t) {
  72. return t ? t->height : 0;
  73. }
  74.  
  75. int heightdif (node *t) {
  76. return height(t->right) - height(t->left);
  77. }
  78.  
  79. void fixheight (node *t) {
  80. unsigned char hl = height(t->left);
  81. unsigned char hr = height(t->right);
  82. t->height = (hl > hr ? hl : hr) + (t->color == 'b' ? 1 : 0);
  83. }
  84.  
  85. node *rotateright (node *t) {
  86. node *q = t->left;
  87. t->left = q->right;
  88. if (q->right != NULL) q->right->parent = t;
  89. if (q != NULL) q->parent = t->parent;
  90. if (t->parent) {
  91. if (t == t->parent->right)
  92. t->parent->right = q;
  93. else
  94. t->parent->left = q;
  95. }
  96. else {
  97. root = q;
  98. }
  99. q->right = t;
  100. if (t != NULL) t->parent = q;
  101. fixheight (t);
  102. fixheight (q);
  103. return q;
  104. }
  105.  
  106. node *rotateleft (node *t) {
  107. node *q = t->right;
  108. t->right = q->left;
  109. if (q->left != NULL) q->left->parent = t;
  110. if (q != NULL) q->parent = t->parent;
  111. if (t->parent) {
  112. if (t == t->parent->left)
  113. t->parent->left = q;
  114. else
  115. t->parent->right = q;
  116. }
  117. else {
  118. root = q;
  119. }
  120. q->left = t;
  121. if (t != NULL) t->parent = q;
  122. fixheight (t);
  123. fixheight (q);
  124. return q;
  125. }
  126.  
  127.  
  128. // AVL tree
  129.  
  130.  
  131.  
  132. node *balance (node *t) {
  133. fixheight(t);
  134. if(heightdif(t) == 2)
  135. {
  136. if(heightdif(t->right) < 0)
  137. t->right = rotateright(t->right);
  138. return rotateleft(t);
  139. }
  140. if(heightdif(t)==-2)
  141. {
  142. if(heightdif(t->left) > 0)
  143. t->left = rotateleft(t->left);
  144. return rotateright(t);
  145. }
  146. return t;
  147. }
  148.  
  149. node *ins_avl(node *t, int k) {
  150. if(!t) return new node(k, 'b');
  151. if (k < t->elem)
  152. t->left = ins_avl (t->left, k);
  153. else if (k > t->elem)
  154. t->right = ins_avl(t->right, k);
  155. return balance(t);
  156. }
  157.  
  158. node *findmin (node *t) {
  159. return t->left ? findmin (t->left) : t;
  160. }
  161.  
  162. node *removemin (node *t)
  163. {
  164. if(t->left == NULL)
  165. return t->right;
  166. t->left = removemin (t->left);
  167. return balance (t);
  168. }
  169.  
  170. node *remove(node *t, int k) {
  171. if(t == NULL) return 0;
  172. if (k < t->elem)
  173. t->left = remove (t->left, k);
  174. else if (k > t->elem)
  175. t->right = remove (t->right, k);
  176. else // k == t->elem
  177. {
  178. node *q = t->left;
  179. node *r = t->right;
  180. delete t;
  181. if (r == NULL) return q;
  182. node *min = findmin(r);
  183. min->right = removemin(r);
  184. min->left = q;
  185. return balance (min);
  186. }
  187. return balance(t);
  188. }
  189.  
  190.  
  191. // red-black tree
  192.  
  193. void insreb(node *&x) {
  194. while (x != root && x->parent->color == 'r') {
  195. if (x->parent == x->parent->parent->left) {
  196. node *u = x->parent->parent->right;
  197. if (u != NULL && u->color == 'r') { // uncle is red
  198. x->parent->color = 'b';
  199. u->color = 'b';
  200. x->parent->parent->color = 'r';
  201. x = x->parent->parent;
  202. }
  203. else { // uncle is black
  204. if (x == x->parent->right) {
  205. // rotate
  206. x = x->parent;
  207. x = rotateleft(x);////////////////
  208. }
  209. // recolor and rotate
  210. x->parent->color = 'b';
  211. x->parent->parent->color = 'r';
  212. x->parent->parent = rotateright(x->parent->parent);
  213. }
  214. }
  215. else {
  216. // mirror
  217. node *u = x->parent->parent->left;
  218. if (u != NULL && u->color == 'r') {
  219. x->parent->color = 'b';
  220. u->color = 'b';
  221. x->parent->parent->color = 'r';
  222. x = x->parent->parent;
  223. }
  224. else {
  225. if (x == x->parent->left) {
  226. x = x->parent;
  227. x = rotateright(x);///////////////
  228. }
  229. x->parent->color = 'b';
  230. x->parent->parent->color = 'r';
  231. x->parent->parent = rotateleft(x->parent->parent);
  232. }
  233. }
  234. }
  235. root->color = 'b';
  236. }
  237.  
  238. node *ins_rb(node *&T, int k) {
  239. if (T == NULL) {
  240. T = new node (k,'b');
  241. root = T;
  242. return T;
  243. }
  244. node *t(T);
  245. while (!((k > t->elem && t->right == NULL) || (k < t->elem && t->left == NULL))) {
  246. if (k < t->elem)
  247. t = t->left;
  248. else if (k > t->elem)
  249. t = t->right;
  250. else break;
  251. }
  252. if (k < t->elem) {
  253. t->left = new node (k,'r');
  254. t->left->parent = t;
  255. insreb (t->left);
  256. }
  257. else if (k > t->elem) {
  258. t->right = new node (k,'r');
  259. t->right->parent = t;
  260. insreb (t->right);
  261. }
  262. return root;
  263. }
  264.  
  265.  
  266. int main(){
  267. node *T(NULL);
  268. T = ins_rb (T,10);
  269. T = ins_rb (T,32);
  270. T = ins_rb (T,15);
  271. //T = ins_rb (T,24);
  272. //T = ins_rb (T,1);
  273. cout << T->elem;
  274. //cout << T->color;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement