热转印www.yxheatpress.com

公司网站模板http://qiyemoban.software8.co/

常用链接

统计

友情链接

最新评论

数据结构:胜者树与败者树

假设有k个称为顺串的有序序列,我们希望将他们归并到一个单独的有序序列中。每一个顺串包含一些记录,并且这些记录按照键值的大小,以非递减的顺序排列。令n为k个顺串中的所有记录的总数。并归的任务可以通过反复输出k个顺串中键值最小的记录来完成。键值最小的记录的选择有k种可能,它可能是任意有一个顺串中的第1个记录。并归k个顺串的最直接的办法就是进行k-1次比较确定下一个输出的记录。对k>2,我们可以通过使用选择数这种数据结构来降低寻找下一个最小元素所需要进行的比较次数。有两个种选择树:胜利树和失败树。

胜者树与败者树是完全二叉树。就像是参加比赛一样,每个选手有不同的实力,两个选手PK,实力决定胜负,晋级下一轮,经过几轮之后,就能得到冠军。不同的是,胜者树的中间结点记录的是胜者的标号;而败者树的中间结点记录的败者的标号。 胜者树与败者树可以在log(n)的时间内找到最值。任何一个叶子结点的值改变后,利用中间结点的信息,还是能够快速地找到最值。在k路归并排序中经常用到。

一、胜者树

胜者树的一个优点是,如果一个选手的值改变了,可以很容易地修改这棵胜者树。只需要沿着从该结点到根结点的路径修改这棵二叉树,而不必改变其他比赛的结果。下面是选择一个最小的数字为最胜利者(见图1所示),第一次把各个数组里面的第一个元素都放进去,这是根据胜利树的规则两两比较,得到最小的值,第一次弄完之后,就得出1数字是胜利的,也就是1是最小的。在下一次输出第二小的数字时候,只需要把1所在的数组里面的元素加进去,然后从叶子节点到根节点一直比较得出第二小的值,这样就减少了很多次无用的比较(见图2所示)。

                        

图 一                                                                                                               图二

问题:有20个有序数组,每个数组有500个uint变量,降序排序。要求从这10000个元素中选出最大的500个。

程序:

  1. #include <iostream>  
  2. #include <vector>  
  3. #include <cmath>  
  4. #include <ctime>  
  5. #include <algorithm>  
  6.   
  7. /** 
  8. *    
  9. *   Author: w397090770 
  10. *   Data  : 2012.10.15 
  11. *   Email : wyphao.2007@163.com 
  12. *   转载请注明出处,谢谢。  
  13. *        
  14. */   
  15. #define N 10  
  16. #define INF (1 << 31) - 1  
  17. using namespace std;  
  18.   
  19. typedef struct Node{  
  20.     int which;  //记录是哪个子数组   
  21.     int index;  //记录是上个标记数组的第几个元素了   
  22.     int data;   //记录数组的元素   
  23. }Node;  
  24.   
  25. int com(const void *a, const void *b){  
  26.     if(*(int *)a > *(int *)b){  
  27.         return 1;  
  28.     }else if(*(int *)a < *(int *)b){  
  29.         return -1;  
  30.     }  
  31.       
  32.     return 0;  
  33. }  
  34.   
  35. void adjustTreeForFirst(Node *tempArray, int len){  
  36.     int i = len / 2;  
  37.     int j = 0;  
  38.     while(i > 1){  
  39.         for(j = i; j < 2 * i - 1; j += 2){  
  40.             if(tempArray[j].data > tempArray[j + 1].data){  
  41.                 tempArray[j / 2] = tempArray[j + 1];  
  42.             }else{  
  43.                 tempArray[j / 2] = tempArray[j];  
  44.             }  
  45.         }  
  46.         i /= 2;  
  47.     }  
  48. }  
  49.   
  50. //col 是列  
  51. //row 是行  
  52. //len 是选择出前多少个元素   
  53. void winTree(int **a, int row, int col, int len){  
  54.     int *result = (int *)malloc(len * sizeof(int));  
  55.     Node tempArray[row * 2];  
  56.     int index = 0;  
  57.     int i = 0, j = 0;  
  58.     memset(tempArray, 0, sizeof(struct Node) * 2 * row);  
  59.       
  60.     for(i = 0; i < row; i++){  
  61.         tempArray[row + i].data = a[i][0];  
  62.         tempArray[row + i].which = i;  
  63.         tempArray[row + i].index = 0;  
  64.     }  
  65.       
  66.     for(i = 0 ; i < len; i++){  
  67.         adjustTreeForFirst(tempArray, 2 * row);//为了代码减少代码量,我把每一次调用都调整整个树,其实是不必要的,有兴趣的用户可以自己写写  
  68.         result[i] = tempArray[1].data;  
  69.         if(tempArray[1].index + 1 < col){  
  70.             tempArray[row + tempArray[1].which].data = a[tempArray[1].which][tempArray[1].index + 1];  
  71.             tempArray[row + tempArray[1].which].index = tempArray[1].index + 1;  
  72.             tempArray[row + tempArray[1].which].which = tempArray[1].which;  
  73.         }else{//如果某个数组里面的数据处理完了,就把那个节点赋值为无穷大   
  74.             tempArray[row + tempArray[1].which].data = INF;  
  75.             //tempArray[row + tempArray[1].which].index = tempArray[1].index + 1;  
  76.             //tempArray[row + tempArray[1].which].which = tempArray[1].which;  
  77.         }         
  78.     }  
  79.       
  80.     for(i = 0; i < len; i++){  
  81.         cout << result[i] << endl;  
  82.     }   
  83.     free(result);  
  84. }  
  85.   
  86. int main(){  
  87.     /*int a[N - 2][N] = { 
  88.         3, 4, 5, 6, 10, 11, 12, 13, 20, 21, 
  89.         1, 2, 7, 8, 9, 30, 31, 32, 33, 34, 
  90.         14, 15, 16, 17, 18, 19, 22, 23, 24, 25, 
  91.         26, 27, 28, 29, 35, 36, 37, 38, 39, 40, 
  92.         50, 51, 52, 54, 55, 65, 66, 67, 68, 69, 
  93.         53, 56, 57, 58, 59, 60, 61, 62, 63, 64, 
  94.         41, 42, 43, 44, 45, 46, 47, 48, 49, 2222, 
  95.         1, 2, 2, 4, 5, 6, 12, 13, 20, 21 
  96.     };*/  
  97.     const int size = 500;  
  98.     const int del = 20;  
  99.       
  100.     int *a[del];  
  101.     int i = 0, j = 0;  
  102.     //分配内存空间   
  103.     for(i = 0; i < del; i++){  
  104.         a[i] = (int *)malloc(size * sizeof(int));     
  105.     }  
  106.       
  107.     //初始化数组   
  108.     srand( time(NULL) );   
  109.     for(i = 0; i < size; i++){  
  110.         for(j = 0; j < del; j++){  
  111.             a[j][i] = rand();  
  112.         }     
  113.     }  
  114.       
  115.     //排序   
  116.     for(i = 0; i < del; i++){  
  117.         qsort(a[i], size, sizeof(int), com);  
  118.     }  
  119.       
  120.     //利用胜利树进行处理   
  121.     winTree(a, del, size, size);  
  122.     return 0;  
  123. }  

二、败者树

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


posted on 2012-10-16 14:21 不听话的 阅读(1934) 评论(1)  编辑 收藏 引用

评论

# re: 数据结构:胜者树与败者树 2013-09-11 18:00 lxl

代码应该是选出最小的500个元素吧  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理