def ladderize(self, direction=<NUM_LIT:0>):
if not self.is_leaf():<EOL><INDENT>n2s = {}<EOL>for n in self.get_children():<EOL><INDENT>s = n.ladderize(direction=direction)<EOL>n2s[n] = s<EOL><DEDENT>self.children.sort(key=lambda x: n2s[x])<EOL>if direction == <NUM_LIT:1>:<EOL><INDENT>self.children.reverse()<EOL><DEDENT>size = sum(n2s.values())<EOL><DEDENT>else:<EOL><INDENT>size = <NUM_LIT:1><EOL><DEDENT>return size<EOL>
Sort the branches of a given tree (swapping children nodes)
according to the size of each partition.