C++ PROGRAMMING: HEAPS Using the array-based implementation, complete the heap ADT in the arrayheap.h. void insert(int num) This will insert the num to the heap. This number shall be initially added in the last position and will constantly be compared to its parent and get swapped if the parent is greater. int removeMin() This will remove the root by it being replaced by the last position. Remember to store it to some temporary variable first and return it later. The replaced number will also have to be constantly compared to its children and swapped by the smaller element if it is smaller than the number. int size() This will simply return the number of elements currently in the heap.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
icon
Concept explainers
Question

C++ PROGRAMMING: HEAPS

Using the array-based implementation, complete the heap ADT in the arrayheap.h.

  • void insert(int num)

This will insert the num to the heap. This number shall be initially added in the last position and will constantly be compared to its parent and get swapped if the parent is greater.

  • int removeMin()

This will remove the root by it being replaced by the last position. Remember to store it to some temporary variable first and return it later. The replaced number will also have to be constantly compared to its children and swapped by the smaller element if it is smaller than the number.

  • int size()

This will simply return the number of elements currently in the heap.

 

Given that this is an array, it must also be dynamic. If the array is full when attempting to add an element, increase the capacity of the memory by 50% (rounded up). For instance, the array of capacity 4 is already of size 4, we add 4 * 50% = 2 when we try to add an element. And so, the array should now have capacity 6 and size 5 by the end of the process.

If the elements in the array after any removal has less than 2/3 of the capacity, reduce the memory allocation by 1/3 (rounded down). For instance, we try to remove an existing element in the array with capacity 6 and current size 4, the size after the remove is 3, therefore less than 2/3 that of 6. Hence, we reduce the memory allocation by 6 * 1/3 = 2 so 6-2 = 4 is now its new capacity. NOTE that the minimum capacity of the array must be four (4).

main.cpp

#include <iostream>
#include "arrayheap.h"
using namespace std;
int main(void) {
    Heap* heap = new ArrayHeap();
    int test;
    cin >> test;

    switch (test) {

     case 1:

            heap->insert(7);
            heap->print();
            heap->insert(9);
            heap->print();
            cout << heap->removeMin() << endl;
            cout << heap->size() << endl;
            heap->print();
            break;

  return 0;
}

heap.h

class Heap {
    public:
        virtual void insert(int num) = 0;
        virtual int removeMin() = 0;
        virtual int size() = 0;
        virtual void print() = 0;
};

arrayheap.h

#include <iostream>
#include "heap.h"
using namespace std;
class ArrayHeap : public Heap {
    int* array;
    int capacity;
    int index;

    public:
        ArrayHeap() {
            capacity = 4;
            index = 0;
            array = (int*) malloc( capacity * sizeof(int) );
        }

        void insert(int num) {
            //DO THIS
        }

        int removeMin() {

             //DO THIS
            return 0;
        }

        int size() {

              //DO THIS
            return 0;
        }

        // DO NOT MODIFY this method.
        void print() {
            cout << "[";
            for (int i = 0; i < capacity; i++) {
                if (i < index) {
                    cout << *(array + i);
                } else {
                    cout << "?";
                }
                
                if (i != capacity - 1) {
                    cout << ", ";
                }
            }
            cout << "]" << endl;
        }
};

EXPECTED OUTPUT

[7, ?, ?, ?]

[7, 9, ?, ?]

7

1

[9, ?, ?, ?]

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY