> Erlang中文手册 > enter/3 向树里插入(或更新)一个跟键相关的值

gb_trees:enter/3

向树里插入(或更新)一个跟键相关的值

用法:

enter(Key, Val, Tree1) -> Tree2

如果键 Key 在树里不存在,则向树 Tree1 插入一个跟 Key 相关的值 Val,否则在树 Tree1 里更新跟 Key 相关的值为 Val。最后返回一个新树 Tree2。

Tree1 = gb_trees:empty(),
gb_trees:enter(a, 1, Tree1).
Tree1 = gb_trees:empty(),
Tree2 = gb_trees:enter(a, 1, Tree1),
gb_trees:enter(b, 2, Tree2).
Tree1 = gb_trees:empty(),
Tree2 = gb_trees:enter(a, 1, Tree1),
gb_trees:enter(a, 2, Tree2).