Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fix constructHeap() function with as few changes as possible // Title: heap.cpp #include #include #include using namespace std; bool checkForHeap(vector < int > nums)

Please fix constructHeap() function with as few changes as possible

// Title: heap.cpp

#include

#include

#include

using namespace std;

bool checkForHeap(vector<int> nums) {

for (int i = 0; i <= (nums.size()-2)/2; i++) {

if (nums[2*i + 1] > nums[i]) {

return false;

}

if (nums[2*i + 2] > nums[i]) {

return false;

}

}

return true;

}

void constructHeap(vector<int> nums) {

int k, v, j;

bool heap;

for (int i = floor(nums.size()/2); i >= 0; i--) {

k = i;

v = nums[k];

heap = false;

while (!heap && (2*k <= nums.size())) {

j = 2*k;

if (j < nums.size()) {

if (nums[j] < nums[j+1]) {

j = j + 1;

}

}

if (v >= nums[j]) {

heap = true;

}

else {

nums[k] = nums[j];

k = j;

}

}

nums[k] = v;

}

for (int num : nums) {

cout << num << " ";

}

}

int main() {

int user_size, user_choice;

vector<int> nums;

cout << "Input size: ";

cin >> user_size;

cout << "Enter numbers: ";

for (int i = 0; i < user_size; i++) {

int num;

cin >> num;

nums.push_back(num);

}

if (!checkForHeap(nums)) {

cout << "This is NOT a heap." << endl;

cout << "Heap constructed: ";

constructHeap(nums);

cout << endl;

cout << "Select an operation" << endl;

cout << " 1. Insert a number" << endl;

cout << " 2. Delete the max" << endl;

cout << " 3. Heapsort & Quit" << endl;

cin >> user_choice;

while (user_choice == 1 || user_choice == 2 || user_choice == 3) {

if (user_choice == 1) {

cout << "Enter a number: ";

int insert_num;

cin >> insert_num;

nums.push_back(insert_num);

for (int i = 0; i < nums.size(); i++) {

cout << nums[i] << " ";

}

cout << endl;

cout << "Updated heap: ";

constructHeap(nums);

} /*else if (user_choice == 2) {

} else if (user_choice == 3) {

}*/

cout << "Select an operation" << endl;

cout << " 1. Insert a number" << endl;

cout << " 2. Delete the max" << endl;

cout << " 3. Heapsort & Quit" << endl;

cin >> user_choice;

}

}

else {

cout << "This is a heap." << endl;

cout << "Select an operation" << endl;

cout << " 1. Insert a number" << endl;

cout << " 2. Delete the max" << endl;

cout << " 3. Heapsort & Quit" << endl;

// cin >> user_choice;

// while (user_choice == 1 || user_choice == 2 || user_choice == 3) {

// if (user_choice == 1) {

// } else if (user_choice == 2) {

// } else if (user_choice == 3) {

// }

// cout << "Select an operation" << endl;

// cout << " 1. Insert a number" << endl;

// cout << " 2. Delete the max" << endl;

// cout << " 3. Heapsort & Quit" << endl;

// cin >> user_choice;

// }

}

system("pause");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

=+2 How does the preparation and support for each type of IE vary?

Answered: 1 week ago

Question

=+What is the extent of the use of each type of IE?

Answered: 1 week ago