Advertisement
Guest User

BinaryTree ZADACA

a guest
Nov 27th, 2013
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.05 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.  
  32.     *Tp=(celltype*)malloc(sizeof(celltype));
  33.     (*Tp)->label=l;
  34.     (*Tp)->leftchild=TL;
  35.     (*Tp)->rightchild=TR;
  36.     }
  37.  
  38. void BiLeftSubtree (BinaryTree T, BinaryTree *Tlp) { /*vraca lijevo podstablo stabla T: */
  39.     if (T->leftchild)
  40.         *Tlp=T->leftchild;
  41.     }
  42.  
  43. void BiRightSubtree (BinaryTree T, BinaryTree *Trp) { /*vraca desno podstablo stabla T: */
  44.     if (T->rightchild)
  45.         *Trp=T->rightchild;
  46.     }
  47.  
  48. node BiInsertLeftChild (labeltype l, node i, BinaryTree *Tp) { /*stablu T dodaje lijevo dijete sa ozakom l i oba pokazivaca LAMBDA*/
  49.     node a;
  50.     a=(node)malloc(sizeof(node));
  51.     i->leftchild=a;
  52.     a->label=l;
  53.     a->leftchild=LAMBDA;
  54.     a->rightchild=LAMBDA;
  55.     return a;
  56.     }
  57.  
  58. node BiInsertRightChild (labeltype l, node i, BinaryTree *Tp) { /*stablu T dodaje desno dijete sa ozakom l i oba pokazivaca LAMBDA*/
  59.     node a;
  60.     a=(node)malloc(sizeof(node));
  61.     i->rightchild=a;
  62.     a->label=l;
  63.     a->leftchild=LAMBDA;
  64.     a->rightchild=LAMBDA;
  65.     return a;
  66.     }
  67.  
  68. node BiRoot (BinaryTree T) { /*vraca korijen od T*/
  69.     return T;
  70.     }
  71.  
  72. node BiLeftChild (node i, BinaryTree T) { /*vraca lijevo dijete cvora i*/
  73.     return i->leftchild;
  74.     }
  75.  
  76. node BiRightChild (node i, BinaryTree T) { /*vraca desno dijete cvora i*/
  77.     return i->rightchild;
  78.     }
  79.  
  80. node BiParent (node i, BinaryTree T) { /*vraca roditelja cvora i u stablu T*/
  81.     if (i==T) return LAMBDA;
  82.     if ((i==T->rightchild)||(i==T->leftchild)) return T;
  83.     node a;
  84.     if (T->leftchild!=LAMBDA)
  85.         if ((a=BiParent (i, T->leftchild))!=LAMBDA) return a;
  86.     if (T->rightchild!=LAMBDA)
  87.         if ((a=BiParent (i, T->rightchild))!=LAMBDA) return a;
  88.     return LAMBDA;
  89.     }
  90.  
  91.  
  92. void BiDelete (node i, BinaryTree *Tp) { /*brise cvor iz stabla*/
  93.     node a=BiParent (i, *Tp);
  94.     if (a->rightchild==i)
  95.         a->rightchild=LAMBDA;
  96.     else
  97.         a->leftchild=LAMBDA;
  98.     free(i);
  99.     }
  100.  
  101. labeltype BiLabel (node i, BinaryTree T) { /*vraca oznaku  cvora i stabla T) */
  102.     return i->label;
  103.     }
  104.  
  105. void BiChangeLabel (labeltype l, node i, BinaryTree *Tp) { /* mijenja oznaku l cvora i stabla *Tp  */
  106.     i->label=l;
  107.     }
  108.  
  109.  
  110.  
  111. /*
  112. PreOrder, InOrder i PostOrder obilasci binarnoga stabla:
  113.     -u programu je potrebno definirati sto funkcija_Xorder radi
  114.     pri obilasku binarnoga stabla
  115. */
  116.  
  117. void funkcija_Xorder (node N);
  118.  
  119. void BiPreOrder (BinaryTree T) {
  120.     funkcija_Xorder (T);
  121.     if (BiLeftChild(T,T)!=LAMBDA)
  122.         BiPreOrder (BiLeftChild(T,T));
  123.     if (BiRightChild(T,T)!=LAMBDA)
  124.         BiPreOrder (BiRightChild(T,T));
  125.     return;
  126.     }
  127.  
  128. void BiInOrder (BinaryTree T) {
  129.     if (BiLeftChild(T,T)!=LAMBDA)
  130.         BiInOrder (BiLeftChild(T,T));
  131.     funkcija_Xorder (T);
  132.     if (BiRightChild(T,T)!=LAMBDA)
  133.         BiInOrder (BiRightChild(T,T));
  134.     return;
  135.     }
  136.  
  137. void BiPostOrder (BinaryTree T) {
  138.     if (BiLeftChild(T,T)!=LAMBDA)
  139.         BiPostOrder (BiLeftChild(T,T));
  140.     if (BiRightChild(T,T)!=LAMBDA)
  141.         BiPostOrder (BiRightChild(T,T));
  142.     funkcija_Xorder (T);
  143.     return;
  144.     }
  145.  
  146. /*
  147. Zadatak:
  148. Implementirajte a.t.p. BinaryTree koji pomocu pointera i napisite potprogram koji kreira binarno stablo
  149. iz prefix oblika logickog izraza. Za kontrolu napravite i preorder obilazak dobivenog stabla.
  150.  
  151. ULAZNI PODACI: string koji sadrzi prefix oblik izraza
  152. IZLAZNI PODACI: binarno stablo koje odgovara unesenom izrazu: ako stablo ima n cvorova onda treba ispisati n
  153. redova, svaki red je oblika cvor, lijevo dijete, desno dijete (ako nekog djeteta nema, ispisite NULL). Na
  154. kraju jos ispisite PREORDER obilazak dobivenog stabla.
  155. Na primjer, za ulazne podatke:
  156. |A&B|^CED
  157. treba ispisati:
  158. | A &
  159. A NULL NULL
  160. & B |
  161. B NULL NULL
  162. | ^ D
  163. ^ C E
  164. C NULL NULL
  165. E NULL NULL
  166. D NULL NULL
  167. |A&B|^CED
  168. */
  169.  
  170. /*
  171. RJESENJE:
  172. */
  173.  
  174. void funkcija_Xorder (node n) {
  175.     printf ("%c", BiLabel(n,n));
  176.     return;
  177.     }
  178.  
  179. int operator (labeltype a) { /*vraća 1 ako je a=&,|,^*/
  180.     if ((a=='|')||(a=='^')||(a=='&'))
  181.         return 1;
  182.     return 0;
  183.     }
  184.  
  185. void Unos (node P, BinaryTree T) {/*stvara stablo*/
  186.     labeltype a=getchar();
  187.     if (operator(a)) { /*stvara cvor ako je unesen neki od operatora*/
  188.         if (BiLeftChild(P,T)==LAMBDA) { /*ako je lijevo podstablo 'slobodno' koristi ga (jer je binarni operator)*/
  189.             BiInsertLeftChild(a, P, &T);
  190.             Unos(BiLeftChild (P,T),T);
  191.             }
  192.         else { /*ako je lijevo podstablo vec iskoristeno ide u desno*/
  193.             BiInsertRightChild(a, P, &T);
  194.             Unos(BiRightChild (P,T),T);
  195.             }
  196.         }
  197.     else { /*ako znak koji je na redu nije operator*/
  198.         if (BiLeftChild(P,T)==LAMBDA) { /*ako je lijevo podstablo slobodno smjesta ga lijevo*/
  199.             BiInsertLeftChild(a, P, &T);
  200.             Unos (P,T); /*iduci ce znak biti dijete istog cvora, ali ce ici desno*/
  201.             }
  202.         else { /*ako je lijevo podstablo zauzeto operator ce biti smjesten desno*/
  203.             BiInsertRightChild(a, P, &T);
  204.             do { /*ide unazad i trazi prvi cvor koji nema desno dijete*/
  205.                 P=BiParent(P,T);
  206.                 } while ((BiRightChild(P,T)!=LAMBDA)&&(P!=BiRoot(T)));
  207.             if ((BiRoot(T)==P)&&((BiRightChild(P,T))!=LAMBDA)) return;
  208.             Unos (P,T);
  209.             }
  210.         }
  211.     return;
  212.     }
  213.  
  214. BinaryTree Unesi (BinaryTree T) { /*stvara korijen stabla, ako je stablo samo korijen*/
  215.     labeltype a=getchar();
  216.     node P;
  217.     BiCreate (a, LAMBDA, LAMBDA, &T);
  218.     if (operator(a)) {
  219.         P=BiRoot(T);
  220.         Unos (P,T);
  221.         }
  222.     return T;
  223.     }
  224.  
  225. void Ispisi (BinaryTree T) {
  226.     printf ("%c ", BiLabel(T,T));
  227.     if (BiLeftChild(T,T)==LAMBDA)
  228.         printf ("NULL ");
  229.     else
  230.         printf ("%c ", BiLabel (BiLeftChild(T,T),T));
  231.     if (BiRightChild(T,T)==LAMBDA)
  232.         printf ("NULL\n");
  233.     else
  234.         printf ("%c\n", BiLabel (BiRightChild(T,T),T));
  235.     if (BiLeftChild(T,T)!=LAMBDA)
  236.         Ispisi(BiLeftChild(T,T));
  237.     if (BiRightChild(T,T)!=LAMBDA)
  238.         Ispisi (BiRightChild(T,T));
  239.     return;
  240.     }
  241.  
  242. void Ispis (BinaryTree T) {
  243.     if (BiLabel(T,T)=='\n')
  244.         printf ("NULL");
  245.     else
  246.         Ispisi (T);
  247.     }
  248.  
  249. int main () {
  250.     BinaryTree T;
  251.     T=Unesi (T);
  252.     Ispis (T);
  253.     BiPreOrder(T);
  254.     return 0;
  255.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement