I found the other answers confusing so I decided to explain it with an actual example heap.
Assume original heap is size N and you want to find the kth largest elements,
This solution takes O(klogk) time and O(k) space.
10
/ \
5 3
/ \ /\
4 1 2 0
Original Heap, N = 7
Want to find 5th largest element. k = 5
Note: In the new heap, you need to store the pointer to the original heap.
This means, you do not remove or change the original heap. The original heap is read-only. Therefore, you never have to do any operations that requires O(logN) time.
Let x' be the pointer to value x in the original heap.
1st Iteration: Get root node's pointer into new heap
Step 1: Add pointer to node 10
10'
New Heap, size = 1, root = 10', root->left = 5, root right->3
Print the 1st largest element = 10
2nd iteration: Refer to the original heap and insert both it's children into the new heap. (Storing the pointers to them and not the value themselves). The reason you want to store the pointer is so that you can access them in O(1) from the original heap later to search for their children instead of O(N) to search for where that value is located in the original heap.
Step 2a: Look for left child of root node of new heap from the original heap.
Add a pointer for the left child (in this case, 5') to the new heap.
10'
/
5'
New Heap, size = 2, root = 10', root->left = 5, root right->3
Step 2b: Look for right child of root node of new heap from the original heap.
Add a pointer for the left child (in this case, 3') to the new heap.
10'
/ \
5' 3'
New Heap, size = 3, root = 10', root->left = 5, root right->3
Step 2c: Remove root node from New Heap.
(Swap max node with right most leave, remove root node and bubble down current root to maintain heap property)
10' swap 3' remove & bubble 5'
/ \ => / \ => /
5' 3' 5' 10' 3'
New Heap, size = 2, root = 5', root->left = 4, root right->1
Print the 2nd largest element = 5
Step 3a: Look for left child of root node of new heap from the original heap.
Add a pointer for the left child (in this case, 4') to the new heap.
5'
/ \
3' 4'
New Heap, size = 3, root = 5', root->left = 4, root right->1
Step 3b: Look for right child of root node of new heap from the original heap.
Add a pointer for the left child (in this case, 1') to the new heap.
5'
/ \
3' 4'
/
1'
New Heap, size = 4, root = 5', root->left = 4, root right->1
Step 3c: Remove root node from New Heap.
(Swap max node (5') of New Heap with its right most leave from the original heap (1') from the New Heap, remove root node and bubble down current root to maintain heap property)
5' Swap 1' remove & bubble 4'
/ \ => / \ => / \
3' 4' 3' 4' 3' 1'
/ /
1' 5'
New Heap, size = 3, root = 4', root->left = NULL, root right->NULL
Print the 3rd largest element = 4
Step 4a & Step 4b does nothing since root node does not have any children in this case from original heap.
Step 4c: Remove root node from New Heap.
(Swap max node with right most leave, remove root node and bubble down current root to maintain heap property in New Heap)
4' Swap 1' remove & bubble 3'
/ \ => / \ => /
3' 1' 3' 4' 1'
New Heap, size = 2, root = 3', root->left = 2, root right->0
Print the 4th largest element = 3
Step 5a: Look for left child of root node of new heap from the original heap.
Add a pointer for the left child (in this case, 2') to the new heap.
3'
/ \
1' 2'
New Heap, size = 3, root = 3', root->left = 2, root right->0
Step 5b: Look for right child of root node of new heap from the original heap.
Add a pointer for the left child (in this case, 0') to the new heap.
3'
/ \
1' 2'
/
0'
New Heap, size = 4, root = 3', root->left = 2, root right->0
Step 5c: Remove root node from New Heap.
(Swap max node (3') with its right most leave from the original heap (which is 0') in the New Heap, remove root node and bubble down current root to maintain heap property in New Heap)
3' Swap 0' Remove & Bubble 2'
/ \ => / \ => / \
1' 2' 1' 2' 1' 0'
/ /
0' 3'
New Heap, size = 3, root = 2', root->left = NULL, root->right = NULL
Print the 5th largest element = 2
Finally, since we have gone through k iterations, k = 5. We can now extract the root element's value from the new heap. In this case, the value is 2.
Therefore, we have found the kth largest value from the original heap.
Time Complexity, T(N,k) = O(klogk)
Space Complexity, S(N,k) = O(k)
Hope this helps!
Soon Chee Loong,
University of Toronto.