Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fix constructHeap() function in my code // Title: heap.cpp #include #include #include using namespace std; bool checkForHeap(vector nums) { for (int i = 0;

Please fix constructHeap() function in my code

// Title: heap.cpp #include #include #include using namespace std;

bool checkForHeap(vector 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 nums) { int k, v, j; bool heap; for (int i = floor(nums.size()/2); i >= 1; 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 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

Step: 3

blur-text-image

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

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

ANALYZE in detail three basic ca uses of accidents.

Answered: 1 week ago