diff --git a/heap/ownHeap.cpp b/heap/ownHeap.cpp index 994cbe7..d2adb09 100644 --- a/heap/ownHeap.cpp +++ b/heap/ownHeap.cpp @@ -1,16 +1,136 @@ +#include +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< #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; @@ -63,4 +183,5 @@ struct Heap{ else SiftDown(i); } -}; \ No newline at end of file +}; +*/ \ No newline at end of file