The SBT template is incomplete and will be filled in later
Source: Internet
Author: User
1 int top;
2 struct SBT {
3 int L, R, size, key;
4 void Init (){
5 L = r = 0; size = 1;
6}
7} t [Max];
8
9 void r_roate (Int & T) {// right Rotation
10 int K = T [T]. L;
11 T [T]. L = T [K]. R;
12 t [K]. r = T;
13 T [K]. size = T [T]. size;
14 t [T]. size = T [T [T]. R]. Size + T [T [T]. L]. Size + 1;
15 t = K;
16 return;
17}
18 void l_roate (Int & T) {// left Rotation
19 int K = T [T]. R;
20 t [T]. r = T [K]. L;
21 t [K]. L = T;
22 t [K]. size = T [T]. size;
23 t [T]. size = T [T [T]. L]. Size + T [T [T]. R]. Size + 1;
24 t = K;
25 return;
26}
27
28 void maintain (Int & T, bool flag) {// adjust
29 If (! Flag ){
30 if (T [T [T [T]. L]. L]. size> T [T [T]. R]. Size ){
31 r_roate (t );
32}
33 else if (T [T [T [T]. L]. R]. size> T [T [T]. R]. Size ){
34 r_roate (T [T]. L );
35 r_roate (t );
36}
37 Else return;
38}
39 else {
40 if (T [T [T [T]. R]. R]. size> T [T [T]. L]. Size ){
41 l_roate (t );
42}
43 else if (T [T [T [T]. R]. L]. size> T [T [T]. L]. Size ){
44 r_roate (T [T]. R );
45 l_roate (t );
46}
47 else return;
48}
49 maintain (T [T]. l, false );
50 maintain (T [T]. R, true );
51 maintain (T, false );
52 maintain (T, true );
53}
54
55 void insert (Int & T, int v) {// insert
56 If (t = 0 ){
57 t = ++ top;
58 T [T]. INIT ();
59 T [T]. Key = V;
60}
61 else {
62 T [T]. Size ++;
63 If (T [T]. Key> V) insert (T [T]. l, V );
64 else insert (T [T]. R, V );
65 maintain (T, V> = T [T]. Key );
66}
67}
68 int PRED (int t, int v) {// The maximum number smaller than v. If there is no number smaller than V, V is returned.
69 If (t = 0 ){
70 return V;
71}
72 If (V> T [T]. Key ){
73 int res = PRED (T [T]. R, V );
74 if (RES = V) return T [T]. Key;
75 return res;
76}
77 else return PRED (T [T]. l, V );
78}
79 int succ (int t, int v) {// return the smallest number greater than v. If all values are less than V, return v.
80 If (t = 0) return V;
81 If (V> = T [T]. Key) return succ (T [T]. R, V );
82 else {
83 int res = succ (T [T]. l, V );
84 If (V = res) return T [T]. Key;
85 return res;
86}
87}
88
89 int exist (int t, int v ){
90 if (t = 0) return false;
91 If (v <t [T]. Key) return exist (T [T]. l, V );
92 else if (V = T [T]. Key) return true;
93 else return exist (T [T]. R, V );
94}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.