Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAXN 1000
  5.  
  6. typedef struct node
  7. {
  8. int value;
  9. struct node *left, *right;
  10. } node;
  11.  
  12. typedef int tip2;
  13.  
  14. typedef struct queue2
  15. {
  16. tip2 arr[MAXN];
  17. int front, rear;
  18. } queue2;
  19.  
  20. typedef node* tip;
  21.  
  22. typedef struct queue
  23. {
  24. tip arr[MAXN];
  25. int front, rear;
  26. } queue;
  27.  
  28. void push(queue *q, tip element);
  29.  
  30. tip pop(queue *q);
  31.  
  32. tip top(queue *q);
  33.  
  34. int empty(queue *q);
  35.  
  36. void push2(queue2 *q, tip2 element);
  37.  
  38. tip2 pop2(queue2 *q);
  39.  
  40. tip2 top2(queue2 *q);
  41.  
  42. int empty2(queue2 *q);
  43.  
  44. node* insertElementBST(node *root, int key);
  45.  
  46. node* deleteElementBST(node *root, int key);
  47.  
  48. node* insertElementBBST(node *root, int key);
  49.  
  50. node* deleteElementBBST(node *root, int key);
  51.  
  52. node* leftRotation(node *x);
  53.  
  54. node* rightRotation(node *x);
  55.  
  56. node* minValueNode(node *root);
  57.  
  58. node* maxValueNode(node *root);
  59.  
  60. void inorder(node *root);
  61.  
  62. //writeInorder zove inorder
  63. void writeInorder(node *root);
  64.  
  65. void levelOrder(node *root);
  66.  
  67. //printAll zove levelOrder
  68. void printAll(node *root);
  69.  
  70. //ispisuje oblik stabla
  71. void myPrintTree(node *root);
  72.  
  73. void preorder(node *root);
  74.  
  75. //writePreorder zove preorder
  76. void writePreorder(node *root);
  77.  
  78. void postorder(node *root);
  79.  
  80. //writePostorder zove postorder
  81. void writePostorder(node *root);
  82.  
  83. //Dubina stabla do dna
  84. int height(node *root);
  85.  
  86. //Provjera da li je stablo balansirano
  87. int checkGood(node *root, int *flag);
  88.  
  89. //isBalanced zove checkGood, za flag = 0 nije balansirano, za flag = 1 je balansirano
  90. void isBalanced(node *root);
  91.  
  92. int max(int a, int b);
  93.  
  94. int main()
  95. {
  96. FILE *fin;
  97. fin = fopen("ulaz.txt", "r");
  98. // int n, x;
  99. // scanf("%d", &n);
  100. //
  101. // node *bst = NULL;
  102. //
  103. // for (int i = 0; i < n; ++i)
  104. // {
  105. // scanf("%d", &x);
  106. // insertElement(&bst, x);
  107. // }
  108.  
  109. node *bst = NULL;
  110. int n = 0, a[100];
  111. while (fscanf(fin, "%d ", &a[n++]) != EOF);
  112.  
  113. --n;
  114. for (int i = 0; i < n; ++i)
  115. bst = insertElementBBST(bst, a[i]);
  116.  
  117. deleteElementBBST(bst, 8);
  118. deleteElementBBST(bst, 4);
  119. writeInorder(bst);
  120. writePreorder(bst);
  121. writePostorder(bst);
  122. printAll(bst);
  123. isBalanced(bst);
  124. myPrintTree(bst);
  125. }
  126.  
  127. node* insertElementBST(node *root, int key)
  128. {
  129. if (root == NULL)
  130. {
  131. node *nju = (node*)malloc(sizeof(node));
  132. nju->left = nju->right = NULL;
  133. nju->value = key;
  134. return nju;
  135. }
  136.  
  137. if (key < root->value)
  138. root->left = insertElementBST(root->left, key);
  139. else if (key > root->value)
  140. root->right = insertElementBST(root->right, key);
  141. }
  142.  
  143. node* deleteElementBST(node *root, int key)
  144. {
  145. if (root == NULL)
  146. return root;
  147.  
  148. if (key < root->value)
  149. root->left = deleteElementBST(root->left, key);
  150. else if (key > root->value)
  151. root->right = deleteElementBST(root->right, key);
  152. else if (key == root->value)
  153. {
  154. if (root->left == NULL)
  155. {
  156. node *p = root->right;
  157. free(root);
  158. return p;
  159. }
  160. else if (root->right == NULL)
  161. {
  162. node *p = root->left;
  163. free(root);
  164. return p;
  165. }
  166.  
  167. node *p = minValueNode(root->right);
  168. root->value = p->value;
  169. root->right = deleteElementBST(root->right, p->value);
  170. }
  171.  
  172. return root;
  173. }
  174.  
  175. node* insertElementBBST(node *root, int key)
  176. {
  177. if (root == NULL)
  178. {
  179. node *nju = (node*)malloc(sizeof(node));
  180. nju->left = nju->right = NULL;
  181. nju->value = key;
  182. return nju;
  183. }
  184.  
  185. if (key < root->value)
  186. root->left = insertElementBBST(root->left, key);
  187. else if (key > root->value)
  188. root->right = insertElementBBST(root->right, key);
  189.  
  190. int lf = height(root->left), rf = height(root->right);
  191. if (lf - rf < -1 || lf - rf > 1)
  192. {
  193. node *pl = root->left, *pr = root->right;
  194. if (lf - rf <= -2)
  195. {
  196. if (height(pr->left) - height(pr->right) <= 0)
  197. return leftRotation(root);
  198. else
  199. {
  200. root->right = rightRotation(root->right);
  201. return leftRotation(root);
  202. }
  203. }
  204. else if (lf - rf >= 2)
  205. {
  206. if (height(pl->left) - height(pl->right) >= 0)
  207. return rightRotation(root);
  208. else
  209. {
  210. root->left = leftRotation(root->left);
  211. return rightRotation(root);
  212. }
  213. }
  214. }
  215.  
  216. return root;
  217. }
  218.  
  219. node* deleteElementBBST(node *root, int key)
  220. {
  221. if (root == NULL)
  222. return NULL;
  223.  
  224. if (key < root->value)
  225. root->left = deleteElementBBST(root->left, key);
  226. else if (key > root->value)
  227. root->right = deleteElementBBST(root->right, key);
  228. else if (key == root->value)
  229. {
  230. if (root->left == NULL)
  231. {
  232. node *p = root->right;
  233. free(root);
  234. return p;
  235. }
  236. else if (root->right == NULL)
  237. {
  238. node *p = root->left;
  239. free(root);
  240. return p;
  241. }
  242.  
  243. node *p = minValueNode(root->right);
  244. root->value = p->value;
  245. root->right = deleteElementBST(root->right, p->value);
  246. }
  247.  
  248. int lf = height(root->left), rf = height(root->right);
  249. if (lf - rf < -1 || lf - rf > 1)
  250. {
  251. node *pl = root->left, *pr = root->right;
  252. if (lf - rf <= -2)
  253. {
  254. if (height(pr->left) - height(pr->right) <= 0)
  255. return leftRotation(root);
  256. else
  257. {
  258. root->right = rightRotation(root->right);
  259. return leftRotation(root);
  260. }
  261. }
  262. else if (lf - rf >= 2)
  263. {
  264. if (height(pl->left) - height(pl->right) >= 0)
  265. return rightRotation(root);
  266. else
  267. {
  268. root->left = leftRotation(root->left);
  269. return rightRotation(root);
  270. }
  271. }
  272. }
  273.  
  274. return root;
  275. }
  276.  
  277. void push(queue *q, tip element)
  278. {
  279. if (q->front == 0)
  280. {
  281. q->front = q->rear = 1;
  282. q->arr[q->front] = element;
  283. }
  284. else
  285. {
  286. q->rear = q->rear%MAXN + 1;
  287. q->arr[q->rear] = element;
  288. }
  289. }
  290.  
  291. tip pop(queue *q)
  292. {
  293. if (q->front == 0)
  294. {
  295. printf("GRESKA: PRAZAN REDpop\n");
  296. exit(1);
  297. }
  298.  
  299. tip ret = q->arr[q->front];
  300. if (q->front == q->rear)
  301. q->front = q->rear = 0;
  302. else
  303. q->front = q->front%MAXN + 1;
  304. return ret;
  305. }
  306.  
  307. tip top(queue *q)
  308. {
  309. if (q->front == 0)
  310. {
  311. printf("GRESKA: PRAZAN REDtop\n");
  312. exit(1);
  313. }
  314.  
  315. return q->arr[q->front];
  316. }
  317.  
  318. int empty(queue *q)
  319. {
  320. if (q->front == 0)
  321. return 1;
  322. else
  323. return 0;
  324. }
  325.  
  326. void push2(queue2 *q, tip2 element)
  327. {
  328. if (q->front == 0)
  329. {
  330. q->front = q->rear = 1;
  331. q->arr[q->front] = element;
  332. }
  333. else
  334. {
  335. q->rear = q->rear%MAXN + 1;
  336. q->arr[q->rear] = element;
  337. }
  338. }
  339.  
  340. tip2 pop2(queue2 *q)
  341. {
  342. if (q->front == 0)
  343. {
  344. printf("GRESKA: PRAZAN REDpop\n");
  345. exit(1);
  346. }
  347.  
  348. tip2 ret = q->arr[q->front];
  349. if (q->front == q->rear)
  350. q->front = q->rear = 0;
  351. else
  352. q->front = q->front%MAXN + 1;
  353. return ret;
  354. }
  355.  
  356. tip2 top2(queue2 *q)
  357. {
  358. if (q->front == 0)
  359. {
  360. printf("GRESKA: PRAZAN REDtop\n");
  361. exit(1);
  362. }
  363.  
  364. return q->arr[q->front];
  365. }
  366.  
  367. int empty2(queue2 *q)
  368. {
  369. if (q->front == 0)
  370. return 1;
  371. else
  372. return 0;
  373. }
  374.  
  375. node *minValueNode(node *root)
  376. {
  377. while (root->left != NULL)
  378. root = root->left;
  379. return root;
  380. }
  381.  
  382. node *maxValueNode(node *root)
  383. {
  384. while (root->right != NULL)
  385. root = root->right;
  386. return root;
  387. }
  388.  
  389. node* leftRotation(node *x)
  390. {
  391. node *y = x->right;
  392. node *temp = y->left;
  393. y->left = x;
  394. x->right = temp;
  395.  
  396. return y;
  397. }
  398.  
  399. node* rightRotation(node *x)
  400. {
  401. node *y = x->left;
  402. node *temp = y->right;
  403. y->right = x;
  404. x->left = temp;
  405.  
  406. return y;
  407. }
  408.  
  409. void inorder(node *root)
  410. {
  411. if (root != NULL)
  412. {
  413. inorder(root->left);
  414. printf("%d ", root->value);
  415. inorder(root->right);
  416. }
  417. }
  418.  
  419. void writeInorder(node *root)
  420. {
  421. printf("INORDER: ");
  422. inorder(root);
  423. printf("\n");
  424. }
  425.  
  426. void levelOrder(node *root)
  427. {
  428. queue q;
  429. q.front = q.rear = 0;
  430. push(&q, root);
  431.  
  432. queue2 q2;
  433. q2.front = q2.rear = 0;
  434. push2(&q2, 0);
  435.  
  436. while (!empty(&q))
  437. {
  438. node *cur = pop(&q);
  439. int height = pop2(&q2);
  440. printf("%d ", cur->value);
  441.  
  442. if (cur->left != NULL)
  443. {
  444. push(&q, cur->left);
  445. push2(&q2, height + 1);
  446. }
  447. if (cur->right != NULL)
  448. {
  449. push(&q, cur->right);
  450. push2(&q2, height + 1);
  451. }
  452.  
  453. if (!empty2(&q2) && top2(&q2) != height)
  454. printf("\n");
  455. }
  456. printf("\n");
  457. }
  458.  
  459. void printAll(node *root)
  460. {
  461. levelOrder(root);
  462. }
  463.  
  464. void myPrintTree(node *root)
  465. {
  466. if (root != NULL)
  467. {
  468. printf("%d ", root->value);
  469. if (root->left != NULL)
  470. printf("L: %d ", root->left->value);
  471. if (root->right != NULL)
  472. printf("R: %d", root->right->value);
  473. printf("\n");
  474. myPrintTree(root->left);
  475. myPrintTree(root->right);
  476. }
  477. }
  478.  
  479. void preorder(node *root)
  480. {
  481. if (root != NULL)
  482. {
  483. printf("%d ", root->value);
  484. preorder(root->left);
  485. preorder(root->right);
  486. }
  487. }
  488.  
  489. void writePreorder(node *root)
  490. {
  491. printf("PREORDER: ");
  492. preorder(root);
  493. printf("\n");
  494. }
  495.  
  496. void postorder(node *root)
  497. {
  498. if (root != NULL)
  499. {
  500. postorder(root->left);
  501. postorder(root->right);
  502. printf("%d ", root->value);
  503. }
  504. }
  505.  
  506. void writePostorder(node *root)
  507. {
  508. printf("POSTORDER: ");
  509. postorder(root);
  510. printf("\n");
  511. }
  512.  
  513. int height(node *root)
  514. {
  515. if (root == NULL)
  516. return 0;
  517. int lf = 0, rf = 0;
  518. if (root->left != NULL)
  519. lf = height(root->left);
  520. if (root->right != NULL)
  521. rf = height(root->right);
  522.  
  523. return max(lf, rf) + 1;
  524. }
  525.  
  526. int checkGood(node *root, int *flag)
  527. {
  528. if (root == NULL)
  529. return 0;
  530.  
  531. int lf = 0, rf = 0;
  532. if (root->left != NULL)
  533. lf = checkGood(root->left, flag);
  534. if (root->right != NULL)
  535. rf = checkGood(root->right, flag);
  536.  
  537. if (lf - rf < -1 || lf - rf > 1)
  538. *flag = 0;
  539. return max(lf, rf) + 1;
  540. }
  541.  
  542. void isBalanced(node *root)
  543. {
  544. int flag = 1;
  545. checkGood(root, &flag);
  546.  
  547. if (!flag)
  548. printf("The tree is not balanced\n");
  549. else
  550. printf("The tree is balanced\n");
  551. }
  552.  
  553. int max(int a, int b)
  554. {
  555. return (a >= b) ? a : b;
  556. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement