CS301 Assignment 3 Solution Spring 2018
#1
CS301 - Data Structures 

Assignment 3 Solution

Semester: Spring 2018

C++ Code
Code:
#include <iostream>
#include <conio.h>
using namespace std;

class Heap {
    public:
        Heap(int capacity = 100);
        void insert(const int &n);
        void buildHeap(int* anArray, int n);
        void perculateDown(int hole);
        void printHeap(int size);
    private:
        int* array ;
        int capacity;
        int currentSize;
};

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

void Heap::insert(const int & x) {        
    int hole = ++currentSize;
    for(; hole > 1 && x < array[hole/2]; hole /= 2)
        array[hole] = array[hole / 2];
        array[hole] = x;
}

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

void Heap::perculateDown(int hole) {
    int tmp = array[hole];
    int child;
    for(; hole * 2 <= currentSize; hole = child) {
        child = hole * 2;
        if(child != currentSize && array[child+1] < array[child])
            child++;
        if(array[child] < tmp)
            array[hole] = array[child];
        else
            break;
        }
    array[hole] = tmp;
    return;
}

void Heap::printHeap(int size) {
    for(int i=1; i<=size; i++) {
        cout << array[i] << " ";
    }
}

int main() {
    
    int Array[] = {18, 31, 82, 85, 37, 20, 23, 79, 47, 51, 96, 97, 42, 94, 57, 29};
    Heap minHeap;
    for(int i=0; i<17; i++) {
        if(!(Array[i] % 2 == 0))
            minHeap.insert(Array[i]);
        
    }
    cout << "Odd Min Heap using insert method: " << endl;
    minHeap.printHeap(10);
        
    minHeap.buildHeap(Array,16);
    cout << "\nMin Heap using build method: " << endl;
    minHeap.printHeap(16);
    getch();
}


Note: Save the code in devC++ and press F11 to compile and run. Comment below if you face any errors.
#2
this code is just mini heap while in our program minimum heap with odd no using insert method and build up heap procedure code . it is not complete plz complete it
#3
(07-25-2018, 03:34 PM)rabia nawaz Wrote: this code is just mini heap while in our program minimum heap with odd no using insert method and build up heap procedure code . it is not complete plz complete it

So what is missing? Adding a check for odd number?
#4

.cpp   Untitled1.cpp (Size: 1.59 KB / Downloads: 9)
cs301 assignment correct solution here
#5
(07-26-2018, 08:45 PM)rabia nawaz Wrote: cs301 assignment correct solution here

Thank you, I've also found one and updated in the main post.
#6
Yar shi chz post kya krain plzzz
#7
(07-26-2018, 10:26 PM)Müdåssãr Råjpüt Wrote: Yar shi chz post kya krain plzzz

Is the solution shared above by Rabia not correct?
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  CS301 Assignment 1 Solution Fall 2018 Mishi Khan 1 145 7 hours ago
Last Post: Pakistani
  need GDB CS301 solution raja kamran 3 935 08-10-2018, 10:35 AM
Last Post: Pakistani
Thumbs Up required CS301 solution fahadmukhtar 3 1,343 07-25-2018, 01:49 PM
Last Post: Admin
  CS301 Assignment 2 Solution Spring 2018 Admin 1 2,046 05-28-2018, 10:10 PM
Last Post: rabia nawaz
  Required CS301 - Data Structures assignment solution please Javed Latif 0 1,225 05-08-2018, 12:47 PM
Last Post: Javed Latif
  CS301 Assignment 3 Solution Pakistani 2 4,435 07-09-2017, 07:13 PM
Last Post: Admin
  CS301 Assignment 2 Solution - Spring 2017 Admin 0 2,924 05-17-2017, 08:54 PM
Last Post: Admin
  CS301 Assignment 3 Solution Admin 8 4,520 01-21-2017, 02:25 PM
Last Post: Admin
  CS301 GDB solution Rubaisha(Moody Girl) 0 1,521 08-09-2016, 09:38 AM
Last Post: Rubaisha(Moody Girl)
  CS301 assignment1 text solution Nadeem Rana vu 1 1,328 05-07-2016, 06:00 PM
Last Post: Rubaisha(Moody Girl)



Users browsing this thread:
6 Guest(s)