Reprinted please indicate the source, thank youHttp://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove
Balance Binary Tree, adjustment includes left rotation and right rotation, where direct rotation and combined rotation, bad drawing, specific SBT can see http://blog.csdn.net/acceptedxukai/article/details/6921334
Click one by yourself and leave it as a template. You can change it to poj 3481.
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # define n 1000005 using namespace STD; struct SBT {// left subtree pointer, right subtree pointer, size, key value int left, right, size, key; void Init () {left = right = Key = 0; size = 1 ;}} T [N]; int root, TOT; // root position and number of nodes // void left_rot (Int & X) {int K = T [X]. right; t [X]. right = T [K]. left; t [K]. left = x; t [K]. size = T [X]. size; t [X]. size = T [T [X]. left]. size + T [T [X]. right]. size + 1; X = K;} // right rotation void right _ Rot (Int & X) {int K = T [X]. left; t [X]. left = T [K]. right; t [K]. right = x; t [K]. size = T [X]. size; t [X]. size = T [T [X]. left]. size + T [T [X]. right]. size + 1; X = K;} // adjust the processing void maintain (Int & R, bool flag) {If (FLAG) {// update the right subtree if (T [T [T [R]. right]. right]. size> T [T [R]. left]. size) left_rot (r); else if (T [T [R]. right]. left]. size> T [T [R]. left]. size) {right_rot (T [R]. right); left_rot (r);} elsereturn;} else {// update in the left subtree if (T [T [T [R]. left]. left]. size> T [T [R]. right]. size) right_rot (r); else if (T [T [R]. left]. right]. size> T [T [R]. right]. size) {left_rot (T [R]. left); right_rot (r);} else return;} // update the subtree, and then update the root until the balance ends maintain (T [R]. left, false); maintain (T [R]. right, true); maintain (R, false); maintain (R, true);} // Insert a new node void insert (Int & R, int K) {If (r = 0) {r = ++ tot; t [R]. init (); t [R]. key = K;} else {T [R]. size ++; If (k <t [R]. key) insert (T [R]. left, k); elseinsert (T [R]. right, k); // adjust the value after insertion Certificate balance maintain (R, k> = T [R]. key) ;}} // Delete the node. Replace int remove (Int & R, int K) {int d_key; If (! R) return 0; t [R]. size --; // The former indicates the node to be deleted, and the latter indicates that this node does not exist if (T [R]. key = k | (T [R]. left = 0 & K <t [R]. key) | (T [R]. right = 0 & K> T [R]. key) {d_key = T [R]. key; If (T [R]. left & T [R]. right) T [R]. key = remove (T [R]. left, k + 1); elser = T [R]. left + T [R]. right;} else remove (k <t [R]. key? T [R]. left: T [R]. right, k);} // get the maximum value, that is, traverse to the rightmost node int get_max (int r) {While (T [R]. right) r = T [R]. right; return r;} // get the minimum value, that is, traverse to the leftmost node int get_min (int r) {While (T [R]. left) r = T [R]. left; return r;} // obtain the first int get_pre (Int & R, int y, int K) {If (r = 0) return y; if (k> T [R]. key) get_pre (T [R]. right, R, k); elseget_pre (T [R]. left, Y, k);} // obtain the next int get_next (Int & R, int y, int K) {If (r = 0) return y; if (k <t [R]. key) get_next (T [R]. left, R, k); elseget_next (T [R]. right, Y, k);} // obtain the number smaller than K. Note: int get_kth (Int & R, int K) with the number of duplicates cannot be solved) {int T = T [T [R]. left]. size + 1; if (t = k) return T [R]. key; If (T <k) return get_kth (T [R]. right, K-R); else return get_kth (T [R]. left, k);} // obtain the rank int get_rank (Int & R, int K) of the node {If (k <t [R]. key) return get_rank (T [R]. left, k); else if (k> T [R]. key) return get_rank (T [R]. right, k) + T [T [R]. left]. size + 1; elsereturn T [T [R]. left]. size + 1 ;}// sort void inorder (Int & R) {If (r = 0) return; inorder (T [R]. left); printf ("% d \ n", t [R]. key); inorder (T [R]. right );}