Search code examples
algorithmheightelementheapsortheap-size

Work out height of heap from elements


I am trying to work out the height of a heap given there are 210 elements.

I have tried using the log(n+1) -1 formula however I am getting a weird answer. I also tried drawing it the heap out and that way I get that it would be a height of 8.

Thanks in advance.


Solution

  • İf your heap is binary, the formula is like that, otherwise you can look at https://cs.stackexchange.com/questions/9914/finding-the-height-of-a-d-ary-heap.

    height = ⌈log(base2)(n+1)⌉-1
    
    height = ⌈log(base2)(200+1)⌉-1
    
    height = ⌈7,..⌉-1
    
    height = 8-1
    
    height = 7