Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <ctype.h>
  5.  
  6. typedef struct Node
  7. {
  8. char info;
  9. struct Node *left, *right;
  10. } NODE;
  11.  
  12. NODE* insertElement(NODE*, int);
  13. NODE* new_node(int );
  14. void writeInorder(NODE* );
  15. void writePreorder(NODE* );
  16. void printAll(NODE* );
  17. int heightoftree(NODE* );
  18. void printGivenLevel(NODE* ,int );
  19. int isBalanced(NODE* );
  20. void delete_tree(NODE* );
  21.  
  22. int main()
  23. {
  24. NODE *root = 0;
  25. FILE *dat;
  26. char c;
  27.  
  28. if (dat = fopen("tekst.txt", "r"))
  29. {
  30. while((c = fgetc(dat))!=EOF)
  31. {
  32. c = toupper(c);
  33. if (c >= 'A' && c<='Z')
  34. root = insertElement(root, c);
  35. }
  36. fclose(dat);
  37. printf("Inorder: ");
  38. writeInorder(root);
  39. printf("\nPreorder: ");
  40. writePreorder(root);
  41. printf("\nLevel: ");
  42. printAll(root);
  43. if (isBalanced(root) == 1)
  44. printf("\nStablo je balansirano.");
  45. else printf("\nStablo nije balansirano.");
  46. }
  47.  
  48. return 0;
  49. }
  50. NODE* new_node(int info)
  51. {
  52. NODE* novi=(NODE*)malloc(sizeof(NODE));
  53. novi->left = novi->right = 0;
  54. novi->info = info;
  55. return novi;
  56. }
  57.  
  58. NODE* insertElement(NODE* root, int info)
  59. {
  60. if (root == 0)
  61. return new_node(info);
  62. if(info<root->info)
  63. root->left = insertElement(root->left, info);
  64. else if(info>root->info)
  65. root->right = insertElement(root->right, info);
  66. return root;
  67. }
  68.  
  69. void writePreorder(NODE* root)
  70. {
  71. if (root != 0)
  72. {
  73. printf("%c", root->info);
  74. writePreorder(root->left);
  75. writePreorder(root->right);
  76. }
  77. }
  78.  
  79. void writeInorder(NODE* root)
  80. {
  81. if (root != 0)
  82. {
  83. writeInorder(root->left);
  84. printf("%c", root->info);
  85. writeInorder(root->right);
  86. }
  87. }
  88.  
  89. int visina(NODE*root)
  90. {
  91. int max;
  92. if(root!=0)
  93. {
  94. int lijevi = visina(root->left);
  95. int desni = visina(root->right);
  96. if (lijevi > desni)
  97. {
  98. max = lijevi + 1;
  99. return max;
  100. }
  101. else
  102. {
  103. max = desni + 1;
  104. return max;
  105. }
  106. }
  107. }
  108.  
  109. void printGivenLevel(NODE* root, int level)
  110. {
  111. if(root == 0)
  112. return;
  113. if(level == 1)
  114. printf("%c", root->info);
  115. else if (level > 1)
  116. {
  117. printGivenLevel(root->left, level-1);
  118. printGivenLevel(root->right, level-1);
  119. }
  120. }
  121.  
  122. void printAll(NODE* root)
  123. {
  124. int h = visina(root);
  125. int i;
  126. for (i=1; i<=h; i++)
  127. printGivenLevel(root, i);
  128. }
  129.  
  130. int isBalanced(NODE* root)
  131. {
  132. int lijevi, desni;
  133.  
  134. if (root == NULL)
  135. return 1;
  136. lijevi = visina(root->left);
  137. desni = visina(root->right);
  138.  
  139. if (fabs(desni-lijevi) <= 1 && isBalanced(root->left) && isBalanced(root->right))
  140. return 1;
  141. return 0;
  142.  
  143. }
  144.  
  145. void delete_tree(NODE *root)
  146. {
  147. if (root != 0)
  148. {
  149. delete_tree(root->left);
  150. delete_tree(root->right);
  151. free(root);
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement