I don't understand where the function f at the end comes from? How does it relate to the tree and node indices and weights?