Question

In: Computer Science

(C++. Please don't use #include <bits/stdc++.h>) This lab exercises your understanding of using a Heap to...

(C++. Please don't use #include <bits/stdc++.h>) This lab exercises your understanding of using a Heap to create a Priority Queue

Follow these steps:

  • Declare an integer array to use for the heap (size of 20 to 30 elements)
  • Create an insertion function (and any needed helper functions) that creates a min-heap (smallest priority is most important)
  • Create a remove function (and any needed helper functions) that removes the min value from the heap and returns it
  • In main, call the insertion function with at least 15 random values
  • In main, call the remove function for all values and print them to the screen (format the numbers so they all fit in a single screen – perhaps 5 to 10 values on each line?)
  • Repeat the insertion and remove/print for a second set of random values before ending the program

Solutions

Expert Solution

#include <iostream>

using namespace std;

int arr[100], n;

void display()
{
int i;
if (n == 0)
{
printf("Heap is empty\n");
return;
}
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
} /*End of display()*/

void insert(int num, int loc)
{
int par;
while (loc > 0)
{
par = (loc - 1) / 2;
if (num <= arr[par])
{
arr[loc] = num;
return;
}
arr[loc] = arr[par];
loc = par;
} /*End of while*/
arr[0] = num; /*assign num to the root node */
} /*End of insert()*/

void del(int num)
{
int left, right, i, temp, par;

for (i = 0; i < n; i++)
{
if (num == arr[i])
break;
}
if (num != arr[i])
{
printf("%d not found in heap\n", num);
return;
}
arr[i] = arr[n - 1];
n = n - 1;
par = (i - 1) / 2; /*find parent of node i */
if (arr[i] > arr[par])
{
insert(arr[i], i);
return;
}
left = 2 * i + 1; /*left child of i*/
right = 2 * i + 2; /* right child of i*/
while (right < n)
{
if (arr[i] >= arr[left] && arr[i] >= arr[right])
return;
if (arr[right] <= arr[left])
{
temp = arr[i];
arr[i] = arr[left];
arr[left] = temp;
i = left;
}
else
{
temp = arr[i];
arr[i] = arr[right];
arr[right] = temp;
i = right;
}
left = 2 * i + 1;
right = 2 * i + 2;
} /*End of while*/
if (left == n - 1 && arr[i] < arr[left]) /* right==n */
{
temp = arr[i];
arr[i] = arr[left];
arr[left] = temp;
}
} /*End of del()*/

int main()
{

insert(10, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(20, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(40, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(33, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(26, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(56, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(90, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(34, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(25, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;
insert(78, n);
n = n + 1;
cout << "insert:";
display();
cout << endl;

del(10);
cout << "Delete 10:";
display();
cout << endl;
del(20);
cout << "Delete 20:";
display();
cout << endl;
del(40);
cout << "Delete 40:";
display();
cout << endl;
del(33);
cout << "Delete 33:";
display();
cout << endl;
del(26);
cout << "Delete 26:";
display();
cout << endl;
del(56);
cout << "Delete 56:";
display();
cout << endl;
del(90);
cout << "Delete 90:";
display();
cout << endl;
del(34);
cout << "Delete 34:";
display();
cout << endl;
del(25);
cout << "Delete 25:";
display();
cout << endl;
del(78);
cout << "Delete 78:";
display();
cout << endl;
}

***********************************************************************************************************************************

In case of any doubt do ask in the comment section


Related Solutions

PS: Please don't use #include <bits/stdc++.h>. Visual studio compatible please. Write a C++ program to find...
PS: Please don't use #include <bits/stdc++.h>. Visual studio compatible please. Write a C++ program to find least common multiple (LCM) of two, three and four integer values. The integer values are entered from the keyboard and the outputs are printed to the console. The LCM of two, three and four integer values are computed using Prime factorization method. You have to use arrays to hold input values and use functions/methods to get data from the keyboard, display output to the...
write in C++ as simple as possible please and thanks don't use header file <bits/stdc++.h> All...
write in C++ as simple as possible please and thanks don't use header file <bits/stdc++.h> All programs work with binary numbers which are powers of 2 (2, 4, 8, …).  Write a program that will have a user enter a number (n) less than 10,000. Then have the program put each power of 2 (int) into an array up to and possibly including the number n. This means you don't know at the start how large the array will be, so...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() {...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() { char repeat = 'Y'; for (;repeat == 'Y';){ char empname[222]; float basicSalary, h_r_a, DearnessAllow, tax, netSalary; int e_id; cout<<"\nEmployee Name :"; cin>>empname; cout<<"\nEmployee Id :"; cin>>e_id; cout << "Enter Basic Salary : "; cin >> basicSalary; DearnessAllow = 0.30 * basicSalary; h_r_a= 800; switch (1) { case 1: if (basicSalary <= 2,50,000) tax = 0; case 2: if (basicSalary > 250000 && basicSalary <=...
Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence)...
Please write code in C++ and include all header files used not bits/stdc: (Same number subsequence) Write an O(n) program that prompts the user to enter a sequence of integers ending with 0 and finds longest subsequence with the same number. Sample Run Enter a series of numbers ending with 0: 2 4 4 8 8 8 8 2 4 4 0 The longest same number sequence starts at index 3 with 4 values of 8
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /*     This code closely mimics the Python hash table we created in class this     week. Each "person" is defined by their name, zipcode, and their pet's name.     Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int); //---------------------------------------------------------------------------- /* define a "person" --------------------*/ class person{     public:     // variables that define a person     string name;     int zipcode;...
Using your language, don't use any sites (by your understanding), write about the difference between the...
Using your language, don't use any sites (by your understanding), write about the difference between the letter of credit and letter of guarantee Word Limit for the assignment: Upper word limit: 1200 words Lower word limit: 600 words
please don't use handwriting .. use your own words i need unique answer .. please don't...
please don't use handwriting .. use your own words i need unique answer .. please don't copy and paste Q1.Dala Corporation is considering a project, which will involve the following cash inflows and (out) flows Details Amount Initial Outlay SAR (400000) After 1 Year SAR 40000 After 2 Years SAR 300000 After 3Years SAR 300000 What will be the NPV of this project if a discount rate of 15% is used? _________________ Q2.Merck Inc. is about to undertake a project...
Please, i need Unique answer, Use your own words (don't copy and paste). *Please, don't use...
Please, i need Unique answer, Use your own words (don't copy and paste). *Please, don't use handwriting. *Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting.*Please, don't use handwriting. _______________ Solve the following questions Q1 Construct a cumulative frequency distribution of the 20 brain volumes(cm3) listed below. Use the classes 900-999, 1000-1099,...
Please, i need Unique answer, Use your own words (don't copy and paste). *Please, don't use...
Please, i need Unique answer, Use your own words (don't copy and paste). *Please, don't use handwriting. *Please, don't use handwriting.* *Please, don't use handwriting. *Please, don't use handwriting.**Please, don't use handwriting. *Please, don't use handwriting.**Please, don't use handwriting. *Please, don't use handwriting.* * i need References URL Link pleasssse help me i need the answer Critically appraise the following cross-sectional study given in the link below: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5989365/ Discuss the strength and limitations of the study in a brief manner...
Please, i need Unique answer, Use your own words (don't copy and paste). Please, don't use...
Please, i need Unique answer, Use your own words (don't copy and paste). Please, don't use handwriting, Use your keyboard. Q. 2. In present scenario, the importance of microeconomics is increasing day by day, in your          Opinion, what are the three ways that we can use macroeconomic analysis.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT