Solved by Expert Tutors
Hello. Can someone please help me with this?
Solved by Expert Tutors
Question

Hello. Can someone please help me with this? C++Write template function called smallestmm

 that has two parameters: the first is a vector containing elements of the template type, the second is an integer referred to as .

The function should return a vector that contains the m smallest values in the function's vector parameter, in ascending order.

If the size of the vector parameter is less than or equal to m, the result should contain all the values in the vector parameter (and no more). That is, that the size of the result should be minimum of m and the vector parameter size.

Requirements

  • Your solution must use an object of your heap template class
  • The elements in the vector parameter may be unordered and you must not sort them, you are also not allowed to use the STL sort algorithm to sort the results vector
  • Other than using a heap object, the function is independent of your heap, class - it is not a heap class method
  • Your solution should run in at most O(n log m) time, where n is the size of the vector parameter

Example

input: {10,5,2,8,6,4,3,9,1} m = 4

output: {1,2,3,4}

this is my heap class, it is a max heap.

template <typename Comparable>

class Heap {

private:

int* heap;

int len = 0;

int elements = 0;

Comparable value;

//helper for destructor

void deleteAll();

//helper for insert

void shiftUp(int index);

//helper for remove

void shiftDown(int index);


public:

//constructor

Heap(int n);

//copy constructor

Heap(const Heap& h);

//destructor

~Heap();

//void insert(int data);

//insert

void insert(Comparable value);

//print

void printHeap();

//remove

Comparable remove();

Comparable peek();

int size();

Heap merge(const Heap& h);


bool empty();

};

Step-by-step answer

dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet,

a molestie consequ

et, consectetur a

icitur laoreet. N

sum dolor sit amet,

, consectetur adipiscing elit. Nam l

a. Fusce dui lectu

acinia pulvinar torto

o

e vel laoreet ac, di

usce dui lectus,

a molestie consequ

o

ipsum dolor sit amet, c

nec facilisis. Pe

lestie con

o

ongue vel laoreet ac, dictum vitae odio. Donec aliquet. Lore

s a molestie consequat, ultrices ac magna. Fusce dui lectu

tesque dapibus efficitur laoreet. Nam risus ante, dapibus a mo

a mole

icitur laoreet. Nam ri

ur laoreet. Nam risus ante,

rem ipsum dolor sit amet, consectetur adipiscing elit. Nam l

ur laoreet. Nam risus ante,

cing elit. Nam lacinia pulvinar tortor nec fa

ongue vel laoreet ac, dictum vitae odio. Donec aliquet.

ac, dictum vitae odio. Donec aliquet. Lorem ipsum d

molestie consequat, ultrices ac magna. Fusce dui lectus,

gue vel laoreet ac, dictum vitae odio. Donec aliquet

sum dolor sit amet, consectetur adipiscing elit. Nam lacini

fficitur laoreet. Nam

ng elit. Nam lacinia pulvinar tortor nec facilisis. Pellent

acinia pulvinar tortor nec facilisis. Pelle

e vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsu

tesque dapibus efficitur laoreet. Nam risu

lestie consequat, ultrices ac magna. Fusce

or nec facilisis. Pellentesque

, dictum vitae odio. Donec aliqu

at, ultrices ac magna. Fusce

u

ec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing eli

nec facilisis. Pe

risus ante, dapibus a

o

Fusce dui lectus, con

a molestie consequat,

, dictum vitae odio. Donec aliqu

o

acinia pulvinar tortor n

nec facilisis. Pe

facilisis. Pellentesque dap

o

ipiscing elit. Nam lacinia pulvina

amet, co

ac, dictum vitae odio. Donec aliquet. Lorem ip

ipsum dolor sit amet, c

sum dolor

at, ultrices ac magna. Fusce dui lectus, congu

or nec facilisis. Pe

ctum vitae odio. Donec aliquet

molestie consequat, ultr

, ultrices ac magna. Fusce dui lectus, congue vel laoreet

iscing elit. Nam lacinia pulvinar tortor nec facilisis. Pel

amet, co

lestie consequat, ultrices ac magna. Fusce dui lectu

ur laoreet. Nam risus

sum dolor

o

ec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

amet, consectetur adipiscing elit.

nec facilisis. Pe

, dictum vitae odio. Donec aliquet. Lorem ips

o

gue vel laoreet ac, dictum vitae

iscing elit. Nam lacinia pulvinar tortor nec facilisis. Pel

amet, co

usce dui lectus, congue vel laoreet ac, dictum v

ur laoreet. Nam risus

sum dolor

o

lestie consequat, ultrices ac magna. Fusce dui lectus, cong

usce dui lectus

nec facilisis. Pe

e vel laoreet ac, dict

o

usce dui lectus, congue vel

fficitur laoreet. Nam risus ant

risus ante, dapibus a moles

amet, co

llentesque dapibus efficitur

consectetur adipiscing elit. Nam la

sum dolor

ce dui lectus, congue vel laoreet ac, dictum vitae odio. Do

iscing elit. Nam lacinia pulvinar tor

ctum vitae odio. Donec aliquet. Lorem ipsum

sum dolor sit amet,

or nec facilisis. Pellentes

nec facilisis. Pe

ce dui lectus, congue vel

o


llentesque dapibus efficitur laoreet. Nam risus ante, dapibu

o. Donec aliquet. Lorem ipsum dolor

nec facilisis. Pe

ctum vitae odio. Donec aliquet

o

ce dui lectus, congue vel laoree

rem ipsum dolor sit

o

lestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laore

sus ante, dapibus a molestie consequat, ultrices ac magna. Fus

nec facilisis. Pe

usce dui lectus, congue vel

o

ultrices ac magna. Fusce dui l

sque dapibus efficitur laoreet.

fficitur laoreet. Nam risu

nec facilisis. Pellentesque dapibus efficitur laoreet.

ia pulvinar tortor nec facili

risus ante, dapibus a molestie consequat, ultrices ac magna. F

e vel laoreet ac, dictum vit

a molestie consequat, ultri

amet, co

lestie consequat, ultrices ac magna. Fusce dui lectus, co

facilisis. Pellentesque dapibus e

sum dolor

o

ur laoreet. Nam risus

nec facilisis. Pe

ng elit. Nam lacinia pulvinar tortor nec f

o

dictum vi

Fusce dui lectus, con

onec aliquet. Lorem i

m ipsum dolor sit amet

amet, consectetur

at, ultrices ac magna. Fusce dui lectus, con

acinia pulvinar torto

o

amet, consectetur

usce

o

ongue

cing elit. Nam lacinia pulvinar tortor nec fa

m ipsum dolor sit amet, consect

o

molestie consequat, ultrices ac

usce

o

consectetur adipisc

rem ipsum dolo

o

entesque dapibus efficitur laoreet. Nam

onec aliqu

o

ac, dictum vitae odio. Donec aliquet. Lorem ipsum d

usce dui lectus, congue vel l

, consectetur adipiscing elit. Nam

m risus ante, dapibus

ipiscing elit. Na

o

Subscribe to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Let our 24/7 C++ Programming tutors help you get unstuck! Ask your first question.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes