HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Searching through a heap complexity

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
complexitysearchingheapthrough

Problem

Pretend you want to search through a max-heap to find a specific element. I know there is no such option but still... Would it take worse case O(n) or O(logn) time? I am assuming O(n) since the property of a max-heap doesn't really help us unlike a bst. Thanks.

Solution

You are correct: it's $\Theta(n)$ in the worst case. Suppose you're looking for something that's no bigger than the smallest value in a max-heap. The max-heap property (that the value of every node is at least as big as everything in the subtree below it) gives you no useful information and you must check both subtrees of every node.

Context

StackExchange Computer Science Q#40591, answer score: 9

Revisions (0)

No revisions yet.