|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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[2]; //ch[0] means left child, and ch[1] 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[decision^1];
- tree->ch[decision^1] = k->ch[decision];
- k->ch[decision] = tree;
- tree = k;
- }
- /*insert a certain number x.*/
- void insert(mynode *&tree,int x)
- {
- if (tree==NULL)
- {
- tree=new mynode();
- tree->ch[0]=NULL;
- tree->ch[1]=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[decision],x);
- if(tree->ch[decision]->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[0]);
- cout << tree->value << endl;
- showtree(tree->ch[1]);
- }
- /*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[0],x);
- if(tree->value<x)
- return search(tree->ch[1],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[0] != NULL)
- {
- return BinaryTreeSearch(tree->ch[0], value);
- }
- else{
- return NULL;
- }
- }else{
- if(tree->ch[1] != NULL)
- {
- return BinaryTreeSearch(tree->ch[1], 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;
- }
复制代码
|
|