Question: Using C, Please help me with this hwk Directions: Complete the following homework assignment using the description given in each section. Purpose: Use of binary
Using C, Please help me with this hwk
Directions: Complete the following homework assignment using the description given in each section.
Purpose:
Use of binary trees
Use of argc/argv
Description:
Part of the advantages of binary trees, or any tree in particular, is that you are able to make sense of large data sets. In this homework, you will have two input files, each containing 5000 entries. However, it is relatively easy to get what we want out of the data through binary trees. Note that the data in the dataset is real data; this makes it easier to check your work. Also note that there is no file format given, because the function to parse through the data is given to you.
Structure Definition
typedef struct n_
{
int zipCode; // A zip code that exists in the given city/state
char* city;//Will point to a city name
char state[3];//Astate abbreviation.Note that we need room for the NULL terminator!
struct _* left;//connections to other nodes
struct n_*right;
}Node;
Function Prototypes
Node* addNode(Node* root, Node* newNode)
Input: the root of the tree, and the new node ready to be added to the tree. Return: the root of the tree
Add the newest node to the tree. The node has been prefilled with data, so you do not have to modify the new node in anyway. Just connect it to the tree in the manner you have been doing. This function must be recursive.
int findStateCount(Node* root, char* state)
Input: the root of the tree and the state you are searching for Return: the number of instances the state shows up throughout the tree
Using the string for the state, get a count of all nodes in the tree that have that same state. This might take a little thought, but with recursion its very simple. This function must be recursive. You are not allowed to use system calls.
Node* findZipCode(Node* root, int zipCode)
Input: the root of the tree and the zip code you are searching for. Return: a pointer to the node that you found matched the zip code, or NULL if not found.
Using the zip code, search the tree for a matching node. There will either be one node found with that zip code, or none found. This function must be recursive. You are not allowed to use system calls.
void freeTree(Node* root)
Input: the root of the tree.
You are to free everything that the tree has allocated. Note that there is more to the node than just an integer and pointers. The difficulty of the function does not change, however.
int main(int argc, char** argv)
Input: your command line arguments. Return: a number.
Get your tree made, present options to the user, get user input and call functions accordingly. Loop the program until the user decides to quit. Take note that you will need to read in the state somehow from the user. You can assume that the user puts in a proper 2-character state code, and that there will be no problem with the input.
This function has been done for you (you just have to call it):
Node* importTree(char* filename)
Bonus opportunity:
You are to thoroughly comment the importTree function, properly explaining how the function does what needs to be done. The more in-depth comments you provide the more points you will earn (this does not mean quantity over quality). Someone with less experience than you should be able to follow your thought process through your comments alone as if you wrote the code yourself. You will need certain specific descriptions in your comments to get full points, and you are only eligible for the bonus credit if you would have gotten 40/40 on the homework, and you have no errors or memory leaks via valgrind.
Maximum bonus credit: 5 points
You are not allowed to change function prototypes or structure definitions. You may add your own functions and structures if it helps with your implementation.
Sample Output
$ ./a.out
Error. Usage: ./a.out
$ ./a.out sample.txt
Find the number of zip codes in a state
Find a zip code
Exit
> 1
Enter the state: MO
The state of MO has 153 zip codes in the file.
Find the number of zip codes in a state
Find a zip code
Exit
> 11 Error not a valid option. Try again.
Find the number of zip codes in a state
Find a zip code
Exit
> 2 Enter the zip code you want to find: 444 No results found for zip code 444
Find the number of zip codes in a state
Find a zip code
Exit
> 2 Enter the zip code you want to find: 57349
Result found for zip code 57349:
City: Howard State: South Dakota
Find the number of zip codes in a state
Find a zip code
Exit
> 3
Program Terminated.
$ grep MO sample.txt | wc -l
You can use the above command sequence to test if you did findStateCount correctly. This will search the text file and count the number of lines that have the state as MO to confirm that there are 153 results from the sample output. You can replace MO with any state abbreviation or replace the filename to test other scenarios. To test the zip code search, you can simply look up that zip code online (these are real zip codes).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
