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
(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...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code...
Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code without testing it. I will downvote your answer and mark as spam.** I have posted this question several times, do not copy / paste the same code that has been posted, IT DOESN'T WORK!! In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a text file data_in.txt with the following information (highlight the piece of text below with numbers and copy it to a text file): 54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75 The 5 indicates that there are five integers...
Question 2: write a java program    A Hypermarket makes an offer for its customers according...
Question 2: write a java program    A Hypermarket makes an offer for its customers according to the number of items to be bought as follows: Number of purchased items Discount percentage Less than 25 2.5% 25 – 50 5.0% 51 – 100 6.25% Greater than 100 7.5% The hypermarket also adds 2% city taxes to the purchases after the sale discount. Reads from the user the number of items to be purchased and the item price. Calculates and prints...
PROGRAM PYHTON Write a function called "calculateInput" that when called, it asks the user to enter...
PROGRAM PYHTON Write a function called "calculateInput" that when called, it asks the user to enter an number. Then, it asks for a second number. These will use the "input" command. Store these as two separate variables. This function takes NO arguments and is also a VOID function, there is no return statement. The function should print the result of these two numbers multiplied together. The program should be able to multiply floats. For example: Test Input Result calculateInput() 5...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT