Well... for handling the state of the tree, you can
name your tree limbs 000 001 002 003 etc in order, which
is basically your key into an array or something, so when
it is clicked you can either expand it, collapse it or
view its contents. Then a child of 002 is 002000 002001 etc,
which makes it easy to maintain hierarchy. Then just maintain
your state in either hidden fields or a cookie, or in a file
or DB on the server. It's not terribly hard to maintain
if you do it right, but I might be misinterpreting your question.