- Take the number of nodes in the heap and write that number down in binary.
- Erase the most significant bit.
- Starting at the root, go to the left child when you see a zero, and go to the right child when you see a one.
- When you run out of binary digits, you will be at the last node.
2 comments:
Trad-off the time to lookup the last node in heap rather than storing a pointer there? Probably not worth it.
But on a coolness rating (that's unintuitive to me at first glance) - 10/10 ;)
Agreed!
Post a Comment
Comments are moderated - please be patient while I approve yours.
Note: Only a member of this blog may post a comment.