您的位置:首页 > 其它

erl_tree-通用二叉查找树 gb_tree

2015-09-14 12:13 387 查看
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。。

在坚强:/article/4598618.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=  Tree

init()->
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).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: