Advertisement
Guest User

malo domacega, ae

a guest
Nov 27th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /*
  4. IMPLEMENTACIJA ATP "BINARNO STABLO"
  5. IZRADIO: TOMISLAV BUKIC, 2013/11
  6. */
  7. #include <stdlib.h>
  8.  
  9. #define LAMBDA NULL
  10.  
  11. typedef char labeltype; /*odreduje sa kojim podacima struktura barata*/
  12.  
  13. typedef struct celltag { /*struktura binarno stablo, sa podatkom label*/
  14.     labeltype label;
  15.     struct celltag *leftchild, *rightchild;
  16.     } celltype;
  17.  
  18. typedef celltype *node; /*pokazivac na cvor*/
  19. typedef celltype *BinaryTree; /*pokazivac na binarno stablo*/
  20.  
  21. void BiMakeNull (BinaryTree *Tp) { /*pretvara *Tp u prazno stablo*/
  22.     *Tp=LAMBDA;
  23.     }
  24.  
  25. int BiEmpty (BinaryTree T) { /*ako je T prazno vraæa 1, inace 0: */
  26.     if (T) return 0;
  27.     return 1;
  28.     }
  29.  
  30. void BiCreate (labeltype l, BinaryTree TL, BinaryTree TR, BinaryTree *Tp) { /*stvara stablo *T sa djecom TL i TR te oznakom l: */
  31.     *Tp=(celltype*)malloc(sizeof(celltype));
  32.     if ((*Tp)==LAMBDA) {
  33.         printf ("greska u alokaciji, BiCreate");
  34.         exit(-1);
  35.         }
  36.     (*Tp)->label=l;
  37.     (*Tp)->leftchild=TL;
  38.     (*Tp)->rightchild=TR;
  39.     }
  40.  
  41. void BiLeftSubtree (BinaryTree T, BinaryTree *Tlp) { /*vraca lijevo podstablo stabla T: */
  42.     if (T->leftchild)
  43.         *Tlp=T->leftchild;
  44.     }
  45.  
  46. void BiRightSubtree (BinaryTree T, BinaryTree *Trp) { /*vraca desno podstablo stabla T: */
  47.     if (T->rightchild)
  48.         *Trp=T->rightchild;
  49.     }
  50.  
  51. node BiInsertLeftChild (labeltype l, node i, BinaryTree *Tp) { /*stablu T dodaje lijevo dijete sa ozakom l i oba pokazivaca LAMBDA*/
  52.     node a;
  53.     printf ("ubacujem lijevo\n");
  54.     BiMakeNull(&a);
  55.     printf ("ul-1\n");
  56.     a=(node)malloc(sizeof(node));
  57.     if((a)==LAMBDA){
  58.         printf ("greska u alokaciji, BiInsertLeftChild");
  59.         exit(-1);
  60.         }
  61.     printf ("ul-2\n");
  62.     i->leftchild=a;
  63.     printf ("ul-3\n");
  64.     a->label=l;
  65.     printf ("ul-4\n");
  66.     a->leftchild=LAMBDA;
  67.     printf ("ul-5\n");
  68.     a->rightchild=LAMBDA;
  69.     printf ("ul-nazad\n");
  70.     return a;
  71.     }
  72.  
  73. node BiInsertRightChild (labeltype l, node i, BinaryTree *Tp) { /*stablu T dodaje desno dijete sa ozakom l i oba pokazivaca LAMBDA*/
  74.     node a;
  75.     printf ("ubacujem desno\n");
  76.     BiMakeNull(&a);
  77.     printf ("ud-1\n");
  78.     a=(node)malloc(sizeof(node));
  79.     if((a)==LAMBDA){
  80.         printf ("greska u alokaciji, BiInsertRightChild");
  81.         exit(-1);
  82.         }
  83.     printf ("ud-2\n");
  84.     i->rightchild=a;
  85.     printf ("ud-3\n");
  86.     a->label=l;
  87.     printf ("ud-4\n");
  88.     a->leftchild=LAMBDA;
  89.     printf ("ud-5\n");
  90.     a->rightchild=LAMBDA;
  91.     printf ("ud-nazad\n");
  92.     return a;
  93.     }
  94.  
  95. node BiRoot (BinaryTree T) { /*vraca korijen od T*/
  96.     return T;
  97.     }
  98.  
  99. node BiLeftChild (node i, BinaryTree T) { /*vraca lijevo dijete cvora i*/
  100.     return i->leftchild;
  101.     }
  102.  
  103. node BiRightChild (node i, BinaryTree T) { /*vraca desno dijete cvora i*/
  104.     return i->rightchild;
  105.     }
  106.  
  107. node BiParent (node i, BinaryTree T) { /*vraca roditelja cvora i u stablu T*/
  108.     node a;
  109.     BiMakeNull(&a);
  110.     if (i==T) return LAMBDA;
  111.     if ((i==T->rightchild)||(i==T->leftchild)) return T;
  112.     if (T->leftchild!=LAMBDA)
  113.         if ((a=BiParent (i, T->leftchild))!=LAMBDA) return a;
  114.     if (T->rightchild!=LAMBDA)
  115.         if ((a=BiParent (i, T->rightchild))!=LAMBDA) return a;
  116.     return LAMBDA;
  117.     }
  118.  
  119.  
  120. void BiDelete (node i, BinaryTree *Tp) { /*brise cvor iz stabla*/
  121.     node a;
  122.     a=BiParent (i, *Tp);
  123.     if (a->rightchild==i)
  124.         a->rightchild=LAMBDA;
  125.     else
  126.         a->leftchild=LAMBDA;
  127.     free(i);
  128.     }
  129.  
  130. labeltype BiLabel (node i, BinaryTree T) { /*vraca oznaku  cvora i stabla T) */
  131.     return i->label;
  132.     }
  133.  
  134. void BiChangeLabel (labeltype l, node i, BinaryTree *Tp) { /* mijenja oznaku l cvora i stabla *Tp  */
  135.     i->label=l;
  136.     }
  137.  
  138.  
  139.  
  140. /*
  141. PreOrder, InOrder i PostOrder obilasci binarnoga stabla:
  142.     -u programu je potrebno definirati sto funkcija_Xorder radi
  143.     pri obilasku binarnoga stabla
  144. */
  145.  
  146. void funkcija_Xorder (node N);
  147.  
  148. void BiPreOrder (BinaryTree T) {
  149.     funkcija_Xorder (T);
  150.     if (BiLeftChild(T,T)!=LAMBDA)
  151.         BiPreOrder (BiLeftChild(T,T));
  152.     if (BiRightChild(T,T)!=LAMBDA)
  153.         BiPreOrder (BiRightChild(T,T));
  154.     return;
  155.     }
  156.  
  157. void BiInOrder (BinaryTree T) {
  158.     if (BiLeftChild(T,T)!=LAMBDA)
  159.         BiInOrder (BiLeftChild(T,T));
  160.     funkcija_Xorder (T);
  161.     if (BiRightChild(T,T)!=LAMBDA)
  162.         BiInOrder (BiRightChild(T,T));
  163.     return;
  164.     }
  165.  
  166. void BiPostOrder (BinaryTree T) {
  167.     if (BiLeftChild(T,T)!=LAMBDA)
  168.         BiPostOrder (BiLeftChild(T,T));
  169.     if (BiRightChild(T,T)!=LAMBDA)
  170.         BiPostOrder (BiRightChild(T,T));
  171.     funkcija_Xorder (T);
  172.     return;
  173.     }
  174.  
  175. /*
  176. Zadatak:
  177. Implementirajte a.t.p. BinaryTree koji pomocu pointera i napisite potprogram koji kreira binarno stablo
  178. iz prefix oblika logickog izraza. Za kontrolu napravite i preorder obilazak dobivenog stabla.
  179.  
  180. ULAZNI PODACI: string koji sadrzi prefix oblik izraza
  181. IZLAZNI PODACI: binarno stablo koje odgovara unesenom izrazu: ako stablo ima n cvorova onda treba ispisati n
  182. redova, svaki red je oblika cvor, lijevo dijete, desno dijete (ako nekog djeteta nema, ispisite NULL). Na
  183. kraju jos ispisite PREORDER obilazak dobivenog stabla.
  184. Na primjer, za ulazne podatke:
  185. |A&B|^CED
  186. treba ispisati:
  187. | A &
  188. A NULL NULL
  189. & B |
  190. B NULL NULL
  191. | ^ D
  192. C NULL NULL
  193. E NULL NULL
  194. D NULL NULL
  195. |A&B|^CED
  196. */
  197.  
  198. /*
  199. RJESENJE:
  200. */
  201.  
  202. void funkcija_Xorder (node n) {
  203.     printf ("%c", BiLabel(n,n));
  204.     return;
  205.     }
  206.  
  207. int bOperator (labeltype a) { /*vraća 1 ako je a=&,|,^*/
  208.     if ((a=='|')||(a=='^')||(a=='&'))
  209.         return 1;
  210.     return 0;
  211.     }
  212.  
  213. void Unos (node P, BinaryTree T) {/*stvara stablo*/
  214.     labeltype a;
  215.     a=getchar();
  216.     printf ("\n a=%c\n",a);
  217.     if (bOperator(a)) { /*stvara cvor ako je unesen neki od operatora*/
  218.         printf ("operator:\n");
  219.         if (BiLeftChild(P,T)==LAMBDA) { /*ako je lijevo podstablo 'slobodno' koristi ga (jer je binarni operator)*/
  220.             printf ("lijevi\n");
  221.             BiInsertLeftChild(a, P, &T);
  222.             printf ("unio a=%c u P= %p\n",a,P);
  223.             Unos(BiLeftChild (P,T),T);
  224.             }
  225.         else { /*ako je lijevo podstablo vec iskoristeno ide u desno*/
  226.             printf ("desni\n");
  227.             BiInsertRightChild(a, P, &T);
  228.             printf ("unio a= %c u P= %p \n",a,P);
  229.             Unos(BiRightChild (P,T),T);
  230.             }
  231.         }
  232.     else { /*ako znak koji je na redu nije operator*/
  233.         printf ("nije operator:\n");
  234.         if (BiLeftChild(P,T)==LAMBDA) { /*ako je lijevo podstablo slobodno smijesta ga lijevo*/
  235.             printf ("lijevi\n");
  236.             BiInsertLeftChild(a, P, &T);
  237.             printf ("unio a= %c u P =%p \n",a,P);
  238.             Unos (P,T); /*iduci ce znak biti dijete istog cvora, ali ce ici desno*/
  239.             }
  240.         else { /*ako je lijevo podstablo zauzeto operator ce biti smjesten desno*/
  241.             printf ("desni\n");
  242.             BiInsertRightChild(a, P, &T);
  243.             printf ("unio a= %c u P= %p \n",a,P);
  244.             do { /*ide unazad i trazi prvi cvor koji nema desno dijete*/
  245.                 printf("P = %p\n", P);
  246.                 P=BiParent(P,T);
  247.                 printf("P = %p\n", P);
  248.                 if (BiRightChild(P,T)==LAMBDA)
  249.                     break;
  250.                 } while (P!=BiRoot(T));
  251.             printf("P = %p\n", P);
  252.             if ((BiRoot(T)==P)&&((BiRightChild(P,T))!=LAMBDA)) return;
  253.             Unos (P,T);
  254.             }
  255.         }
  256.     return;
  257.     }
  258.  
  259. BinaryTree Unesi (BinaryTree T) { /*stvara korijen stabla, ako je stablo samo korijen*/
  260.     labeltype a;
  261.     node P;
  262.     a=getchar();
  263.     BiCreate (a, LAMBDA, LAMBDA, &T);
  264.     if (bOperator(a)) {
  265.         P=BiRoot(T);
  266.         Unos (P,T);
  267.         }
  268.     return T;
  269.     }
  270.  
  271. void Ispisi (BinaryTree T) {
  272.     printf ("%c ", BiLabel(T,T));
  273.     if (BiLeftChild(T,T)==LAMBDA)
  274.         printf ("NULL ");
  275.     else
  276.         printf ("%c ", BiLabel (BiLeftChild(T,T),T));
  277.     if (BiRightChild(T,T)==LAMBDA)
  278.         printf ("NULL\n");
  279.     else
  280.         printf ("%c\n", BiLabel (BiRightChild(T,T),T));
  281.     if (BiLeftChild(T,T)!=LAMBDA)
  282.         Ispisi(BiLeftChild(T,T));
  283.     if (BiRightChild(T,T)!=LAMBDA)
  284.         Ispisi (BiRightChild(T,T));
  285.     return;
  286.     }
  287.  
  288. void Ispis (BinaryTree T) {
  289.     if (BiLabel(T,T)=='\n')
  290.         printf ("NULL");
  291.     else
  292.         Ispisi (T);
  293.     }
  294.  
  295. int main () {
  296.     BinaryTree T;
  297.     BiMakeNull(&T);
  298.     T=Unesi (T);
  299.     Ispis (T);
  300.     BiPreOrder(T);
  301.     return 0;
  302.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement