标签:os io for sp amp new c ef ios
#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; }
标签:os io for sp amp new c ef ios
原文地址:http://blog.csdn.net/cklsoft/article/details/38927157