Numbering matches in tournament bracket

I have a system where I number each match in a tournament the following way:

round 1     |   round 2     |
------------|---------------|
·
[a vs b] -----------
(match 0)   ·       |
·       |
·   [a vs c] ------ [a winner]
·   (match 2)
·       |
·       |
[c vs d] -----------
(match 1)   ·
·


So matches are numbered from top to bottom, and left to right from 0 to N.
When a match is played and a winner is known, I need to know to which match the winner will be placed (Example: in Match 0, winner A will be directed to Match 2)

I've found that:

  next_match_number = (current_match_number + total_number_of_teams)/2


gives me the next match number that I need. Please note that I need to round down the result.
Example:

  next_match_number = (0+4)/2 = 2


Now for a different purpose, I need the Match Number where a Team may come from -> The previous match number.
For example, in match 2 I would like to know that its teams come from match 0 and match 1.

Obviously the same formula doesn`t work. I know that rounding down the result is a dirty trick...

-
just to clarify ... does 'the match number where a team may come from' refer to either of the 2 matches which the two players in a particular match were playing in in the previous round? So in match 2 you would want either match 0 or match 1 (or, perhaps better, both)? –  tom Mar 7 '12 at 8:23
That's it @tom, both matches would be what I need. I blame my english level ... –  clinisbut Mar 7 '12 at 8:24

Given a Match number $m$ and a total number of teams $T$, set
$$m_1 := 2(m-\lceil T/2\rceil)$$
and then, $m_2 := m_1+1$. Treating the cases where $T$ is even/odd, separately, it follows immediately from your formula that the match following both $m_2$ and $m_1$ is $m$.