void add(int x,int y,int value) { if (y<=key[x]) { if (key[x]==y) { v[x]=max(v[x],value); update(x); return; } if (!l[x]) { l[x]=node(x,y,value); Splay(l[x],0); } else add(l[x],y,value); } else if (!r[x]) { r[x]=node(x,y,value); Splay(r[x],0); } else add(r[x],y,value); update(x); }
void Update(int l, int r, int value) { Splay(Get_Kth(root, l), 0); //第l个节点到根节点 因为root根节点的father是0 做完这步操作之后新root为下标l对应的节点 //而因为r-2 > l,所以Get_Kth(root, r+2)出来的节点一定在新root的右边,所以下一步Splay(Get_Kth(root, r+2), root) 才表示旋转r+2对应节点到根节点的右孩子,而不是左孩子。提醒:记住这一步之后root节点变了;Get_Kth()是根据size来找的. Splay(Get_Kth(root, r+2), root); //第r+2个点到根节点的右孩子 Put_self(Key_value, value); }
//将第pos个数开始的连续tot个数进行反转 void Reverse(int pos,int tot) { Splay(Get_kth(root,pos),0); Splay(Get_kth(root,pos+tot+1),root); Update_Rev(Key_value); push_up(ch[root][1]); push_up(root); }
//将从第pos个数开始的连续的tot个数修改为c void Make_Same(int pos,int tot,int c) { Splay(Get_kth(root,pos),0); Splay(Get_kth(root,pos+tot+1),root); Update_Same(Key_value,c); push_up(ch[root][1]); push_up(root); }
//在第pos个数后面插入tot个数 void Insert(int pos,int tot) { for(int i = 0;i < tot;i++)scanf("%d",&a[i]); Splay(Get_kth(root,pos+1),0); Splay(Get_kth(root,pos+2),root); Build(Key_value,0,tot-1,ch[root][1]); push_up(ch[root][1]); push_up(root); }
void Delete(unsigned int st, unsigned int ed) { Insert(st); NODE *p1 = FindPrevValue(st); Insert(ed); NODE *p2 = FindNextValue(ed); Splay(p1, nil); Splay(p2, root); p2->ch[0] = nil; }
//从第pos个数开始连续删除tot个数 void Delete(int pos,int tot) { Splay(Get_kth(root,pos),0); Splay(Get_kth(root,pos+tot+1),root); erase(Key_value); pre[Key_value] = 0; Key_value = 0; push_up(ch[root][1]); push_up(root); }
void Delete(unsigned int value) { NODE *p = root, *q = NULL; FindValue(value, p, q); if (p != nil) { // printf("Enter Delete\n"); Splay(p, nil); NODE *p1 = FindPrevValue(value); NODE *p2 = FindNextValue(value); Splay(p1, nil); Splay(p2, root); p2->ch[0] = nil; } }
inline int Rank(int v) { Node* t = Select(root, v); if (t == null) return 0; else { Splay(t, null); return t->ch[0]->size + 1; } }
inline int CalMax(int v) { Node* t = SelectMax(root, v); if (t == null) return 0; else { Splay(t, null); return t->ch[1]->size + 1; } }
void insertIntoTree(Kmer * kmer, SplayTree ** T) { SplayNode *newNode; if (*T == NULL) { newNode = allocateSplayNode(); copyKmers(&(newNode->kmer), kmer); newNode->left = newNode->right = NULL; *T = newNode; return; } *T = Splay(kmer, *T); if (compareKmers(kmer, &((*T)->kmer)) < 0) { newNode = allocateSplayNode(); copyKmers(&(newNode->kmer), kmer); newNode->left = (*T)->left; newNode->right = *T; (*T)->left = NULL; *T = newNode; } else if (compareKmers(&((*T)->kmer), kmer) < 0) { newNode = allocateSplayNode(); copyKmers(&(newNode->kmer), kmer); newNode->right = (*T)->right; newNode->left = *T; (*T)->right = NULL; *T = newNode; } }
ubi_btNodePtr ubi_sptFind( ubi_btRootPtr RootPtr, ubi_btItemPtr FindMe ) /* ------------------------------------------------------------------------ ** * This function performs a non-recursive search of a tree for any node * matching a specific key. * * Input: * RootPtr - a pointer to the header of the tree to be searched. * FindMe - a pointer to the key value for which to search. * * Output: * A pointer to a node with a key that matches the key indicated by * FindMe, or NULL if no such node was found. * * Note: In a tree that allows duplicates, the pointer returned *might * not* point to the (sequentially) first occurance of the * desired key. In such a tree, it may be more useful to use * ubi_sptLocate(). * ------------------------------------------------------------------------ ** */ { ubi_btNodePtr p; p = ubi_btFind( RootPtr, FindMe ); if( p ) RootPtr->root = Splay( p ); return( p ); } /* ubi_sptFind */
ubi_btNodePtr ubi_sptLocate( ubi_btRootPtr RootPtr, ubi_btItemPtr FindMe, ubi_trCompOps CompOp ) /* ------------------------------------------------------------------------ ** * The purpose of ubi_btLocate() is to find a node or set of nodes given * a target value and a "comparison operator". The Locate() function is * more flexible and (in the case of trees that may contain dupicate keys) * more precise than the ubi_btFind() function. The latter is faster, * but it only searches for exact matches and, if the tree contains * duplicates, Find() may return a pointer to any one of the duplicate- * keyed records. * * Input: * RootPtr - A pointer to the header of the tree to be searched. * FindMe - An ubi_btItemPtr that indicates the key for which to * search. * CompOp - One of the following: * CompOp Return a pointer to the node with * ------ --------------------------------- * ubi_trLT - the last key value that is less * than FindMe. * ubi_trLE - the first key matching FindMe, or * the last key that is less than * FindMe. * ubi_trEQ - the first key matching FindMe. * ubi_trGE - the first key matching FindMe, or the * first key greater than FindMe. * ubi_trGT - the first key greater than FindMe. * Output: * A pointer to the node matching the criteria listed above under * CompOp, or NULL if no node matched the criteria. * * Notes: * In the case of trees with duplicate keys, Locate() will behave as * follows: * * Find: 3 Find: 3 * Keys: 1 2 2 2 3 3 3 3 3 4 4 Keys: 1 1 2 2 2 4 4 5 5 5 6 * ^ ^ ^ ^ ^ * LT EQ GT LE GE * * That is, when returning a pointer to a node with a key that is LESS * THAN the target key (FindMe), Locate() will return a pointer to the * LAST matching node. * When returning a pointer to a node with a key that is GREATER * THAN the target key (FindMe), Locate() will return a pointer to the * FIRST matching node. * * See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf(). * ------------------------------------------------------------------------ ** */ { ubi_btNodePtr p; p = ubi_btLocate( RootPtr, FindMe, CompOp ); if( p ) RootPtr->root = Splay( p ); return( p ); } /* ubi_sptLocate */
boolean findOrInsertOccurenceInSplayTree(Kmer * kmer, IDnum * seqID, Coordinate * position, SplayTree ** T) { SplayNode *newNode; if (*T == NULL) { newNode = allocateSplayNode(); copyKmers(&(newNode->kmer), kmer); newNode->seqID = *seqID; newNode->position = *position; newNode->left = newNode->right = NULL; *T = newNode; return false; } *T = Splay(kmer, *T); if (compareKmers(kmer, &((*T)->kmer)) < 0) { newNode = allocateSplayNode(); copyKmers(&(newNode->kmer), kmer); newNode->seqID = *seqID; newNode->position = *position; newNode->left = (*T)->left; newNode->right = *T; (*T)->left = NULL; *T = newNode; printf("1: sequenceID = %d\n", *seqID); return false; } else if (compareKmers(kmer, &((*T)->kmer)) > 0) { newNode = allocateSplayNode(); copyKmers(&(newNode->kmer), kmer); newNode->seqID = *seqID; newNode->position = *position; newNode->right = (*T)->right; newNode->left = *T; (*T)->right = NULL; *T = newNode; printf("2: sequenceID = %d\n", *seqID); return false; } else { *seqID = (*T)->seqID; *position = (*T)->position; printf("3: sequenceID = %d\n", *seqID); return true; } }
inline void Delete(long t) { long l1 = Prev(root, t), l2 = Succ(root, t); if (l1 != inf && l2 != inf) { Splay(l1, 0); Splay(l2, l1); S[l2].l = 0; }else if (l1 == inf && l2 != inf) { Splay(l2, 0); S[root].l = 0; }else if (l1 != inf && l2 == inf) { Splay(l1, 0); S[root].r = 0; }else root = 0; }
ubi_btNodePtr ubi_sptRemove( ubi_btRootPtr RootPtr, ubi_btNodePtr DeadNode ) /* ------------------------------------------------------------------------ ** * This function removes the indicated node from the tree. * * Input: RootPtr - A pointer to the header of the tree that contains * the node to be removed. * DeadNode - A pointer to the node that will be removed. * * Output: This function returns a pointer to the node that was removed * from the tree (ie. the same as DeadNode). * * Note: The node MUST be in the tree indicated by RootPtr. If not, * strange and evil things will happen to your trees. * ------------------------------------------------------------------------ ** */ { ubi_btNodePtr p; (void)Splay( DeadNode ); /* Move dead node to root. */ if( NULL != (p = DeadNode->Link[ubi_trLEFT]) ) { /* If left subtree exists... */ ubi_btNodePtr q = DeadNode->Link[ubi_trRIGHT]; p->Link[ubi_trPARENT] = NULL; /* Left subtree node becomes root.*/ p->gender = ubi_trPARENT; p = ubi_btLast( p ); /* Find rightmost left node... */ p->Link[ubi_trRIGHT] = q; /* ...attach right tree. */ if( q ) q->Link[ubi_trPARENT] = p; RootPtr->root = Splay( p ); /* Resplay at p. */ } else { if( NULL != (p = DeadNode->Link[ubi_trRIGHT]) ) { /* No left, but right subtree exists... */ p->Link[ubi_trPARENT] = NULL; /* Right subtree root becomes... */ p->gender = ubi_trPARENT; /* ...overall tree root. */ RootPtr->root = p; } else RootPtr->root = NULL; /* No subtrees => empty tree. */ } (RootPtr->count)--; /* Decrement node count. */ return( DeadNode ); /* Return pointer to pruned node. */ } /* ubi_sptRemove */
NODE* Insert(unsigned int value) { NODE *p = root, *q = NULL; FindValue(value, p, q); if (p == nil) p = GetNode(value, q); Splay(p, nil); return p; }
int get(int ID,int ly,int ry) { id=ID; insert(id,ly,0); int p=find(root[id],ly); insert(id,ry,0); int q=find(root[id],ry); if (p==q) return(v[p]); Splay(p,0); Splay(q,p); int ans=max(ma[l[q]],max(v[p],v[q])); if (v[p]==0) del(p); if (v[q]==0) del(q); return(ans); }
NODE* FindNextValue(unsigned int value) { NODE *p = root, *q = NULL; FindValue(value, p, q); Splay(p, nil); q = p->ch[1]; while (q->ch[0] != nil) q = q->ch[0]; return q; }
inline void Insert(Node* p, Node*& now, int v) { if (now == null) { now = Renew(v); now->p = p; Splay(now, null); return; } now->size++; bool d = (v > now->v); Insert(now, now->ch[d], v); }
inline void Delete(int v) { Node* t = Select(root, v); if (t == null) { t = GetMin(root); Splay(t, null); t->ch[1]->p = null; root = t->ch[1]; return; } Node* now = Succ(t); Node* L = Prev(now), *R = Succ(now); Splay(now, null); if (L == null || R == null) { bool d = (R != null); root = now->ch[d], root->p = null; return; } Splay(L, root); Splay(R, root); L->Set(R, 1); L->Update(); L->p = null; }
int Select(int k) { printf("%d\n",k); int x = root; while (sz[l[x]] + 1 != k) { if (sz[l[x]] + 1 < k) k -= sz[l[x]] + 1, x = r[x]; else x = l[x]; } Splay(x); return x; }
lpNode join(lpNode x, lpNode y) { if (x == 0) return y; else { lpNode m = max_min(x, 1); Splay(m, x); m->child[1] = y; if (y) y->father = m; return x; } }
void del(int p) { Splay(p,0); int t=r[p]; if (!l[p]) { f[t]=0; root[id]=t; } else { int q=l[p]; while (r[q]) q=r[q]; Splay(q,p); r[q]=t; f[t]=q; f[q]=0; root[id]=q; update(q); } }
inline void Insert(long p, long t, double xi, double yi) { while (true) { if (!t) { t = ++sid; S[ sid ].x = xi, S[ sid ].y = yi, S[sid].p = p; if (xi < S[p].x) S[p].l = t; else S[p].r = t; Splay(t, 0); break; } if (xi < S[t].x) p = t, t = S[t].l; else if (xi > S[t].x) p = t, t = S[t].r; if (xi == S[t].x) { if (yi > S[t].y) S[t].y = yi; Splay(t, 0); break; } } }
void Merge(Tree* tree) { Vertex<T>* tmp(root); while (tmp->right_son != &null_vertex) { tmp = tmp->right_son; } Splay(tmp); tree->root->father = tmp; if (tmp != &null_vertex) { tmp->right_son = tree->root; } else if (tmp == &null_vertex) { root = tree->root; } tree->root = &null_vertex; }
lpNode find(int x) { lpNode s = root; while (true) if (s == 0) return 0; else if (s->val == x) { Splay(s, root); return s; } else s = s->child[x > s->val]; }
void insert(int x) { if (root == 0) root = mem->alloc(x, 0); else { lpNode s = root, p = 0; while (s) p = s, s = s->child[x > s->val]; s = mem->alloc(x, p); p->child[x > p->val] = s; Splay(s, root); } }
inline void RotateTo(int k,int goal) {//把第k位的数转到goal下边 int x = root; push_down(x); while(sz[ ch[x][0] ] != k) { if(k < sz[ ch[x][0] ]) { x = ch[x][0]; } else { k -= (sz[ ch[x][0] ] + 1); x = ch[x][1]; } push_down(x); } Splay(x,goal); }
void Select(int k,int g) { int x=rt; while(num[son[x][0]] != k) { if(k < num[son[x][0]]) x=son[x][0]; else { k-=1+num[son[x][0]]; x=son[x][1]; } } Splay(x,g); }