如何确定轮的元素在树(锦标赛括号)

How to determinate round by element in tree (Tournament brackets)?

本文关键字:锦标赛 何确定 元素      更新时间:2023-10-16

假设我们有如下树:

1
    9
2
        13
3
    10
4 
            15
5
    11
6 
        14
7   
    12
8

元素(匹配):
1-8是第一轮
9-12是第2轮
13-14是第3轮

我如何确定元素"n"轮在这样的树?

我现在的公式是:

total_rounds = floor(log(totalTeams,2));
matches_per_round = (totalTeams / pow(2, current_round))
next_match_id = (totalTeams/2) + ceil(match_id/2)
total_matches = total_teams - 1

想象这棵树是倒序编号的

15
     7
14
         3
13 
     6
12 
             1
11
     5 
10 
         2
9   
     4
8

在这种情况下,它只是数字的对数,四舍五入。现在我们只需从轮数中减去这个数,就完成了。

reverse_number = total_matches - match_number + 1;
reverse_match_round = floor(log(reverse_number, 2));
match_round = total_rounds - match_round;

(注意,reverse_match_round实际上是0索引,不像match_round。然而,由于我们从total_rounds中减去它,保持它的形式比1索引更容易。如果您希望它以1为索引,只需将+1添加到最后两行。