BST && TREAP 实现简单排序
来源:互联网 发布:看图识番号软件下载 编辑:程序博客网 时间:2024/06/03 15:02
//BST#include<stdio.h>#include<stdlib.h>struct node{int x;struct node *left,*right;};struct node *h,*p,*q;void insert(int s,struct node *t){if(s>t->x){if(t->right==NULL){q=new node;q->x=s;q->left=NULL;q->right=NULL;t->right=q;}elseinsert(s,t->right);}else{if(t->left==NULL){q=new node;q->x=s;q->left=NULL;q->right=NULL;t->left=q;}else insert(s,t->left);}}void dfs(struct node *t){if(t==NULL)return;dfs(t->left);printf("%d ",t->x);dfs(t->right);}int main(){int i,j,k,m,n;scanf("%d",&n);scanf("%d",&k);h=new node;h->x=k;h->left=NULL;h->right=NULL;for(i=2;i<=n;i++){scanf("%d",&k);insert(k,h);}dfs(h);system("pause");return 0;}//TREAP#include<stdio.h>#include<stdlib.h>struct node{int v,w;struct node *h,*l,*r;};struct node *s,*p,*q;void rrotate(struct node *A,struct node *B){A->h=B->h;if(A->h!=NULL) if(A->h->l==B)A->h->l=A; else A->h->r=A;B->l=A->r;if(A->r!=NULL)A->r->h=B;B->h=A;A->r=B;}void lrotate(struct node *A,struct node *B){A->h=B->h;if(A->h!=NULL) if(A->h->l==B)A->h->l=A; else A->h->r=A;B->r=A->l; if(A->l!=NULL)A->l->h=B;B->h=A;A->l=B;}void insert(struct node *t,int x){if(t==NULL)return ;if(x<t->v){if(t->l==NULL){q=new node;q->v=x;q->w=rand()%100;q->h=t;q->l=NULL;q->r=NULL;t->l=q;p=q;while(p->h!=NULL && p->w<p->h->w){if(p->h->l==p)rrotate(p,p->h);elselrotate(p,p->h);if(p->h==NULL)s=p;}}elseinsert(t->l,x);}else{if(t->r==NULL){q=new node;q->v=x;q->w=rand()%100;q->h=t;q->l=NULL;q->r=NULL;t->r=q;p=q;while(p->h!=NULL && p->w<p->h->w){if(p->h->l==p)rrotate(p,p->h);elselrotate(p,p->h);if(p->h==NULL)s=p;}}elseinsert(t->r,x);}}void dfs(struct node *t){if(t==NULL)return;dfs(t->l);printf("%d ",t->v);dfs(t->r);}int main(){int i,j,k,m,n;srand(1235);scanf("%d",&n);scanf("%d",&k);s=new node;s->v=k;s->w=rand()%100;s->h=NULL;s->l=NULL;s->r=NULL;for(i=2;i<=n;i++){scanf("%d",&k);insert(s,k);}dfs(s);system("pause");return 0;}
1 0
- BST && TREAP 实现简单排序
- Treap的动态平衡BST
- [平衡树]Tree(BST) + Heap = Treap
- 简单实现BST以及简单工厂模式进行测试
- 利用Treap排序
- COGS2421 简单的Treap
- 二叉搜索树(BST树)的简单实现
- 2268: SB_cyh and his BST two (treap&&set)
- SWUST 2268 SB_cyh and his BST two (Treap名次树)
- Treap=Tree+Heap! 各种BST大PK(模板)
- Treap 的实现
- Treap实现名次树
- 静态数组实现TREAP
- BST简单测试
- 排序-冒泡排序简单实现
- 排序二叉树和treap
- TREAP示例代码(排序)
- 堆排序简单实现
- 利用Python对二进制数据进行按位处理
- 10个超棒的界面设计工具
- MyEclipse2014安装包下载地址、破解文件及破解方法
- 11 个超棒的移动Web应用开发解决方案
- c++ 、java 、 oc 对象模型比较(干货)
- BST && TREAP 实现简单排序
- AutoLayout
- HTML5移动前端框架:Junior
- 使用Struts2+JavaScript+Ajax实现成绩录入功能
- HTML5 开源前端框架 Amaze UI 发布 1.0 正式版
- Ubuntu 下安裝 GTK
- 15个最好的HTML5前端响应式框架(2014)
- (八)喜马拉雅Demo引出的细节(代理模式和图片缩放)
- 自定义view详解