Advertisement
LinKin

Treap

Apr 11th, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. struct NODE{
  2.     long Key, Prior, Cnt;
  3.     NODE *l, *r;
  4.     NODE( long Key = 0, long Prior = 0 ):Key( Key ), Prior( Prior ), Cnt(1), l( NULL ), r( NULL ){}
  5. };
  6.  
  7. typedef NODE* P_NODE;
  8.  
  9. class TREAP{
  10.     P_NODE Root;
  11.     vector<NODE> Pool;
  12.     long PoolPointer;
  13.  
  14.     P_NODE Create( long Key, long Prior = rand( ) ){
  15.         P_NODE ret = &Pool[PoolPointer++];
  16.         *ret = NODE( Key,Prior );
  17.         return ret;
  18.     }
  19.  
  20.     long Count( P_NODE t ){
  21.         return t ? t->Cnt: 0;
  22.     }
  23.  
  24.     void Update_Count( P_NODE t ){
  25.         if( t ) t->Cnt = 1 + Count(t->l ) + Count(t->r );
  26.     }
  27.  
  28.     void Split( P_NODE t, long Key, P_NODE &l, P_NODE &r ){
  29.         if( !t ) l = r = NULL;
  30.         else if( Key < t->Key ) Split(t->l, Key, l, t->l ), r = t;
  31.         else Split( t->r, Key, t->r, r ), l = t;
  32.         Update_Count( t );
  33.     }
  34.  
  35.     void Merge( P_NODE &t, P_NODE l, P_NODE r ){
  36.         if( !l or !r ) t = l?l:r;
  37.         else if( l->Prior > r->Prior ) Merge(l->r, l->r, r ), t = l;
  38.         else Merge(r->l, l, r->l ), t = r;
  39.         Update_Count( t );
  40.     }
  41.  
  42.     void Insert( P_NODE &t, P_NODE it ){
  43.         if( !t ) t = it;
  44.         else if( it->Prior > t->Prior ) Split(t, it->Key, it->l, it->r ), t = it;
  45.         else Insert((it->Key < t->Key )?t->l:t->r, it );
  46.         Update_Count( t );
  47.     }
  48.  
  49.     void Erase( P_NODE &t, long Key ){
  50.         if( !t ) return;
  51.         if( t->Key == Key ) Merge(t, t->l, t->r );
  52.         else Erase((Key < t->Key )?t->l:t->r, Key );
  53.         Update_Count( t );
  54.     }
  55.  
  56.     P_NODE Unite( P_NODE l, P_NODE r ){
  57.         if( !l or !r ) return l?l:r;
  58.         if( l->Prior < r->Prior ) swap( l, r );
  59.         P_NODE lt, rt;
  60.         Split(r, l->Key, lt, rt );
  61.         l->l = Unite(l->l, lt );
  62.         l->r = Unite(l->r, rt );
  63.         Update_Count(l );
  64.         return l;
  65.     }
  66.  
  67.     long CountLess( P_NODE t, long Key ){
  68.         if( !t ) return 0;
  69.         else if( Key <= t->Key ) return CountLess(t->l,Key );
  70.         else return 1 + Count(t->l ) + CountLess(t->r,Key );
  71.     }
  72.  
  73.     long CountLessEqual( P_NODE t, long Key ){
  74.         if( !t ) return 0;
  75.         else if( Key < t->Key ) return CountLessEqual(t->l,Key );
  76.         else return 1 + Count(t->l ) + CountLessEqual(t->r,Key );
  77.     }
  78.  
  79.     P_NODE Get_Kth( P_NODE &t, long K ){
  80.         if( !t or K >= Count(t ) ) return NULL;
  81.         else if( K < Count(t->l ) ) return Get_Kth(t->l, K );
  82.         else if( K == Count(t->l ) ) return t;
  83.         else return Get_Kth(t->r, K-1-Count(t->l ) );
  84.     }
  85.  
  86. public:
  87.     TREAP( long PoolSize ) : Root( NULL ), PoolPointer( 0 ){
  88.         srand(time(NULL ) );
  89.         Pool.clear( );
  90.         Pool.resize( PoolSize );
  91.     }
  92.     /* Count tot number of node */
  93.     long COUNT( ){
  94.         return Count(Root );
  95.     }
  96.     /* Find a key */
  97.     bool FIND( long Key ){
  98.         return CountLess( Root,Key ) == CountLessEqual( Root,Key );
  99.     }
  100.     /* Insert a key */
  101.     void INSERT( long Key ){
  102.         Insert( Root, Create(Key ) );
  103.     }
  104.     /* Insert a key if the key isnt present in tree */
  105.     void UNIQUE_INSERT( long Key ){
  106.         if(  CountLess( Root,Key ) == CountLessEqual( Root,Key ) ) Insert(Root, Create(Key ) );
  107.     }
  108.     /* Erase a key , if multiple key present then only one will be Erased */
  109.     void ERASE( long Key ){
  110.         Erase(Root, Key );
  111.     }
  112.     /* Find the kth element of the tree , if not found assert will fail */
  113.     long operator[]( long K ){
  114.         P_NODE e = Get_Kth( Root, K );
  115.         assert( e!=NULL );
  116.         return e->Key;
  117.     }
  118. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement