Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.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 = new Node;
  32. node->key = key;
  33. node->left = NULL;
  34. node->right = NULL;
  35. node->height = 1;
  36. return(node);
  37. }
  38.  
  39. struct Node *rightRotate(struct Node *y)
  40. {
  41. struct Node *x = y->left;
  42. struct Node *T2 = x->right;
  43.  
  44.  
  45. x->right = y;
  46. y->left = T2;
  47.  
  48. y->height = max(height(y->left), height(y->right))+1;
  49. x->height = max(height(x->left), height(x->right))+1;
  50.  
  51. return x;
  52. }
  53.  
  54. struct Node *leftRotate(struct Node *x)
  55. {
  56. struct Node *y = x->right;
  57. struct Node *T2 = y->left;
  58.  
  59. y->left = x;
  60. x->right = T2;
  61.  
  62. x->height = max(height(x->left), height(x->right))+1;
  63. y->height = max(height(y->left), height(y->right))+1;
  64.  
  65. return y;
  66. }
  67.  
  68. int getBalance(struct Node *N)
  69. {
  70. if (N == NULL)
  71. return 0;
  72. return height(N->left) - height(N->right);
  73. }
  74.  
  75. struct Node * minValueNode(struct Node* node)
  76. {
  77. struct Node* current = node;
  78.  
  79. while (current->left != NULL)
  80. current = current->left;
  81.  
  82. return current;
  83. }
  84.  
  85. int maxLevel(struct Node* node)
  86. {
  87. if(node == NULL)
  88. {
  89. return 0;
  90. }
  91. else return max(maxLevel(node->left), maxLevel(node->right))+1;
  92. }
  93.  
  94. struct Node* insertAVL(struct Node* node, int key)
  95. {
  96. if (node == NULL)
  97. return(newNode(key));
  98.  
  99. if (key < node->key)
  100. node->left = insertAVL(node->left, key);
  101. else if (key > node->key)
  102. node->right = insertAVL(node->right, key);
  103. else
  104. return node;
  105.  
  106. node->height = 1 + max(height(node->left),
  107. height(node->right));
  108.  
  109. int balance = getBalance(node);
  110.  
  111. if (balance > 1 && key < node->left->key)
  112. return rightRotate(node);
  113.  
  114. if (balance < -1 && key > node->right->key)
  115. return leftRotate(node);
  116.  
  117. if (balance > 1 && key > node->left->key)
  118. {
  119. node->left = leftRotate(node->left);
  120. return rightRotate(node);
  121. }
  122.  
  123. if (balance < -1 && key < node->right->key)
  124. {
  125. node->right = rightRotate(node->right);
  126. return leftRotate(node);
  127. }
  128.  
  129. return node;
  130. }
  131.  
  132. struct Node* deleteNodeAVL(struct Node* root, int key)
  133. {
  134. if (root == NULL)
  135. return root;
  136.  
  137. if ( key < root->key )
  138. root->left = deleteNodeAVL(root->left, key);
  139.  
  140. else if( key > root->key )
  141. root->right = deleteNodeAVL(root->right, key);
  142. else
  143. {
  144. if( (root->left == NULL) || (root->right == NULL) )
  145. {
  146. struct Node *temp = root->left ? root->left :
  147. root->right;
  148.  
  149. if (temp == NULL)
  150. {
  151. temp = root;
  152. root = NULL;
  153. }
  154. else
  155. *root = *temp;
  156. free(temp);
  157. }
  158. else
  159. {
  160. struct Node* temp = minValueNode(root->right);
  161. root->key = temp->key;
  162. root->right = deleteNodeAVL(root->right, temp->key);
  163. }
  164. }
  165. if (root == NULL)
  166. return root;
  167.  
  168. root->height = 1 + max(height(root->left),
  169. height(root->right));
  170.  
  171. int balance = getBalance(root);
  172.  
  173. if (balance > 1 && getBalance(root->left) >= 0)
  174. return rightRotate(root);
  175.  
  176. if (balance > 1 && getBalance(root->left) < 0)
  177. {
  178. root->left = leftRotate(root->left);
  179. return rightRotate(root);
  180. }
  181.  
  182. if (balance < -1 && getBalance(root->right) <= 0)
  183. return leftRotate(root);
  184.  
  185. if (balance < -1 && getBalance(root->right) > 0)
  186. {
  187. root->right = rightRotate(root->right);
  188. return leftRotate(root);
  189. }
  190.  
  191. return root;
  192. }
  193.  
  194.  
  195. void drawTreeTMP(struct Node *current, char*tab, int width, int x, int y)
  196. {
  197. if(current!=NULL)
  198. {
  199. if(current->key/10!=0)
  200. tab[(width+1)*y+x-1] = '0' + current->key/10;
  201. tab[(width+1)*y+x] = '0' + current->key%10;
  202.  
  203. if(current->right!=NULL)
  204. {
  205. tab[(width+1)*(y+1)+(x+1)] ='\\';
  206. drawTreeTMP(current->right, tab, width, x+2, y+2);
  207. }
  208. if(current->left!=NULL)
  209. {
  210. tab[(width+1)*(y+1)+(x-1)] = '/';
  211. drawTreeTMP(current->left, tab, width, x-2, y+2);
  212. }
  213. }
  214. }
  215.  
  216. void drawTree(struct Node *root)
  217. {
  218. int layers = maxLevel(root);
  219. int height = layers*3+1;
  220. int width = layers*5+1;
  221. char *tab = new char[height*(width+1)+1];
  222. tab[height*(width+1)] = '\0';
  223. int x, y;
  224. for(y=0;y<height;y++)
  225. {
  226. for(x=0;x<width;x++)
  227. {
  228. if(x==0||x==width-1||y==0||y==height-1)
  229. tab[(width+1)*y+x] = ' ';
  230. else
  231. tab[(width+1)*y+x] = ' ';
  232. }
  233. tab[(width+1)*y+x] = '\n';
  234. }
  235.  
  236. drawTreeTMP(root, tab, width, width/2, 2);
  237.  
  238.  
  239. cout << tab;
  240. free(tab);
  241. }
  242. struct Node *postorder(struct Node *root)
  243. {
  244. if(root->left != NULL) postorder(root->left);
  245. return root;
  246. if(root->right!=NULL) postorder(root->right);
  247.  
  248. };
  249.  
  250. int main()
  251. {
  252. struct Node *rootavl = NULL;
  253. struct Node *root = NULL;
  254.  
  255. while(1)
  256. {
  257. int nr;
  258. cout<<"Podaj element:";
  259. cin >> nr;
  260. root = insertAVL(root, nr);
  261. drawTree(root);
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement