bobpanda 发表于 2018-12-26 11:23:40

数据结构——treap

treap是一种数据结构。首先treap是一个二叉搜索树,与二叉搜索树不同的是,treap的每一个节点都有一个随机值,这个随机值满足堆的一些性质。我们知道,如果输入数据是有序的,普通的二叉搜索树会退化成线性表,但是引入随机值以后,能够减少退化的几率,但是由于权值是随机的,仍然有一定可能不能达成平衡。treap与splay和avl树相比优点在于代码量更小,更容易理解,缺点在于有退化风险。以下是部分代码:
/*This is an implementation of a senior data structure called treap.
Treap is a binary search tree,so treap has all features of a binary search tree. The difference is that each node of treap has a random weight,so that treap also owns some heap features in terms of the random weight.
The random weight guarantees that the whole tree is almost balance, and prevents the tree from degenerating to linear structures. */
#include <iostream>
#include <stdlib.h>
using namespace std;

struct mynode
{
mynode *ch;    //ch means left child, and ch means right child.
int randweight;
int value;
int size;
int cmp(int x) const
{
    if (x==value) return 0;
    return x<value?0:1;
}


};

mynode *tree=NULL;

/*the core part of treap,rotate. This function does a rotate operation like
AVL tree in order to maintain the balance of the tree. Many blogs on the internetuse two functions "zig" and "zag" to do the operation.However, using a simple variable "decision" could decide which side to rotate, combining two functions to one.Besides, "decision^1" means "1-decision." */
void rotate(mynode *&tree,int decision)
{
mynode *k = tree->ch;
tree->ch = k->ch;
k->ch = tree;
tree = k;
}

/*insert a certain number x.*/
void insert(mynode *&tree,int x)
{
if (tree==NULL)
    {
      tree=new mynode();
      tree->ch=NULL;
      tree->ch=NULL;
      tree->value=x;
      tree->randweight=rand();
    }
else
    {
      int decision = (x < tree->value ? 0 : 1);/*decide which side to insert,left,or right?*/
      insert(tree->ch,x);
      if(tree->ch->randweight > tree->randweight)
      {
          rotate(tree,decision^1);
      }
   
    }

}

/*show the treap in mid order. Obviously the sequence is in a sorted order.*/

void showtree(mynode *tree)
{
if (tree == NULL)
    return;
showtree(tree->ch);
cout << tree->value << endl;
showtree(tree->ch);
}


/*judge whether a certain number x is in the treap or not.
If x is in the treap,return 1.Otherwise,return 0.*/
int search (mynode *tree,int x)
{
if (tree==NULL)
    return 0;
if (tree->value==x)return 1;
if (tree->value > x)
    return search(tree->ch,x);
if(tree->value<x)
    return search(tree->ch,x);

}
/*find the pointer of a certain number x,if x is not in the treap,return NULL*/
mynode* BinaryTreeSearch(mynode* tree, int value)
{
if(tree->value == value)
    {
      return tree;
    }
else if(tree->value > value)
    {
      if(tree->ch != NULL)
      {
          returnBinaryTreeSearch(tree->ch, value);
      }
      else{
      return NULL;
      }
    }else{
    if(tree->ch != NULL)
      {
      returnBinaryTreeSearch(tree->ch, value);
      }else{
      return NULL;
    }
}
}


int main()
{
srand(time(NULL));
int n,m;
cin >> n;
for (int i=1;i<=n;i++)
    {
      cin >> m;

      insert(tree,m);
    }

   showtree(tree);

return 0;
}
页: [1]
查看完整版本: 数据结构——treap