# Tournament Tree (Winner Tree) and Binary Heap

Given a team of N players. How many minimum games are required to find second best player?

We can use adversary arguments based on tournament tree (Binary Heap).

Tournament tree is a form of min (max) heap which is a complete binary tree. Every external node represents a player and internal node represents winner. In a tournament tree every internal node contains winner and every leaf node contains one player.

There will be N – 1 internal nodes in a binary tree with N leaf (external) nodes. For details see this post (put n = 2 in equation given in the post).

It is obvious that to select the best player among N players, (N – 1) players to be eliminated, i.e. we need minimum of (N – 1) games (comparisons). Mathematically we can prove it. In a binary tree I = E – 1, where I is number of internal nodes and E is number of external nodes. It means to find maximum or minimum element of an array, we need N – 1 (internal nodes) comparisons.

Second Best Player

The information explored during best player selection can be used to minimize the number of comparisons in tracing the next best players. For example, we can pick second best player in (N + log2N – 2) comparisons. For details read this comment.

The following diagram displays a  tournament tree (winner tree) as a max heap. Note that the concept of loser tree is different.

The above tree contains 4 leaf nodes that represent players and have 3 levels 0, 1 and 2. Initially 2 games are conducted at level 2, one between 5 and 3 and another one between 7 and 8. In the next move, one more game is conducted between 5 and 8 to conclude the final winner. Overall we need 3 comparisons. For second best player we need to trace the candidates participated with final winner, that leads to 7 as second best.

Median of Sorted Arrays

Tournament tree can effectively be used to find median of sorted arrays. Assume, given M sorted arrays of equal size L (for simplicity). We can attach all these sorted arrays to the tournament tree, one array per leaf. We need a tree of height CEIL (log2M) to have atleast M external nodes.

Consider an example. Given 3 (M = 3) sorted integer arrays of maximum size 5 elements.

```{ 2, 5, 7, 11, 15 } ---- Array1
{1, 3, 4} ---- Array2
{6, 8, 12, 13, 14} ---- Array3```

What should be the height of tournament tree? We need to construct a tournament tree of height log23 .= 1.585 = 2 rounded to next integer. A binary tree of height 2 will have 4 leaves to which we can attach the arrays as shown in the below figure.

After the first tournament, the tree appears as below,

We can observe that the winner is from Array2. Hence the next element from Array2 will dive-in and games will be played along the winner path of previous tournament.

Note that infinity is used as sentinel element. Based on data being hold in nodes we can select the sentinel character. For example we usually store the pointers in nodes rather than keys, so NULL can serve as sentinel. If any of the array exhausts we will fill the corresponding leaf and upcoming internal nodes with sentinel.

After the second tournament, the tree appears as below,

The next winner is from Array1, so next element of Array1 array which is 5 will dive-in to the next round, and next tournament played along the path of 2.

The tournaments can be continued till we get median element which is (5+3+5)/2 = 7th element. Note that there are even better algorithms for finding median of union of sorted arrays, for details see the related links given below.

In general with M sorted lists of size L1, L2 … Lm requires time complexity of O((L1 + L2 + … + Lm) * logM) to merge all the arrays, and O(m*logM) time to find median, where m is median position.

Select smallest one million elements from one billion unsorted elements:

As a simple solution, we can sort the billion numbers and select first one million.

On a limited memory system sorting billion elements and picking the first one million seems to be impractical. We can use tournament tree approach. At any time only elements of tree to be in memory.

Split the large array (perhaps stored on disk) into smaller size arrays of size one million each (or even smaller that can be sorted by the machine). Sort these 1000 small size arrays and store them on disk as individual files. Construct a tournament tree which can have atleast 1000 leaf nodes (tree to be of height 10 since 29 < 1000 < 210, if the individual file size is even smaller we will need more leaf nodes). Every leaf node will have an engine that picks next element from the sorted file stored on disk. We can play the tournament tree game to extract first one million elements.

Total cost = sorting 1000 lists of one million each + tree construction + tournaments

Implementation

We need to build the tree (heap) in bottom-up manner. All the leaf nodes filled first. Start at the left extreme of tree and fill along the breadth (i.e. from 2k-1 to 2k – 1 where k is depth of tree) and play the game. After practicing with few examples it will be easy to write code. We will have code in an upcoming article.

Related Posts

— by Venki. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

# Company Wise Coding Practice    Topic Wise Coding Practice

25 Comments Category: Heap

Software Engineer

Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.

• prashant jha

for median of sorted array using tournament tree
http://ideone.com/aqUeRy

• prashant jha

here is my implementation for median of sorted array using tournament tree

#include
#define infinity 999
using namespace std;
struct tnode
{
tnode* lchild;
int data;
tnode* rchild;
int *aptr;
int ind;
tnode()
{
lchild=NULL;
aptr=NULL;
ind=0;
rchild=NULL;
}
};

tnode* create(tnode* root,int *s1,int *s2,int *s3,int level,int &cnt)
{
if(level==0)
{
root=new tnode();
if(cnt==1)
{
root->aptr=s1;
cnt++;
return root;
}
else if(cnt==2)
{
root->aptr=s2;
cnt++;
return root;
}
else if(cnt==3)
{
root->aptr=s3;
cnt++;
return root;
}
else
return root;
}
root=new tnode();
root->lchild=create(root->lchild,s1,s2,s3,level-1,cnt);
root->rchild=create(root->rchild,s1,s2,s3,level-1,cnt);
return root;
}
void fun(tnode* root,int level,int n)
{
if(level==0)
{
if((root->ind>=n)||(root->aptr==NULL))
{
root->data=infinity;
return;
}
else
{
root->data=root->aptr[root->ind];
return;
}
}
fun(root->lchild,level-1,n);
fun(root->rchild,level-1,n);
root->data=min(root->lchild->data,root->rchild->data);
}
void display(tnode* root)
{
if(root)
{
display(root->lchild);
cout<data<rchild);
}
}
void get_roottoleaf(tnode* root,int k,int level)
{
if(level==0)
{
if(root->data==k)
{
root->ind++;
return;
}
else
return;
}
get_roottoleaf(root->lchild,k,level-1);
get_roottoleaf(root->rchild,k,level-1);
}
int main()
{
tnode* root=NULL;
int s1[]={2,5,8,12};
int s2[]={1,3,4,6};
int s3[]={7,9,10,11};
int level=2,cnt=1;
int n=sizeof(s1)/sizeof(s1[0]);
root=create(root,s1,s2,s3,level,cnt);
for(int i=0;idata,level);
}
cout<data<<" is the median of the sorted array.n";
return 0;
}

• danny

For second best player we need to trace the candidates participated with final winner, that leads to 7 as second best.

It must be 5 instead of 7 please admin correct me if I am wrong, This is the last line to find the second best player.

• danny

For second best player we need to trace the candidates participated with final winner, that leads to 7 as second best.

It must be 5 instead of 7 please admin correct me if I am wrong, This is the last line to find the second best player.

• geeky

does it means we first make a max heap and then backtrack to find the initial competitor of the 1st best player….it would be great if you post the code..

• Nitin

can u provide me basic code for tournament trees

• levis

@venki nice post !gives more clarity to the problem !!

• vinit

nice

• Algoseekar

@venki plz post code for above

“The above tree contains 4 leaf nodes that represent players and have 3 levels 0, 1 and 2. Initially 2 games are conducted at level 2, one between 5 and 3 and another one between 7 and 8. In the next move, one more game is conducted between 5 and 8 to conclude the final winner. Overall we need 3 comparisons. For second best player we need to trace the candidates participated with final winner, that leads to 7 as second best.”

above explanation concludes 7 as second best.. is this right ?? or it should be 5 ??

• @slimshady, why confusion? The candidates participated with the winner are 5 and 7. So, 7 is next higher number (next best player).

@venki: thanks for clarifying. I was under impression since 5 played with 8 at an higher level(say semi-finals) it made him runner up n next best player after 8..

@venki: sorry for my last comment.. i meant since 5 played with 8 at an higher level(say finals) it made him runner up n next best player after 8..

• bbqabbq

the problem description of “second best player” is vague, since 5 and 7 are all losers, what is the second best?

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```
• abc

In the median of sorted array solution: how is “In general with M sorted lists of size L1, L2 … Lm requires time complexity of O(max(L1, L2 … Lm) * logM).” calculated? Thanks!

• @abc, thanks for pointing. I have updated the post.

can someone explain
“The tournaments can be continued till we get median element which is (5+3+5)/2 = 7th element.”

how can we use tournament tree to calculate median ?

thanks

• abc

there are 3 arrays of sizes 5,3, and 5 respectively. So total number of elements would be 5+3+5 = 13. the median of 13 elements would be 7th element when arranged in sorted order.
So we play tournament (loser tree) to find the the 7th min element

• @abc, The concept of loser tree is different, same thing I emphasized in the post. We covered only winner tree here. In the median of sorted arrays case the winner is an element with smaller numerical value.

In otherwords, elements with smaller values get high priority in the game, and still the winner moves up.

In a winner tree whatever may be the winner, whether smaller value number wins or larger value numbers wins, only the winner moves up. In case of loser tree, both the loser and winner moves up in the tree. We will cover it later.

• abc

@Venki: Ahh ok! Will wait for the post