Treap=Tree+Heap
Treap是一棵
二叉搜索树
,它的左子树和右子树分别是一个Treap,和一般的二叉搜索树不同的是,Treap结构中每个节点x有两个域,一个是其关键字值key,一个是优先级数priority(他是一个独立选取的随机数),对于Treap结构,其关键字遵循二叉搜索树性质,其优先级遵循堆性质。但是这里要注意的是Treap和二叉堆有一点不同,就是
二叉堆
必须是完全二叉树,而Treap可以并不一定是。
在这里用的链表实现的旋转的Treap,简单实现是思路比较简单,但代码稍微有点繁琐。有点懒,不想整理了。
TreapNode结构如下:
template<class T>
struct treapNode
{
int key;//用于二叉搜索树的关键字
int pri;//用于堆性质的优先级数字
T element;//其中的元素
treapNode<T> *leftChild, //左子树
*rightChild,
*father; //右子树
treapNode() {leftChild = rightChild = father=NULL;}//构造函数
treapNode(const T& theElement):element(theElement)
{
leftChild = rightChild = father=NULL;
}
treapNode(const T& theElement,
treapNode *theLeftChild,
treapNode *theRightChild,
treapNode *theFather)
:element(theElement)
{
leftChild = theLeftChild;
rightChild = theRightChild;
f