# gb_trees

### MODULE

gb_trees

### MODULE SUMMARY

General Balanced Trees

### DESCRIPTION

An efficient implementation of Prof. Arne Andersson's General Balanced Trees. These have no storage overhead compared to unbalaced binary trees, and their performance is in general better than AVL trees.

### Data structure

Data structure:

    - {Size, Tree}, where `Tree' is composed of nodes of the form:
      - {Key, Value, Smaller, Bigger}, and the "empty tree" node:
      - nil.

There is no attempt to balance trees after deletions. Since deletions don't increase the height of a tree, this should be OK.

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.

Performance is comparable to the AVL trees in the Erlang book (and faster in general due to less overhead); the difference is that deletion works for these trees, but not for the book's trees. Behaviour is logaritmic (as it should be).

### EXPORTS

`empty()`

Returns a new, empty tree.

`is_empty(T)`

Returns 'true' if T is an empty tree, and 'false' otherwise.

`size(T)`

Returns the number of nodes in the tree as an integer. Returns 0 (zero) if the tree is empty.

`lookup(X, T)`

Looks up key X in tree T; returns {value, V}, or `none' if the key is not present.

`get(X, T)`

Retreives the value stored with key X in tree T. Assumes that the key is present in the tree, crashes otherwise.

`insert(X, V, T)`

Inserts key X with value V into tree T; returns the new tree. Assumes that the key is *not* present in the tree, crashes otherwise.

`update(X, V, T)`

Updates key X to value V in tree T; returns the new tree. Assumes that the key is present in the tree.

`enter(X, V, T)`

Inserts key X with value V into tree T if the key is not present in the tree, otherwise updates key X to value V in T. Returns the new tree.

`delete(X, T)`

Removes key X from tree T; returns new tree. Assumes that the key is present in the tree, crashes otherwise.

`delete_any(X, T)`

Removes key X from tree T if the key is present in the tree, otherwise does nothing; returns new tree.

`balance(T)`

Rebalances tree T. Note that this is rarely necessary, but may be motivated when a large number of entries have been deleted from the tree without further insertions. Rebalancing could then be forced in order to minimise lookup times, since deletion only does not rebalance the tree.

`is_defined(X, T)`

Returns `true' if key X is present in tree T, and `false' otherwise.

`keys(T)`

Returns an ordered list of all keys in tree T.

`values(T)`

Returns a list of all values in tree T.

`to_list(T)`

Returns an ordered list of {Key, Value} pairs for all keys in tree T.

`from_orddict(L)`

turns an ordered list L of {Key, Value} pairs into a tree. The list must not contain duplicate keys.

`take_smallest(T)`

Returns {X, V, T1}, where X is the smallest key in tree T, V is the value associated with X in T, and T1 is the tree T with key X deleted. Assumes that the tree T is nonempty.

`iterator(T)`

Returns an iterator that can be used for traversing the entries of tree T; see `next'. The implementation of this is very efficient; traversing the whole tree using `next' is only slightly slower than getting the list of all elements using `to_list' 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.

`next(S)`

Returns {X, V, S1} where X is the smallest key referred to by the iterator S, and S1 is the new iterator to be used for traversing the remaining entries, or the atom `none' if no entries remain.

### SEE ALSO

gb_sets(3), dict(3),

### AUTHORS

Sven-Olof Nystrom, Richard Carlsson - support@erlang.ericsson.se

stdlib 1.10.1
Copyright © 1991-2002 Ericsson Utvecklings AB