Splay* splay(Splay* &Root) { Splay *y,*z,*rf=Root->fa; while (fa!=rf) { y=fa; z=y->fa; if (z==null) this==y->l?zig():zag(); else if (y==z->l) this==y->l?(y->zig(),zig()):(zag(),zig()); else this==y->r?(y->zag(),zag()):(zig(),zag()); } updata(); return Root=this; }
Splay* splay() { Splay *y,*z; if (this==null) return this; while (fa!=null) { y=fa; z=y->fa; if (z==null) this==y->l?zig():zag(); else if (y==z->l) this==y->l?(y->zig(),zig()):(zag(),zig()); else this==y->r?(y->zag(),zag()):(zig(),zag()); } update(); return this; }
splay_tree splay (splay_tree node) { while (node->parent != NULL) { if (node->parent->parent != NULL) { if (node->parent->parent->lchild == node->parent) // left { if (node->parent->lchild == node) // left-left { node->parent = zig(node->parent); node = zig(node); } else // left-right { node = zag(node); node = zig(node); } } else // right { if (node->parent->rchild == node) // right-right { node->parent = zag(node->parent); node = zag(node); } else // right-left { node = zig(node); node = zag(node); } } } else { if (node->parent->lchild = node) // left { node = zig(node); } else // right { node = zag(node); } } } return node; }
void splay(node *p) { while (p->p != NULL) { reverse(p->p->p); reverse(p->p); reverse(p); if (p->p->p == NULL) { if (p == p->p->l) { zig(p); } else { zag(p); } } else if (p->p == p->p->p->l) { if (p == p->p->l) { zigzig(p); } else { zagzig(p); } } else { if (p == p->p->l) { zigzag(p); } else { zagzag(p); } } } }
bool zigzag(struct hand **handp_loc, struct hand *parent) { struct hand *root = *handp_loc; struct hand *target, *new_left, *new_right; if(parent == root->left) { target = parent->right; new_left = parent; new_right = root; } else { target = parent->left; new_left = root; new_right = parent; } if (!target) { //Could not completely zig zag zig(handp_loc, parent); return false; } new_right->left = target->right; new_left->right = target->left; target->left = new_left; target->right = new_right; *handp_loc = target; return true; }
void SplayTree::splay(TreeNode* node) { while (node != this->root) { if (node->parent == this->root) { zig(node); } else { if ((node == node->parent->left) && (node->parent == node->parent->parent->left)) { // оба левые дети zigZig(node, ZIG_LEFT); } else if ((node == node->parent->right) && (node->parent == node->parent->parent->right)) { // оба правые дети then zigZig(node, ZIG_RIGHT); } else if ((node == node->parent->left) && (node->parent == node->parent->parent->right)) { // node — левый ребенок, а node->parent — правый ребенок then zigZag(node, ZIG_LEFT); } else if ((node == node->parent->right) && (node->parent == node->parent->parent->left)) { // node — правый ребенок, а node->parent — левый ребенок then zigZag(node, ZIG_RIGHT); } } } }
struct elem * ins(struct elem *const e, const KEY_T key) { int b; if (!e) return new_elem(key); if (e->key == key) return e; if (key < e->key) e->left = ins(e->left, key); else e->right = ins(e->right, key); update(e), b = balance_factor(e); return b > 1 && key < e->left->key ? zig(e) : b > 1 && key > e->left->key ? (e->left = zag(e->left), zig(e)) : b < -1 && key > e->right->key ? zag(e) : b < -1 && key < e->right->key ? (e->right = zig(e->right), zag(e)) : e; }
inline void splay(int &root,int x) { for (int y,z;Par(x);) { y=Par(x),z=Par(y); if (!z) if (Lch(y)==x) zig(x); else zag(x); else if (Lch(z)==y) if (Lch(y)==x) zig(y),zig(x); else zag(x),zig(x); else if (Rch(y)==x) zag(y),zag(x); else zig(x),zag(x); } Tupdate(root=x); }
void turnup(long pp) { if (f[pp]==0) return; if (r[pp]>=r[f[pp]]) return; if (p[0][f[pp]]==pp) { zig(pp); turnup(pp); return; } if (p[1][f[pp]]==pp) { zag(pp); turnup(pp); return; } pp=pp/(pp-pp); }
//Assumes *handp_loc is not NULL void splay(struct hand **handp_loc, int needle) { struct hand *hand = *handp_loc; while (needle != hand->card.rank) { struct hand *side; int factor = 1; if (needle < hand->card.rank) { side = hand->left; } else { side = hand->right; factor = -1; } if (!side) { //target does not exist, stop break; } else if (factor * (needle - side->card.rank)>0) { //Target is an inner node if(zigzag(&hand, side)) { continue; } else { //could not complete a zigzag, target does not exist break; } } else { //Target is on the outside zig(&hand, side); continue; } } *handp_loc = hand; }
void treapins(number * & p,ll pl,ll pr,number * np){ if(!p ) { tisp = p = np; np->lc = np->rc = NULL; } else{ int c = numxcmp(np,p); if(c < 0){ treapins(p->lc,pl,p->id-1,np); if(p->lc->pri < p->pri) zig(p); } else if(c > 0) { treapins(p->rc,p->id+1,pr,np); if(p->rc->pri < p->pri) zag(p); } else tisp = p; } if(p == np) tispl = pl,tispr = pr; }
inline void rotate(root& o, int d) { d? zig(o): zag(o); d? o->rson->maintain(): o->lson->maintain(); o->maintain(); }
void zagzig(node* p) { zag(p); zig(p); }
void zigzag(node* p) { zig(p); zag(p); }
void zigzig(node* p) { zig(p->p); zig(p); }
struct elem * post_del(struct elem *const e) { int b; if (!e) return NULL; update(e), b = balance_factor(e); return b > 1 ? (balance_factor(e->left) >= 0 ? zig(e) : (e->left = zag(e->left), zig(e))) : b < -1 ? (balance_factor(e->right) <= 0 ? zag(e) : (e->right = zig(e->right), zag(e))) : e; }
void splay(SPTNode *T){ SPTNode *p = T; while(p&&p->father){ p->value < p->father->value ? zig(p) : zag(p); } }