erl_tree-通用二叉查找树 gb_tree

来源:互联网 发布:ubuntu 测试软件 编辑:程序博客网 时间:2024/06/09 23:10

gb_tree :(General Balanced Trees) 通用二叉查找树,通常被用作有序字典.
gb_trees={Size,Tree}
Tree= {Key, Value, Smaller, Bigger} |nil
Smaller=Tree
Bigger= Tree

整个结构都是用key 来判断节点位置,gb_tree对key有这样一段注释:
%% The term order is an arithmetic total order, so we should not
%% test exact equality for the keys. (If we do, then it becomes
%% possible that neither >',<’, nor `=:=’ matches.) Testing ‘<’
%% and ‘>’ first is statistically better than testing for
%% equality, and also allows us to skip the test completely in the
%% remaining case.
在我的测试中key可以是term int string。。

在坚强:http://www.cnblogs.com/me-sa/archive/2012/06/23/erlang-gb_trees.html 博客里有对各个函数的相应分析,

以下是对部分函数的关注点分析:
insert 函数 插入不是每次都balance :
insert_1(Key, Value, nil, S) when S =:= 0 ->
{{Key, Value, nil, nil}, 1, 1};
….
if
H > P ->
balance(T, SS);

gb_trees balance 是重构树,先to_list_1(T) 然后递归二分构造。

在后面一章中我们会分析avl_tree 的balance LL\RR\LR\RL 操作

以下是对gb_trees 的一些封装:

%%=========General Balanced Trees==============%% gb_trees={Size,Tree}%% Tree=  {Key, Value, Smaller, Bigger} |nil%% Smaller=Tree%% Bigger=  Treeinit()->    gb_trees:empty().insert(Key, Val, Tree1)->    gb_trees:insert(Key, Val, Tree1).lookup(Key, Tree)->    gb_trees:lookup(Key, Tree).get(Key, Tree)->    gb_trees:get(Key, Tree).balance(Tree1) ->    gb_trees:balance(Tree1).delete(Key, Tree1)->    gb_trees:delete(Key, Tree1).
0 0
原创粉丝点击