Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There's an error in the code that I can't spot that's leaving spaces. Please help! Output below: #include #include #include using namespace std; void inputHeap(vector

There's an error in the code that I can't spot that's leaving spaces. Please help! Output below: image text in transcribed

#include

#include #include using namespace std; void inputHeap(vector &heap); void print(vector v); bool maxHeap(vector heap); void heapify(vector &heap); void inputCommand(vector &heap); void insert(vector &heap, int node_val); void deleteNode(vector &heap, int node_val); void update(vector &heap, int node_index, int new_val); void displayMax(vector heap); void deleteMax(vector &heap);

int main() { vector heap; inputHeap(heap); if (maxHeap(heap)) { cout

inputCommand(heap); return 0; }

void inputHeap(vector &heap) { int cycles; cin >> cycles; heap.resize(cycles + 1); int input; for (int x = 1; x > input; heap[x] = input; } }

void print(vector v) { for (int x = 1; x

bool maxHeap(vector heap) { int parent; int l_child; int r_child; for (int x = heap.size() / 2; x > 0; x--) { parent = x; l_child = x * 2; r_child = x * 2 + 1; if (l_child heap[parent]) { return false; } } if (r_child heap[parent]) { return false; } } } return true; } void heapify(vector &heap) { int parent; int currVal; bool is_heap; int l_child; int r_child;

while (!maxHeap(heap)) { for (parent = heap.size() / 2; parent > 0; parent--) { is_heap = false; l_child = 2 * parent; r_child = 2 * parent + 1;

while (!is_heap && l_child = heap[l_child]) { is_heap = true; } else { swap(heap[parent], heap[l_child]); } } } } } } void insert(vector &heap, int node_val) { heap.push_back(node_val); heapify(heap); } void deleteNode(vector &heap, int node_val) { for (int x = 1; x &heap, int node_index, int new_val) { heap[node_index] = new_val; heapify(heap); }

//function displays maximum value in the heap void displayMax(vector heap) { cout &heap) { heap[1] = heap.back(); heap.pop_back(); heapify(heap); } void inputCommand(vector &heap) { int num_commands; cin >> num_commands;

string command; int node_val; int update_val;

for (int x = 0; x > command;

if (command == "insert") { cin >> node_val; insert(heap, node_val); } else if (command == "delete") { cin >> node_val; deleteNode(heap, node_val); } else if (command == "update") { cin >> node_val >> update_val; update(heap, node_val, update_val); } else if (command == "display") { print(heap); } else if (command == "displayMax") { displayMax(heap); } else if (command == "deleteMax") { deleteMax(heap); } } }

Expected output? 1 This is NOT a heap. 399 Actual output ? 1ThisisNOTaheap.399 Difference

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

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

Explain why a business might wish to acquire another business.

Answered: 1 week ago