Build a binary search tree for messages