Advertisement
elica123

Untitled

Nov 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define LAMBDA NULL
  4.  
  5. //vrijeme izvrsavanja konstantno
  6.  
  7. typedef char* labeltype; //oznaka cvora(jer su nam ulazni podaci stringovi)
  8.  
  9. typedef struct cell_tag{ //svaki cvor prikazujemo jednom celijom
  10. labeltype label; //oznaka cvora
  11. struct cell_tag *leftchild; //pointer na lijevo dijete
  12. struct cell_tag *rightchild; //pointer na desno dijete
  13. }celltype;
  14.  
  15. typedef celltype *node; //cvor je odreden pointerom na tu celiju
  16. typedef celltype *BinaryTree; //binarno stablo poistovjecujemo s pointerom na korjen
  17.  
  18. void BiMakeNull(BinaryTree *Tp){ //stavljamo pointer na stablo na null pointer
  19. *Tp=NULL;
  20. }
  21.  
  22. int BiEmpty(BinaryTree T){ //provjera je li nase stablo prazno
  23. if(T==NULL)return 1;
  24. return 0;
  25. }
  26.  
  27. void BiCreate(labeltype l, BinaryTree TL, BinaryTree TR, BinaryTree *Tp){ //kreiramo novo
  28. (*Tp)=(celltype*)malloc(sizeof(celltype)); //alociramo memoriju
  29. (*Tp)->label=l; //oznaka
  30. (*Tp)->leftchild=TL; //lijevo dijete
  31. (*Tp)->rightchild=TR; //desno dijete
  32. }
  33.  
  34. void BiLeftSubtree(BinaryTree T, BinaryTree *TLp){
  35. (*TLp)=T->leftchild; //vracamo lijevo podstablo
  36. }
  37.  
  38. void BiRightSubtree (BinaryTree T, BinaryTree *TRp){ //vraca deno podstablo
  39. (*TRp)=T->rightchild; //vracamo desno podstablo
  40. }
  41. node BiInsertLeftChild(labeltype l, node i, BinaryTree *Tp){ //umetanje lijevog djeteta
  42. if (!i){
  43. printf("Takav cvor ne postoji.");
  44. exit(2); //exit(2)->ne postoji cvor kome bi umetnuli lijevo dijete
  45. }
  46. if (i->leftchild != NULL){
  47. printf("Takav cvor vec ima lijevo dijete."); //exit(4)-> cvor vec ima lijevo dijete
  48. exit(4);
  49. }
  50. if (!(i->leftchild = (celltype*)malloc(sizeof(celltype)))){ //provjera ima li memorije za cvor
  51. printf("Mem out.");
  52. exit(8); //exit(8)->Nemam memorije za novi cvor
  53. }
  54.  
  55. i->leftchild->label = l; //oznaka cvora je l
  56. i->leftchild->leftchild = i->leftchild->rightchild = NULL; //pointeri na lijevo i desno dijete novog cvora su NULL
  57.  
  58. return i->leftchild; //funkcija vraca pointer na novi cvor
  59. }
  60.  
  61. node BiInsertRightChild(labeltype l, node i, BinaryTree *Tp){ //umetanje desnog djeteta
  62. if (!i){
  63. printf("Takav cvor ne postoji.");
  64. exit(2); //exit(2)->ne postoji cvor kome bi umetnuli desno dijete
  65. }
  66.  
  67. if (i->rightchild != NULL){
  68. printf("Takav cvor vec ima desno dijete.");
  69. exit(4); //exit(4)-> cvor vec ima desno dijete
  70. }
  71.  
  72. if (!(i->rightchild = (celltype*)malloc(sizeof(celltype)))){
  73. printf("Mem out.");
  74. exit(8); //exit(8)->Nemam memorije za novi cvor
  75. }
  76.  
  77. i->rightchild->label = l; //oznaka cvora je l
  78. i->rightchild->leftchild = i->rightchild->rightchild = NULL; //pointeri na lijevo i desno dijete novog cvora su NULL
  79.  
  80. return i->rightchild; //funkcija vraca pointer na novi cvor
  81. }
  82.  
  83. node BiRoot(BinaryTree T){ //vraca korijen binarnog stabla
  84. return T;
  85. }
  86.  
  87. node BiLeftChild( node i, BinaryTree T){ //vraca lijevo dijete cvora
  88. if (!i)
  89. return LAMBDA;
  90. if (i->leftchild == NULL)
  91. return LAMBDA;
  92.  
  93. return i->leftchild;
  94. }
  95.  
  96. node BiRightChild(node i, BinaryTree T){ //vraca desno dijete cvora
  97. if (i==NULL)
  98. return LAMBDA;
  99. if (i->rightchild == NULL)
  100. return LAMBDA;
  101.  
  102. return i->rightchild;
  103. }
  104.  
  105. void ParentSearch(node *parent, node i, node root){ //pomocna funkcija, trazi roditelja od cvora
  106. if ((root->leftchild == i) || (root->rightchild == i))
  107. *parent = root;
  108. if (*parent) return;
  109. if (root->leftchild)
  110. ParentSearch(parent, i, root->leftchild);
  111. if (root->rightchild)
  112. ParentSearch(parent, i, root->rightchild);
  113. }
  114.  
  115. node BiParent(node i, BinaryTree T){ // vraca roditelja od cvora
  116. if (!i){
  117. printf("Taj cvor ne postoji.");
  118. exit(1);
  119. }
  120. if(i == T) //korjen nema cvora
  121. return NULL;
  122. node roditelj = NULL;
  123. ParentSearch(&roditelj, i, T);
  124. return roditelj;
  125. }
  126.  
  127. labeltype BiLabel(node i, BinaryTree T){ //vraca oznaku cvora
  128. if (i==NULL){
  129. printf ("Takav cvor ne postoji");
  130. exit(2);
  131. }
  132. return i->label;
  133. }
  134.  
  135. void BiChangeLabel(labeltype l,node i, BinaryTree *Tp){ // mijenja oznaku cvora
  136. if (i==NULL){
  137. printf("Takav cvor ne postoji");
  138. exit(2);
  139. }
  140. i->label = l;
  141. }
  142.  
  143. void BiDelete(node i, BinaryTree *Tp){ //izbacujemo list i iz stabla
  144. if (!i){
  145. printf("Takav cvor ne postoji.");
  146. exit(2);
  147. }
  148. if (i->rightchild || i->leftchild){ //nije list
  149. printf("Taj cvor ima dijete pa se ne moze izbrisati!");
  150. exit(16);
  151. }
  152. node r=BiParent(i, *Tp);
  153. if (r==LAMBDA){ //taj list je ujutro i korjen
  154. free (Tp);
  155. return;
  156. }
  157. if ( BiLeftChild(r,*Tp)==i)
  158. r->leftchild=NULL;
  159. else
  160. r->rightchild==NULL; //roditelja ostavljamo bez djeteta
  161. free(i); //oslobadamo memoriju
  162. }
  163.  
  164. int search(char arr[], int strt, int end, char value);
  165.  
  166. struct node* newNode(char data);
  167.  
  168. struct node* buildTree(char in[], char pre[], int inStrt, int inEnd){
  169. static int preIndex =(sizeof(pre)/sizeof(pre[0]))-1; //zadnji u postorderu je korjen
  170. if(inStrt > inEnd){
  171. return NULL;
  172. }
  173. struct node *tNode = newNode(pre[preIndex--]); //krecem se od iza zbog defiicje postordera
  174. if(inStrt == inEnd) //tu nam je bazicni slucaj
  175. return tNode;
  176. int inIndex = search(in, inStrt, inEnd, tNode->data);
  177. tNode->left= buildTree(in, pre, inStrt, inIndex-1);
  178. tNode->right = buildTree(in, pre, inIndex+1, inEnd);
  179. return tNode;
  180. }
  181.  
  182. int search(char arr[], int strt, int end, char value){
  183. int i;
  184. for(i = strt; i <= end; i++){
  185. if(arr[i] == value)
  186. return i;
  187. }
  188. }
  189.  
  190.  
  191. struct node* newNode(char data){
  192. struct node* node = (struct node*)malloc(sizeof(struct node));
  193. node->data = data;
  194. node->left = NULL;
  195. node->right = NULL;
  196. return(node);
  197. }
  198. void print_tree(struct node* node, int l){
  199. int i;
  200. if(!node) return;
  201. printf ("%c", node->data);
  202. if (node->left!=NULL) printf(" %c", node->left->data);
  203. else printf (" NULL");
  204. if (node->right!=NULL) printf(" %c", node->right->data);
  205. else printf (" NULL");
  206. printf ("\n");
  207. print_tree(node->left, l+1);
  208. print_tree(node->right, l+1);
  209. }
  210.  
  211. int main(){
  212. BinaryTree T;
  213. BiMakeNull(&T);
  214. char in[] = {'D', 'G', 'B', 'F', 'C', 'E', 'A'};
  215. char pre[] = {'G', 'D', 'F', 'E', 'C', 'A', 'B'};
  216. int len = sizeof(in)/sizeof(in[0]);
  217. struct node *root = buildTree(in, pre, 0, len - 1);
  218. print_tree(root, len);
  219. return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement