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.
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
C++
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, ?, ?, ?]
Step by step
Solved in 3 steps