mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-03 09:46:17 +01:00
docs: Move the tree SECTION
Move the content to the new data-structures.md file. Helps: #3037
This commit is contained in:
parent
0ce33a6fee
commit
2191c6024a
@ -11,7 +11,7 @@ SPDX-FileCopyrightText: 2020 Endless OS Foundation, LLC
|
||||
# Data Structures
|
||||
|
||||
GLib includes a number of basic data sructures, such as arrays, linked lists, hash tables,
|
||||
queues, etc.
|
||||
queues, trees, etc.
|
||||
|
||||
## Arrays
|
||||
|
||||
@ -368,3 +368,27 @@ the `_unlocked` suffix).
|
||||
In many cases, it may be more convenient to use [struct@GLib.ThreadPool] when you need to distribute work
|
||||
to a set of worker threads instead of using `GAsyncQueue` manually. `GThreadPool` uses a `GAsyncQueue`
|
||||
internally.
|
||||
|
||||
## Binary Trees
|
||||
|
||||
The [struct@GLib.Tree] structure and its associated functions provide a sorted collection of key/value
|
||||
pairs optimized for searching and traversing in order. This means that most of the operations (access,
|
||||
search, insertion, deletion, …) on `GTree` are O(log(n)) in average and O(n) in worst case for time
|
||||
complexity. But, note that maintaining a balanced sorted `GTree` of n elements is done in time O(n log(n)).
|
||||
|
||||
To create a new `GTree` use [ctor@GLib.Tree.new].
|
||||
|
||||
To insert a key/value pair into a `GTree` use [method@GLib.Tree.insert] (O(n log(n))).
|
||||
|
||||
To remove a key/value pair use [method@GLib.Tree.remove] (O(n log(n))).
|
||||
|
||||
To look up the value corresponding to a given key, use [method@GLib.Tree.lookup] and
|
||||
[method@GLib.Tree.lookup_extended].
|
||||
|
||||
To find out the number of nodes in a `GTree`, use [method@GLib.Tree.nnodes].
|
||||
To get the height of a `GTree`, use [method@GLib.Tree.height].
|
||||
|
||||
To traverse a `GTree`, calling a function for each node visited in
|
||||
the traversal, use [method@GLib.Tree.foreach].
|
||||
|
||||
To destroy a `GTree`, use [method@GLib.Tree.destroy].
|
||||
|
32
glib/gtree.c
32
glib/gtree.c
@ -36,38 +36,6 @@
|
||||
#include "gtestutils.h"
|
||||
#include "gslice.h"
|
||||
|
||||
/**
|
||||
* SECTION:trees-binary
|
||||
* @title: Balanced Binary Trees
|
||||
* @short_description: a sorted collection of key/value pairs optimized
|
||||
* for searching and traversing in order
|
||||
*
|
||||
* The #GTree structure and its associated functions provide a sorted
|
||||
* collection of key/value pairs optimized for searching and traversing
|
||||
* in order. This means that most of the operations (access, search,
|
||||
* insertion, deletion, ...) on #GTree are O(log(n)) in average and O(n)
|
||||
* in worst case for time complexity. But, note that maintaining a
|
||||
* balanced sorted #GTree of n elements is done in time O(n log(n)).
|
||||
*
|
||||
* To create a new #GTree use g_tree_new().
|
||||
*
|
||||
* To insert a key/value pair into a #GTree use g_tree_insert()
|
||||
* (O(n log(n))).
|
||||
*
|
||||
* To remove a key/value pair use g_tree_remove() (O(n log(n))).
|
||||
*
|
||||
* To look up the value corresponding to a given key, use
|
||||
* g_tree_lookup() and g_tree_lookup_extended().
|
||||
*
|
||||
* To find out the number of nodes in a #GTree, use g_tree_nnodes(). To
|
||||
* get the height of a #GTree, use g_tree_height().
|
||||
*
|
||||
* To traverse a #GTree, calling a function for each node visited in
|
||||
* the traversal, use g_tree_foreach().
|
||||
*
|
||||
* To destroy a #GTree, use g_tree_destroy().
|
||||
**/
|
||||
|
||||
#define MAX_GTREE_HEIGHT 40
|
||||
/* G_MAXUINT nodes will be covered by tree height of log2(G_MAXUINT) + 2. */
|
||||
G_STATIC_ASSERT ((G_GUINT64_CONSTANT (1) << (MAX_GTREE_HEIGHT - 2)) >= G_MAXUINT);
|
||||
|
Loading…
Reference in New Issue
Block a user