Treap原理和实现方法


分类: 数据结构   320人阅读  评论(0)  收藏  举报

Treap是一棵二叉搜索树,只是每个节点多了一个优先级fix,对于每个节点,该节点的优先级小于等于其所有孩子的优先级。

当然,引入优先级fix的目的就是防止BST退化成一条链,从而影响查找效率。

 

所以,这样看来就是:Treap中对于节点的关键字key来说,它是一棵二叉搜索树,而对于fix来说,它是一个最小堆,所以

Treap可以看成是Tree+Heap,只是这里的Heap不一定是完全二叉树。Treap的平均时间复杂度为log(n).

 

Treap跟笛卡尔树几乎是一模一样的,只是用途不同。

笛卡尔树是把已有的一些(key, fix)二元组拿来构造树,然后利用构树过程和构造好的树来解决LCA,RMQ等等问题。而

Treap的目的只是一些key进行二叉搜索,但是为了保证树的平衡性,为每个key随机地额外增加了一个fix属性,这样从概

率上来讲可以让这树更加平衡。

 

对于Treap来说,主要有几大操作:插入,删除,查找,旋转,找第K大元素,找关键字x的排名,计算Treap的高度,删除

Treap,其它的操作比如合并,分离,反转等等以后再说,另外,对于Treap来说,它的中序遍历的结果就是按照关键字从小到

大的顺序排列的。

 

 

下面用一个题来看看Treap的各种操作。

题目:http://poj.org/problem?id=1442

 

题意:给一个序列,然后给出m个查询,每次查询输入一个数x,对于第i次查询,输出前x个数中第i大的关键字的值。

 

分析:我们可以用其它数据结构解决,这里我们先学用Treap怎么做吧,方法就是:每次我们都插入前x个数中没有插过的,当

然程序中我们用index来划分界限,然后输出Kth(root,i)即可。

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5.   
  6. using namespace std;  
  7.   
  8. struct Treap  
  9. {  
  10.     int size;  
  11.     int key,fix;  
  12.     Treap *ch[2];  
  13.     Treap(int key)  
  14.     {  
  15.         size=1;  
  16.         fix=rand();  
  17.         this->key=key;  
  18.         ch[0]=ch[1]=NULL;  
  19.     }  
  20.     int compare(int x) const  
  21.     {  
  22.         if(x==key) return -1;  
  23.         return x<key? 0:1;  
  24.     }  
  25.     void Maintain()  
  26.     {  
  27.         size=1;  
  28.         if(ch[0]!=NULL) size+=ch[0]->size;  
  29.         if(ch[1]!=NULL) size+=ch[1]->size;  
  30.     }  
  31. };  
  32.   
  33. void Rotate(Treap* &t,int d)  
  34. {  
  35.     Treap *k=t->ch[d^1];  
  36.     t->ch[d^1]=k->ch[d];  
  37.     k->ch[d]=t;  
  38.     t->Maintain();  //必须先维护t,再维护k,因为此时t是k的子节点  
  39.     k->Maintain();  
  40.     t=k;  
  41. }  
  42.   
  43. void Insert(Treap* &t,int x)  
  44. {  
  45.     if(t==NULL) t=new Treap(x);  
  46.     else  
  47.     {  
  48.         //int d=t->compare(x);   //如果值相等的元素只插入一个  
  49.         int d=x < t->key ? 0:1;  //如果值相等的元素都插入  
  50.         Insert(t->ch[d],x);  
  51.         if(t->ch[d]->fix > t->fix)  
  52.             Rotate(t,d^1);  
  53.     }  
  54.     t->Maintain();  
  55. }  
  56.   
  57. //一般来说,在调用删除函数之前要先用Find()函数判断该元素是否存在  
  58. void Delete(Treap* &t,int x)  
  59. {  
  60.     int d=t->compare(x);  
  61.     if(d==-1)  
  62.     {  
  63.         Treap *tmp=t;  
  64.         if(t->ch[0]==NULL)  
  65.         {  
  66.             t=t->ch[1];  
  67.             delete tmp;  
  68.             tmp=NULL;  
  69.         }  
  70.         else if(t->ch[1]==NULL)  
  71.         {  
  72.             t=t->ch[0];  
  73.             delete tmp;  
  74.             tmp=NULL;  
  75.         }  
  76.         else  
  77.         {  
  78.             int k=t->ch[0]->fix > t->ch[1]->fix ? 1:0;  
  79.             Rotate(t,k);  
  80.             Delete(t->ch[k],x);  
  81.         }  
  82.     }  
  83.     else Delete(t->ch[d],x);  
  84.     if(t!=NULL) t->Maintain();  
  85. }  
  86.   
  87. bool Find(Treap *t,int x)  
  88. {  
  89.     while(t!=NULL)  
  90.     {  
  91.         int d=t->compare(x);  
  92.         if(d==-1) return true;  
  93.         t=t->ch[d];  
  94.     }  
  95.     return false;  
  96. }  
  97.   
  98. int Kth(Treap *t,int k)  
  99. {  
  100.     if(t==NULL||k<=0||k>t->size)  
  101.         return -1;  
  102.     if(t->ch[0]==NULL&&k==1)  
  103.         return t->key;  
  104.     if(t->ch[0]==NULL)  
  105.         return Kth(t->ch[1],k-1);  
  106.     if(t->ch[0]->size>=k)  
  107.         return Kth(t->ch[0],k);  
  108.     if(t->ch[0]->size+1==k)  
  109.         return t->key;  
  110.     return Kth(t->ch[1],k-1-t->ch[0]->size);  
  111. }  
  112.   
  113. int Rank(Treap *t,int x)  
  114. {  
  115.     int r;  
  116.     if(t->ch[0]==NULL) r=0;  
  117.     else  r=t->ch[0]->size;  
  118.     if(x==t->key) return r+1;  
  119.     if(x<t->key)  
  120.         return Rank(t->ch[0],x);  
  121.     return r+1+Rank(t->ch[1],x);  
  122. }  
  123.   
  124. void DeleteTreap(Treap* &t)  
  125. {  
  126.     if(t==NULL) return;  
  127.     if(t->ch[0]!=NULL) DeleteTreap(t->ch[0]);  
  128.     if(t->ch[1]!=NULL) DeleteTreap(t->ch[1]);  
  129.     delete t;  
  130.     t=NULL;  
  131. }  
  132.   
  133. void Print(Treap *t)  
  134. {  
  135.     if(t==NULL) return;  
  136.     Print(t->ch[0]);  
  137.     cout<<t->key<<endl;  
  138.     Print(t->ch[1]);  
  139. }  
  140.   
  141. int val[1000005];  
  142.   
  143. int main()  
  144. {  
  145.     int n,x,m;  
  146.     while(~scanf("%d%d",&n,&m))  
  147.     {  
  148.         for(int i=1; i<=n; i++)  
  149.             scanf("%d",&val[i]);  
  150.         int index=1;  
  151.         Treap *root=NULL;  
  152.         for(int i=1; i<=m; i++)  
  153.         {  
  154.             scanf("%d",&x);  
  155.             for(int j=index; j<=x; j++)  
  156.                 Insert(root,val[j]);  
  157.             index=x+1;  
  158.             printf("%d\n",Kth(root,i));  
  159.         }  
  160.         DeleteTreap(root);  
  161.     }  
  162.     return 0;  
  163. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值