Question
Reading in file for data struct C Creating a binary search tree program that reads in an input file and sorts it through my code
Reading in file for data struct C
Creating a binary search tree program that reads in an input file and sorts it through my code to add onto the tree. I'm having difficulty trying to figure out how to go about grabbing the integer given to me on a line and using it. For instance INSERT 6, I can identify insert by strncmp, but I don't know how to grab the 6 afterwards. I'll provide my broken code as well as the header file.
#include "BinarySearchTree.h"
int main() { BinarySearchTree tree = newBinarySearchTree(); //read in input FILE *input; char buffer[30]; Element Val; input = fopen("p4Input.txt", "r"); //if file isn't opening properly, close program if (input == NULL) { printf("Can't open input file, closing program ); break; }
while(fgets(buffer, 30, input)) { if (strncmp(buffer, "INSERT", 6) == 0) { insert(tree, val); continue; } if (strncmp(buffer, "SEARCH", 6) == 0) { } if (strncmp(buffer, "INORDER", 7) == 0) {
} if (strncmp(buffer, "PREORDER", 8) == 0) {
}
}
return 0; }
/************************************************************************ BinarySearchTree.h
Purpose: Define constants used in the project. Struct definitions for a general Binary Search Tree. Define function prototypes used by general Binary Search Trees. ************************************************************************/ #include #include #include #include
//#define constant values #define MAX_URL_LENGTH 50
#define TRUE 1 #define FALSE 0
//typedef for the Element struct which constains a c string to store a URL in the BrowserList typedef struct { int key; } Element;
//Typedef for a node in the doubly linked list (has next and previous pointers). typedef struct NodeT { Element element; struct NodeT *pLeft; struct NodeT *pRight; } NodeT;
//Typedef for a binary search tree implementation. //Contains a pointer to the root node of the tree. typedef struct { NodeT *pRoot; } BinarySearchTreeImp;
typedef BinarySearchTreeImp *BinarySearchTree;
/*****Prototypes*******/
//Malloc a new BinarySearchTreeImp and return it's address. BinarySearchTree newBinarySearchTree();
//Free the BinarySearchTree and any nodes that still exist in the tree. void freeBinarySearchTree(BinarySearchTree tree);
//Allocate a new node and store "value" as the Element in the node. Return the address of the node. NodeT *allocateNode(Element value);
//Recursive algorithm for searching for a node with key value equal to searchValue. Return a pointer to the node if you find it or return NULL if it does not exist. NodeT *search(NodeT *p, int searchValue);
//Create a node to store the given Element and add it as a leaf in the BinarySearchTree. Do not worry about balancing the tree for this project. //Return true if the insert worked successfully, and return false if the node already existed in the tree. int insert(BinarySearchTree tree, Element value);
//Recursivly print the key values of all nodes in the subtree rooted at p in increasing order. void printInOrder(NodeT *p);
//Recursivly print the key values of all nodes in the subtree rooted at p according to a preorder traversal. void printPreOrder(NodeT *p);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started