标签:std style color logs pen else oid hid lld
#include<cstdio> struct TreeNode{ long long val; TreeNode *left,*right,*fa; }node[200010],*root; int node_num; void tree_insert(TreeNode* k,TreeNode* now){ if(k==now) return; if(now->val>=k->val){ if(now->left==NULL){ now->left=k; k->fa=now; } else tree_insert(k,now->left); } else if(now->val<k->val){ if(now->right==NULL){ now->right=k; k->fa=now; } else tree_insert(k,now->right); } return; } TreeNode* tree_ques(long long k,TreeNode* now){ if(now->val==k){ return now; } else{ if(k<=now->val){ if(now->left==NULL) return NULL; else return tree_ques(k,now->left); } if(k>now->val){ if(now->right==NULL) return NULL; else{ TreeNode *a=NULL; if(now->right==NULL) return NULL; else return tree_ques(k,now->right); } } } return NULL; } void tree_delete(TreeNode* now){ TreeNode *l=now->left,*r=now->right,*f=now->fa; if(f==NULL){ if(l==NULL||r==NULL){ now->val=0; if(l!=NULL){ l->fa=NULL; root=l; } else if(r!=NULL){ r->fa=NULL; root=r; } else root=NULL; return; } else{ if(l!=NULL) l->fa=NULL; if(r!=NULL) r->fa=NULL; now->val=0; root=l; tree_insert(r,l); return; } } else{ if(now==f->left) f->left=NULL; if(now==f->right) f->right=NULL; if(l!=NULL) l->fa=NULL; if(r!=NULL) r->fa=NULL; if(l!=NULL) tree_insert(l,f); if(r!=NULL) tree_insert(r,f); now->val=0; return; } return; } void grand_order(TreeNode *now){ if(now==NULL) return; grand_order(now->left); printf("%lld ",now->val); grand_order(now->right); return; } void prev_order(TreeNode *now){ if(now==NULL) return; printf("%lld ",now->val); prev_order(now->left); prev_order(now->right); return; } void rotate(TreeNode *now){ if(now->fa==NULL) return; TreeNode *f=now->fa,*gf=f->fa,*l=now->left,*r=now->right; now->fa=gf; if(gf!=NULL){ if(gf->left==f) gf->left=now; if(gf->right==f) gf->right=now; } if(f->left==now){ now->right=f; f->left=r; if(r!=NULL) r->fa=f; } if(f->right==now){ now->left=f; f->right=l; if(l!=NULL) l->fa=f; } f->fa=now; if(now->fa==NULL) root=now; return; } void splay(TreeNode *now,TreeNode *anc){ if(now==anc) return; TreeNode *f,*gf; while(now->fa!=anc){ f=now->fa; gf=f->fa; if(gf==anc){ rotate(now); } else if(gf->left==f&&f->right==now){ rotate(now); rotate(now); } else if(gf->right==f&&f->left==now){ rotate(now); rotate(now); } else if(gf->left==f&&f->left==now){ rotate(f); rotate(now); } else if(gf->right==f&&f->right==now){ rotate(f); rotate(now); } } if(now->fa==NULL) root=now; return; } int n,q,x,p; int main(){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%lld",&x); node[node_num].val=x; node[node_num].fa=NULL; node[node_num].left=NULL; node[node_num].right=NULL; tree_insert(&node[node_num],&node[0]); node_num++; } root=&node[0]; scanf("%d",&q); while(q--){ scanf("%d",&p); if(p==1){ scanf("%lld",&x); if(tree_ques(x,root)!=NULL){ printf("found.\n"); splay(tree_ques(x,root),root); } else printf("not found.\n"); } if(p==2){ scanf("%lld",&x); while(tree_ques(x,root)!=NULL){ tree_delete(tree_ques(x,root)); } printf("deleted.\n"); } if(p==3){ scanf("%lld",&x); node[node_num].val=x; node[node_num].fa=NULL; node[node_num].left=NULL; node[node_num].right=NULL; tree_insert(&node[node_num],root); node_num++; printf("Inserted.\n"); } if(p==4){ grand_order(root); printf("\n"); } if(p==5){ prev_order(root); printf("\n"); } } return 0; }
写了个最基本操作的splay。
同时上一张神妈的图。
博客园的头像没有用神妈,主要是我的图清晰度都不好,这张龙娘的算十分完美的,就用了。
标签:std style color logs pen else oid hid lld
原文地址:http://www.cnblogs.com/halifuda/p/7905841.html