PAT 1066 平衡树
来源:互联网 发布:瓦格纳 马拉 知乎 编辑:程序博客网 时间:2024/05/16 05:19
#include<iostream>#include<algorithm>#include<string>#include<malloc.h>#include<cstring>#include<vector>using namespace std;#define Max(x,y) ((x)>(y)?(x):(y))#define ABS(x) ((x)>0?(x):-(x))struct Node{Node* l,*r;int val,h;Node(int x){l=r=NULL;h=1;val=x;}int getH(Node*p){return p==NULL?0:p->h;}bool updateH(){int lh=getH(l),rh=getH(r);h=Max(lh,rh)+1;return ABS(lh-rh)>1;}};Node *LLRotate(Node* p){Node *q=p->l;p->l=q->r;q->r=p;p->updateH();q->updateH();return q;}Node *RRRotate(Node*p){Node *q=p->r;p->r=q->l;q->l=p;p->updateH();q->updateH();return q;}Node* LRRotate(Node*p){p->l=RRRotate(p->l);return LLRotate(p);}Node* RLRotate(Node*p){p->r=LLRotate(p->r);return RRRotate(p);}Node* build(Node* p,int x){if(p==NULL)return new Node(x);if(p->val>x){chooseLeft=true;p->l=build(p->l,x);if(p->updateH()){if(p->getH(p->l->l)>p->getH(p->l->r))p=LLRotate(p);else p=LRRotate(p);}}else if(p->val<x){chooseLeft=false;p->r=build(p->r,x);if(p->updateH()){if(p->getH(p->r->l)>p->getH(p->r->r))p=RLRotate(p);else p=RRRotate(p);}}p->updateH();return p;}int main(){int i,n,x;scanf("%d",&n);Node *root=NULL;for(i=0;i<n;++i){scanf("%d",&x);root=build(root,x);}printf("%d\n",root->val);return 0;}
0 0
- PAT 1066 平衡树
- pat 1066 二叉平衡树求根节点
- **浙大PAT甲级 1066 平衡二叉查找树
- PAT 1123. Is It a Complete AVL Tree (30) 平衡树构建+ 完全二叉树判断
- (PAT)1123.[平衡树旋转] Is It a Complete AVL Tree
- 平衡树
- 平衡树
- 平衡树
- 平衡树
- 平衡树
- 平衡树
- 平衡树
- 平衡树
- 平衡树
- 平衡树
- 【平衡树】平衡树教学
- 平衡二叉树平衡法则
- Tyvj P1728 普通平衡树 (平衡树)
- 3.选择排序
- QML类型说明-Calendar
- 八、 异步调用WebService
- Creating a timer dispatch source
- LeetCode Path Sum II
- PAT 1066 平衡树
- HDU 1242 Rescue(求最短时间救出同伴,BFS+DP)
- iOS摄像头和相册-UIImagePickerController-浅析
- 九、 编写Module模块
- jq笔记
- POJ 3378——Crazy Thairs(树状数组+dp+高精度)数据结构优化的DP
- 四种map的访问方法
- 黑马程序员_Java 高新技术(5)
- 面试题总结