Skip to content

Commit

Permalink
Update heap class based implementation c++
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik committed Jul 19, 2021
1 parent 2f7b684 commit 67b58a0
Showing 1 changed file with 125 additions and 4 deletions.
129 changes: 125 additions & 4 deletions heap/ownHeap.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,136 @@
#include <bits/stdc++.h>
using namespace std;
class Heap
{
private:
int capacity, heapType;
int size = -1;
int *heap;

public:
// type -> 0 -> minHeap
// type -> 1 -> maxHeap
// considering 0 based indexing
Heap(int heapsize)
{
capacity = heapsize;
heap = new int[capacity];
// heapType = type;
}
bool empty()
{
return size == -1;
}
int parent(int i)
{
return (i - 1) / 2;
}
int leftChild(int i)
{
return 2 * i + 1;
}
int rightChild(int i)
{
return 2 * i + 2;
}
void siftUp(int i)
{
while (i > 0 && heap[parent(i)] < heap[i])
{
swap(heap[parent(i)], heap[i]);
i = parent(i);
}
}
void siftDown(int i)
{
int maxIndex = i;
int l = leftChild(i);
if (l <= size && heap[l] > heap[maxIndex])
{
maxIndex = l;
}
int r = rightChild(i);
if (r <= size && heap[r] > heap[maxIndex])
{
maxIndex = r;
}
if (i != maxIndex)
{
swap(heap[i], heap[maxIndex]);
siftDown(maxIndex);
}
}
void insert(int element)
{
if (size == capacity - 1)
{
cout << "Heap is full\n";
return;
}
size += 1;
heap[size] = element;
siftUp(size);
}
void remove(int i)
{
if (size == -1)
{
cout << "Heap is empty\n";
return;
}
else if (i > size - 1)
{
cout << "Index out of bound\n";
return;
}
heap[i] = INT_MAX;
siftUp(i);
extractMax();
}
int extractMax()
{
int res = heap[0];
heap[0] = heap[size];
size -= 1;
siftDown(0);
return res;
}
int heapSize()
{
return size + 1;
}

// int extractMin(){}
};
int main()
{
Heap p(4);
p.insert(10);
p.insert(20);
p.insert(30);
p.insert(2);
p.insert(29);
// cout<<p.heapSize()<<" \n";
while (!p.empty())
{
cout << p.extractMax() << " ";
}
return 0;
}

/*
#include<bits/stdc++.h>
#define INF INT_MAX;
#define maxSize 1000;
using namespace std;
/* Max Heap Class based Implementation */
struct Heap{
int maxSize;
int size; /*size of the heap*/
int size;
int H[maxSize];
int parent(int i)
{
return i/2;/*one based indexing*/
return i/2;
}
int leftChild(int i)
return 2*i;
Expand Down Expand Up @@ -63,4 +183,5 @@ struct Heap{
else
SiftDown(i);
}
};
};
*/

0 comments on commit 67b58a0

Please sign in to comment.