Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. struct node {
  7. int key;
  8. struct node* left;
  9. struct node* right;
  10. };
  11.  
  12. struct node* createnode(int n){
  13. struct node* newnode = (struct node*)malloc(sizeof(struct node));
  14. newnode->key = n;
  15. newnode->left = NULL;
  16. newnode->right = NULL;
  17. return newnode;
  18. }
  19.  
  20. struct node* add(struct node* node, int n) {
  21. if (node == NULL) return;
  22.  
  23. if (n < node->key) {
  24. if (!node->left) node->left = createnode(n);
  25. else add(node->left, n);
  26. }
  27. if (n > node->key) {
  28. if (!node->right) node->right = createnode(n);
  29. else add(node->right, n);
  30. }
  31. }
  32.  
  33.  
  34. int height(struct node* node) {
  35. if (node == NULL) return 0;
  36. else {
  37. int lheight = height(node->left);
  38. int rheight = height(node->right);
  39. if (lheight > rheight) return(lheight + 1);
  40. else return(rheight + 1);
  41. }
  42. }
  43. int size(struct node* node) {
  44. if (!node) return 0;
  45. return size(node->left) + size(node->right) + 1;
  46. }
  47.  
  48. int count(struct node* node) {
  49. if (!node) return 0;
  50. if ((node->left && !node->right) || (!node->left && node->right)) return count(node->left) + count(node->right) + 1;
  51. return count(node->left) + count(node->right);
  52. }
  53.  
  54.  
  55. int LeafCount(struct node* node) {
  56. if (node == NULL) return 0;
  57. if (node->left == NULL && node->right == NULL) return 1;
  58. else return LeafCount(node->left) + LeafCount(node->right);
  59. }
  60.  
  61. void printTree(struct node* node, int odstep, int poziom) {
  62. if (node == NULL) return;
  63. printTree(node->right, odstep, poziom + 1);
  64. printf("\n");
  65. for (int i = 0; i < poziom * odstep; i++)
  66. printf(" ");
  67. printf("%d\n", node->key);
  68. printTree(node->left, odstep, poziom + 1);
  69.  
  70. }
  71. void deltree(struct node* node) {
  72. if (node != NULL) {
  73. deltree(node->left);
  74. deltree(node->right);
  75. free(node);
  76. }
  77. }
  78.  
  79.  
  80. int main() {
  81.  
  82. struct node* root = NULL;
  83.  
  84. int k;
  85.  
  86. FILE* plik = fopen("test.txt", "r");
  87. if (plik == NULL)
  88. {
  89. printf("File does not exist");
  90. exit(1);
  91. }
  92.  
  93. int counter = 0;
  94.  
  95. while (!feof(plik)) {
  96. if (fscanf(plik, "%d", &k)) {
  97. if (root == NULL) root = createnode(k);
  98. else add(root, k);
  99. counter++;
  100. }
  101. else {
  102. char c;
  103. fscanf(plik, "%c", &c);
  104. }
  105. }
  106.  
  107. if (counter >= 1000) {
  108. printf("Liczba liczb pobierana z pliku przekroczyla zakres n<=1000");
  109. deltree(root);
  110. root = NULL;
  111. exit(1);
  112. }
  113.  
  114. fclose(plik);
  115.  
  116. int a = 0;
  117. while (a != 7) {
  118. printf("\n\n");
  119. printf("Nacisnij:\n");
  120. printf("1: Jezeli chcesz wyswietlic drzewo z pliku.\n");
  121. printf("2: Jezeli chcesz wyswietlic wysokosc drzewa.\n");
  122. printf("3: Jezeli chcesz wyswietlic ilosc lisci drzewa.\n");
  123. printf("4: Jezeli chcesz wyswietlic ilosc wezlow z jednym potomkiem.\n");
  124. printf("5: Jezeli chcesz wyswietlic wielkosc drzewa.\n");
  125. printf("6: Jezeli chcesz usunac drzewo.\n");
  126. printf("7: Jezeli chcesz zamknac program.\n");
  127. scanf("%d", &a);
  128. if (a == 1 || a == 2 || a == 3 || a == 4 || a == 5 || a == 6 || a == 7) {
  129.  
  130. if (a == 1) {
  131. system("cls");
  132. printTree(root, 5, 2);
  133. }
  134. if (a == 2) {
  135. system("cls");
  136. printf("Wysokosc drzewa: %d\n", height(root));
  137. }
  138. if (a == 3) {
  139. system("cls");
  140. printf("Ilosc lisci w drzewie:%d\n", LeafCount(root));
  141. }
  142. if (a == 4) {
  143. system("cls");
  144. printf("Ilosc wezlow z jednym potomkiem:%d\n", count(root));
  145. }
  146. if (a == 5) {
  147. system("cls");
  148. printf("Wielkosc drzewa:%d\n", size(root));
  149. }
  150. if (a == 6) {
  151. system("cls");
  152. printf("Drzewo usuniete\n");
  153. deltree(root);
  154. root = NULL;
  155. }
  156. }
  157.  
  158. else {
  159. system("cls");
  160. printf("Wybrana zostala zla cyfra:");
  161. }
  162. }
  163.  
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement