Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. a(b(cd)e(fg))
  2.  
  3. a
  4. /
  5. b e
  6. / /
  7. c d f g
  8.  
  9. #include<stdio.h>
  10. #include<malloc.h>
  11. #include<string.h>
  12.  
  13. struct node
  14. {
  15. char x;
  16. struct node *left;
  17. struct node *right;
  18. }*root;
  19.  
  20. char a[30];
  21. int i=0,n;
  22.  
  23. void tre(struct node * p) //function that I am using to convert the string to a tree
  24. {
  25. if(a[i]=='(')
  26. {
  27. i++;
  28. struct node *temp=malloc(sizeof(struct node));
  29. temp->x=a[i];
  30. temp->left=temp->right=NULL;
  31. i++;
  32. if(p->left==NULL)
  33. {
  34. p->left=temp;
  35. tre(p->left);
  36. }
  37. if(a[i]!='('&&a[i]!=')')
  38. {
  39. struct node *tempp=malloc(sizeof(struct node));
  40. tempp->x=a[i];
  41. i++;
  42. p->right=tempp;
  43. tre(p->right);
  44. }
  45. }
  46. else
  47. if(a[i]==')')
  48. {
  49. i++;
  50. }
  51.  
  52. }
  53.  
  54. void inorder(struct node *p)//inorder traversal of the tree made
  55. {
  56. if(p!=NULL)
  57. {
  58. inorder(p->left);
  59. printf("%c ",p->x);
  60. inorder(p->right);
  61. }
  62. }
  63.  
  64. main()
  65. {
  66. printf("Enter the string : ");
  67. scanf("%s",a);
  68. struct node *temp=malloc(sizeof(struct node));
  69. temp->x=a[i];
  70. temp->left=temp->right=NULL;
  71. i++;
  72. root=temp;
  73. tre(root);
  74. inorder(root);
  75. }
  76.  
  77. char a[30];
  78. int i=0,n;
  79.  
  80. typedef struct node
  81. {
  82. char x;
  83. struct node *left;
  84. struct node *right;
  85. } Node;
  86.  
  87. void tre(struct node * p) //function that I am using to convert the string to a tree
  88.  
  89. Node* tre(char const* input) // potentially more parameters to deal with position.
  90.  
  91. Node* parseTree(char const* data, size_t* pos)
  92. {
  93. if (data[(*pos)] == '')
  94. { // Appropriate error msg
  95. exit(1);
  96. }
  97.  
  98. // Allocate the tree here. (initialize all members)
  99. Node* result = malloc(sizeof(Node));
  100. result->value = data[(*pos)++];
  101. result->left = NULL;
  102. result->right = NULL;
  103.  
  104. // Now check for sub branches.
  105. if (data[(*pos)] == '(')
  106. {
  107. // Move past the '('
  108. (*pos)++;
  109.  
  110. result->left = parseTree(data, pos);
  111. result->right = parseTree(data, pos);
  112.  
  113. if ((*pos) != ')')
  114. { // Appropriate error msg
  115. exit(1);
  116. }
  117. // Move past the ')'
  118. (*pos)++;
  119. }
  120. // Done.
  121. return result;
  122. }
  123. Node* buildTree(char const* data)
  124. {
  125. if (data == NULL)
  126. { // Appropriate error msg
  127. exit(1);
  128. }
  129. size_t pos = 0;
  130. return parseTree(data, &pos);
  131. }
  132.  
  133. a
  134. /
  135. b e
  136. / /
  137. c d f g
  138.  
  139. void tre(struct node * p) //function that I am using to convert the string to a tree
  140. {
  141. if(a[i]=='(')
  142. {
  143.  
  144. i++;
  145. struct node *temp=malloc(sizeof(struct node));
  146. temp->x=a[i];
  147. temp->left=temp->right=NULL;
  148.  
  149. i++;
  150. if(p->left==NULL)
  151.  
  152. {
  153. p->left=temp;
  154. tre(p->left);
  155. }
  156. if(a[i]!='('&&a[i]!=')')
  157.  
  158. {
  159. struct node *tempp=malloc(sizeof(struct node));
  160. tempp->x=a[i];
  161. i++;
  162. p->right=tempp;
  163. tre(p->right);
  164. }
  165.  
  166. }
  167. else
  168. if(a[i]==')')
  169. {
  170. i++;
  171. }
  172.  
  173. }
  174.  
  175. stuct node * tre()
  176. {
  177. struct node * node = malloc(sizeof(struct node));
  178. node->x = a[i++];
  179. if(a[i] == '(')
  180. {
  181. node->left = tre();
  182. node->right = tre();
  183. assert(a[i++] == ')');
  184. }
  185. else
  186. {
  187. node->left = node->right = NULL;
  188. }
  189. return node;
  190. }
  191.  
  192. a(b(cd)e(fg))
  193.  
  194. a
  195. /
  196. b e
  197. / /
  198. c d f g
  199.  
  200. #include <string.h>
  201.  
  202. int main(int argc, char *argv[])
  203. {
  204. if(argc >= 2 && strcmp(argv[1], "a(b(cd)e(fg))") == 0)
  205. puts(
  206. " a" "n"
  207. " / " "n"
  208. " b e" "n"
  209. " / / " "n"
  210. " c d f g" "n"
  211. );
  212. return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement