These are standard heap algorithms whose operation should be easily recognisable.  The build loop runs over the set of non-leaf nodes, implicitly accessing both children which are automatically at double the index.

The timeout is simply there to elide the overhead of maintaining the heap when hard dropping hasn't been necessary for a while.

- Jonathan Morton