标签:
/* b本树较弱 支持插入+查询(删除太麻烦0.0) 复杂度 对于随机数据O(n*logn) 特殊的 如果一条链下来 O(n*n) (升级版:平衡树. ) */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 100010 using namespace std; struct node { int lch; int rch; int val; }tree[maxn]; int n,m,topt; void Add_tree(int &now,int val)//插入1 & 可以改变上一层 tree[now].lch,的值 { if(!now) { now=++topt; tree[now].val=val; return ; } if(val<tree[now].val)Add_tree(tree[now].lch,val); if(val>=tree[now].val)Add_tree(tree[now].rch,val); } /*void Add_tree2(int val)//插入2 { if(topt==0) { topt++; tree[1].val=val; return ; } int now=1; while(1) { if(val<tree[now].val) { if(tree[now].lch) { now=tree[now].lch; continue; } else { tree[now].lch=++topt; tree[topt].val=val; return; } } else { if(tree[now].rch) { now=tree[now].rch; continue; } else { tree[now].rch=++topt; tree[topt].val=val; return; } } } }*/ int Find_tree(int val) { int now=1; while(1) { if(val==tree[now].val)return 1; if(tree[now].lch==0&&tree[now].rch==0) return 2; if(val<tree[now].val) { if(tree[now].lch) { now=tree[now].lch; continue; } else return 2; } if(val>tree[now].val) { if(tree[now].rch) { now=tree[now].rch; continue; } else return 2; } } } int main() { scanf("%d",&n); int x,y,root=0; for(int i=1;i<=n;i++) { scanf("%d%d",&x,&y); if(x==0)Add_tree(root,y); if(x==1)Delete_tree(y); if(x==2)printf("%d ",y,Find_tree(y)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/yanlifneg/p/5463554.html