Tournament Tree (Winner Tree)What is a tournament tree?A tournament tree is a form of complete binary tree in which each node denotes a player. The last level has n1 nodes (external nodes) used to represent all the players, and the rest of the nodes (internal nodes) represent either the winner or loser among them. It is also referred to as a Selection tree. Some of the important properties of the tournament tree are listed below:
Types of tournament trees:There exist a loser and a winner in every match. So, there are two methods to represent both ideas:
What is a winner tree?In a tournament tree, when the internal nodes represent the winner of the match, the tree obtained is referred to as the winner tree. Each internal node stores either the smallest or greatest of its children, depending on the winning criteria. When the winner is the smaller value then the winner tree is referred to as the minimum winner tree, and when the winner is the larger value, then the loser tree is referred to as the maximum winner tree. The tournament's winner is always the smallest or the greatest of all the players or values and can be found in O(1). The time needed to create the winner tree is O(Log N), where N represents the number of players. Q. Eight players participate in a tournament where a number represents each player, from 1 to 8. The pairings of these players are given below: Group A: 1, 3, 5, 7 (Matches: 1  7 and 3  5) Group B: 2, 4, 6, 8 (Matches: 2  6, and 4  8) Winning Criteria  The player having the largest value wins the match. Represent the winner tree (maximum winner tree) for this tournament tree. The winner tree (maximum winner tree) is represented below in the figure: Here, the tree formed is a max heap, the value of all the parents is either equal to or larger than its children, and the winner is 8, which is the largest of all the players. We can observe from this that every tournament tree is a binary tree, but the reverse is not true because, in the tournament tree, the winner is always equal to one of its children, but in binary heaps, the parent is not always equal to its children. What is a loser tree?In a tournament tree, when the internal nodes are used to represent the loser of the match between two, then the tree obtained is referred to as the loser tree. When the loser is the smaller value then the loser tree is referred to as the minimum loser tree, and when the loser is the larger value, then the loser tree is referred to as the maximum loser tree. It is also called the minimum or maximum loser tree. The same idea is also applied here, the loser (or parent) is always equal to one of its children, and the loser is always the greatest or smallest of all the players and can be found in O(1). Also, the time needed to create a loser tree is O(Log N), where N is the number of players. Q. Eight players participate in a tournament where a number represents each player, from 1 to 8. The pairings of these players are given below: Group A: 1, 3, 5, 7 (Matches: 1  7 and 3  5) Group B: 2, 4, 6, 8 (Matches: 2  6, and 4  8) Winning Criteria  The player having the largest value wins the match. Represent the loser tree (minimum loser tree) for this tournament tree. The loser tree (minimum loser tree) obtained is shown in the given figure: Here, the tree obtained is a minheap, the value of the parent is either smaller or equal to its children, and 1 is the overall loser of the tournament. Finding the secondbest player: In the winner tree, the winner is found at the top of the tree. Now, the problem is to find the secondbest player in the tournament. Here, we need to note that the secondbest player is only beaten by the best player. To find the secondbest player, we need to check the last two games (or comparison) with the best player. Let us see the above idea with an example. Here, 8 is the tournament winner, and when we look for the secondbest player, we find 7 is the secondbest player. Applications of a tournament tree:
How to find the median of Nsorted arrays using the Tournament tree? One of the applications of a tournament tree is finding the median of Nsorted arrays. The minimum winner tree is used to find the median efficiently. We place the array in the leaf of the tournament tree and the winners into the internal nodes. Let us understand this with an example: Assume that there are three sorted arrays as follows: A = {12, 14, 15, 16, 17, 19} B = {4, 5, 8, 10} C = {1, 3, 6, 7, 9} of sizes 6, 4, and 5 respectively. Sorted merged array = {1, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 17, 19} Total no. of elements = 15 Median = (N + 1) / 2 = (15 + 1) / 2 = 8. The eighth element is the median = 10. The height of the tree is calculated using Log_{2}(M) where N is the number of arrays. We take the upper bound of the calculated value. Here, Log_{2}(3) = 1.585 @ 2. So, the tree we need to construct will be a height of 2 with 4 leave nodes shown in the figure below. Now, the minimum winner tree of the first tournament tree will look like as below: Here, we need to note that in the last leave we have filled infinity so that the match against it will always be won by the element and when the elements in an array are exhausted, we fill infinity in that leave node. Here also, 1 from array C is the winner of the tournament. So, the next element = 3 from array C replaces it. The minimum winner tree of the second tournament will be like as shown in the figure: Here, 3 is the overall winner of the tournament. In the next tournament, 6 from array C will replace it. The minimum winner tree of the third tournament will be like as shown in the figure: Here, 4 is the overall winner of the tournament. So, in the next tournament 5 from array B will replace it. In order to find the median element which is the eighth element in our case. We need a total of eight tournaments. We follow the same steps for the rest of the tournament and the respective trees are shown below in the figure: After the 8^{th} tournament, we get the median of the array. The time complexity of merging Nsorted arrays of sized n_{1}, n_{2}, n_{3}, …... n_{n} into a single sorted array is O((n_{1} + n_{2} + ….. + n_{n}) * Log_{2}N) and the time complexity of finding the median is O(m * Log_{2}N) where m is the position of the median. When the sizes of the N arrays are equal then the time complexity of merging them becomes O(M * Log_{2}N) where m is the size of the array.
Next TopicLazy Propagation in Segment Tree
