在树形选择排序中,利用锦标赛思想建立的树称为胜者树。

1、每个非终端节点存储的是左右孩子节点中的优胜者。

2、通过减少比较次数,提高效率。

3、胜者树就是一颗特殊的线段树。

winner tree 胜者树-LMLPHP

一、构建树

winner tree 胜者树-LMLPHP

Procedure buildmint;  复杂度 O(n)
var i,j,k:longint;
begin
  i:=N; j:=N+n-; { 其中 N 为大于等于 n 的最小 2 次方数 }
  while i> do
  begin
    k:=i;
    while k<j do
    begin
      f[k div ]:=min(f[k],f[k+]);
      k:=k+;
    end;
    if j and = then f[j div ]:=f[j];
    i:=i div ; j:=j div ;
  end;
end;

二、查询

核心思想:自下向上,奇偶缩进。

Function findmin(x,y:longint):longint;  复杂度 O(logn)
var i,j,k,r:longint;
begin
  i:=x+N-; j:=y+N-; r:=i;
  while i<=j do
  begin
    if (f[r]>f[i]) then r:=i;
    if (f[r]>f[j]) then r:=j;
    if (i and =) then inc(i);
    if (j and =) then dec(j);
    i:=i shr ; j:=j shr ;
  end; exit(f[r]);
  while r<N do
  if (f[r]=f[r*]) then r:=r* else r:=r*+;
  exit(r-N+);
end;

三、更新

核心思想:自下而上,更新父节点。

胜者树不擅长更新,因为,某个结点更新的时候,它还需要跟兄弟结点比较。更新频繁的情况,用败者树。

四、败者树

败者树是胜者树的一种变体。在败者树中,用父结点记录其左右子结点进行比赛的败者,而让胜者参加下一轮的比赛。败者树的根结点记录的是败者,需要加一个结点来记录整个比赛的胜利者。采用败者树可以简化重构的过程。

典型应用:多路归并排序,可以减少比较次数。

winner tree 胜者树-LMLPHP

五、代码实现胜者树

poj2328 http://poj.org/problem?id=2823

Description

An array of size n ≤ 10 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is [1 3 -1 -3 5 3 6 7], and k is 3.

Window positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

#include <iostream>
#include <cmath> using namespace std; const int MX = ; int n, k;
int a[MX], minRes[MX], maxRes[MX];
int treeMin[*MX], treeMax[*MX];
int N; //构建一颗winner tree,tree用数组存储,根结点是数组的第二个结点,即A[1]
//叶子结点是原始数组。树的最左结点的下标是2的指数
void build() {
int i = N, j = N+n-;
while (i > ) {
int t = i;
while (t < j) {
treeMin[t/] = min(treeMin[t], treeMin[t+]);
treeMax[t/] = max(treeMax[t], treeMax[t+]);
t += ;
}
if ((j&) == ) {//下标j是偶数,说明这个结点是落单的
treeMin[j/] = treeMin[j];
treeMax[j/] = treeMax[j];
}
i = i>>;
j = j>>;
}
} int query(int x, int y) {
int i=x+N, j=y+N, t = i;
while (i <= j) {
if (treeMin[t] > treeMin[i]) t = i;
if (treeMin[t] > treeMin[j]) t = j;
if ((i&) == ) i++;
if ((j&) == ) j--;
i = i>>;
j = j>>;
}
//return treeMin[t];
while (t < N) {
if (treeMin[t] == treeMin[t*]) t = t*;
else t = t*+;
}
return treeMin[t];
//return t - N;
} void work() {
for (int i=; i<n-k+; i++) {
int x=N+i, y=x+k-, t=x, r=x;
while (x<=y) {
if (treeMin[t] > treeMin[x]) t = x;
if (treeMin[t] > treeMin[y]) t = y;
if (treeMax[r] < treeMax[x]) r = x;
if (treeMax[r] < treeMax[y]) r = y;
if ((x&) == ) x++;
if ((y&) == ) y--;
x = x>>;
y = y>>;
}
minRes[i] = treeMin[t];
maxRes[i] = treeMax[r];
}
} void printRes() {
for (int i=; i<n-k+; i++) {
if (i!=) cout<<" ";
cout<<minRes[i];
}
cout<<endl;
for (int i=; i<n-k+; i++) {
if (i!=) cout<<" ";
cout<< maxRes[i];
}
cout<<endl;
} int calN(int x) {
int i = ;
while (i < x) {
i = i<<;
}
return i;
} void printTree() {
cout << "min winner tree:" << endl;
for (int i=; i< n+N; i++) {
cout << treeMin[i] << " ";
}
cout << endl << "max winner tree:" << endl;
for (int i=; i<n + N; i++) {
cout << treeMax[i] << " ";
}
cout << endl;
} int main()
{
cin >> n >> k;
N = calN(n);
for (int i=; i<n; i++) {
cin >> a[i];
treeMax[N+i] = treeMin[N+i] = a[i];
} build(); //printTree(); work();
printRes(); //cout<<"query:"<< query(0, 2)<<endl; //cout << "Hello world!" << endl;
return ;
}
05-06 21:13