Advertisement
elica123

Untitled

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