gb_tree, gb_set, 均为一个二叉树。具体怎么实现,这边不在累赘,官方有手册,
how to use ? 才是我们的重点
1. 初始化
1> gb_trees:empty().
{0,nil}
> gb_sets:empty().
{0,nil}
3>
这样即可完成初始化
2. 插入
gb_trees:insert/3
gb_sets:insert/2
3> gb_trees:insert(1,{1,100}, v(1)).
{1,{1,{1,100},nil,nil}}
4> gb_sets:in
insert/2 intersection/1 intersection/2
5> gb_sets:insert({1,100}, v(2)).
{1,{{1,100},nil,nil}
注意的是 gb_trees:insert(Key, Value, Trees).
gb_set:insert(Tuple, Sets).
3. 删除
6> gb_trees:delete(1, v(3)).
{0,nil}
7> gb_sets:del
del_element/2 delete/2 delete_any/2
7> gb_sets:delete(1, v(5)).
** exception error: no function clause matching gb_sets:delete_1(1,nil) (gb_sets.erl, line 408)
in function gb_sets:delete_1/2 (gb_sets.erl, line 409)
in call from gb_sets:delete/2 (gb_sets.erl, line 406)
8> gb_sets:delete({1,100}, v(5)).
{0,nil}
gb_trees:delete/2 根据主键删除
gb_sets:delete/2 根据tuple 删除
4.查询
gb_trees:lookup/2,
gb_trees:lookup(1, v(3)).
{value,{1,100}}
因为gb_sets 是在一切数据已知的情况下操作的,所以不存在查询操作
12> gb_sets:is_member({1,100},v(5)).
true
13>
5.求长度
gb_trees:size(v(3)).
1
gb_sets:size(v(5)).
1
6. 遍历
15> gb_trees:iterator(v(3)). 先获取一个gb_trees的迭代树
[{1,{1,100},nil,nil}]
16> gb_trees:next(v(15)). 根据迭代数,不断的去迭代,
{1,{1,100},[]}
gb_sets 也是一样的操作
但是他还有一个更好的函数 叫fold ,有兴趣的可以去看看哦,
这就是语法题gb_trees, gb_sets 的使用