I need to answer in C, I have the following code for a binary search tree data structre and need to: #include #include #include typedef struct node *BSTREE; struct node { int d; BSTREE lft; BSTREE rgt; }; BSTREE insert(BSTREE r, int num); BSTREE find(BSTREE root, int number); void inOrderTraversal(BSTREE sbtree, FILE *fp); int main() { BSTREE r = NULL; r = insert(r,20000); insert(r,50000); insert(r, 100000); BSTREE result = find(r,45); if (result == NULL) printf("Element 45 is not present in tree\n"); else printf("Element 45 is present in tree\n"); FILE *fp = fopen("sorted.txt","w"); inOrderTraversal(r,fp); fclose(fp); return 0; } BSTREE insert(BSTREE r, int num) { if (r == NULL) { BSTREE newNode = (BSTREE)malloc(sizeof(struct node)); newNode->d=num; newNode->lft= NULL; newNode->rgt= NULL; return newNode; } if (num < r->d) r->lft=insert(r->lft,num); else if(num > r->d) r->rgt=insert(r->rgt,num); return r; } BSTREE find(BSTREE r, int num) { if (r == NULL || r->d == num) return r; if (r->d < num) return find(r->rgt,num); else return find(r->lft,num); } void inOrderTraversal(BSTREE sbtree, FILE *fp) { if (sbtree != NULL) { inOrderTraversal(sbtree->lft,fp); fprintf(fp,"%d ",sbtree->d); inOrderTraversal(sbtree->rgt,fp); } } A) Add code to insert the numbers 1 to n in that order in an initially empty binary search tree. Run it on different values of n where : n = 20,000 n = 50,000 n = 100,000 Do an in-order traversal of the tree, printing out the contents to a file (named “sorted”) to verify that you indeed built the data structure correctly. Note the time for each program executions to just build the data structure (i.e., don’t include printing in the time) and include the timings in your report. For each n, you must run it 3 times and then take the average time. Don’t forget to submit all your data though. Report the results of the analyzed algorithms for each data structure using the Big-O and the timings. B) Dataset is random - Add code to read in values from “.txt” file and properly insert them in an initially empty doubly linked list and binary search tree. Run your program on different values of n by doing the following: Read in the first 20,000 entries only found in “.txt” file. Read in the first 50,000 entries only found in “.txt” file. Read in the first 100,000 entries only found in “.txt” file. Do an in-order traversal of the tree, printing out the contents to a file (named “unsorted”) to verify that you indeed built the data structure correctly. Note the time to build the data structure. For each n, you must run it at least 3 times and then take the average. Include all timings as well as the average in your report.
I need to answer in C,
I have the following code for a binary search tree data structre and need to:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct node *BSTREE;
struct node
{
int d;
BSTREE lft;
BSTREE rgt;
};
BSTREE insert(BSTREE r, int num);
BSTREE find(BSTREE root, int number);
void inOrderTraversal(BSTREE sbtree, FILE *fp);
int main()
{
BSTREE r = NULL;
r = insert(r,20000);
insert(r,50000);
insert(r, 100000);
BSTREE result = find(r,45);
if (result == NULL)
printf("Element 45 is not present in tree\n");
else
printf("Element 45 is present in tree\n");
FILE *fp = fopen("sorted.txt","w");
inOrderTraversal(r,fp);
fclose(fp);
return 0;
}
BSTREE insert(BSTREE r, int num)
{
if (r == NULL)
{
BSTREE newNode = (BSTREE)malloc(sizeof(struct node));
newNode->d=num;
newNode->lft= NULL;
newNode->rgt= NULL;
return newNode;
}
if (num < r->d)
r->lft=insert(r->lft,num);
else if(num > r->d)
r->rgt=insert(r->rgt,num);
return r;
}
BSTREE find(BSTREE r, int num)
{
if (r == NULL || r->d == num)
return r;
if (r->d < num)
return find(r->rgt,num);
else
return find(r->lft,num);
}
void inOrderTraversal(BSTREE sbtree, FILE *fp)
{
if (sbtree != NULL)
{
inOrderTraversal(sbtree->lft,fp);
fprintf(fp,"%d ",sbtree->d);
inOrderTraversal(sbtree->rgt,fp);
}
}
A) Add code to insert the numbers 1 to n in that order in an initially empty binary search tree.
- Run it on different values of n where :
- n = 20,000
- n = 50,000
- n = 100,000
- Do an in-order traversal of the tree, printing out the contents to a file (named “sorted”) to verify that you indeed built the data structure correctly.
- Note the time for each
program executions to just build the data structure (i.e., don’t include printing in the time) and include the timings in your report. For each n, you must run it 3 times and then take the average time. Don’t forget to submit all your data though. - Report the results of the analyzed
algorithms for each data structure using the Big-O and the timings.
B) Dataset is random - Add code to read in values from “.txt” file and properly insert them in an initially empty doubly linked list and binary search tree.
-
- Run your program on different values of n by doing the following:
- Read in the first 20,000 entries only found in “.txt” file.
- Read in the first 50,000 entries only found in “.txt” file.
- Read in the first 100,000 entries only found in “.txt” file.
- Run your program on different values of n by doing the following:
- Do an in-order traversal of the tree, printing out the contents to a file (named “unsorted”) to verify that you indeed built the data structure correctly.
- Note the time to build the data structure. For each n, you must run it at least 3 times and then take the average. Include all timings as well as the average in your report.
Step by step
Solved in 2 steps with 3 images