Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started