Advertisement
Guest User

Untitled

a guest
Nov 27th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.82 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAXLENGTH 1023
  5. #define LAMBDA -1
  6. #define PRAZNO -1
  7.  
  8. typedef char labeltype;
  9. typedef int node;
  10.  
  11. //implementacija
  12.  
  13. typedef struct {
  14.     node last;
  15.     labeltype labels[MAXLENGTH];
  16. } BTREE;
  17.  
  18. void MAKE_NULL(BTREE *);
  19. int EMPTY(BTREE);
  20. //void kopiraj_stablo(BTREE, BTREE *, node, node);
  21. node CREATE(labeltype, BTREE, BTREE, BTREE *);
  22. void LEFT_SUBTREE(BTREE, BTREE *);
  23. void RIGHT_SUBTREE(BTREE, BTREE *);
  24. node INSERT_LEFT_CHILD(labeltype, node, BTREE *);
  25. node INSERT_RIGHT_CHILD(labeltype, node, BTREE *);
  26. void DELETE(node, BTREE *);
  27. node ROOT(BTREE);
  28. node LEFT_CHILD(node, BTREE);
  29. node RIGHT_CHILD(node, BTREE);
  30. node PARENT(node, BTREE);
  31. labeltype LABEL(node, BTREE);
  32. void CHANGE_LABEL(labeltype, node, BTREE *);
  33.  
  34. void MAKE_NULL(BTREE *T) {
  35.   int i;
  36.   for (i = 0; i < MAXLENGTH; ++i)
  37.     T->labels[i] = PRAZNO;
  38. }
  39.  
  40. int EMPTY(BTREE T) {
  41.   return T.labels[0] == PRAZNO;
  42. }
  43.  
  44. /*void kopiraj_stablo(BTREE T1, BTREE *T2, node i, node j) {
  45.   MAKE_NULL(T2);
  46.   if (i < 0 || i >= MAXLENGTH || T1.labels[i] == PRAZNO)
  47.     return;
  48.   if (j < 0 || j >= MAXLENGTH)
  49.     return;
  50.   T2->labels[j] = T1.labels[i];
  51.   kopiraj_stablo(T1, T2, 2 * i + 1, 2 * j + 1);
  52.   kopiraj_stablo(T1, T2, 2 * i + 2, 2 * j + 2);
  53. }
  54.  
  55. node CREATE(labeltype l, BTREE TL, BTREE TR, BTREE *T) {
  56.   T->labels[0] = l;
  57.   kopiraj_stablo(TL, T, 0, 1);
  58.   kopiraj_stablo(TR, T, 0, 2);
  59.   return 0;
  60. }*/
  61.  
  62. //
  63.  
  64. void fun(node i, BTREE *T, node j, BTREE *T1) {
  65.     if (j >= MAXLENGTH || i >= MAXLENGTH || 2*i>=MAXLENGTH || 2*j>=MAXLENGTH) return;
  66.     T->labels[i] = T1->labels[j];
  67.     fun(2*i + 1, T, 2*j + 1, T1);
  68.     fun(2*i + 2, T, 2*j + 2, T1);
  69. }
  70.  
  71. node CREATE(labeltype l, BTREE TL, BTREE TR, BTREE *T) {
  72.     T->labels[0]=l;
  73.     fun(1, T, 0, &TL);
  74.     fun(2, T, 0, &TR);
  75.     return 0;
  76. }
  77.  
  78. //
  79.  
  80. void LEFT_SUBTREE(BTREE T, BTREE *TL) {
  81.   //kopiraj_stablo(T, TL, 1, 0);
  82.     fun(0, TL, 1, &T);
  83. }
  84.  
  85. void RIGHT_SUBTREE(BTREE T, BTREE *TR) {
  86.   //kopiraj_stablo(T, TR, 2, 0);
  87.     fun(0, TR, 2, &T);
  88. }
  89.  
  90. node INSERT_LEFT_CHILD(labeltype l, node i, BTREE *T) {
  91.   if (i < 0 || i >= MAXLENGTH || T->labels[i] == PRAZNO) {
  92.     printf("INSERT_LEFT_CHILD: Ne postoji taj cvor.");
  93.     exit(1);
  94.   }
  95.   if (2 * i + 1 >= MAXLENGTH) {
  96.     printf("INSERT_LEFT_CHILD: Ne mogu dodati lijevo dijete.");
  97.     exit(1);
  98.   }
  99.   T->labels[2 * i + 1] = l;
  100.   return 2 * i + 1;
  101. }
  102.  
  103. node INSERT_RIGHT_CHILD(labeltype l, node i, BTREE *T) {
  104.   if (i < 0 || i >= MAXLENGTH || T->labels[i] == PRAZNO) {
  105.     printf("INSERT_RIGHT_CHILD: Ne postoji taj cvor.");
  106.     exit(1);
  107.   }
  108.   if (2 * i + 2 >= MAXLENGTH) {
  109.     printf("INSERT_RIGHT_CHILD: Ne mogu dodati desno dijete.");
  110.     exit(1);
  111.   }
  112.   T->labels[2 * i + 2] = l;
  113.   return 2 * i + 2;
  114. }
  115.  
  116. void DELETE(node i, BTREE *T) {
  117.   if (i < 0 || i >= MAXLENGTH || T->labels[i] == PRAZNO) {
  118.     printf("DELETE: Ne postoji taj cvor.");
  119.     exit(1);
  120.   }
  121.   if (2 * i + 1 < MAXLENGTH && T->labels[2 * i + 1] != PRAZNO) {
  122.     printf("DELETE: Cvor ima dijete.");
  123.     exit(1);
  124.   }
  125.   if (2 * i + 2 < MAXLENGTH && T->labels[2 * i + 2] != PRAZNO) {
  126.     printf("DELETE: Cvor ima dijete.");
  127.     exit(1);
  128.   }
  129.   T->labels[i] = PRAZNO;
  130. }
  131.  
  132. node ROOT(BTREE T) {
  133.   if (T.labels[0] == PRAZNO)
  134.     return LAMBDA;
  135.   return 0;
  136. }
  137.  
  138. node LEFT_CHILD(node i, BTREE T) {
  139.   if (i < 0 || i >= MAXLENGTH || T.labels[i] == PRAZNO) {
  140.     printf("LEFT_CHILD: Ne postoji taj cvor.");
  141.     exit(1);
  142.   }
  143.   if (2 * i + 1 >= MAXLENGTH || T.labels[2 * i + 1] == PRAZNO)
  144.     return LAMBDA;
  145.   return 2 * i + 1;
  146. }
  147.  
  148. node RIGHT_CHILD(node i, BTREE T) {
  149.   if (i < 0 || i >= MAXLENGTH || T.labels[i] == PRAZNO) {
  150.     printf("RIGHT_CHILD: Ne postoji taj cvor.");
  151.     exit(1);
  152.   }
  153.   if (2 * i + 2 >= MAXLENGTH || T.labels[2 * i + 2] == PRAZNO)
  154.     return LAMBDA;
  155.   return 2 * i + 2;
  156. }
  157.  
  158. node PARENT(node i, BTREE T) {
  159.   if (i < 0 || i >= MAXLENGTH || T.labels[i] == PRAZNO) {
  160.     printf("PARENT: Ne postoji taj cvor.");
  161.     exit(1);
  162.   }
  163.   if (i == 0)
  164.     return LAMBDA;
  165.   return (i - 1) / 2;
  166. }
  167.  
  168. labeltype LABEL(node i, BTREE T) {
  169.   if (i < 0 || i >= MAXLENGTH || T.labels[i] == PRAZNO) {
  170.     printf("LABEL: Ne postoji taj cvor.");
  171.     exit(1);
  172.   }
  173.   return T.labels[i];
  174. }
  175.  
  176. void CHANGE_LABEL(labeltype l, node i, BTREE *T) {
  177.   if (i < 0 || i >= MAXLENGTH || T->labels[i] == PRAZNO) {
  178.     printf("CHANGE_LABEL: Ne postoji taj cvor.");
  179.     exit(1);
  180.   }
  181.   T->labels[i] = l;
  182. }
  183.  
  184.  
  185. //kraj implementacije
  186.  
  187. char upis[100];
  188. int j=0,tst=0;
  189.  
  190. void kreiraj (BTREE *B, int n) {
  191.     printf("\n u funkciji %d. put\n", tst);
  192.     tst++;
  193.     BTREE TL, TR;
  194.     MAKE_NULL(&TL);
  195.     MAKE_NULL(&TR);
  196.     labeltype k;
  197.     if(j>=n)
  198.         return;
  199.     k=upis[j];
  200.     if(k=='N')
  201.     {
  202.         j=j+4;
  203.         return;
  204.     }
  205.     else
  206.         j++;
  207.     kreiraj(&TL, n);
  208.     kreiraj(&TR, n);
  209.     CREATE(k, TL, TR, B);
  210. }
  211.  
  212. int broj_listova (node n, BTREE B) {
  213.     int l=0;
  214.     if(n==NULL) return 0;
  215.     if(LEFT_CHILD(n, B)==LAMBDA && RIGHT_CHILD(n, B)==LAMBDA)
  216.         l=1;
  217.     return (l + broj_listova(LEFT_CHILD(n, B), B) + broj_listova(RIGHT_CHILD(n, B), B));
  218. }
  219.  
  220. void ispis_listova (node n, BTREE B) {
  221.     if(n==NULL) return;
  222.     if(LEFT_CHILD(n, B)==LAMBDA && RIGHT_CHILD(n, B)==LAMBDA)
  223.         printf("%s ", LABEL(n, B));
  224.     ispis_listova(LEFT_CHILD(n, B), B);
  225.     ispis_listova(RIGHT_CHILD(n, B), B);
  226. }
  227.  
  228. int main(void) {
  229.     BTREE B;
  230.     int i=0,br=0;
  231.     MAKE_NULL(&B);
  232.     gets(upis);
  233.     while(upis[i]!='\0')
  234. {
  235.     if(upis[i]=='N')
  236.     {
  237.         br++;
  238.         i=i+4;
  239.     }
  240.     else
  241.     {
  242.     i++;
  243.     br++;
  244.     }
  245. }
  246.     printf("\n i:%d br: %d upis od 2:%c", i,br, upis[2]);
  247.     kreiraj(&B,br);
  248.     printf("\n funkcija kreiraj gotova");
  249.     printf("\n oznaka drugog čvora %c", LABEL(2,B));
  250.     printf("\n %d: ", broj_listova(ROOT(B),B));
  251.     ispis_listova(ROOT(B),B);
  252.     return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement