Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. #include<iostream>
  2. #include<queue>
  3.  
  4. using namespace std;
  5.  
  6. int max(int a, int b)
  7. {
  8. if (a >= b) return a;
  9. return b;
  10. }
  11.  
  12. struct NOD
  13. {
  14. int key;
  15. int factorBalansare;
  16. NOD* left;
  17. NOD* right;
  18. NOD* parent;
  19. NOD(int inf = 0)
  20. {
  21. key = inf;
  22. factorBalansare = 0;
  23. left = NULL;
  24. right = NULL;
  25. parent = NULL;
  26.  
  27. }
  28. };
  29.  
  30. struct AVL
  31. {
  32. NOD* radacina;
  33. AVL()
  34. {
  35. radacina = NULL;
  36. }
  37. int height(NOD* x)
  38. {
  39. if (x == NULL)
  40. return 0;
  41. return (1 + max(height(x->left), height(x->right)));
  42. }
  43. /*void setBalance(NOD* x)
  44. {
  45. x->factorBalansare = height(x->right) - height(x->left);
  46. }*/
  47. void Insert(NOD* newNod)
  48. {
  49. NOD* nodParinte;
  50. nodParinte = NULL;
  51. NOD *x = radacina;
  52. while (x != NULL)
  53. {
  54. nodParinte = x;
  55. if (newNod->key < x->key)
  56. x = x->left;
  57. else
  58. x = x->right;
  59. }
  60. newNod->parent = nodParinte;
  61. if (nodParinte == NULL)
  62. radacina = newNod;
  63. else
  64. {
  65. if (newNod->key < nodParinte->key)
  66. nodParinte->left = newNod;
  67. else
  68. nodParinte->right = newNod;
  69. newNod->parent = nodParinte;
  70. }
  71. insert_repair(newNod);
  72. }
  73. void insert_repair( NOD* x)
  74. {
  75. BALANCE_FACTOR(x);
  76. if (x->factorBalansare == -2)
  77. {
  78. if (x->left->factorBalansare == 1)
  79. rotatieST(x->left);
  80. rotatieDR(x);
  81.  
  82. }
  83. else
  84. {
  85. if (x->factorBalansare == 2)
  86. {
  87. if (x->right->factorBalansare == -1)
  88. rotatieDR(x->right);
  89. rotatieST(x);
  90. }
  91. }
  92. if (x->parent != NULL)
  93. insert_repair( x->parent);
  94. }
  95. void BALANCE_FACTOR(NOD* x)
  96. {
  97. int hst=1, hdr=1;
  98. if (x->left != NULL)
  99. hst = height(x->left);
  100. else
  101. hst = 0;
  102.  
  103. if (x->right != NULL)
  104. hdr = height(x->right);
  105. else
  106. hdr = 0;
  107.  
  108. x->factorBalansare = hdr - hst;
  109. }
  110. void transplant(NOD* u, NOD* v)
  111. {
  112. if (u->parent == NULL)
  113. radacina = v;
  114. else
  115. {
  116. if (u == u->parent->left)
  117. u->parent->left = v;
  118. else
  119. u->parent->right = v;
  120. }
  121. if (v != NULL)
  122. v->parent = u->parent;
  123. }
  124. void delete_repair(NOD* x)
  125. {
  126. BALANCE_FACTOR(x);
  127. if (x->factorBalansare == -2)
  128. {
  129. if (x->left->factorBalansare == -1 || x->left->factorBalansare == 0)
  130. rotatieDR(x);
  131. if (x->left->factorBalansare == 1)
  132. {
  133. rotatieST(x->left);
  134. rotatieDR(x);
  135. }
  136. }
  137. if (x->factorBalansare == 2)
  138. {
  139. if (x->right->factorBalansare == 1 || x->right->factorBalansare == 0)
  140. rotatieST(x);
  141. if (x->right->factorBalansare == -1)
  142. {
  143. rotatieDR(x->right);
  144. rotatieST(x);
  145. }
  146. }
  147. if (x->parent != NULL)
  148. delete_repair(x->parent);
  149. }
  150. void delete_AVL(NOD* z)
  151. {
  152. NOD* y;
  153. NOD* s = z->parent;
  154. if (z->left == NULL)
  155. transplant(z, z->right);
  156. else
  157. if (z->right == NULL)
  158. transplant(z, z->left);
  159. else
  160. {
  161. y = succesor(z);
  162. s = y->parent;
  163. if (y != z->right)
  164. {
  165. transplant(y, y->right);
  166. y->right = z->right;
  167. z->right->parent = y;
  168. }
  169.  
  170. y->left = z->left;
  171. z->left->parent = y;
  172. transplant(z, y);
  173. }
  174. delete_repair(s);
  175. delete z;
  176. }
  177. NOD* min_AB(NOD*x)
  178. {
  179. NOD* y = x;
  180. while (y->left != NULL)
  181. y = y->left;
  182. return y;
  183. }
  184.  
  185. NOD* max_AB(NOD*x)
  186. {
  187. NOD* y = x;
  188. while (y->right != NULL)
  189. y = y->right;
  190. return y;
  191. }
  192.  
  193. NOD* succesor(NOD*x)
  194. {
  195. NOD* y;
  196. if (x->right != NULL)
  197. {
  198. y = min_AB(x->right);
  199. return y;
  200. }
  201. y = x->parent;
  202. while (y != NULL && x == y->right)
  203. {
  204. x = y;
  205. y = y->parent;
  206. }
  207. return y;
  208. }
  209. NOD* predecesor(NOD*x)
  210. {
  211. NOD* y;
  212. if (x->left != NULL)
  213. {
  214. y = max_AB(x->left);
  215. return y;
  216. }
  217. y = x->parent;
  218. while (y != NULL && x == y->left)
  219. {
  220. x = y;
  221. y = y->parent;
  222. }
  223. return y;
  224. }
  225. void rotatieST(NOD* y)
  226. {
  227. NOD*x = y->right;
  228. y->right = x->left;
  229. if (x->left != NULL)
  230. x->left->parent = y;
  231. x->parent = y->parent;
  232. if (y->parent == NULL)
  233. radacina = x;
  234. else
  235. {
  236. if (y == y->parent->left)
  237. y->parent->left = x;
  238. else
  239. y->parent->right = x;
  240. }
  241. x->left = y;
  242. y->parent = x;
  243. }
  244.  
  245. void rotatieDR(NOD* y)
  246. {
  247. NOD*x = y->left;
  248. y->left = x->right;
  249. if (x->right != NULL)
  250. x->right->parent = y;
  251. x->parent = y->parent;
  252. if (y->parent == NULL)
  253. radacina = x;
  254. else
  255. {
  256. if (y == y->parent->right)
  257. y->parent->right = x;
  258. else
  259. y->parent->left = x;
  260. }
  261. x->right = y;
  262. y->parent = x;
  263. }
  264. void printPreorder(NOD* rad)
  265. {
  266. if (rad != NULL)
  267. {
  268. cout << rad->key << " ";
  269. printPreorder(rad->left);
  270. printPreorder(rad->right);
  271. }
  272.  
  273. }
  274. void prntInorder(NOD* rad)
  275. {
  276. if (rad != NULL)
  277. {
  278. prntInorder(rad->left);
  279. cout << rad->key << " ";
  280. prntInorder(rad->right);
  281. }
  282.  
  283. }
  284. void printPostorder(NOD* rad)
  285. {
  286. if (rad != NULL)
  287. {
  288. printPostorder(rad->left);
  289. printPostorder(rad->right);
  290. cout << rad->key << " ";
  291. }
  292. }
  293. void printLevel(NOD* x)
  294. {
  295. queue<NOD*> coada;
  296. coada.push(x);
  297. while (!coada.empty())
  298. {
  299. NOD *y = coada.front();
  300. cout << y->key << " ";
  301. if (y->left != NULL) coada.push(y->left);
  302. if (y->right != NULL) coada.push(y->right);
  303. coada.pop();
  304. }
  305. }
  306.  
  307. };
  308.  
  309. int main()
  310. {
  311. int n,key;
  312. AVL a;
  313. cin >> n;
  314. for (int i = 0; i < n; i++)
  315. {
  316. cin >> key;
  317. NOD* nod = new NOD(key);
  318. a.Insert(nod);
  319. }
  320. a.printLevel(a.radacina);
  321. cout << endl;
  322. cin >> key;
  323. NOD* nod = new NOD(key);
  324. a.delete_AVL(nod);
  325. a.printLevel(a.radacina);
  326. system("pause");
  327. return 0;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement