full HEAP in detail {LOVE BABBAR}
Mon Nov 11 2024 10:09:59 GMT+0000 (Coordinated Universal Time)
Saved by @E23CSEU1151
#include <iostream> #include <queue> using namespace std; class heap { public: int arr[100]; int size; heap() { arr[0] = -1; size = 0; } void insert(int val) { size = size + 1; int index = size; arr[index] = val; while (index > 1) { int parent = index / 2; if (arr[parent] < arr[index]) { swap(arr[parent], arr[index]); index = parent; } else { return; } } } void print() { for (int i = 1; i <= size; i++) { cout << arr[i] << " "; } cout << endl; } void deletefromHeap() { if (size == 0) { cout << "nothing to delete " << endl; return; } // Step 1: Replace root with last element arr[1] = arr[size]; size--; // Step 2: Take root to its correct position int i = 1; while (i <= size) { int leftIndex = 2 * i; int rightIndex = 2 * i + 1; int largest = i; // Check if left child exists and is greater if (leftIndex <= size && arr[largest] < arr[leftIndex]) { largest = leftIndex; } // Check if right child exists and is greater if (rightIndex <= size && arr[largest] < arr[rightIndex]) { largest = rightIndex; } // If largest is still the parent node, break the loop if (largest == i) { break; } // Swap with the largest child and continue down the heap swap(arr[i], arr[largest]); i = largest; } } }; // Heapify function to maintain max heap property void heapify(int arr[], int n, int i) { int largest = i; int left = 2 * i; int right = 2 * i + 1; if (left <= n && arr[largest] < arr[left]) { largest = left; } if (right <= n && arr[largest] < arr[right]) { largest = right; } // If the largest element is not the parent node if (largest != i) { swap(arr[largest], arr[i]); heapify(arr, n, largest); } } // Heap Sort function to sort the array void heapSort(int arr[], int n) { int size = n; // While the heap size is greater than 1 while (size > 1) { // removed semicolon here // Step 1: Swap the last element with the root swap(arr[size], arr[1]); // Step 2: Reduce heap size by 1 size--; // Call heapify on the reduced heap heapify(arr, size, 1); } } int main() { heap h; h.insert(6); h.insert(54); h.insert(57); h.insert(59); h.insert(58); h.print(); // Delete the root of the heap h.deletefromHeap(); cout << "After deleting root: "; h.print(); // Example array to demonstrate heapify and heap sort int arr[6] = {-1, 54, 53, 55, 52, 50}; int n = 5; // Build the heap for (int i = n / 2; i > 0; i--) { heapify(arr, n, i); } cout << "PRINTING THE ARRAY AFTER HEAPIFY" << endl; for (int i = 1; i <= n; i++) { cout << arr[i] << " "; } cout << endl; // Perform heap sort heapSort(arr, n); cout << "Array after heap sort: "; for (int i = 1; i <= n; i++) { cout << arr[i] << " "; } cout << endl; cout << "priority queueu"<< endl; // max heap priority_queue<int> pq; pq.push(6); pq.push(10); pq.push(57); pq.push(5); cout << "element at top "<<pq.top() << endl; pq.pop(); cout << "element at top "<<pq.top() << endl; cout << "size of "<<pq.size() << endl; if(pq.empty()){ cout << "empty" << endl; } else{ cout << "not empty" << endl; } // Min heap using priority_queue with greater comparator priority_queue<int, vector<int>, greater<int>> minheap; // Adding elements to the min heap minheap.push(10); minheap.push(33); minheap.push(43); minheap.push(67); minheap.push(75); // Displaying the top element in the min heap cout << "Element at top: " << minheap.top() << endl; // Removing the top element minheap.pop(); // Displaying the new top element cout << "Element at top after pop: " << minheap.top() << endl; // Displaying the size of the min heap cout << "Size of minheap: " << minheap.size() << endl; // Checking if the min heap is empty if (minheap.empty()) { cout << "Minheap is empty" << endl; } else { cout << "Minheap is not empty" << endl; } }
Comments