SPLAY是一种平衡树,特点是每次操作都将操作节点旋转到根节点。
维护的信息有大小sz
,次数cnt
,值val
,父亲节点fa[MAXN]
,儿子节点ch[MAXN][2]
全局变量有:根节点rt
,节点编码总数tot
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 #include <iostream> using namespace std;const int MAXN=1e5 +2 ;int rt,tot,fa[MAXN],ch[MAXN][2 ],val[MAXN],cnt[MAXN],sz[MAXN];void maintain (int x) {sz[x]=sz[ch[x][0 ]]+sz[ch[x][1 ]]+cnt[x];}bool get (int x) {return x==ch[fa[x]][1 ];}void clear (int x) {fa[x]=ch[x][0 ]=ch[x][1 ]=val[x]=cnt[x]=sz[x]=0 ;}void rotate (int x) { int y=fa[x],z=fa[y],chk=get (x); ch[y][chk]=ch[x][chk^1 ]; if (ch[x][chk^1 ])fa[ch[x][chk^1 ]]=y; ch[x][chk^1 ]=y; fa[y]=x; if (z)ch[z][y==ch[z][1 ]]=x; fa[x]=z; maintain (y); maintain (x); } void splay (int x) { for (int f=fa[x];f=fa[x],f;rotate (x)){ if (fa[f])rotate (get (x)==get (f)?f:x); } rt=x; } void ins (int k) { if (!rt){ val[++tot]=k; cnt[tot]++; rt=tot; maintain (rt); return ; } int cur=rt,f=0 ; while (1 ){ if (val[cur]==k){ cnt[cur]++; maintain (cur); maintain (f); splay (cur); break ; } f=cur; cur=ch[cur][val[cur]<k]; if (!cur){ val[++tot]=k; cnt[tot]++; fa[tot]=f; ch[f][val[f]<k]=tot; maintain (tot); maintain (f); splay (tot); break ; } } } int rk (int k) { int res=0 ,cur=rt; while (1 ){ if (val[cur]>k){ cur=ch[cur][0 ]; }else { res+=sz[ch[cur][0 ]]; if (val[cur]==k){ splay (cur); return res+1 ; } res+=cnt[cur]; cur=ch[cur][1 ]; } } } int kth (int k) { int cur=rt; while (1 ){ if (ch[cur][0 ]&&k<=sz[ch[cur][0 ]]){ cur=ch[cur][0 ]; }else { k-=sz[ch[cur][0 ]]+cnt[cur]; if (k<=0 ){ splay (cur); return val[cur]; } cur=ch[cur][1 ]; } } } int pre () { int cur=ch[rt][0 ]; if (!cur)return cur; while (ch[cur][1 ])cur=ch[cur][1 ]; splay (cur); return cur; } int nxt () { int cur=ch[rt][1 ]; if (!cur)return cur; while (ch[cur][0 ])cur=ch[cur][0 ]; splay (cur); return cur; } void del (int k) { rk (k); if (cnt[rt]>1 ){ cnt[rt]--; maintain (rt); return ; } if (!ch[rt][0 ]&&!ch[rt][1 ]){ clear (rt); rt=0 ; return ; } if (!ch[rt][0 ]){ int cur=rt; rt=ch[rt][1 ]; fa[rt]=0 ; clear (cur); return ; } if (!ch[rt][1 ]){ int cur=rt; rt=ch[rt][0 ]; fa[rt]=0 ; clear (cur); return ; } int cur=rt,x=pre (); fa[ch[cur][1 ]]=x; ch[x][1 ]=ch[cur][1 ]; clear (cur); maintain (rt); } int main () { ins (1 ); ins (2 ); ins (3 ); ins (114 ); ins (514 ); ins (1919 ); cout<<rk (114 )<<endl; cout<<kth (3 )<<endl; del (114 ); del (3 ); cout<<rk (2 )<<endl; cout<<kth (3 )<<endl; return 0 ; }
总结:SPLAY支持了基本操作,例如查询rank和kth_element,是一种较简单的平衡树
动画:SPLAY
Tree