Fix heap tree bugs, and add some tests that exercise heap reading, writing and merging.