Advertisement
elica123

Untitled

Nov 24th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define LAMBDA NULL
  4.  
  5. typedef char labeltype;
  6.  
  7. typedef struct cell_tag{
  8. labeltype label;
  9. struct celltag *leftchild;
  10. struct celltag *rightchild;
  11. }celltype;
  12.  
  13. typedef celltype *node;
  14. typedef celltype *BinaryTree;
  15.  
  16. void BiCreate(labeltype l, BinaryTree TL, BinaryTree TR, BinaryTree *Tp){
  17. *Tp=(celltype*)malloc(sizeof(celltype));
  18. (*Tp)->label=l;
  19. (*Tp)->leftchild=TL;
  20. (*Tp)->rightchild=TR;
  21. }
  22.  
  23. void BiMakeNull(BinaryTree *Tp){
  24. *Tp=NULL;
  25. }
  26.  
  27. int BiEmpty(BinaryTree T){
  28. if(T==NULL) return 1;
  29. return 0;
  30. }
  31.  
  32. void BiLeftSubtree(BinaryTree T, BinaryTree *TLp){
  33. if(BiEmpty(T))exit(8); //nije definirano
  34. (*TLp)=T->leftchild;
  35. }
  36.  
  37. void BiRightSubtree(BinaryTree T, BinaryTree *TRp){
  38. if(BiEmpty(T))exit(8);
  39. (*TRp)=T->rightchild;
  40. }
  41.  
  42. node BiInsertLeftChild(labeltype l, node i, BinaryTree *Tp){
  43. if(i==NULL) exit(8); //nije definirana
  44. if(i->leftchild!=NULL) return LAMBDA;
  45. i->leftchild->label=l;
  46. i->leftchild->leftchild=i->leftchild->rightchild=NULL;
  47. return i->leftchild;
  48. }
  49.  
  50. node BiInsertRightChild(labeltype l, node i, BinaryTree *Tp){
  51. if(i==NULL) exit(8); // nije definirana
  52. if(i->rightchild!=NULL) return LAMBDA;
  53. i->rightchild->label=l;
  54. i->rightchild->leftchild=i->rightchild->rightchild=NULL;
  55. return i->rightchild;
  56. }
  57.  
  58. void BiDelete(node i, BinaryTree *Tp){
  59. if(i==NULL) exit(1);
  60. if(i->leftchild!=NULL || i->rightchild!=NULL) exit(1);
  61. i=NULL;
  62. }
  63.  
  64. node BiRoot(BinaryTree T){
  65. if(T==NULL)
  66. return LAMBDA;
  67. return T;
  68. }
  69.  
  70. node BiLeftChild(node i, BinaryTree T){
  71. if(i->leftchild==NULL)
  72. return LAMBDA;
  73. return i->leftchild;
  74. }
  75.  
  76. node BiRightChild(node i, BinaryTree T){
  77. if(i->rightchild==NULL)
  78. return LAMBDA;
  79. return i->rightchild;
  80. }
  81.  
  82. void roditelj(node *otac, node i, node kor, BinaryTree T) { //pomocna funkcija
  83. if (BiLeftChild(i, T)== i || BiRightChild(i, T)== i)
  84. *otac = kor;
  85. if (*otac)
  86. return;
  87. if (BiLeftChild(i, T))
  88. roditelj(otac, i, BiLeftChild(i, T),T);
  89. if (BiRightChild(i, T))
  90. roditelj(otac, i, BiRightChild(i, T),T);
  91. }
  92.  
  93. node BiParent(node i, BinaryTree T){
  94. if(i==NULL) exit(8); //nije definirana
  95. if(i==T) return LAMBDA;
  96. node otac=BiCreate();
  97. roditelj(&otac, i, T, T);
  98. return otac;
  99. }
  100.  
  101. labeltype BiLabel(node i, BinaryTree T){
  102. if(i==NULL) exit(8); //nije definirana
  103. return i->label;
  104. }
  105.  
  106. void BiChangeLabel(labeltype l, node i, BinaryTree *Tp){
  107. if(i==NULL) exit(8); //nije definirana
  108. i->label=l;
  109. }
  110.  
  111.  
  112. int search(char arr[], int strt, int end, char value);
  113.  
  114. struct node* newNode(char data);
  115.  
  116. struct node* buildTree(char in[], char pre[], int inStrt, int inEnd){
  117. static int preIndex =(sizeof(pre)/sizeof(pre[0]))-1; //zadnji u postorderu je korjen
  118.  
  119. if(inStrt > inEnd){
  120. return NULL;
  121. }
  122. struct node *tNode = newNode(pre[preIndex--]); //krecem se od iza zbog defiicje postordera
  123.  
  124. if(inStrt == inEnd) //tu nam je bazicni slucaj
  125. return tNode;
  126.  
  127. int inIndex = search(in, inStrt, inEnd, tNode->data);
  128. tNode->right = buildTree(in, pre, inIndex+1, inEnd);
  129. tNode->left= buildTree(in, pre, inStrt, inIndex-1);
  130. return tNode;
  131. }
  132.  
  133. int search(char arr[], int strt, int end, char value){ //pomocna funkcija za trazenje znaka
  134. int i;
  135. for(i = strt; i <= end; i++){
  136. if(arr[i] == value)
  137. return i;
  138. }
  139. }
  140.  
  141.  
  142. struct node* newNode(char znak){ //stvaram novi cvor
  143. struct node* node = (struct node*)malloc(sizeof(struct node));
  144. BiLabel(newNode, T)=znak;
  145. BiLeftChild(newNode, T)=LAMDA;
  146. BiRightChild(newNode, T)=LAMDA;
  147. return(node);
  148. }
  149.  
  150.  
  151. void print_tree(struct node* node, int l, BinaryTree T){
  152. int i;
  153. if(!node) return;
  154. printf ("%c", Bilabel(node, T));
  155. if (BiLeftChild(node, T)!=NULL) printf(" %c", Bilabel((BiLeftChild(node, T)), T));
  156. else printf (" NULL");
  157. if (BiRightChild(node, T)!=NULL) printf(" %c", Bilabel((BiRightChild(node, T)), T));
  158. else printf (" NULL");
  159. printf ("\n");
  160. printf("---------------\n");
  161. print_tree(BiLeftChild(node, T), l+1, T);
  162. print_tree(BiRightChild(node,T) l+1, T);
  163. }
  164.  
  165.  
  166. int main(){
  167. BinaryTree T;
  168. BiMakeNull(&T);
  169. char in[] = {'D', 'G', 'B', 'F', 'C', 'E', 'A'};
  170. char pre[] = {'G', 'D', 'F', 'E', 'C', 'A', 'B'};
  171. int len = sizeof(in)/sizeof(in[0]);
  172. struct node *root = buildTree(in, pre, 0, len - 1);
  173. print_tree(root, len, T);
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement