Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9. int key;
  10. struct Node *left;
  11. struct Node *right;
  12. int height;
  13. };
  14.  
  15. int max(int a, int b);
  16.  
  17. int height(struct Node *N)
  18. {
  19. if (N == NULL)
  20. return 0;
  21. return N->height;
  22. }
  23.  
  24. int max(int a, int b)
  25. {
  26. return (a > b)? a : b;
  27. }
  28.  
  29. struct Node* newNode(int key)
  30. {
  31. struct Node* node = (struct Node*)
  32. malloc(sizeof(struct Node));
  33. node->key = key;
  34. node->left = NULL;
  35. node->right = NULL;
  36. node->height = 1;
  37. return(node);
  38. }
  39.  
  40. struct Node *rightRotate(struct Node *y)
  41. {
  42. struct Node *x = y->left;
  43. struct Node *T2 = x->right;
  44.  
  45.  
  46. x->right = y;
  47. y->left = T2;
  48.  
  49. y->height = max(height(y->left), height(y->right))+1;
  50. x->height = max(height(x->left), height(x->right))+1;
  51.  
  52. return x;
  53. }
  54.  
  55. struct Node *leftRotate(struct Node *x)
  56. {
  57. struct Node *y = x->right;
  58. struct Node *T2 = y->left;
  59.  
  60. y->left = x;
  61. x->right = T2;
  62.  
  63. x->height = max(height(x->left), height(x->right))+1;
  64. y->height = max(height(y->left), height(y->right))+1;
  65.  
  66. return y;
  67. }
  68.  
  69. int getBalance(struct Node *N)
  70. {
  71. if (N == NULL)
  72. return 0;
  73. return height(N->left) - height(N->right);
  74. }
  75.  
  76. struct Node * minValueNode(struct Node* node)
  77. {
  78. struct Node* current = node;
  79.  
  80. while (current->left != NULL)
  81. current = current->left;
  82.  
  83. return current;
  84. }
  85.  
  86. int maxLevel(struct Node* node)
  87. {
  88. if(node == NULL)
  89. {
  90. return 0;
  91. }
  92. else return max(maxLevel(node->left), maxLevel(node->right))+1;
  93. }
  94.  
  95. struct Node* insertAVL(struct Node* node, int key)
  96. {
  97. if (node == NULL)
  98. return(newNode(key));
  99.  
  100. if (key < node->key)
  101. node->left = insertAVL(node->left, key);
  102. else if (key > node->key)
  103. node->right = insertAVL(node->right, key);
  104. else
  105. return node;
  106.  
  107. node->height = 1 + max(height(node->left),
  108. height(node->right));
  109.  
  110. int balance = getBalance(node);
  111.  
  112. if (balance > 1 && key < node->left->key)
  113. return rightRotate(node);
  114.  
  115. if (balance < -1 && key > node->right->key)
  116. return leftRotate(node);
  117.  
  118. if (balance > 1 && key > node->left->key)
  119. {
  120. node->left = leftRotate(node->left);
  121. return rightRotate(node);
  122. }
  123.  
  124. if (balance < -1 && key < node->right->key)
  125. {
  126. node->right = rightRotate(node->right);
  127. return leftRotate(node);
  128. }
  129.  
  130. return node;
  131. }
  132.  
  133. struct Node* deleteNodeAVL(struct Node* root, int key)
  134. {
  135. if (root == NULL)
  136. return root;
  137.  
  138. if ( key < root->key )
  139. root->left = deleteNodeAVL(root->left, key);
  140.  
  141. else if( key > root->key )
  142. root->right = deleteNodeAVL(root->right, key);
  143. else
  144. {
  145. if( (root->left == NULL) || (root->right == NULL) )
  146. {
  147. struct Node *temp = root->left ? root->left :
  148. root->right;
  149.  
  150. if (temp == NULL)
  151. {
  152. temp = root;
  153. root = NULL;
  154. }
  155. else
  156. *root = *temp;
  157. free(temp);
  158. }
  159. else
  160. {
  161. struct Node* temp = minValueNode(root->right);
  162. root->key = temp->key;
  163. root->right = deleteNodeAVL(root->right, temp->key);
  164. }
  165. }
  166. if (root == NULL)
  167. return root;
  168.  
  169. root->height = 1 + max(height(root->left),
  170. height(root->right));
  171.  
  172. int balance = getBalance(root);
  173.  
  174. if (balance > 1 && getBalance(root->left) >= 0)
  175. return rightRotate(root);
  176.  
  177. if (balance > 1 && getBalance(root->left) < 0)
  178. {
  179. root->left = leftRotate(root->left);
  180. return rightRotate(root);
  181. }
  182.  
  183. if (balance < -1 && getBalance(root->right) <= 0)
  184. return leftRotate(root);
  185.  
  186. if (balance < -1 && getBalance(root->right) > 0)
  187. {
  188. root->right = rightRotate(root->right);
  189. return leftRotate(root);
  190. }
  191.  
  192. return root;
  193. }
  194.  
  195.  
  196.  
  197.  
  198. void preOrder(struct Node *root)
  199. {
  200. if(root != NULL)
  201. {
  202. printf("%d ", root->key);
  203. preOrder(root->left);
  204. preOrder(root->right);
  205. }
  206. }
  207.  
  208. void inOrder(struct Node *root)
  209. {
  210. if(root != NULL)
  211. {
  212. inOrder(root->left);
  213. printf("%d ", root->key);
  214. inOrder(root->right);
  215. }
  216. }
  217.  
  218. void postOrder(struct Node *root)
  219. {
  220. if(root != NULL)
  221. {
  222. postOrder(root->left);
  223. postOrder(root->right);
  224. printf("%d ", root->key);
  225. }
  226. }
  227.  
  228. void drawTreeTMP(struct Node *current, char*tab, int width, int x, int y)
  229. {
  230. if(current!=NULL)
  231. {
  232. if(current->key/10!=0)
  233. tab[(width+1)*y+x-1] = '0' + current->key/10;
  234. tab[(width+1)*y+x] = '0' + current->key%10;
  235.  
  236. if(current->right!=NULL)
  237. {
  238. tab[(width+1)*(y+1)+(x+1)] ='\\';
  239. drawTreeTMP(current->right, tab, width, x+2, y+2);
  240. }
  241. if(current->left!=NULL)
  242. {
  243. tab[(width+1)*(y+1)+(x-1)] = '/';
  244. drawTreeTMP(current->left, tab, width, x-2, y+2);
  245. }
  246. }
  247. }
  248.  
  249. void drawTree(struct Node *root)
  250. {
  251. int layers = maxLevel(root);
  252. int height = layers*3+1;
  253. int width = layers*5+1;
  254. char *tab = new char[height*(width+1)+1];
  255. tab[height*(width+1)] = '\0';
  256. int x, y;
  257. for(y=0;y<height;y++)
  258. {
  259. for(x=0;x<width;x++)
  260. {
  261. if(x==0||x==width-1||y==0||y==height-1)
  262. tab[(width+1)*y+x] = ' ';
  263. else
  264. tab[(width+1)*y+x] = ' ';
  265. }
  266. tab[(width+1)*y+x] = '\n';
  267. }
  268.  
  269. drawTreeTMP(root, tab, width, width/2, 2);
  270.  
  271.  
  272. printf("%s", tab);
  273. free(tab);
  274. }
  275. struct Node *postorder(struct Node *root)
  276. {
  277. if(root->left != NULL) postorder(root->left);
  278. return root;
  279. if(root->right!=NULL) postorder(root->right);
  280.  
  281. };
  282.  
  283. int main()
  284. {
  285. struct Node *rootavl = NULL;
  286. struct Node *root = NULL;
  287. int tmp;
  288.  
  289. while(1)
  290. {
  291.  
  292. drawTree(root);
  293. cout<<"1.Usun element\t\t";
  294. cout<<"2.Dodaj element\t\t";
  295. cout<<"3.Usun cale drzewo\t\t";
  296. int tmp2;
  297. scanf("%d", &tmp2);
  298. if(tmp2 == 1)
  299. {
  300. int nr;
  301. cin >> nr;
  302. root = deleteNodeAVL(root, nr);
  303. }
  304. else if(tmp2 == 2)
  305. {
  306. int nr;
  307. cout<<"Podaj element:";
  308. cin >> nr;
  309. root = insertAVL(root, nr);
  310. }
  311. else if(tmp2 == 3)
  312. {
  313. while(root->height>1) deleteNodeAVL(postorder(root),postorder(root)->key);
  314. root=NULL;
  315. }
  316. }
  317.  
  318.  
  319.  
  320. return 0;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement