Advertisement
Guest User

Untitled

a guest
Oct 29th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct node
  5. {
  6. char data;
  7. char array[11];
  8. struct node* left;
  9. struct node* right;
  10. }binTreeNode;
  11.  
  12. typedef struct item
  13. {
  14. struct node* treeNode;
  15. struct item *next;
  16. }Q_ITEM;
  17.  
  18. void push(Q_ITEM **start, struct node* input)
  19. {
  20. Q_ITEM *temp = (Q_ITEM*) malloc(sizeof(Q_ITEM));
  21. temp->treeNode = input;
  22.  
  23.  
  24. if ((*start) == NULL)
  25. {
  26. temp->next = temp;
  27. (*start) = temp;
  28. }
  29. else
  30. {
  31. temp->next = (*start)->next;
  32. (*start)->next = temp;
  33. (*start)= temp;
  34. }
  35. }
  36.  
  37. int isEmpty( Q_ITEM ** start)
  38. {
  39. if ((*start) == NULL)
  40. return 1;
  41. else
  42. return 0;
  43. }
  44. struct node* pop(Q_ITEM ** start)
  45. {
  46. struct node* temp;
  47. temp = (*start)->treeNode;
  48.  
  49.  
  50. if( ((*start)->next) == NULL)
  51. {
  52.  
  53. (*start) = NULL;
  54. return(temp);
  55. }
  56. else
  57. {
  58. Q_ITEM* temp2 = (*start)->next;
  59. (*start)->next = temp2->next;
  60. if ((*start)==temp2)
  61. {
  62. (*start)=NULL;
  63. }
  64. return(temp);
  65.  
  66. }
  67.  
  68. }
  69. void printInorder(struct node** root, Q_ITEM** pointerTotopOfStack)
  70. {
  71. int done=0;
  72. struct node * TrvPtr = (*root);
  73. while(!done)
  74. {
  75. while(TrvPtr != NULL)
  76. {
  77. if(TrvPtr -> left != NULL)
  78. {
  79. push(pointerTotopOfStack, TrvPtr);
  80. TrvPtr = TrvPtr -> left;
  81. }
  82. else
  83. {
  84. printf("%c", TrvPtr -> data);
  85. TrvPtr = TrvPtr -> right;
  86. }
  87. }
  88. if( isEmpty(pointerTotopOfStack) == 1)
  89. {
  90. done = 1;
  91. }
  92. else
  93. {
  94. TrvPtr = pop(pointerTotopOfStack);
  95. printf("%c", TrvPtr -> data);
  96. TrvPtr = TrvPtr -> right;
  97. }
  98.  
  99. }
  100. }
  101. void printPreorder(struct node** root, Q_ITEM** pointerTotopOfStack)
  102. {
  103. int done=0;
  104. struct node * TrvPtr = (*root);
  105. while(!done)
  106. {
  107. while(TrvPtr != NULL)
  108. {
  109. printf("%c", TrvPtr -> data);
  110.  
  111. if(TrvPtr -> left != NULL)
  112. {
  113. push(pointerTotopOfStack, TrvPtr);
  114.  
  115. TrvPtr = TrvPtr -> left;
  116.  
  117. }
  118. else
  119. {
  120. TrvPtr = TrvPtr -> right;
  121.  
  122. }
  123. }
  124. if( isEmpty(pointerTotopOfStack)==1)
  125. {
  126. done = 1;
  127.  
  128. }
  129. else
  130. {
  131. TrvPtr = pop(pointerTotopOfStack);
  132.  
  133. TrvPtr = TrvPtr -> right;
  134. }
  135.  
  136. }
  137. }
  138. void printInorder2(struct node* node)
  139. {
  140. if (node == NULL)
  141. return;
  142.  
  143. printInorder2(node->left);
  144.  
  145. printf("%c", node->data);
  146.  
  147. printInorder2(node->right);
  148. }
  149.  
  150.  
  151. void printPreorder2(struct node* node)
  152. {
  153. if (node == NULL)
  154. return;
  155.  
  156. printf("%c", node->data);
  157.  
  158. printPreorder2(node->left);
  159.  
  160. printPreorder2(node->right);
  161. }
  162. struct node* insert(struct node *root, char letter, char *array)
  163. {
  164. struct node * SavedRoot = root;
  165. if(root == NULL)
  166. {
  167. struct node* node = (struct node*) malloc(sizeof(struct node));
  168. strcpy(node -> array, array);
  169. node->data = letter;
  170. node->left = NULL;
  171. node->right = NULL;
  172. return(node);
  173. }
  174. else if(root->data < letter) // if the new letter is greater than, gets put on right side
  175. {
  176. root = insert(root -> right, letter, array);
  177. SavedRoot -> right = root;
  178. //printf("\n just set right child of %c to %c\n", SavedRoot->data, root->data);
  179. return(SavedRoot);
  180. }
  181. else
  182. {
  183. root = insert(root -> left, letter, array);
  184. SavedRoot -> left = root;
  185. //printf("\n just set left child of %c to %c\n", SavedRoot->data, root->data);
  186. return(SavedRoot);
  187. }
  188.  
  189.  
  190. }
  191. // Find function
  192. void find(struct node * root, char letter)
  193. {
  194. if(root==NULL)
  195. {
  196. printf("Not found");
  197. return;
  198. }
  199. else if( root -> data == letter)
  200. {
  201. printf("Found the string \"%s\" there", root -> array);
  202. }
  203. else if( root ->data < letter)
  204. {
  205. find(root -> right, letter);
  206. }
  207. else
  208. {
  209. find(root -> left, letter);
  210. }
  211.  
  212. }
  213.  
  214. int main()
  215. {
  216.  
  217. char input;
  218. char unUsed;
  219. char letter;
  220. char array[11];
  221.  
  222. printf("\nEnter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: \n");
  223. scanf("%c", &input);
  224. scanf("%c", &unUsed);
  225.  
  226. struct node* root = NULL;
  227. Q_ITEM* topOfStack =NULL;
  228.  
  229. while(input != 'Q' && input != 'q')
  230. {
  231. if(input == 'I' || input == 'i')
  232. {
  233. printf("\nEnter a node\n");
  234. scanf("%c", &letter);
  235. scanf("%c", &unUsed);
  236.  
  237. printf("\n Enter a string of up to 10 characters for %c's data: ", letter);
  238. scanf("%s", array);
  239. scanf("%c", &unUsed);
  240.  
  241. root = insert(root, letter, array);
  242. printf("Preorder traversal is: ");
  243. printPreorder(&root,&topOfStack );
  244. printf("\nRecursive PreOrder: ");
  245. printPreorder2(root);
  246. printf("\n");
  247. printf("\nInorder traversal is: ");
  248. printInorder(&root, &topOfStack);
  249. printf("\nRecursive inOrder: ");
  250. printInorder2(root);
  251.  
  252.  
  253. }
  254. else
  255. {
  256. printf("\nEnter a node\n");
  257. scanf("%c", &letter);
  258. scanf("%c", &unUsed);
  259. find(root, letter);
  260. }
  261. printf("\nEnter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: \n");
  262. scanf("%c", &input);
  263. scanf("%c", &unUsed);
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement