File: Stare vježbe/vjezbe14/xx__binarno_stablo.c

  1. /*
  2.   xx__binarno_stablo.c
  3.   -----
  4.   Primjer implementacije binarnog stabla.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11.  
  12. struct cvor {
  13. char slovo;
  14. int count; /* broj pojavljivanja */
  15. struct cvor *left; /* left child */
  16. struct cvor *right; /* right child */
  17. };
  18.  
  19. struct cvor *addToTree(struct cvor *, char);
  20. void preorder(struct cvor *);
  21. void postorder(struct cvor *);
  22. void inorder(struct cvor *);
  23. void izbrisistablo(struct cvor*);
  24. void poNivoima(struct cvor*);
  25. void nivo(struct cvor*, int, int);
  26. int visina(struct cvor*);
  27.  
  28. int main() {
  29. struct cvor *root = NULL;
  30. char recenica[256];
  31. int i=0;
  32.  
  33. printf("Unesite neku recenicu:\n");
  34. scanf("%[^\n]%*c", recenica);
  35.  
  36. while (recenica[i]!='\0') {
  37. while (recenica[i]==' ' || recenica[i]=='\t' || recenica[i]==',' ||
  38. recenica[i]==':')
  39. i++;
  40. root = addToTree(root, tolower(recenica[i++]));
  41. }
  42.  
  43. printf("Inorder:\n");
  44. inorder(root);
  45. scanf("%*c"); /* cekamo da se stisne enter */
  46. printf("Preorder:\n");
  47. preorder(root);
  48. scanf("%*c"); /* cekamo da se stisne enter */
  49. printf("Postorder:\n");
  50. postorder(root);
  51. scanf("%*c"); /* cekamo da se stisne enter */
  52. printf("Ispis po nivoima:\n");
  53. poNivoima(root);
  54.  
  55. izbrisistablo(root);
  56.  
  57. return 0;
  58. }
  59.  
  60. /* Funkcija addTotree dodaje slovo c u stablo na ili ispod cvora p.
  61.   Ukoliko vec postoji, povecava se broj pojavljivanja. */
  62. struct cvor *addToTree(struct cvor *p, char c) {
  63. if (p == NULL) {
  64. p = (struct cvor*) malloc(sizeof(struct cvor));
  65. p->slovo = c;
  66. p->count = 1;
  67. p->left = p->right = NULL;
  68. }
  69. else if (c == p->slovo)
  70. p->count++;
  71. /* ako je slovo manje od onog u trenutnom cvoru stavi ga u
  72.   lijevo podstablo */
  73. else if (c < p->slovo)
  74. p->left = addToTree(p->left, c);
  75. /* ako je slovo vece od onog u trenutnom cvoru stavi ga u
  76.   desno podstablo */
  77. else
  78. p->right = addToTree(p->right, c);
  79.  
  80. return p;
  81. }
  82.  
  83. void inorder(struct cvor *p) {
  84. if (p != NULL) {
  85. inorder(p->left);
  86. printf("%c %4d\n", p->slovo, p->count);
  87. inorder(p->right);
  88. }
  89. }
  90.  
  91. void postorder(struct cvor *p) {
  92. if (p != NULL) {
  93. postorder(p->left);
  94. postorder(p->right);
  95. printf("%c %4d\n", p->slovo, p->count);
  96. }
  97. }
  98.  
  99. void preorder(struct cvor *p) {
  100. if (p != NULL) {
  101. printf("%c %4d\n", p->slovo, p->count);
  102. preorder(p->left);
  103. preorder(p->right);
  104. }
  105. }
  106.  
  107. void izbrisistablo(struct cvor *p) {
  108. if (p != NULL) {
  109. izbrisistablo(p->left);
  110. izbrisistablo(p->right);
  111. free(p);
  112. }
  113. }
  114.  
  115. int visina(struct cvor *p) {
  116. int l,d;
  117. if (p==NULL) return -1;
  118. else {
  119. l=visina(p->left);
  120. d=visina(p->right);
  121. return l>d ? l+1 : d+1;
  122. }
  123. }
  124.  
  125. /* Funkcija nivo ispisuje oznake svih cvorova na i-tom nivou zadanog stabla) */
  126. void nivo(struct cvor *p, int i, int j) {
  127. if (p!=NULL) {
  128. if (i==j) printf("%c ", p->slovo);
  129. else {
  130. nivo(p->left, i, j+1);
  131. nivo(p->right, i, j+1);
  132. }
  133. }
  134. }
  135.  
  136. void poNivoima(struct cvor* p) {
  137. int i, v = visina(p);
  138. printf("Visina stabla -> %d\n", v);
  139. for (i=0; i<=v; i++) {
  140. printf("Nivo %d: ", i);
  141. nivo(p,i,0);
  142. printf("\n");
  143. }
  144. }
  145.