0

I have a question and it goes like this:

There is a Max heap binary tree.

Let's assume that in the Heap there are (2^2017)-2017 nodes at the bottom-most level.

A)How many levels are there in the heap?

B)What are the number of the Leaves in the heap?

Thanks

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Jack
  • 21
  • 1
  • 3
  • "(2^2017)-2017 nodes at the bottom-most level". Doesn't this mean those are the leaves? Have you attempted this question? If yes, show your method and what you think the problem is with it. – Stuti Rastogi May 15 '17 at 10:44
  • That's what i was thinking but it seems too simple of an answer. Maybe it's a trick question... I'm not sure how to answer A though. I'm guessing that the correct answer would be "A minimum of 2016 levels" – Jack May 15 '17 at 10:47
  • Anyone got any ideas? – Jack May 15 '17 at 11:42
  • A full heap with three levels has 2^(3-1) nodes at at the bottom-most (leaf) level. A heap with four levels has 2^(4-1) nodes at the leaf level. Your proposed heap with (2^2017)-2017 leaves means that the leaf level is not completely full. (If it were full, it would have 2^2017 nodes.) In any case, that heap would have 2018 levels. – Jim Mischel May 15 '17 at 14:40
  • @JimMischel Thank you for the answer. And do you agree that the number of leaves is 2^2017-2017? – Jack May 15 '17 at 17:03

1 Answers1

1

A full binary heap that has 2x nodes at the bottom-most level has (x+1) levels. Consider this heap:

    1
 2     3
4 5   6 7

It has 4 (22) nodes at the bottom level, and 3 levels.

If the bottom level is full, then the number of leaf nodes is the same as the number of nodes on the bottom level.

In your case, the bottom level has (22017)-2017 leaf nodes. We know that the maximum number of nodes possible on this level is 22017 (because maximum is always a power of 2). And we also know, from the example above, that there are 2018 levels in the tree.

The number of leaf nodes, however, is not (22017)-2017. Consider this heap:

         1
     2       3
   4   5   6   7
  8 9

It has two leaf nodes at the bottom level, and three leaf nodes at the level above the bottom level (5, 6, and 7).

I think you'll find that in your case, the number of leaf nodes is (22017)-2017 + 2017/2. The 2017/2 is the number of leaf nodes on the level above.

Riz
  • 6,486
  • 19
  • 66
  • 106
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • 1
    First off, thank you very much for such a detailed response! I'd like to ask a few things: 1)I believe you meant to say that A full binary heap that has 2^x nodes at the bottom has x levels, correct? 2)At the end you said that the number of leaf nodes is 2^2017-2017+(2017/2). But that is an odd number when each level, if its complete, has to have an even number. Could you please explain to me how you got to this number? How did you figure out that this is the leaf node amount? – Jack May 15 '17 at 23:10
  • I meant to say that 2017/2 is a Rational number. – Jack May 15 '17 at 23:22
  • There are 2017 nodes missing from the bottom row. There are 1008 nodes on the level above that have no children, and one node that has one child. When working with integers, 2017/2 is 1008. So the number of leaf nodes is (2^2017)-2017+1008. – Jim Mischel May 16 '17 at 02:55
  • Got it. Thanks again, Jim, you helped a lot. – Jack May 16 '17 at 08:49