Traverses the tree (javascript object) recursively and calls function for each node