def _remove_node(tt: TourneyTree, pos: Position) -> int: children = tt.get_children(pos) val = tt[pos] if len(children) == 0: tt[pos] = -1 return -1 if len(children) == 1: new_val = _remove_node(tt, children[0]) tt[pos] = new_val return new_val same_child = tt.get_same_child(pos) other_child = tt.get_sibling(same_child) other_val = tt[other_child] new_same_val = _remove_node(tt, same_child) if new_same_val == -1: tt[pos] = other_val return other_val tt[pos] = new_same_val if COMPARE(new_same_val, other_val) else other_val return tt[pos]
def _populate_row(tt: TourneyTree, row: int): for pos in tt.iter_row(row): children = tt.get_children(pos) if len(children) == 1: tt[pos] = tt[children[0]] continue if len(children) == 0: tt[pos] = -1 continue if len(children) != 2: raise Exception("should only be one or two children") tt[pos] = tt[children[0]] if COMPARE( tt[children[0]], tt[children[1]]) else tt[children[1]]