Treap

来源:互联网 发布:disabled js移除 编辑:程序博客网 时间:2024/05/22 10:47

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)即可。


    #include <iostream>      #include <string.h>      #include <stdlib.h>      #include <stdio.h>            using namespace std;            struct Treap      {          int size;          int key,fix;          Treap *ch[2];          Treap(int key)          {              size=1;              fix=rand();              this->key=key;              ch[0]=ch[1]=NULL;          }          int compare(int x) const          {              if(x==key) return -1;              return x<key? 0:1;          }          void Maintain()          {              size=1;              if(ch[0]!=NULL) size+=ch[0]->size;              if(ch[1]!=NULL) size+=ch[1]->size;          }      };            void Rotate(Treap* &t,int d)      {          Treap *k=t->ch[d^1];          t->ch[d^1]=k->ch[d];          k->ch[d]=t;          t->Maintain();  //必须先维护t,再维护k,因为此时t是k的子节点          k->Maintain();          t=k;      }            void Insert(Treap* &t,int x)      {          if(t==NULL) t=new Treap(x);          else          {              //int d=t->compare(x);   //如果值相等的元素只插入一个              int d=x < t->key ? 0:1;  //如果值相等的元素都插入              Insert(t->ch[d],x);              if(t->ch[d]->fix > t->fix)                  Rotate(t,d^1);          }          t->Maintain();      }            //一般来说,在调用删除函数之前要先用Find()函数判断该元素是否存在      void Delete(Treap* &t,int x)      {          int d=t->compare(x);          if(d==-1)          {              Treap *tmp=t;              if(t->ch[0]==NULL)              {                  t=t->ch[1];                  delete tmp;                  tmp=NULL;              }              else if(t->ch[1]==NULL)              {                  t=t->ch[0];                  delete tmp;                  tmp=NULL;              }              else              {                  int k=t->ch[0]->fix > t->ch[1]->fix ? 1:0;                  Rotate(t,k);                  Delete(t->ch[k],x);              }          }          else Delete(t->ch[d],x);          if(t!=NULL) t->Maintain();      }            bool Find(Treap *t,int x)      {          while(t!=NULL)          {              int d=t->compare(x);              if(d==-1) return true;              t=t->ch[d];          }          return false;      }            int Kth(Treap *t,int k)      {          if(t==NULL||k<=0||k>t->size)              return -1;          if(t->ch[0]==NULL&&k==1)              return t->key;          if(t->ch[0]==NULL)              return Kth(t->ch[1],k-1);          if(t->ch[0]->size>=k)              return Kth(t->ch[0],k);          if(t->ch[0]->size+1==k)              return t->key;          return Kth(t->ch[1],k-1-t->ch[0]->size);      }           int Rank(Treap *t,int x) //小于等于x的个数    {    if(t == NULL) return 0;    if(x < t->key)    {        return Rank(t->ch[0], x);    }    else {        int r = t->ch[0]==NULL? 1 : (t->ch[0]->size) + 1;        return r + Rank(t->ch[1],x);    }    }          void DeleteTreap(Treap* &t)      {          if(t==NULL) return;          if(t->ch[0]!=NULL) DeleteTreap(t->ch[0]);          if(t->ch[1]!=NULL) DeleteTreap(t->ch[1]);          delete t;          t=NULL;      }            void Print(Treap *t)      {          if(t==NULL) return;          Print(t->ch[0]);          cout<<t->key<<endl;          Print(t->ch[1]);      }            int val[1000005];            int main()      {          int n,x,m;          while(~scanf("%d%d",&n,&m))          {              for(int i=1; i<=n; i++)                  scanf("%d",&val[i]);              int index=1;              Treap *root=NULL;              for(int i=1; i<=m; i++)              {                  scanf("%d",&x);                  for(int j=index; j<=x; j++)                      Insert(root,val[j]);                  index=x+1;                  printf("%d\n",Kth(root,i));              }              DeleteTreap(root);          }          return 0;      }  


0 0
原创粉丝点击