0% found this document useful (0 votes)
41 views

Naïve Approach-: 2) O (K) Time. The Total Time Complexity Will Be O (K)

The document discusses deletion and finding the kth smallest element in a min heap. It provides two approaches: (1) Naively deleting and extracting k times which takes O(klogn) time, and (2) Reducing the heap to k levels and extracting k times which takes O(k^2) time and is more efficient if n >> k. It then provides C++ code to find the kth smallest element using the second approach by reducing the heap size and extracting k times.

Uploaded by

Sia Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Naïve Approach-: 2) O (K) Time. The Total Time Complexity Will Be O (K)

The document discusses deletion and finding the kth smallest element in a min heap. It provides two approaches: (1) Naively deleting and extracting k times which takes O(klogn) time, and (2) Reducing the heap to k levels and extracting k times which takes O(k^2) time and is more efficient if n >> k. It then provides C++ code to find the kth smallest element using the second approach by reducing the heap size and extracting k times.

Uploaded by

Sia Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

A min heap is a binary tree in which the parent is less than or equal to child node.

Deletion in min heap-

Since deleting an element at any intermediary position in the heap can be costly, so we can simply
replace the element to be deleted by the last element and delete the last element of the Heap.

 Replace the root or element to be deleted by the last element.

 Delete the last element from the Heap.

 Since, the last element is now placed at the position of the root node. So, it may not follow
the heap property. Therefore, heapify the last node placed at the position of root.

Heapify in min heap-

Heapify is the process of converting a binary tree into a heap data structure.

After each deletion process, the heap gets distorted, so we need to heapify it for each deletion.

A kth least element can be extracted from a min heap by the following two approaches-

1. Naïve Approach-

We will extract the minimum element k times and the last element will be the kth least element.

Each deletion operation will take O(log n) times, so the total time complexity will be O(k * log n).

2. Efficient Approach-

In a min-heap, an element x at ith level has i – 1 ancestors. By the property of min-heaps, these i – 1
ancestors will be smaller than x. This means that x cannot be among the least i – 1 elements of the
heap. Using this property, we can conclude that the kth least element can have a level of at most k.

The size of the min-heap is reduced such that it has only k levels.

We can then obtain the kth least element by our previous strategy of extracting the minimum
element k times.

Since, the heap is reduced to a maximum of 2k – 1, therefore each heapify operation will take O(log
2k) = O(k) time. The total time complexity will be O(k2).

If n >> k, then this approach performs better than the previous one.

For finding the 3rd smallest element, k=3, so we will perform deletion 3 times and extract the 3rd
deleted element.

Following is the C++ implementation of finding the kth smallest element-

Source- https://www.geeksforgeeks.org/kth-least-element-in-a-min-heap

// C++ program to find k-th smallest


// element in Min Heap using k levels
#include <iostream>
using namespace std;
// Structure for the heap
struct Heap {
vector<int> v;
int n; // Size of the heap

Heap(int i = 0)
: n(i)
{
v = vector<int>(n);
}
};

// Generic function to
// swap two integers
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}

// Returns the index of


// the parent node
inline int parent(int i)
{
return (i - 1) / 2;
}

// Returns the index of


// the left child node
inline int left(int i)
{
return 2 * i + 1;
}

// Returns the index of


// the right child node
inline int right(int i)
{
return 2 * i + 2;
}

// Maintains the heap property


void heapify(Heap& h, int i)
{
int l = left(i), r = right(i), m = i;
if (l < h.n && h.v[i] > h.v[l])
m = l;
if (r < h.n && h.v[m] > h.v[r])
m = r;
if (m != i) {
swap(h.v[m], h.v[i]);
heapify(h, m);
}
}

// Extracts the minimum element


int extractMin(Heap& h)
{
if (!h.n)
return -1;
int m = h.v[0];
h.v[0] = h.v[h.n-- - 1];
heapify(h, 0);
return m;
}

int findKthSmalles(Heap &h, int k)


{
h.n = min(h.n, int(pow(2, k) - 1));
for (int i = 1; i < k; ++i)
extractMin(h);
return extractMin(h);
}

int main()
{
Heap h(7);
h.v = vector<int>{ 10, 50, 40, 75, 60, 65, 45 };
int k = 2;
cout << findKthSmalles(h, k);
return 0;
}

Output:
40

You might also like