views:

103

answers:

1

I'm working on developing a tournament bracket for Single Elimination tournaments. However, I cannot seem to get the bracket to display properly.

I need an implementation of a 'perfect binary tree' I believe. (or maybe complete since byes are a factor?)

Currently, I have this:

<?php
   $teams = 8;
   $num_rounds = round(log($teams, 2)) + 1;

   for ($i = 0; $i < $num_rounds; ++$i)
   {
    $matches = $teams * pow(.5, $i - 1) / 2;
    for ($j = 0; $j < $matches; ++$j)
    {
     echo "<div style=\"border-style: inset;\"><span>Round $i Match $j</span></div>\n";
    }
   }
?>

You can view it here. I'm using the Frank Mich jQuery Binary Tree plugin to display the data, but as I said before I believe I need a binary tree in order to display it correctly.

If there's a better way, or I'm just doing it wrong, what would the solution be?