Refactor tree structure to use arity

Instead of hardcoded branches, the tree structure now uses the arity
property to determine how many branches there are, it also implicitly
assumes that the first element in tree.branches is the leftmost branch.
This allows the tree contain any arity the function may have.