Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------------------------------------------------------------------
- // proj2.c
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include "io.h"
- #include "bst.h"
- void main() {
- char* traversalResult; // declare a variable to be used for the inorder traversal
- while (1 == 1) { // loop runs always
- switch (getInput()) { // depending on the result of getInput
- case 'i': // if 'i' is returned
- insert(doAction('i'), NULL); // prompt the user for a number to insert, and insert it into the tree
- break; // break out of switch case
- case 's': // if 's' is returned
- if (searchTree(doAction('s'), NULL) == true) // prompt the user for a number to search for, and print the result STILL NEEDS WORK
- break;
- case 't':
- traversalResult = malloc(sizeof(char) * 100);
- printTree(traversalResult, NULL);
- printOutput(traversalResult);
- free(traversalResult);
- break;
- case 'q':
- freeTree(NULL);
- exit(0);
- default:
- printf("Error");
- }
- }
- }
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include "bst.h"
- typedef struct {
- int key; // the data for the tree
- struct node* leftChild; // the child that has a lower value
- struct node* rightChild; // the child that has a larger value
- } node;
- node *root = NULL; // pointer for the root for the entire tree
- bool searchTree(int key, node *pRoot) {
- bool found = true; // the "found" variable stores whether or not the key is found within the tree, defaults as true
- if ((*pRoot).key != key) { // if the key doesn't match the passed in root node's key value
- if ((*pRoot).leftChild == NULL && (*pRoot).rightChild == NULL) { // AND if the passed in node's children are null
- found = false; // then the key isnt contained within the tree
- }
- else if ((*pRoot).leftChild != NULL) { // if the passed in node's left child isn't null
- found = searchTree(key, (*pRoot).leftChild); // search the left child and its children for the key and store the result in found
- 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
- found = searchTree(key, (*pRoot).rightChild); // search the right child and all its children and store the result in found
- }
- }
- }
- return found; // return the result
- }
- void insert(int key, node *pRoot) {
- if (pRoot == NULL) {
- pRoot = root;
- }
- if (searchTree(key, root) == true) { // if the tree already has the key in it
- return; // end the method
- }
- if (root == NULL) { // if the tree is empty
- node *temp = malloc(sizeof(node)); // make a node for the tree
- (*temp).key = key; // set the value of the node to the given key
- root = temp; // set the root to the value of the node
- free(temp); // remove the temporary node
- return; // end the method there
- }
- if (key > (*pRoot).key) { // if the key value is greater than the key value stored in the root
- if ((*pRoot).rightChild == NULL) { // if the right child of the root is empty
- node *temp = malloc(sizeof(node)); // make a node for the tree
- (*temp).key = key; // set the value of the node to the given key
- (*pRoot).rightChild = temp; // set the right child to the value of the node
- free(temp); // remove the temporary node
- return; // end it
- }
- else { // if the right child isnt empty
- insert(key, (*pRoot).rightChild); // recursively call the function on the tree's right child
- return;
- }
- }
- if (key < (*pRoot).key) { // if the key value is less than the key value stored in the root
- if ((*pRoot).leftChild == NULL) { // if the left child of the root is empty
- node *temp = malloc(sizeof(node)); // make a node for the tree
- (*temp).key = key; // set the value of the node to the given key
- (*pRoot).leftChild = temp; // set the left child to the value of the node
- free(temp); // remove the temporary node
- return; // end it
- }
- else { // if the left child isnt empty
- insert(key, (*pRoot).leftChild); // recursively call the function of the tree's left child
- return;
- }
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- //bst.h
- #pragma once
- #ifndef BST_H_
- #define BST_H_
- typedef struct {
- int key; // the data for the tree
- struct node* leftChild; // the child that has a lower value
- struct node* rightChild; // the child that has a larger value
- } node;
- bool searchTree(int key, node *pRoot);
- void insert(int key, node *pRoot);
- void printTree(char *stringOfNodes, node *pRoot);
- void freeTree(node *pRoot);
- #endif
- ---------------------------------------------------------------------------------------------------------------------------------------
- //bst.c
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include "bst.h"
- typedef struct {
- int key; // the data for the tree
- struct node* leftChild; // the child that has a lower value
- struct node* rightChild; // the child that has a larger value
- } node; // <---------- Error occurs on this line
- node *root = NULL; // pointer for the root for the entire tree
- bool searchTree(int key, node *pRoot) {
- bool found = true; // the "found" variable stores whether or not the key is found within the tree, defaults as true
- if ((*pRoot).key != key) { // if the key doesn't match the passed in root node's key value
- if ((*pRoot).leftChild == NULL && (*pRoot).rightChild == NULL) { // AND if the passed in node's children are null
- found = false; // then the key isnt contained within the tree
- }
- else if ((*pRoot).leftChild != NULL) { // if the passed in node's left child isn't null
- found = searchTree(key, (*pRoot).leftChild); // search the left child and its children for the key and store the result in found
- 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
- found = searchTree(key, (*pRoot).rightChild); // search the right child and all its children and store the result in found
- }
- }
- }
- return found; // return the result
- }
- void insert(int key, node *pRoot) {
- if (pRoot == NULL) {
- pRoot = root;
- }
- if (searchTree(key, root) == true) { // if the tree already has the key in it
- return; // end the method
- }
- if (root == NULL) { // if the tree is empty
- node *temp = malloc(sizeof(node)); // make a node for the tree
- (*temp).key = key; // set the value of the node to the given key
- root = temp; // set the root to the value of the node
- free(temp); // remove the temporary node
- return; // end the method there
- }
- if (key > (*pRoot).key) { // if the key value is greater than the key value stored in the root
- if ((*pRoot).rightChild == NULL) { // if the right child of the root is empty
- node *temp = malloc(sizeof(node)); // make a node for the tree
- (*temp).key = key; // set the value of the node to the given key
- (*pRoot).rightChild = temp; // set the right child to the value of the node
- free(temp); // remove the temporary node
- return; // end it
- }
- else { // if the right child isnt empty
- insert(key, (*pRoot).rightChild); // recursively call the function on the tree's right child
- return;
- }
- }
- if (key < (*pRoot).key) { // if the key value is less than the key value stored in the root
- if ((*pRoot).leftChild == NULL) { // if the left child of the root is empty
- node *temp = malloc(sizeof(node)); // make a node for the tree
- (*temp).key = key; // set the value of the node to the given key
- (*pRoot).leftChild = temp; // set the left child to the value of the node
- free(temp); // remove the temporary node
- return; // end it
- }
- else { // if the left child isnt empty
- insert(key, (*pRoot).leftChild); // recursively call the function of the tree's left child
- return;
- }
- }
- }
- void printTree(char *stringOfNodes, node *pRoot) {
- if (pRoot == NULL) {
- pRoot == root;
- }
- if ((*pRoot).leftChild != NULL) { // if the root's left child has data
- printTree(stringOfNodes, (*pRoot).leftChild); // pass in the string and the root's left child to be recursively handled
- }
- char temp[sizeof(int) * 8]; // make a temporary string
- sprintf(temp, "%d ", (*pRoot).key); // copy the root node's data into the string
- strcat((*stringOfNodes), temp); // add that onto the passed in string
- free(temp); // clean up the temporary string
- if ((*pRoot).rightChild != NULL) { // if the root's right child has data
- printTree(stringOfNodes, (*pRoot).rightChild); // pass in the string and the root's right child to be recursively handled
- }
- }
- void freeTree(node *pRoot) {
- if (pRoot == NULL)
- pRoot = root;
- if ((*pRoot).leftChild != NULL) { // the root node's left child isn't empty
- freeTree((*pRoot).leftChild); // remove the data from the left child
- }
- if ((*pRoot).rightChild != NULL) { // the root node's right child isn't empty
- freeTree((*pRoot).rightChild); // remove the data from the right child
- }
- free(pRoot); // remove the data from the root node
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- //io.h
- #pragma once
- #ifndef IO_H_
- #define IO_H_
- char getInput();
- int doAction(char actionCharacter);
- void printOutput(char *output);
- #endif
- ---------------------------------------------------------------------------------------------------------------------------------------
- //io.c
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include "io.h"
- char getInput() { // prompts the user for input on what to do
- char inputChar = 'a'; // initializes the character value for the input to an value which causes it to enter the loop
- while (inputChar != 'i' || inputChar != 's' || inputChar != 't' || inputChar != 'q') { // while inputChar is an invalid character
- printf("Enter (i)nsert, (s)earch, inorder (t)raversal, or (q)uit: "); // prompt the user for input
- inputChar = getchar(); //sets the inputChar to the character given by the user
- getchar();
- }
- return inputChar; // return the inputChar if it is valid
- }
- int doAction(char actionCharacter) {
- int actionInt = -1;
- switch (actionCharacter) {
- case 'i':
- printf("Enter a number to insert: ");
- scanf("%d", actionInt);
- getchar();
- break;
- case 's':
- printf("Enter a number to search: ");
- scanf("%d", actionInt);
- getchar();
- break;
- default:
- printf("Error, invalid input");
- }
- return actionInt;
- }
- void printOutput(char *output) { // prints the output of the binary tree search
- printf("%s\n", output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement