حل شدہ اسائنمنٹس ڈانلوڈ کرنے کے لئے پہلے رجسڑ کریں پوسٹ کا جواب بھی دیا کریں جس اسائمنٹ کا حل نہ ملے اس کی سوالات کی فائل اپلوڈ کی کریں تاکہ اُس کا حل پوسٹ کیا جا سکے

## How to Write C++ program to implement min heap Data Structure Old Assignment With Solution

This Section is For Vu Old Solved Assignments From Instructors which will be help full for you finding your Solution.
Forum rules
Post Only Old Assignments Which are Solved By Instructors Thanks
Ali Raza Tawary
★★★★★★★★★★★★★ Posts: 190
Joined: Fri Dec 22, 2017 8:10 am
Location: Lahore
Contact:
Status: Offline

### How to Write C++ program to implement min heap Data Structure Old Assignment With Solution

Your program should fulfill requirements given below in points a) and b).

Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29

a) Consider the data given above and build min heap of odd nodes/values only using insert method you learned in handouts/video lectures.
b) Consider the data given above and build min heap of all nodes/values in data using buildHeap method given in handouts/video lectures.

Assignment solution instructions:
• Graphical representation of odd min heap using insert() method and min using buildHeap() method is given below for your understanding.
• Given data should be used for input to heap in the form of array.
• For Odd min heap construction use data given above while constructing min heap, check if the number/node is odd then use it for heap construction. In case of even discard it and move to next number.
Data after extracting odd nodes/values : 31, 85, 37, 23, 79, 47, 51, 97, 57, 29
b):
Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29

Answer is Given Below #include <iostream>
using namespace std;

class Heap {
public:
Heap (int capacity);
bool insert ( const int & x);
bool isEmpty ();
bool isFull();
void buildHeap(int * anArray, int n);
void traverse();

public:
int currentSize; // number of elements in heap
int * array; // the heap array
int capacity;
void percolateDown( int hole );
};

Heap::Heap (int capacity) {
array = new int[capacity+1];
currentSize = 0;
}

bool Heap::insert ( const int & x) {
if(isFull()) {
cout <<endl<<"cannot insert Heap is full!!" << endl;
return 0;
}

int hole = ++currentSize;
for(/* declaration is above*/ ; hole>1 && x< array[hole/2]; hole /= 2) {
array[hole]= array[hole/2];
}
array[hole] = x;
}

void Heap::percolateDown(int hole) {
int child;
int temp = array[hole];
for ( /*nothing*/ ; hole * 2 <=currentSize; hole = child) {
child = hole*2;
if (child !=currentSize && array[child+1] < array[child] )
child++;
if (array[child]< temp)
array[hole] = array[child];
else break;
}
array[hole] = temp;
}

void Heap::buildHeap(int * anArray, int n) {
for (int i = 1; i <= n ; i++ )
array = anArray[i-1];
currentSize = n;
for (int i =currentSize/2; i>0; i--)
percolateDown(i);
}

void Heap::traverse() {
for (int i = 1; i<=currentSize; i++)
cout <<" "<< array << "  ";
}

bool Heap::isEmpty() {
return currentSize == 0;
}

bool Heap::isFull() {
return currentSize==capacity;
}

main() {
int size = 16, arr[size] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
Heap heap(size);

for (int i = 0; i< size; i++)
if(arr % 2 == 1)  // if number is odd
heap.insert(arr); // insert number into heap one by one

cout << " Odd Min Heap using insert method : "<<endl;
heap.traverse();

heap.buildHeap(&arr,size);
cout << "\n Min Heap using build method: "<<endl;
heap.traverse();
} waqar88n
***New Member*** Posts: 5
Joined: Tue Jul 07, 2020 5:17 pm
Status: Offline

### Re: How to Write C++ program to implement min heap Data Structure Old Assignment With Solution

Ali Raza Tawary wrote:
Sun Mar 15, 2020 8:05 am
Your program should fulfill requirements given below in points a) and b).

Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29

a) Consider the data given above and build min heap of odd nodes/values only using insert method you learned in handouts/video lectures.
b) Consider the data given above and build min heap of all nodes/values in data using buildHeap method given in handouts/video lectures.

Assignment solution instructions:
• Graphical representation of odd min heap using insert() method and min using buildHeap() method is given below for your understanding.
• Given data should be used for input to heap in the form of array.
• For Odd min heap construction use data given above while constructing min heap, check if the number/node is odd then use it for heap construction. In case of even discard it and move to next number.
Data after extracting odd nodes/values : 31, 85, 37, 23, 79, 47, 51, 97, 57, 29
b):
Data: 18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29

Answer is Given Below #include <iostream>
using namespace std;

class Heap {
public:
Heap (int capacity);
bool insert ( const int & x);
bool isEmpty ();
bool isFull();
void buildHeap(int * anArray, int n);
void traverse();

public:
int currentSize; // number of elements in heap
int * array; // the heap array
int capacity;
void percolateDown( int hole );
};

Heap::Heap (int capacity) {
array = new int[capacity+1];
currentSize = 0;
}

bool Heap::insert ( const int & x) {
if(isFull()) {
cout <<endl<<"cannot insert Heap is full!!" << endl;
return 0;
}

int hole = ++currentSize;
for(/* declaration is above*/ ; hole>1 && x< array[hole/2]; hole /= 2) {
array[hole]= array[hole/2];
}
array[hole] = x;
}

void Heap::percolateDown(int hole) {
int child;
int temp = array[hole];
for ( /*nothing*/ ; hole * 2 <=currentSize; hole = child) {
child = hole*2;
if (child !=currentSize && array[child+1] < array[child] )
child++;
if (array[child]< temp)
array[hole] = array[child];
else break;
}
array[hole] = temp;
}

void Heap::buildHeap(int * anArray, int n) {
for (int i = 1; i <= n ; i++ )
array = anArray[i-1];
currentSize = n;
for (int i =currentSize/2; i>0; i--)
percolateDown(i);
}

void Heap::traverse() {
for (int i = 1; i<=currentSize; i++)
cout <<" "<< array << "  ";
}

bool Heap::isEmpty() {
return currentSize == 0;
}

bool Heap::isFull() {
return currentSize==capacity;
}

main() {
int size = 16, arr = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
Heap heap(size);

for (int i = 0; i< size; i++)
if(arr % 2 == 1)  // if number is odd
heap.insert(arr); // insert number into heap one by one

cout << " Odd Min Heap using insert method : "<<endl;
heap.traverse();

heap.buildHeap(&arr,size);
cout << "\n Min Heap using build method: "<<endl;
heap.traverse();
} Ali Raza Tawary (3), waqar88n (2)