Refactored to make hash function typesafe

Hash function is not a type parameter of the tree.
This is to ensure that two trees with different hash functions
cannot be merged.