gb_trees(3erl)
π³ gb_trees β general balanced trees (Erlang stdlib, module definition)
okay bestie come sit with me because this one is genuinely iconic π
this is the man page for gb_trees, straight out of the Erlang stdlib docs. buckle up β¨
π NAME
gb_trees β General balanced trees. that's it, that's the whole name, and honestly? slay.
πΈ DESCRIPTION
so this module gives you Prof. Arne Andersson's General Balanced Trees. and the flex here is real, because these bad boys have no storage overhead compared to unbalanced binary trees π and their performance is better than AVL trees π₯ like, not me casually outperforming AVL, but okay.
lowkey important detail: this module considers two keys as different if and only if they do not compare equal (==). so == is the whole vibe check for keys, fr fr π«Ά
π DATA STRUCTURE
Trees and iterators are built using opaque data structures that you should not pattern-match from outside this module. like, hands off, no peeking, we don't do that here π
There is no attempt to balance trees after deletions. and before you panic β deletions don't increase the height of a tree, so this should be OK π
The original balance condition h(T) <= ceil(c * log(|T|)) has been changed to the similar (but not quite equivalent) condition 2 ^ h(T) <= |T| ^ c. This should also be OK β¨ (yes, the man page literally says "should be OK" twice, and honestly? same energy.)
π¦ DATA TYPES
tree(Key, Value)
A general balanced tree. icon behavior only π
tree() = tree(term(), term())
iter(Key, Value)
A general balanced tree iterator. it iterates, it slays, it moves on π§
iter() = iter(term(), term())
β¨ EXPORTS β¨
balance(Tree1) -> Tree2
Types:
Tree1 = Tree2 = tree(Key, Value)
Rebalances Tree1. okay so heads up: this is rarely necessary, but it can be motivated when many nodes have been deleted from the tree without further insertions. Rebalancing can then be forced to minimize lookup times, because deletion does not rebalance the tree. so like, it's the cleanup era of your tree π
delete(Key, Tree1) -> Tree2
Types:
Tree1 = Tree2 = tree(Key, Value)
Removes the node with key Key from Tree1 and returns the new tree. β οΈ Assumes that the key is present in the tree, crashes otherwise. so don't be messy about it bestie, the tree will absolutely call you out π
delete_any(Key, Tree1) -> Tree2
Types:
Tree1 = Tree2 = tree(Key, Value)
Removes the node with key Key from Tree1 if the key is present in the tree, otherwise does nothing. Returns the new tree. this is the chill, no-drama version π
take(Key, Tree1) -> {Value, Tree2}
Types:
Tree1 = Tree2 = tree(Key, term())
Key = Value = term()
Returns a value Value from node with key Key and new Tree2 without the node with this value. β οΈ Assumes that the node with key is present in the tree, crashes otherwise. take it and dip π«΄
take_any(Key, Tree1) -> {Value, Tree2} | error
Types:
Tree1 = Tree2 = tree(Key, term())
Key = Value = term()
Returns a value Value from node with key Key and new Tree2 without the node with this value. Returns error if the node with the key is not present in the tree. no crash, just a polite lil error π
empty() -> tree()
Returns a new empty tree. blank slate, main character energy, fresh start π±
enter(Key, Value, Tree1) -> Tree2
Types:
Tree1 = Tree2 = tree(Key, Value)
Inserts Key with value Value into Tree1 if the key is not present in the tree, otherwise updates Key to value Value in Tree1. Returns the new tree. one function, two moods, both iconic π
from_orddict(List) -> Tree
Types:
List = [{Key, Value}]
Tree = tree(Key, Value)
Turns an ordered list List of key-value tuples into a tree. π¨ The list must not contain duplicate keys. that's the one rule, respect it π
get(Key, Tree) -> Value
Types:
Tree = tree(Key, Value)
Retrieves the value stored with Key in Tree. β οΈ Assumes that the key is present in the tree, crashes otherwise. no key, no value, big feelings π
insert(Key, Value, Tree1) -> Tree2
Types:
Tree1 = Tree2 = tree(Key, Value)
Inserts Key with value Value into Tree1 and returns the new tree. β οΈ Assumes that the key is not present in the tree, crashes otherwise. so like, do a is_defined check first if you're not sure, iykyk π
is_defined(Key, Tree) -> boolean()
Types:
Tree = tree(Key, Value :: term())
Returns true if Key is present in Tree, otherwise false. the vibe check function π
is_empty(Tree) -> boolean()
Types:
Tree = tree()
Returns true if Tree is an empty tree, othwewise false. (yes that typo is in the original docs, we keep it, it's part of the lore now π)
iterator(Tree) -> Iter
Types:
Tree = tree(Key, Value)
Iter = iter(Key, Value)
Returns an iterator that can be used for traversing the entries of Tree; see next/1. The implementation of this is very efficient β¨ traversing the whole tree using next/1 is only slightly slower than getting the list of all elements using to_list/1 and traversing that. The main advantage of the iterator approach is that it does not require the complete list of all elements to be built in memory at one time. lowkey the memory-friendly queen of this module π
iterator_from(Key, Tree) -> Iter
Types:
Tree = tree(Key, Value)
Iter = iter(Key, Value)
Returns an iterator that can be used for traversing the entries of Tree; see next/1. The difference as compared to the iterator returned by iterator/1 is that the first key greater than or equal to Key is returned. so like, start me at Key or the next best thing πΈ
keys(Tree) -> [Key]
Types:
Tree = tree(Key, Value :: term())
Returns the keys in Tree as an ordered list. alphabetical-ish, organized, gorgeous π
largest(Tree) -> {Key, Value}
Types:
Tree = tree(Key, Value)
Returns {Key, Value}, where Key is the largest key in Tree, and Value is the value associated with this key. β οΈ Assumes that the tree is not empty. the biggest, the best, the final boss π
lookup(Key, Tree) -> none | {value, Value}
Types:
Tree = tree(Key, Value)
Looks up Key in Tree. Returns {value, Value}, or none if Key is not present. no crash here, just a gentle none if it's not giving π
map(Function, Tree1) -> Tree2
Types:
Function = fun((K :: Key, V1 :: Value1) -> V2 :: Value2)
Tree1 = tree(Key, Value1)
Tree2 = tree(Key, Value2)
Maps function F(K, V1) -> V2 to all key-value pairs of tree Tree1. Returns a new tree Tree2 with the same set of keys as Tree1 and the new set of values V2. glow-up for the values only, the keys stay exactly as they are π
next(Iter1) -> none | {Key, Value, Iter2}
Types:
Iter1 = Iter2 = iter(Key, Value)
Returns {Key, Value, Iter2}, where Key is the smallest key referred to by iterator Iter1, and Iter2 is the new iterator to be used for traversing the remaining nodes, or the atom none if no nodes remain. so it's step-by-step, one node at a time, no rushing π¦
size(Tree) -> integer() >= 0
Types:
Tree = tree()
Returns the number of nodes in Tree. and it's always >= 0, because obviously π«Ά
smallest(Tree) -> {Key, Value}
Types:
Tree = tree(Key, Value)
Returns {Key, Value}, where Key is the smallest key in Tree, and Value is the value associated with this key. β οΈ Assumes that the tree is not empty. smol but mighty π
take_largest(Tree1) -> {Key, Value, Tree2}
Types:
Tree1 = Tree2 = tree(Key, Value)
Returns {Key, Value, Tree2}, where Key is the largest key in Tree1, Value is the value associated with this key, and Tree2 is this tree with the corresponding node deleted. β οΈ Assumes that the tree is not empty. grab the biggest one and yeet it out π«΄π₯
take_smallest(Tree1) -> {Key, Value, Tree2}
Types:
Tree1 = Tree2 = tree(Key, Value)
Returns {Key, Value, Tree2}, where Key is the smallest key in Tree1, Value is the value associated with this key, and Tree2 is this tree with the corresponding node deleted. β οΈ Assumes that the tree is not empty. grab the tiniest one and yeet it out π§
to_list(Tree) -> [{Key, Value}]
Types:
Tree = tree(Key, Value)
Converts a tree into an ordered list of key-value tuples. the tree gets a whole new format era β¨
update(Key, Value, Tree1) -> Tree2
Types:
Tree1 = Tree2 = tree(Key, Value)
Updates Key to value Value in Tree1 and returns the new tree. β οΈ Assumes that the key is present in the tree. no adding new keys here, that's enter's job π
values(Tree) -> [Value]
Types:
Tree = tree(Key :: term(), Value)
Returns the values in Tree as an ordered list, sorted by their corresponding keys. β οΈ Duplicates are not removed. so if two keys share a value, you get both, periodt π
π SEE ALSO
go give them a little visit too, they're family π«Ά
π and that's the whole page bestie β every flag, every type, every crash warning, every "should be OK". no notes, no skips, nothing cut. you're welcome β¨
mwah π β written by Ericsson AB, shipped as stdlib 4.3.1.3, dated the same way the original footer says it. this man page ate and left absolutely no crumbs π
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms