首页 > 编程知识 正文

二叉排序树的作用,Calculus AB 教学计划

时间:2023-05-04 10:44:46 阅读:171204 作者:30

模板测试问题

可重复数字平衡树注意:使用lower_bound和upper_bound非常慢,因为比普通的手写平衡树慢3倍,并且维护的要素是结构,所以使用orper _ bound代替orper _ bound

# include bits/stdc.h # include ext/Pb _ ds/tree _ policy.HPP # include ext/Pb _ ds/assoc _ container.HPP _ d int N,op,x; struct Tree{int inf=1e9,idx=0; struct node{int x,zddwx; OOLoperator(nodeo ) const (if ) x!=o.x ) return x o.x; else return zddwx o.zddwx; }; treenode、null_type、lessnode、rb_tree_tag、tree _ order _ statistics _ node _ updatet; voidinsert(intx ) qkdkfd (idx ); }boolhave(intx ) xnode cur={x,0}; intorder=t.order_of_key(cur; if(order1(int ) T.size ) )返回0; autoit=t.find_by_order(order; if () it ).x!=x )返回0; 返回1; }voiderase(intx ) if! have(x ) )返回; node cur={x,0}; intorder=t.order_of_key(cur; autoit=t.find_by_order(order; t.erase(it ); }intgetrank(intx )//查询x的排名returnt.order_of_key ) {x,0} ) 1; (通过intgetbyrank(intrank )//等级x的元素是谁if ) (int ) T.size ) rank ) return -inf; return(t.find_by_order(rank-1 ) ).x; (intgetpre(intx ) ) /查询小于x且最大数量的int rank=getrank(x ) ); if(rank==1)返回信息; 返回类型byrank (rank-1; (intgetNEX(intx ) ) /查询大于x且最小数量的intrank=getrank ) x1 ); if(rank(int ) T.size ) )返回- INF; returngetbyrank(rank; }tr; int main () ) { cinN; for(intI=1; i=N; I ) {int op,x; scanf('%d%d ),op,x ); if(op==1) tr.insert(x ) x; if(op==2) tr.erase(x ) x; if(op==3) printf('%dn”,tr.getrank(x ) x ); if(op==4) printf('%dn”,tr.getbyrank(x ) x ); if(op==5) printf('%dn”,tr.getpre(x ) x ); if(op==6) printf('%dn”,tr.getnex(x ) x ); } return 0; }可重复数字平衡树的版本2如果主题是每个步骤都是合法的,则可以使用此版本来减少代码量

# include bits/stdc.h # include ext/Pb _ ds/tree _ policy.HPP # include ext/Pb _ ds/assoc _ container.HPP _ d int N,op,x; struct Tree{int idx=0; struct node{int x,zddwx; OOLoperator(nodeo ) const (if ) x!=o.x ) return x o.x; else return zddwx o.zddwx; }; treenode、null_type、lessnode、rb_tree_tag、tree _ order _ statistics _ node _ updatet; voidinsert(intx ) qkdkfd (idx ); }voiderase(intx ) intrank=t.order_of_key ) ) x,0 ); autoit=t.find_by_order(rank; t.erase(it ); }intgetrank(intx ) returnt.order_of_key ) ) x,0 ) 1; }intgetbyrank(intx ) return ) t.find_by_order ) x-1 ).x; }intgetpre(intx ) intrank=t.order_of_key ) ) x,0 ); return(t.find_by_order(rank-1 ) ).x; }intgetNEX(intx ) intrank=t.order_of_key ) ) x1,0 ); return(t.find_by_order(rank ) ).x; }tr; int main ()//Freopen(in.txt )、(r )、stdin ); cinN; for(intI=1; i=N; I ) scanf('%d%d )、op、x ); if(op==1) tr.insert(x ) x; if(op==2) tr.erase(x ) x; if(op==3) printf('%dn”,tr.getrank(x ) x ); if(op==4) printf('%dn”,tr.getbyrank(x ) x ); if(op==5) printf('%dn”,tr.getpre(x ) x ); if(op==6) printf('%dn”,tr.getnex(x ) x ); } return 0; }没有重复的数字版本可以直接使用

treenode、null_type、lessnode、rb_tree_tag、tree _ order _ statistics _ node _ updatet; 之所以写重复数字版本,是因为此库的树中没有重复数字,所以自己封装了可重复版本。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。