Question: 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: There's an error in the code that I can't spot that's leaving

#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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!