Question

Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...

Question 2:

Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following:
The number of comparisons made to search for a given integer in the BST
And
The number of comparisons made to search for the same integer in the array


Question 3
Run the program developed in Question 2 ten times. The given values for each time are 90, 49, 100, 30, 75, 79, 25, 5, 15, and 55 respectively. Use the results of these ten runs to produce a chart to represent these ten values vs. the number of comparisons made to search for these values in the array and the BST respectively.

integers.txt file:

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
7
8
9
10
11
12
13
14
38
39
40
41
42
43
44
45
1
2
3
4
5
6
66
67
68
69
70
71
72
73
91
92
93
94
95
96
97
98
99
100
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

Homework Answers

Answer #1

Here is the solution to above problem in C++. Please read the code comments for more information

Please give a thumbs up!!!

C++ code

#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include<string>
using namespace std;


int cmp=0;
class tnode
{
       public:
   int data;
   tnode * left;
   tnode *right;

       tnode(int data=0,tnode * left=NULL, tnode *right=NULL)
       {
           this->data=data;
           this->left=NULL;
           this->right=NULL;
       }
};

//inorder
void inorder(tnode *root)
{
   if(root==NULL)
       return;
   inorder(root->left);
   cout<<root->data<<" ";
   inorder(root->right);
}

//insert the data
   tnode * insert(tnode *root,int data )
   {
       if(root==NULL)
       {
           root = new tnode(data);
           return root;
          
       }
      
       if(data>root->data)  
           root->right = insert(root->right,data);
       else if(data<=root->data)
           root->left=insert(root->left,data);
       return root;
   }
  
   //search the tree for number of nodes and return the number of comparisions
   void search(tnode *root,int value)
   {
       if(root==NULL)
       {
           cmp++; //comparision increased
           return;
       }
       if(root->data==value)
       {   cmp++;//comparision increased
           return;
       }
       if(root->data>value)
       {   cmp++;
           return search(root->right,value);
       }
       else
       {
           cmp++;
           return search(root->left,value);
       }
   }


int main()
{
   ifstream fin("integers.txt");
   tnode *root=NULL;
   int num;
   //read the input file;
   if(!fin)
       {
           cout<<"CANNOT READ FILE\n";
           return 0;
       }
   while(fin>>num)
   {n
       root=insert(root,num);
   }
   cout<<"TREE IS CREATED\n";
   inorder(root);
   cout<<"\nCHART FOR COMPARISIONS\n";
   int v[]={90,49,100,30,75,79,25,5,15,55};
   cout<<"VALUE"<<setw(20)<<"COMPARISIONS\n";
   for(int i=0;i<10;++i)
   {
   cmp=0;
   search(root,v[i]);
   cout<<v[i]<<setw(20)<<cmp<<endl;
   }  
   return 0;
}

Screenshot of output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
In C++ Write a simple program to generate random integers between X and Y and store...
In C++ Write a simple program to generate random integers between X and Y and store them in a file, one number per line. The user should input the number of elements to generate and the boundary numbers X and Y (e.g. the inputs 100, 0, 999 mean your program should generate a list of 100 integers with values between 0 and 999, inclusive).
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for...
create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name i.e. jSmith). Next it will need to read three integer values that will represent the 3 e xam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below is an...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints the number of integer values in the file. Your program's input will be a string with the name of the file. If the file does not exist, then the program must print: Error opening the file For example, given the following CSV file input1.csv: 1,10,20,30,40 The output of your program must be: 5 You can safely assume that the input file will be a...
1) Write a C program to print the prime numbers from 1 to 100. (A prime...
1) Write a C program to print the prime numbers from 1 to 100. (A prime integer is any integer that can be divided evenly only by itself and 1) Requirement: Use an array to take the number from 1 to 100 and another array to take the prime number.
(a) Write a function in C++ called readNumbers() to read data into an array from a...
(a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of doubles The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows is exceeded. (b) Write a program with the...
Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose...
Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose input is taken by user, the array is passed to a functions named i.e. smallest(int A[20) and largest(int B[20]) to determine minimum and maximum values respectively. Also create a function named modify(int *p) which modifies the value at the index given by user.
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
Use C++ 1 a)Write a console program which creates an array of size 100 integers. Then...
Use C++ 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT