The rotation of the AVL tree. Actually 1 a ....
Once you know how to rotate, the smaller data can be written as a simulation.
#include <cstdio>#include<cstring>#include<cmath>#include<vector>#include<map>#include<stack>#include<queue>#include<string>#include<algorithm>using namespacestd;Const intmaxn= -;intN,A[MAXN];structnode{intnum; intL,r; intLh,rh; intFA;} NODE[MAXN];intsz;intRoot;voidinit () {root=0; SZ=0; NODE[ROOT].FA=-1; Node[root]. L=-1, Node[root]. r=-1; Node[root].num=a[1]; Node[root]. LH=0, Node[root]. Rh=0;}voidDfsintNow ) { if(Node[now]. l!=-1) DFS (Node[now]. L); if(Node[now]. r!=-1) DFS (Node[now]. R); Node[now]. LH=Max (Node[node[now]. L]. LH, Node[node[now]. L]. RH)+1; Node[now]. RH=Max (Node[node[now]. R]. LH, Node[node[now]. R]. RH)+1;}voidUpdate (intFaintNuminttag) {SZ++; NODE[SZ].FA=FA; NODE[SZ]. L=-1, Node[sz]. r=-1; Node[sz].num=num; NODE[SZ]. LH=0, Node[sz]. Rh=0; if(tag==1) NODE[FA]. R=sz; ElseNODE[FA]. L=sz; DFS (root);}voidInsert (intnum) { intp=Root; while(1) { if(num>=node[p].num) { if(Node[p]. r!=-1) p=Node[p]. R Else{Update (P,num,1); Break; } } Else { if(Node[p]. l!=-1) p=Node[p]. L Else{Update (P,num,0); Break; } } }}voidLL (intID) { intlson=Node[id]. L Node TMP1=Node[id]; Node TMP2=Node[lson]; if(tmp1.fa!=-1) { if(Node[tmp1.fa]. L==id) NODE[TMP1.FA]. L=Lson; ElseNODE[TMP1.FA]. R=Lson; } NODE[LSON].FA=Tmp1.fa; Node[lson]. R=ID; NODE[ID].FA=Lson; Node[id]. L=TMP2. R Node[tmp2. R].FA=ID;}voidRR (intID) { intrson=Node[id]. R Node TMP1=Node[id]; Node TMP2=Node[rson]; if(tmp1.fa!=-1) { if(Node[tmp1.fa]. L==id) NODE[TMP1.FA]. L=Rson; ElseNODE[TMP1.FA]. R=Rson; } NODE[RSON].FA=Tmp1.fa; Node[rson]. L=ID; NODE[ID].FA=Rson; Node[id]. R=TMP2. L Node[tmp2. L].FA=ID;}voidLR (intID) { intlson=Node[id]. L RR (Lson); LL (ID);}voidRL (intID) { intrson=Node[id]. R LL (Rson); RR (ID);}voidRotate (intID) { intneed=-1; intp=Node[id].fa; while(1) { if(p==-1) Break; if(ABS (NODE[P). LH-NODE[P]. RH) >1) {need=p; Break; } P=Node[p].fa; } if(need==-1)return; if(Node[need]. Lh>Node[need]. RH) {intlson=Node[need]. L if(Node[lson]. Lh>Node[lson]. RH) LL (need); ElseLR (need); } Else { intrson=Node[need]. R if(Node[rson]. Rh>Node[rson]. LH) RR (need); ElseRL (need); } for(intI=0; i<=sz;i++)if(node[i].fa==-1) root=i; DFS (root);}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Init (); for(intI=2; i<=n;i++) {Insert (a[i]); Rotate (SZ); } printf ("%d\n", Node[root].num); return 0;}
PAT (Advanced Level) 1066. Root of AVL Tree (25)