### Struct

# GLib Tree

#### Description [src]

    struct GTree {
      /* No available fields */
    }

The GTree struct is an opaque data structure representing a balanced binary tree. It should be accessed only by using the following functions.

#### Constructors

###### g_tree_new

Creates a new `GTree`.

###### g_tree_new_full

Creates a new `GTree` like `g_tree_new()` and allows to specify functions to free the memory allocated for the key and value that get called when removing the entry from the `GTree`.

###### g_tree_new_with_data

Creates a new `GTree` with a comparison function that accepts user data. See `g_tree_new()` for more details.

#### Instance methods

###### g_tree_destroy

Removes all keys and values from the `GTree` and decreases its reference count by one. If keys and/or values are dynamically allocated, you should either free them first or create the `GTree` using g_tree_new_full(). In the latter case the destroy functions you supplied will be called on all keys and values before destroying the `GTree`.

###### g_tree_foreach

Calls the given function for each of the key/value pairs in the `GTree`. The function is passed the key and value of each pair, and the given `data` parameter. The tree is traversed in sorted order.

###### g_tree_foreach_node

Calls the given function for each of the nodes in the `GTree`. The function is passed the pointer to the particular node, and the given `data` parameter. The tree traversal happens in-order.

since: 2.68

###### g_tree_height

Gets the height of a `GTree`.

###### g_tree_insert

Inserts a key/value pair into a `GTree`.

###### g_tree_insert_node

Inserts a key/value pair into a `GTree`.

since: 2.68

###### g_tree_lookup

Gets the value corresponding to the given key. Since a `GTree` is automatically balanced as key/value pairs are added, key lookup is O(log n) (where n is the number of key/value pairs in the tree).

###### g_tree_lookup_extended

Looks up a key in the `GTree`, returning the original key and the associated value. This is useful if you need to free the memory allocated for the original key, for example before calling g_tree_remove().

###### g_tree_lookup_node

Gets the tree node corresponding to the given key. Since a `GTree` is automatically balanced as key/value pairs are added, key lookup is O(log n) (where n is the number of key/value pairs in the tree).

since: 2.68

###### g_tree_lower_bound

Gets the lower bound node corresponding to the given key, or `NULL` if the tree is empty or all the nodes in the tree have keys that are strictly lower than the searched key.

since: 2.68

###### g_tree_nnodes

Gets the number of nodes in a `GTree`.

###### g_tree_node_first

Returns the first in-order node of the tree, or `NULL` for an empty tree.

since: 2.68

###### g_tree_node_last

Returns the last in-order node of the tree, or `NULL` for an empty tree.

since: 2.68

###### g_tree_ref

Increments the reference count of `tree` by one.

since: 2.22

###### g_tree_remove

Removes a key/value pair from a `GTree`.

###### g_tree_remove_all

Removes all nodes from a `GTree` and destroys their keys and values, then resets the `GTree`’s root to `NULL`.

since: 2.70

###### g_tree_replace

Inserts a new key and value into a `GTree` as `g_tree_replace_node()` does, only this function does not return the inserted or set node.

###### g_tree_replace_node

Inserts a new key and value into a `GTree` similar to g_tree_insert_node(). The difference is that if the key already exists in the `GTree`, it gets replaced by the new key. If you supplied a `value_destroy_func` when creating the `GTree`, the old value is freed using that function. If you supplied a `key_destroy_func` when creating the `GTree`, the old key is freed using that function.

since: 2.68

###### g_tree_search

Searches a `GTree` using `search_func`.

###### g_tree_search_node

Searches a `GTree` using `search_func`.

since: 2.68

###### g_tree_steal

Removes a key and its associated value from a `GTree` without calling the key and value destroy functions.

###### g_tree_traverse

Calls the given function for each node in the `GTree`.

deprecated: 2.2

###### g_tree_unref

Decrements the reference count of `tree` by one. If the reference count drops to 0, all keys and values will be destroyed (if destroy functions were specified) and all memory allocated by `tree` will be released.

since: 2.22

###### g_tree_upper_bound

Gets the upper bound node corresponding to the given key, or `NULL` if the tree is empty or all the nodes in the tree have keys that are lower than or equal to the searched key.

since: 2.68