Question

what is the worst case run time when finding the maximum value in a binary min...

what is the worst case run time when finding the maximum value in a binary min heap(implemented using array ) containing N elements?
worst case run time:
explain:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The worst case run time when finding the maximum value in a binary min heap(implemented using array ) containing N elements

A heap is a tree with some exceptional properties. The essential prerequisite of a heap is that the estimation of a hub must be ≥ (or ≤) to the estimations of its child. This is called heap property.

A heap likewise has the extra property that all leaves ought to be at h or h-1 levels (where h is the tallness of the tree) for some h>0 (complete binary trees). That implies heap ought to shape a complete tree (as demonstrated as follows).

In the beneath illustrations, the left tree is a heap (every component/element is more prominent than its children) and the right tree is not a heap (since 11 is more noteworthy than 2).

Sorts of Heaps?

In light of the heap property we can order the heap into two sorts:

· Min heap: The estimation of a node must be not exactly or equivalent to the estimations of its children.

· Max heap: The estimation of a node must be more noteworthy than or equivalent to the estimations of its children.

binary Heaps

In the binary heap, every node may have up to two youngsters. By and by, a binary heap is sufficient and we focus on binary min-heap and binary max heap for outstanding discussion .

Representing Heaps

Before taking a gander at heap operations, let us perceive how to represent to the heap. One probability is utilizing array. Since heap is shaping finished binary trees, there won't be any wastage of areas. For the underneath exchange let us accept that components i.e. element are put away in exhibits which begin at record 0. The binary max heap can be spoken to as:

Note: For the rest of the talk let us expect that we are doing controls in max heap.

Existing Solutions

For a given min heap the most extreme component/element will dependably be at leaf as it were. Presently, the following inquiry is how to discover the leaf node in the tree?

In the event that we deliberately watch, the following node of last components parent is the principal leaf node. Since the last component is dependably at h->count-1 area, the following node of its (parent at area (h->count-1)/2) can be figured as:

( h->count-1)/2+1 = (h->count+1)/2

Presently, the main stride remaining is examining the leaf node and finding the most extreme among them.

int FindMaxInMinHeap(struct Heap *h) {

int Max = - 1;

for(int i = (h→count+1)/2; i < h→count; i++)

if(h→array[i] > Max)

Max = h→array[i];

}

This would give the time Complexity: O(n/2)=O(n)

Proposed Solution with O(1) Time Complexity

According to the past discussion, we know that minimum component will be in leaf node as it were. In view of this property, we can make an assistant heap with min heap properties.

Expect that the first max-heap is called OrigMaxHeap and the assistant min-heap is named AuxMinHeap. Take note of that AuxMinHeap is with min heap properties.

Since a heap is a total i.e complete binary tree, a load with n components will have most extreme of n/2+1 leaves and n/2 inside hubs. This implies AuxMinHeap will have the size equivalent to half of the measure of OrigMaxHeap. Along these lines, on the off chance that we build a pile with leaf notes of OrigMaxHeap, the base component will dependably be at the root.

Operations with alterations to support this:

Insertion : We have to embed to OrigMaxHeap and embed to AuxMinHeap if there is any change to the leaf notes of OrigMaxHeap amid its addition/insertion.

This would take O(logn) + O(logn/2)

Delete : We have to erase from OrigMaxHeap and erase from AuxMinHeap if there is any change to the leaf notes of OrigMaxHeap amid its inclusion/INSERTION

This would take O(logn) + O(logn/2)

MIN: Just return root element/component of AuxMinHeap with O(1) time-complexity

This would take O(1)

Add a comment
Know the answer?
Add Answer to:
what is the worst case run time when finding the maximum value in a binary min...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT