3 Programming Question (45 points) 3.1 Instructions You need to write the code b
ID: 3601400 • Letter: 3
Question
3 Programming Question (45 points) 3.1 Instructions You need to write the code by yourself. Your implementation must use C/C++ and your code must run on the Linux machine general.asu.edu. Please refer to the programming guide (available under the Assignments folder on Blackboard) for using the general.asu.edu server, as well as compiling and running C/C++ code under Linux For this question, you need to provide a Makefile that compiles your program to an executable named a3 that runs on the Linux machine general.asu.edu. Our TA will write a script to compile and run all student submissions; therefore, executing the command make in the Code folder must produce the executable a3 also located in the Code folder 3.2 Requirements For this part, you need to first review Section 10.4, Representing rooted trees. The requirements are as follows. You will write a serial program that executes a sequence of commands that operate on individual files. Valid commands include: Start Name, where Name is a character string of maximum length 20 alphabetic characters representing the name of the data file (Name.txt). Name.txt contains a set of data entries (integer type, one per line). This command reads all the data entries into an array (dynamically allocated). The output of the Start command is: Processing data from: Name.txt Build-BST, which builds a BST for the data entries in the array by calling the TREE-INSERT algorithm (pseudo code can be found in the lecture notes) repeatedly to insert the numbers one by one and output Building BST . INORDER-TREE-WALK, which calls the INORDER-TREE-WALK algorithm (pseudo code can be found in the lecture notes). Note that if the BST has not been built yet, output Build BST first! Otherwise, the output of this step should be sorted. For example, if the user-provided file contains the following data entries (one per line): 9, 2, 10, 4, 5, the output of your INORDER-TREE-WALK algorithm should be 2, 4, 5, 9, 10 End Name, which indicates the end of the processing for the data in file Name.txt. The Start and End commands will always come in pairs with matching names. Any memory dynamically allocated for Name must be freed on an End command. The output of the End command is: End of processing data from: Name.txt . Exit, which indicates that there are no more commands to execute, i.e., the program terminates Here is a sample command sequence Start test 1 Build-BST INORDER-TREE-WALK End test1 Start test 2 INORDER-TREE-WALKExplanation / Answer
#include<stdio.h>
#include<malloc.h>
#include<fcntl.h>
#include<string.h>
int num(char *num)
{
int dec = 0, i, j, len;
len = strlen(num);
for(i=0; i<len; i++){
dec = dec * 10 + ( num[i] - '0' );
}
return dec;
}
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *tree;
void create(struct node *tree)
{
tree=NULL;
}
struct node *TREE_INSERT(struct node *tree,int val)
{
struct node *ptr,*nodeptr,*parentptr;
ptr=(struct node *)malloc(sizeof(struct node));
ptr->data=val;
ptr->left=NULL;
ptr->right=NULL;
if(tree==NULL)
{
tree=ptr;
tree->left=NULL;
tree->right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
parentptr=nodeptr;
if(val<nodeptr->data)
nodeptr=nodeptr->left;
else
nodeptr=nodeptr->right;
}
if(val<parentptr->data)
parentptr->left=ptr;
else
parentptr->right=ptr;
}
return tree;
}
void INORDER_TREE_WALK(struct node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
printf("%d ",tree->data);
inorder(tree->right);
}
}
int main()
{
int n,i,count=0;
char buf[2];
int fd=open("name.txt",777);
while(read(fd,buf,1)!='')
{
if(buf[0]==' ')
count++;
}
close(fd);
fd=open("name.txt",777);
int arr[count];
create(tree);
for(i=0;i<count;i++)
{
read(fd,buf,1);
int t=num(buf[0]);
tree=TREE_INSERT(tree,t);
}
INORDER_TREE_WALK(tree);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.