•Using C language: •Read the .csv file such as the one attached below. Construct
ID: 3741833 • Letter: #
Question
•Using C language:
•Read the .csv file such as the one attached below. Construct a binary search tree to store the age of each person in.
Create a Makefile and use the Makefile to direct the program to search the binary search tree for the person "c"
Output the record to a .txt file, as well as to stdout
• Examples of use: – dict1 datafile outputfile < keyfile(The key is person "c")
• Example output:
– output file (information): Person: c || Age: 25
– stdout (comparisons): The number of key comparisons used to find "c" is %d
14573491 22263123 bcdefgh 12345678Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char* person;
int age;
struct node* left, *right;
};
struct node *newNode(char* person, int age)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
strcpy(temp->person, person);
temp->age = age;
temp->left = temp->right = NULL;
return temp;
}
struct node* insert(struct node* node, char* person, int age)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(person,age);
/* Otherwise, recur down the tree */
if (person < node->person)
node->left = insert(node->left, person, age);
else if (person > node->person)
node->right = insert(node->right, person, age);
/* return the (unchanged) node pointer */
return node;
}
struct node* search(struct node* root, char* person)
{
// Base Cases: root is null or key is present at root
if (root == NULL || root->person == person)
return root;
// Key is greater than root's key
if (strcmp(root->person, person) < 0)
return search(root->right, person);
// Key is smaller than root's key
return search(root->left, person);
}
const char* getfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ";");
tok && *tok;
tok = strtok(NULL, "; "))
{
if (!--num)
return tok;
}
return NULL;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%s || %d ", root->person,root->age);
inorder(root->right);
}
}
int main(int argc, char *argv[])
{
FILE* stream = fopen(argv[1], "r");
FILE* outstream = fopen(argv[2], "w");
char line[1024];
struct node *root = NULL;
int i;
char *person;
int age;
while (fgets(line, 1024, stream))
{
char* tmp = strdup(line);
char *data = getfield(tmp, 1);
printf("%s ", data);
char *token = strtok(data, ",");
int i = 1;
while (token != NULL)
{
if (i == 1) {
person = token;
printf("%s ", token);
}
else if (i == 2) {
age = atoi(token);
printf("%s ", token);
}
else
break;
i++;
strtok(NULL, ",");
}
root = insert(root, person, age);
free(tmp);
}
inorder(root);
struct node *n = search(root, "c");
if (n != NULL) {
fprintf(outstream, "Person: %s || Age: %d", n->person,n->age);
printf("The number of key comparisons used to find c is %d", n->age);
}
}
if any problem let me know in comment.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.