Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //#include <conio.h>
  4. #include <string.h>
  5. #include "tree_stack_ops.h"
  6.  
  7. #define newline printf("\n")
  8.  
  9. struct edge
  10. {
  11. int name;
  12. int lson;
  13. int rson;
  14. };
  15. typedef struct edge Edge;
  16.  
  17. void CREATE(BinaryTreeNode **);
  18. void POSTORDER (BinaryTreeNode *);
  19. void PREORDER(BinaryTreeNode *);
  20. void INORDER (BinaryTreeNode *);
  21. void POSTORDER_REC (BinaryTreeNode *);
  22. void PREORDER_REC (BinaryTreeNode *);
  23. void INORDER_REC (BinaryTreeNode *);
  24. void VISIT (BinaryTreeNode *);
  25. void FREE_TREE (BinaryTreeNode *);
  26.  
  27. BinaryTreeNode *create_node(BinaryTreeDataType);
  28.  
  29. int main()
  30. {
  31. BinaryTreeNode *T;
  32. CREATE(&T);
  33.  
  34. INORDER_REC(T);
  35. newline;
  36.  
  37.  
  38. FREE_TREE(T);
  39. return 0;
  40. }
  41.  
  42. BinaryTreeNode *create_node(BinaryTreeDataType data)
  43. {
  44. BinaryTreeNode *temp = (BinaryTreeNode *) malloc(sizeof(BinaryTreeNode));
  45.  
  46. if (temp == NULL) fprintf(stderr, "Error in memory allocation.\n");
  47. else
  48. {
  49. temp->DATA = data;
  50. temp->LSON = NULL;
  51. temp->RSON = NULL;
  52. }
  53.  
  54. return temp;
  55. }
  56.  
  57. int read_tree_input (Edge **edges)
  58. {
  59. int i, n;
  60. int lson, rson;
  61.  
  62. scanf("%d\n", &n);
  63. *edges = (Edge *) malloc (n*sizeof(Edge));
  64. for (i = 0; i < n; i++)
  65. {
  66. (*edges)[i].name = i+1;
  67.  
  68. scanf("%d %d\n", &lson, &rson);
  69. (*edges)[i].lson = lson;
  70. (*edges)[i].rson = rson;
  71.  
  72. // printf("%d %d %d\n", edges[i].name, edges[i].lson, edges[i].rson);
  73. }
  74.  
  75. return n;
  76. //free(edges);
  77. }
  78.  
  79. void CREATE (BinaryTreeNode **T)
  80. {
  81. Edge *edges;
  82. BinaryTreeNode **nodes;
  83. int n;
  84. int i = 0;
  85.  
  86. n = read_tree_input(&edges);
  87.  
  88. nodes = (BinaryTreeNode **) malloc (n*sizeof(BinaryTreeNode*));
  89. for (i = 0; i < n; i++)
  90. {
  91. nodes[i] = (BinaryTreeNode *) malloc (sizeof(BinaryTreeNode));
  92. nodes[i]->DATA = edges[i].name;
  93. nodes[i]->LSON = NULL;
  94. nodes[i]->RSON = NULL;
  95. }
  96.  
  97. // printf("okay\n");
  98. for (i = 0; i < n; i++)
  99. {
  100. // printf("%d %d %d\n", edges[i].name, edges[i].lson, edges[i].rson);
  101.  
  102. if (edges[i].lson > 0) nodes[i]->LSON = nodes[edges[i].lson-1];
  103. if (edges[i].rson > 0) nodes[i]->RSON = nodes[edges[i].rson-1];
  104.  
  105. // printf("Node %d :: ", nodes[i]->DATA);
  106.  
  107. // if (nodes[i]->LSON == NULL) printf("LSON NULL :: ");
  108. // else printf("LSON %d :: ", nodes[i]->LSON->DATA);
  109.  
  110. // if (nodes[i]->RSON == NULL) printf("RSON NULL\n");
  111. // else printf("RSON %d\n", nodes[i]->RSON->DATA);
  112. }
  113.  
  114. *T = nodes[0];
  115.  
  116.  
  117. free(edges);
  118. }
  119.  
  120. void POSTORDER(BinaryTreeNode *T)
  121. {
  122. BinaryTreeNode *alpha; Stack S; StackElemType elem;
  123. InitStack(&S);
  124. alpha = T;
  125.  
  126. A:
  127. while (alpha != NULL)
  128. {
  129. elem.address = alpha;
  130. elem.tag = '-';
  131. PUSH (&S, elem);
  132. alpha = alpha ->LSON;
  133. }
  134.  
  135. B:
  136. if (IsEmptyStack(&S)) return;
  137. else
  138. {
  139. POP(&S, &elem);
  140. alpha = elem.address;
  141. if (elem.tag == '-')
  142. {
  143. elem.tag = '+';
  144. PUSH(&S, elem);
  145. alpha = alpha->RSON;
  146. goto A;
  147. }
  148. else
  149. {
  150. VISIT(alpha);
  151. goto B;
  152. }
  153. }
  154.  
  155. return;
  156. }
  157.  
  158. void PREORDER(BinaryTreeNode *T)
  159. {
  160. BinaryTreeNode *alpha; Stack S; StackElemType elem;
  161. InitStack(&S);
  162. alpha = T;
  163.  
  164. while (alpha != NULL)
  165. {
  166. while (alpha != NULL)
  167. {
  168. VISIT(alpha);
  169. elem.address = alpha;
  170. PUSH (&S, elem);
  171. alpha = alpha ->LSON;
  172. }
  173. while (alpha == NULL)
  174. {
  175. if (IsEmptyStack(&S)) return;
  176. POP(&S, &elem);
  177. alpha=elem.address->RSON;
  178. }
  179. }
  180. return;
  181. }
  182.  
  183. void INORDER(BinaryTreeNode *T)
  184. {
  185. BinaryTreeNode *alpha; Stack S; StackElemType elem;
  186. InitStack(&S);
  187. alpha = T;
  188.  
  189. while (alpha != NULL)
  190. {
  191. while (alpha != NULL)
  192. {
  193. elem.address = alpha;
  194. PUSH (&S, elem);
  195. alpha = alpha ->LSON;
  196. }
  197. while (alpha == NULL)
  198. {
  199. if (IsEmptyStack(&S)) return;
  200. POP(&S, &elem);
  201. VISIT(elem.address);
  202. alpha=elem.address->RSON;
  203. }
  204. }
  205. return;
  206. }
  207.  
  208. void PREORDER_REC(BinaryTreeNode *T)
  209. {
  210. BinaryTreeNode *alpha;
  211. alpha=T;
  212. VISIT(alpha);
  213. if(alpha->LSON!=NULL)PREORDER_REC(alpha->LSON);
  214. if(alpha->RSON!=NULL)PREORDER_REC(alpha->RSON);
  215. }
  216.  
  217. void INORDER_REC(BinaryTreeNode *T)
  218. {
  219. BinaryTreeNode *alpha;
  220. alpha=T;
  221. if(alpha->LSON!=NULL)PREORDER_REC(alpha->LSON);
  222. VISIT(alpha);
  223. if(alpha->RSON!=NULL)PREORDER_REC(alpha->RSON);
  224. }
  225.  
  226. void VISIT(BinaryTreeNode *T)
  227. {
  228. printf("-%d-", T->DATA);
  229. }
  230.  
  231. /**
  232. Deallocating a Binary Tree should be
  233. done in POSTORDER. Suggested is iterative
  234. to avoid stack overflow.
  235. **/
  236. void FREE_TREE (BinaryTreeNode *T)
  237. {
  238. if (T != NULL)
  239. {
  240. FREE_TREE(T->LSON);
  241. FREE_TREE(T->RSON);
  242. free(T);
  243. }
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement