Question: c + + Write a function that takes two heaps and merges them into one heap using the heap class provided. #ifndef HEAP _ H
c Write a function that takes two heaps and merges them into one heap using the heap class provided.
#ifndef HEAPH
#define HEAPH
#include
#include
using namespace std;
template
class Heap
public:
vector values;
int root
return ;
int last
return thisvalues.size;
int leftint index
return index ;
int rightint index
return index ;
int parentint index
return index ;
int size
return thisvalues.size;
void swapint indexOne, int indexTwo
TYPE temp valuesindexOne;
valuesindexOne valuesindexTwo;
valuesindexTwo temp;
bool hasLargerChildint index
return thisleftindex thissize &&
valuesindex valuesthisleftindex
thisrightindex thissize &&
valuesindex valuesthisrightindex;
int largerChildint index
ifthisrightindex thissize
return thisleftindex;
ifvaluesthisleftindex valuesthisrightindex
return thisleftindex;
else
return thisrightindex;
void print
forTYPE value : values
cout value ;
cout endl;
void insertTYPE value
values.pushbackvalue;
int newIndex thislast;
whilenewIndex &&
valuesnewIndex valuesthisparentnewIndex
thisswapnewIndex thisparentnewIndex;
newIndex thisparentnewIndex;
void remove
thisswapthisroot thislast;
values.popback;
int parentIndex largerChildIndex ;
whilethishasLargerChildparentIndex
largerChildIndex largerChildparentIndex;
swapparentIndex largerChildIndex;
parentIndex largerChildIndex;
TYPE read
return valuesthisroot;
;
#endif
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
