Data Structure: Winner tree and loser tree

Source: Internet
Author: User

Moved to: http://www.wypblog.com/archives/93

Suppose there are k sequential sequences called shun strings, and we want to merge them into a separate ordered sequence. Each substring contains some records, which are arranged in a non-descending order based on the key value. Set n to the total number of all records in k shun strings. The parallel task can be completed by repeatedly outputting k records with the smallest key value in the string. There are k possibilities for selecting the record with the smallest key value. It may be any 1st record in a substring. The most direct way to return k shun strings is to compare the K-1 times to determine the next output record. For k> 2, we can use the data structure such as select number to reduce the number of comparisons required to find the next smallest element. There are two decision trees: Victory tree and Failure Tree.

The winner tree and the loser tree are completely Binary Trees. Just like participating in the competition, each contestant has a different strength. Two contestants have a PK, And the strength determines the outcome. After several rounds of promotion, they will be able to win the championship. The difference is that the middle node of the winner tree records the number of the winner, while the middle node of the loser tree records the number of the loser. The winner tree and the loser tree can find the greatest value in log (n) time. After the value of any leaf node changes, you can use the information of the intermediate node to quickly find the maximum value. It is often used in k-path Merge Sorting.

I. Winner tree

One advantage of the winner tree is that if the value of a contestant changes, the winner tree can be easily modified. You only need to modify the binary tree along the path from the node to the root node, without changing the results of other competitions. The following is a selection of the smallest number as the winner (as shown in Figure 1). For the first time, the first element in each array is put in. This is a comparison based on the rules of the victory tree, obtain the minimum value. After the first result is obtained, the number 1 is the victory, that is, the value 1 is the smallest. In the next output of the second small number, you only need to add the elements in the array where 1 is located, and then compare the second small value from the leaf node to the root node, this reduces the number of useless comparisons (see figure 2 ).

Figure 1 Figure 2

Problem: there are 20 ordered arrays, each of which has 500 uint variables, sorted in descending order. The maximum number of elements is 10000.

Program:

# Include <iostream> # include <vector> # include <cmath> # include <ctime> # include <algorithm>/*** Author: w397090770 * Data: 2012.10.15 * Email: wyphao.2007@163.com * reprint please indicate the source, thank you. **/# Define N 10 # define INF (1 <31)-1 using namespace std; typedef struct Node {int which; // records which sub-array int index is used; // record is the last element that marks the array with int data; // record array element} Node; int com (const void * a, const void * B) {if (* (int *) a> * (int *) B) {return 1;} else if (* (int *) a <* (int *) B) {return-1;} return 0;} void adjustTreeForFirst (Node * tempArray, int len) {int I = len/2; int j = 0; while (I> 1) {for (j = I; j <2 * I-1; j + = 2) {if (tempArray [j]. data> tempArray [j + 1]. data) {tempArray [j/2] = tempArray [j + 1];} else {tempArray [j/2] = tempArray [j];} i/= 2;} // col is a column // row is a row // len is the number of elements selected before the output void winTree (int ** a, int row, int col, int len) {int * result = (int *) malloc (len * sizeof (int); Node tempArray [row * 2]; int index = 0; int I = 0, j = 0; memset (tempArray, 0, sizeof (struct Node) * 2 * row); for (I = 0; I <row; I ++) {tempArray [row + I]. data = a [I] [0]; tempArray [row + I]. which = I; tempArray [row + I]. index = 0 ;}for (I = 0; I <len; I ++) {adjustTreeForFirst (tempArray, 2 * row); // to reduce the amount of code, it is unnecessary for me to adjust the entire tree for every call. If you are interested, you can write result [I] = tempArray [1]. data; if (tempArray [1]. index + 1 <col) {tempArray [row + tempArray [1]. which]. data = a [tempArray [1]. which] [tempArray [1]. index + 1]; tempArray [row + tempArray [1]. which]. index = tempArray [1]. index + 1; tempArray [row + tempArray [1]. which]. which = tempArray [1]. which;} else {// if the data in an array is processed, assign that node to an infinite tempArray [row + tempArray [1]. which]. data = INF; // tempArray [row + tempArray [1]. which]. index = tempArray [1]. index + 1; // tempArray [row + tempArray [1]. which]. which = tempArray [1]. which ;}}for (I = 0; I <len; I ++) {cout <result [I] <endl ;}free (result );} int main () {/* int a [N-2] [N] = {3, 4, 5, 6, 10, 11, 12, 13, 20, 21, 1, 2, 7, 8, 9, 30, 31, 32, 33, 34, 14, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 27, 28, 29, 35, 36, 37, 38, 39, 40, 50, 51, 52, 54, 55, 65, 66, 67, 68, 69, 53, 56, 57, 58, 59, 60, 61, 62, 63, 64, 41, 42, 43, 44, 45, 46, 47, 48, 49,222 2, 1, 2, 2, 4, 5, 6, 12, 13, 20, 21}; */const int size = 500; const int del = 20; int * a [del]; int I = 0, j = 0; // allocate memory space for (I = 0; I <del; I ++) {a [I] = (int *) malloc (size * sizeof (int);} // initialize the array srand (time (NULL); for (I = 0; I <size; I ++) {for (j = 0; j <del; j ++) {a [j] [I] = rand ();}} // sort for (I = 0; I <del; I ++) {qsort (a [I], size, sizeof (int), com );} // use the victory tree to process winTree (a, del, size, size); return 0 ;}
Ii. Loser tree

The loser tree is a variant of the winner tree. In the loser tree, the parent node is used to record the losers of the left and right child nodes for the competition, and the winner is allowed to participate in the next round of competition. The root node of the loser tree records the loser. You need to add a node to record the winner of the entire game. Using the loser tree can simplify the reconstruction process.



Reprinted Please note: http://blog.csdn.net/w397090770/article/details/8074831

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.