Advertisement
Guest User

Untitled

a guest
May 26th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. // avltree.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <cstdlib>
  8.  
  9. typedef struct AVLTree
  10. {
  11. int data;
  12. struct AVLTree *left, *right;
  13. int ht;
  14. }AVLTree;
  15.  
  16. AVLTree *insert(AVLTree *, int);
  17. AVLTree *Delete(AVLTree *, int);
  18. void preorder(AVLTree *);
  19. void inorder(AVLTree *);
  20. int height(AVLTree *);
  21. AVLTree *rotateright(AVLTree *);
  22. AVLTree *rotateleft(AVLTree *);
  23. AVLTree *RR(AVLTree *);
  24. AVLTree *LL(AVLTree *);
  25. AVLTree *LR(AVLTree *);
  26. AVLTree *RL(AVLTree *);
  27. int BF(AVLTree *);
  28.  
  29.  
  30.  
  31. AVLTree * insert(AVLTree *T, int x)
  32. {
  33. if (T == NULL)
  34. {
  35. T = (AVLTree*)malloc(sizeof(AVLTree));
  36. T->data = x;
  37. T->left = NULL;
  38. T->right = NULL;
  39. }
  40. else
  41. if (x > T->data) // wstaw do prawego poddrzewa
  42. {
  43. T->right = insert(T->right, x);
  44. if (BF(T) == -2)
  45. if (x > T->right->data)
  46. T = RR(T);
  47. else
  48. T = RL(T);
  49. }
  50. else
  51. if (x < T->data)
  52. {
  53. T->left = insert(T->left, x);
  54. if (BF(T) == 2)
  55. if (x < T->left->data)
  56. T = LL(T);
  57. else
  58. T = LR(T);
  59. }
  60.  
  61. T->ht = height(T);
  62.  
  63. return(T);
  64. }
  65.  
  66. AVLTree * Delete(AVLTree *T, int x)
  67. {
  68. AVLTree *p;
  69.  
  70. if (T == NULL)
  71. {
  72. return NULL;
  73. }
  74. else
  75. if (x > T->data) // wstawianie do prawego poddrzewa
  76. {
  77. T->right = Delete(T->right, x);
  78. if (BF(T) == 2)
  79. if (BF(T->left) >= 0)
  80. T = LL(T);
  81. else
  82. T = LR(T);
  83. }
  84. else
  85. if (x < T->data)
  86. {
  87. T->left = Delete(T->left, x);
  88. if (BF(T) == -2) //Ponowne zrównoważenie
  89. if (BF(T->right) <= 0)
  90. T = RR(T);
  91. else
  92. T = RL(T);
  93. }
  94. else
  95. {
  96. //znalezienie usuwanego elementu
  97. if (T->right != NULL)
  98. { //usuwanie następcy procedury inorder
  99. p = T->right;
  100.  
  101. while (p->left != NULL)
  102. p = p->left;
  103.  
  104. T->data = p->data;
  105. T->right = Delete(T->right, p->data);
  106.  
  107. if (BF(T) == 2)//Ponowne równoważenie
  108. if (BF(T->left) >= 0)
  109. T = LL(T);
  110. else
  111. T = LR(T); \
  112. }
  113. else
  114. return(T->left);
  115. }
  116. T->ht = height(T);
  117. return(T);
  118. }
  119.  
  120. int height(AVLTree *T)
  121. {
  122. int lh, rh;
  123. if (T == NULL)
  124. return(0);
  125.  
  126. if (T->left == NULL)
  127. lh = 0;
  128. else
  129. lh = 1 + T->left->ht;
  130.  
  131. if (T->right == NULL)
  132. rh = 0;
  133. else
  134. rh = 1 + T->right->ht;
  135.  
  136. if (lh > rh)
  137. return(lh);
  138.  
  139. return(rh);
  140. }
  141.  
  142. AVLTree * rotateright(AVLTree *x)
  143. {
  144. AVLTree *y;
  145. y = x->left;
  146. x->left = y->right;
  147. y->right = x;
  148. x->ht = height(x);
  149. y->ht = height(y);
  150. return(y);
  151. }
  152.  
  153. AVLTree * rotateleft(AVLTree *x)
  154. {
  155. AVLTree *y;
  156. y = x->right;
  157. x->right = y->left;
  158. y->left = x;
  159. x->ht = height(x);
  160. y->ht = height(y);
  161.  
  162. return(y);
  163. }
  164.  
  165. AVLTree * RR(AVLTree *T)
  166. {
  167. T = rotateleft(T);
  168. return(T);
  169. }
  170.  
  171. AVLTree * LL(AVLTree *T)
  172. {
  173. T = rotateright(T);
  174. return(T);
  175. }
  176.  
  177. AVLTree * LR(AVLTree *T)
  178. {
  179. T->left = rotateleft(T->left);
  180. T = rotateright(T);
  181.  
  182. return(T);
  183. }
  184.  
  185. AVLTree * RL(AVLTree *T)
  186. {
  187. T->right = rotateright(T->right);
  188. T = rotateleft(T);
  189. return(T);
  190. }
  191.  
  192. int BF(AVLTree *T)
  193. {
  194. int lh, rh;
  195. if (T == NULL)
  196. return(0);
  197.  
  198. if (T->left == NULL)
  199. lh = 0;
  200. else
  201. lh = 1 + T->left->ht;
  202.  
  203. if (T->right == NULL)
  204. rh = 0;
  205. else
  206. rh = 1 + T->right->ht;
  207.  
  208. return(lh - rh);
  209. }
  210.  
  211. void preorder(AVLTree *T)
  212. {
  213. if (T != NULL)
  214. {
  215. printf("%d(bf=%d)", T->data, BF(T));
  216. preorder(T->left);
  217. preorder(T->right);
  218. }
  219. }
  220.  
  221. void inorder(AVLTree *T)
  222. {
  223. if (T != NULL)
  224. {
  225. inorder(T->left);
  226. printf("%d(bf=%d)", T->data, BF(T));
  227. inorder(T->right);
  228. }
  229. }
  230.  
  231. int main()
  232. {
  233. AVLTree *root = NULL;
  234. int x, n, i, op;
  235. printf(" DRZEWO AVL \n");
  236. do
  237. {
  238. printf("\n1)Utworz drzewo:");
  239. printf("\n2)Wstaw dane:");
  240. printf("\n3)Usun dane:");
  241. printf("\n4)Wyswietl drzewo:");
  242. printf("\n5)Zakoncz:");
  243. printf("\n\nPodaj opcje:");
  244. scanf_s("%d", &op);
  245.  
  246. switch (op)
  247. {
  248. case 1: printf("\nPodaj liczbę elementow:");
  249. scanf_s("%d", &n);
  250. printf("\nPodaj dane do drzewa:");
  251. root = NULL;
  252. for (i = 0; i < n; i++)
  253. {
  254. scanf_s("%d", &x);
  255. root = insert(root, x);
  256. }
  257. break;
  258.  
  259. case 2: printf("\nPodaj dane:");
  260. scanf_s("%d", &x);
  261. root = insert(root, x);
  262. break;
  263.  
  264. case 3: printf("\nPodaj dane:");
  265. scanf_s("%d", &x);
  266. root = Delete(root, x);
  267. break;
  268.  
  269. case 4: printf("\nSekwencja PREORDER:\n");
  270. preorder(root);
  271. printf("\n\nSekwencja INORDER:\n");
  272. inorder(root);
  273. printf("\n");
  274. break;
  275. }
  276. } while (op != 5);
  277.  
  278. return 0;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement