Where is the tree structure derived? You mention you generated a hash, is this meant to be the hard coded tree? Otherwise, inevitably the first task is to parse the tree structure into some kind of usable Perl data structure. Maybe provide some more information.

(Update: I wrote a very rough script (not great) to demonstate some fundamental aspects i.e. recursive subroutine etc):