Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.93 KB | None | 0 0
  1. ---------------------------------------------------------------------------------------------------------------------------------------
  2. // proj2.c
  3. #define _CRT_SECURE_NO_WARNINGS
  4.  
  5. #include <stdio.h>
  6. #include <stdbool.h>
  7. #include <string.h>
  8. #include "io.h"
  9. #include "bst.h"
  10.  
  11. void main() {
  12. char* traversalResult; // declare a variable to be used for the inorder traversal
  13. while (1 == 1) { // loop runs always
  14. switch (getInput()) { // depending on the result of getInput
  15. case 'i': // if 'i' is returned
  16. insert(doAction('i'), NULL); // prompt the user for a number to insert, and insert it into the tree
  17. break; // break out of switch case
  18. case 's': // if 's' is returned
  19. if (searchTree(doAction('s'), NULL) == true) // prompt the user for a number to search for, and print the result STILL NEEDS WORK
  20. break;
  21. case 't':
  22. traversalResult = malloc(sizeof(char) * 100);
  23. printTree(traversalResult, NULL);
  24. printOutput(traversalResult);
  25. free(traversalResult);
  26. break;
  27. case 'q':
  28. freeTree(NULL);
  29. exit(0);
  30. default:
  31. printf("Error");
  32. }
  33. }
  34. }
  35.  
  36. #define _CRT_SECURE_NO_WARNINGS
  37.  
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdbool.h>
  41. #include "bst.h"
  42.  
  43. typedef struct {
  44. int key; // the data for the tree
  45. struct node* leftChild; // the child that has a lower value
  46. struct node* rightChild; // the child that has a larger value
  47. } node;
  48.  
  49. node *root = NULL; // pointer for the root for the entire tree
  50.  
  51. bool searchTree(int key, node *pRoot) {
  52. bool found = true; // the "found" variable stores whether or not the key is found within the tree, defaults as true
  53.  
  54. if ((*pRoot).key != key) { // if the key doesn't match the passed in root node's key value
  55. if ((*pRoot).leftChild == NULL && (*pRoot).rightChild == NULL) { // AND if the passed in node's children are null
  56. found = false; // then the key isnt contained within the tree
  57. }
  58. else if ((*pRoot).leftChild != NULL) { // if the passed in node's left child isn't null
  59. found = searchTree(key, (*pRoot).leftChild); // search the left child and its children for the key and store the result in found
  60. if (found == false && (*root).rightChild != NULL) { // if the left child and all it's children don't contain the key AND the right child isn't null
  61. found = searchTree(key, (*pRoot).rightChild); // search the right child and all its children and store the result in found
  62. }
  63. }
  64. }
  65.  
  66.  
  67. return found; // return the result
  68. }
  69.  
  70. void insert(int key, node *pRoot) {
  71. if (pRoot == NULL) {
  72. pRoot = root;
  73. }
  74.  
  75. if (searchTree(key, root) == true) { // if the tree already has the key in it
  76. return; // end the method
  77. }
  78.  
  79. if (root == NULL) { // if the tree is empty
  80. node *temp = malloc(sizeof(node)); // make a node for the tree
  81. (*temp).key = key; // set the value of the node to the given key
  82. root = temp; // set the root to the value of the node
  83. free(temp); // remove the temporary node
  84. return; // end the method there
  85. }
  86.  
  87. if (key > (*pRoot).key) { // if the key value is greater than the key value stored in the root
  88. if ((*pRoot).rightChild == NULL) { // if the right child of the root is empty
  89. node *temp = malloc(sizeof(node)); // make a node for the tree
  90. (*temp).key = key; // set the value of the node to the given key
  91. (*pRoot).rightChild = temp; // set the right child to the value of the node
  92. free(temp); // remove the temporary node
  93. return; // end it
  94. }
  95. else { // if the right child isnt empty
  96. insert(key, (*pRoot).rightChild); // recursively call the function on the tree's right child
  97. return;
  98. }
  99. }
  100. if (key < (*pRoot).key) { // if the key value is less than the key value stored in the root
  101. if ((*pRoot).leftChild == NULL) { // if the left child of the root is empty
  102. node *temp = malloc(sizeof(node)); // make a node for the tree
  103. (*temp).key = key; // set the value of the node to the given key
  104. (*pRoot).leftChild = temp; // set the left child to the value of the node
  105. free(temp); // remove the temporary node
  106. return; // end it
  107. }
  108. else { // if the left child isnt empty
  109. insert(key, (*pRoot).leftChild); // recursively call the function of the tree's left child
  110. return;
  111. }
  112. }
  113. }
  114.  
  115. ---------------------------------------------------------------------------------------------------------------------------------------
  116.  
  117. //bst.h
  118.  
  119. #pragma once
  120. #ifndef BST_H_
  121. #define BST_H_
  122.  
  123. typedef struct {
  124. int key; // the data for the tree
  125. struct node* leftChild; // the child that has a lower value
  126. struct node* rightChild; // the child that has a larger value
  127. } node;
  128.  
  129. bool searchTree(int key, node *pRoot);
  130. void insert(int key, node *pRoot);
  131. void printTree(char *stringOfNodes, node *pRoot);
  132. void freeTree(node *pRoot);
  133.  
  134. #endif
  135.  
  136. ---------------------------------------------------------------------------------------------------------------------------------------
  137.  
  138. //bst.c
  139.  
  140. #define _CRT_SECURE_NO_WARNINGS
  141.  
  142. #include <stdio.h>
  143. #include <string.h>
  144. #include <stdbool.h>
  145. #include "bst.h"
  146.  
  147. typedef struct {
  148. int key; // the data for the tree
  149. struct node* leftChild; // the child that has a lower value
  150. struct node* rightChild; // the child that has a larger value
  151. } node; // <---------- Error occurs on this line
  152.  
  153. node *root = NULL; // pointer for the root for the entire tree
  154.  
  155. bool searchTree(int key, node *pRoot) {
  156. bool found = true; // the "found" variable stores whether or not the key is found within the tree, defaults as true
  157.  
  158. if ((*pRoot).key != key) { // if the key doesn't match the passed in root node's key value
  159. if ((*pRoot).leftChild == NULL && (*pRoot).rightChild == NULL) { // AND if the passed in node's children are null
  160. found = false; // then the key isnt contained within the tree
  161. }
  162. else if ((*pRoot).leftChild != NULL) { // if the passed in node's left child isn't null
  163. found = searchTree(key, (*pRoot).leftChild); // search the left child and its children for the key and store the result in found
  164. if (found == false && (*root).rightChild != NULL) { // if the left child and all it's children don't contain the key AND the right child isn't null
  165. found = searchTree(key, (*pRoot).rightChild); // search the right child and all its children and store the result in found
  166. }
  167. }
  168. }
  169.  
  170.  
  171. return found; // return the result
  172. }
  173.  
  174. void insert(int key, node *pRoot) {
  175. if (pRoot == NULL) {
  176. pRoot = root;
  177. }
  178.  
  179. if (searchTree(key, root) == true) { // if the tree already has the key in it
  180. return; // end the method
  181. }
  182.  
  183. if (root == NULL) { // if the tree is empty
  184. node *temp = malloc(sizeof(node)); // make a node for the tree
  185. (*temp).key = key; // set the value of the node to the given key
  186. root = temp; // set the root to the value of the node
  187. free(temp); // remove the temporary node
  188. return; // end the method there
  189. }
  190.  
  191. if (key > (*pRoot).key) { // if the key value is greater than the key value stored in the root
  192. if ((*pRoot).rightChild == NULL) { // if the right child of the root is empty
  193. node *temp = malloc(sizeof(node)); // make a node for the tree
  194. (*temp).key = key; // set the value of the node to the given key
  195. (*pRoot).rightChild = temp; // set the right child to the value of the node
  196. free(temp); // remove the temporary node
  197. return; // end it
  198. }
  199. else { // if the right child isnt empty
  200. insert(key, (*pRoot).rightChild); // recursively call the function on the tree's right child
  201. return;
  202. }
  203. }
  204. if (key < (*pRoot).key) { // if the key value is less than the key value stored in the root
  205. if ((*pRoot).leftChild == NULL) { // if the left child of the root is empty
  206. node *temp = malloc(sizeof(node)); // make a node for the tree
  207. (*temp).key = key; // set the value of the node to the given key
  208. (*pRoot).leftChild = temp; // set the left child to the value of the node
  209. free(temp); // remove the temporary node
  210. return; // end it
  211. }
  212. else { // if the left child isnt empty
  213. insert(key, (*pRoot).leftChild); // recursively call the function of the tree's left child
  214. return;
  215. }
  216. }
  217. }
  218.  
  219. void printTree(char *stringOfNodes, node *pRoot) {
  220. if (pRoot == NULL) {
  221. pRoot == root;
  222. }
  223.  
  224. if ((*pRoot).leftChild != NULL) { // if the root's left child has data
  225. printTree(stringOfNodes, (*pRoot).leftChild); // pass in the string and the root's left child to be recursively handled
  226. }
  227.  
  228. char temp[sizeof(int) * 8]; // make a temporary string
  229. sprintf(temp, "%d ", (*pRoot).key); // copy the root node's data into the string
  230. strcat((*stringOfNodes), temp); // add that onto the passed in string
  231. free(temp); // clean up the temporary string
  232.  
  233. if ((*pRoot).rightChild != NULL) { // if the root's right child has data
  234. printTree(stringOfNodes, (*pRoot).rightChild); // pass in the string and the root's right child to be recursively handled
  235. }
  236. }
  237.  
  238. void freeTree(node *pRoot) {
  239. if (pRoot == NULL)
  240. pRoot = root;
  241.  
  242. if ((*pRoot).leftChild != NULL) { // the root node's left child isn't empty
  243. freeTree((*pRoot).leftChild); // remove the data from the left child
  244. }
  245.  
  246. if ((*pRoot).rightChild != NULL) { // the root node's right child isn't empty
  247. freeTree((*pRoot).rightChild); // remove the data from the right child
  248. }
  249.  
  250. free(pRoot); // remove the data from the root node
  251. }
  252.  
  253. ---------------------------------------------------------------------------------------------------------------------------------------
  254.  
  255. //io.h
  256.  
  257. #pragma once
  258. #ifndef IO_H_
  259. #define IO_H_
  260.  
  261. char getInput();
  262. int doAction(char actionCharacter);
  263. void printOutput(char *output);
  264.  
  265. #endif
  266.  
  267. ---------------------------------------------------------------------------------------------------------------------------------------
  268.  
  269. //io.c
  270.  
  271. #define _CRT_SECURE_NO_WARNINGS
  272.  
  273. #include <stdio.h>
  274. #include <string.h>
  275. #include <stdbool.h>
  276. #include "io.h"
  277.  
  278. char getInput() { // prompts the user for input on what to do
  279. char inputChar = 'a'; // initializes the character value for the input to an value which causes it to enter the loop
  280.  
  281. while (inputChar != 'i' || inputChar != 's' || inputChar != 't' || inputChar != 'q') { // while inputChar is an invalid character
  282. printf("Enter (i)nsert, (s)earch, inorder (t)raversal, or (q)uit: "); // prompt the user for input
  283. inputChar = getchar(); //sets the inputChar to the character given by the user
  284. getchar();
  285. }
  286.  
  287. return inputChar; // return the inputChar if it is valid
  288. }
  289.  
  290. int doAction(char actionCharacter) {
  291. int actionInt = -1;
  292. switch (actionCharacter) {
  293. case 'i':
  294. printf("Enter a number to insert: ");
  295. scanf("%d", actionInt);
  296. getchar();
  297. break;
  298. case 's':
  299. printf("Enter a number to search: ");
  300. scanf("%d", actionInt);
  301. getchar();
  302. break;
  303. default:
  304. printf("Error, invalid input");
  305. }
  306. return actionInt;
  307. }
  308.  
  309. void printOutput(char *output) { // prints the output of the binary tree search
  310. printf("%s\n", output);
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement