📄 trees-binary.sgml
字号:
<refentry id="glib-Balanced-Binary-Trees"><refmeta><refentrytitle>Balanced Binary Trees</refentrytitle><manvolnum>3</manvolnum><refmiscinfo>GLIB Library</refmiscinfo></refmeta><refnamediv><refname>Balanced Binary Trees</refname><refpurpose>a sorted collection of key/value pairs optimized for searchingand traversing in order.</refpurpose></refnamediv><refsynopsisdiv><title>Synopsis</title><synopsis>#include <glib.h>struct <link linkend="GTree">GTree</link>;<link linkend="GTree">GTree</link>* <link linkend="g-tree-new">g_tree_new</link> (<link linkend="GCompareFunc">GCompareFunc</link> key_compare_func);<link linkend="GTree">GTree</link>* <link linkend="g-tree-new-with-data">g_tree_new_with_data</link> (<link linkend="GCompareDataFunc">GCompareDataFunc</link> key_compare_func, <link linkend="gpointer">gpointer</link> key_compare_data);<link linkend="GTree">GTree</link>* <link linkend="g-tree-new-full">g_tree_new_full</link> (<link linkend="GCompareDataFunc">GCompareDataFunc</link> key_compare_func, <link linkend="gpointer">gpointer</link> key_compare_data, <link linkend="GDestroyNotify">GDestroyNotify</link> key_destroy_func, <link linkend="GDestroyNotify">GDestroyNotify</link> value_destroy_func);void <link linkend="g-tree-insert">g_tree_insert</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="gpointer">gpointer</link> key, <link linkend="gpointer">gpointer</link> value);void <link linkend="g-tree-replace">g_tree_replace</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="gpointer">gpointer</link> key, <link linkend="gpointer">gpointer</link> value);<link linkend="gint">gint</link> <link linkend="g-tree-nnodes">g_tree_nnodes</link> (<link linkend="GTree">GTree</link> *tree);<link linkend="gint">gint</link> <link linkend="g-tree-height">g_tree_height</link> (<link linkend="GTree">GTree</link> *tree);<link linkend="gpointer">gpointer</link> <link linkend="g-tree-lookup">g_tree_lookup</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="gconstpointer">gconstpointer</link> key);<link linkend="gboolean">gboolean</link> <link linkend="g-tree-lookup-extended">g_tree_lookup_extended</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="gconstpointer">gconstpointer</link> lookup_key, <link linkend="gpointer">gpointer</link> *orig_key, <link linkend="gpointer">gpointer</link> *value);void <link linkend="g-tree-foreach">g_tree_foreach</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="GTraverseFunc">GTraverseFunc</link> func, <link linkend="gpointer">gpointer</link> user_data);void <link linkend="g-tree-traverse">g_tree_traverse</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="GTraverseFunc">GTraverseFunc</link> traverse_func, <link linkend="GTraverseType">GTraverseType</link> traverse_type, <link linkend="gpointer">gpointer</link> user_data);<link linkend="gboolean">gboolean</link> (<link linkend="GTraverseFunc">*GTraverseFunc</link>) (<link linkend="gpointer">gpointer</link> key, <link linkend="gpointer">gpointer</link> value, <link linkend="gpointer">gpointer</link> data);enum <link linkend="GTraverseType">GTraverseType</link>;<link linkend="gpointer">gpointer</link> <link linkend="g-tree-search">g_tree_search</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="GCompareFunc">GCompareFunc</link> search_func, <link linkend="gconstpointer">gconstpointer</link> user_data);void <link linkend="g-tree-remove">g_tree_remove</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="gconstpointer">gconstpointer</link> key);void <link linkend="g-tree-steal">g_tree_steal</link> (<link linkend="GTree">GTree</link> *tree, <link linkend="gconstpointer">gconstpointer</link> key);void <link linkend="g-tree-destroy">g_tree_destroy</link> (<link linkend="GTree">GTree</link> *tree);</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The <link linkend="GTree">GTree</link> structure and its associated functions provide a sorted collectionof key/value pairs optimized for searching and traversing in order.</para><para>To create a new <link linkend="GTree">GTree</link> use <link linkend="g-tree-new">g_tree_new</link>().</para><para>To insert a key/value pair into a <link linkend="GTree">GTree</link> use <link linkend="g-tree-insert">g_tree_insert</link>().</para><para>To lookup the value corresponding to a given key, use <link linkend="g-tree-lookup">g_tree_lookup</link>() and<link linkend="g-tree-lookup-extended">g_tree_lookup_extended</link>().</para><para>To find out the number of nodes in a <link linkend="GTree">GTree</link>, use <link linkend="g-tree-nnodes">g_tree_nnodes</link>().To get the height of a <link linkend="GTree">GTree</link>, use <link linkend="g-tree-height">g_tree_height</link>().</para><para>To traverse a <link linkend="GTree">GTree</link>, calling a function for each node visited in thetraversal, use <link linkend="g-tree-foreach">g_tree_foreach</link>().</para><para>To remove a key/value pair use <link linkend="g-tree-remove">g_tree_remove</link>().</para><para>To destroy a <link linkend="GTree">GTree</link>, use <link linkend="g-tree-destroy">g_tree_destroy</link>().</para></refsect1><refsect1><title>Details</title><refsect2><title><anchor id="GTree">struct GTree</title><programlisting>struct GTree;</programlisting><para>The <structname>GTree</structname> struct is an opaque data structure representing a<link linkend="glib-Balanced-Binary-Trees">Balanced Binary Tree</link>.It should be accessed only by using the following functions.</para></refsect2><refsect2><title><anchor id="g-tree-new">g_tree_new ()</title><programlisting><link linkend="GTree">GTree</link>* g_tree_new (<link linkend="GCompareFunc">GCompareFunc</link> key_compare_func);</programlisting><para>Creates a new <link linkend="GTree">GTree</link>.</para><para></para><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><parameter>key_compare_func</parameter> :</entry><entry> the function used to order the nodes in the <link linkend="GTree">GTree</link>. It should return values similar to the standard <function><link linkend="strcmp">strcmp</link>()</function> function - 0 if the two arguments are equal, a negative value if the first argument comes before the second, or a positive value if the first argument comes after the second.</entry></row><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> a new <link linkend="GTree">GTree</link>.</entry></row></tbody></tgroup></informaltable></refsect2><refsect2><title><anchor id="g-tree-new-with-data">g_tree_new_with_data ()</title><programlisting><link linkend="GTree">GTree</link>* g_tree_new_with_data (<link linkend="GCompareDataFunc">GCompareDataFunc</link> key_compare_func, <link linkend="gpointer">gpointer</link> key_compare_data);</programlisting><para>Creates a new <link linkend="GTree">GTree</link> with a comparison function that accepts user data.See <link linkend="g-tree-new">g_tree_new</link>() for more details.</para><para></para><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><parameter>key_compare_func</parameter> :</entry><entry> <function><link linkend="qsort">qsort</link>()</function>-style comparison function.</entry></row><row><entry align="right"><parameter>key_compare_data</parameter> :</entry><entry> data to pass to comparison function.</entry></row><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> a new <link linkend="GTree">GTree</link>.</entry></row></tbody></tgroup></informaltable></refsect2><refsect2><title><anchor id="g-tree-new-full">g_tree_new_full ()</title><programlisting><link linkend="GTree">GTree</link>* g_tree_new_full (<link linkend="GCompareDataFunc">GCompareDataFunc</link> key_compare_func, <link linkend="gpointer">gpointer</link> key_compare_data, <link linkend="GDestroyNotify">GDestroyNotify</link> key_destroy_func, <link linkend="GDestroyNotify">GDestroyNotify</link> value_destroy_func);</programlisting><para>Creates a new <link linkend="GTree">GTree</link> like <link linkend="g-tree-new">g_tree_new</link>() and allows to specify functions to free the memory allocated for the key and value that get called when removing the entry from the <link linkend="GTree">GTree</link>.</para><para></para><informaltable pgwide="1" frame="none" role="params"><tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*"><tbody><row><entry align="right"><parameter>key_compare_func</parameter> :</entry><entry> <function><link linkend="qsort">qsort</link>()</function>-style comparison function.</entry></row><row><entry align="right"><parameter>key_compare_data</parameter> :</entry><entry> data to pass to comparison function.</entry></row><row><entry align="right"><parameter>key_destroy_func</parameter> :</entry><entry> a function to free the memory allocated for the key used when removing the entry from the <link linkend="GTree">GTree</link> or <literal>NULL</literal> if you don't want to supply such a function.</entry></row><row><entry align="right"><parameter>value_destroy_func</parameter> :</entry><entry> a function to free the memory allocated for the value used when removing the entry from the <link linkend="GTree">GTree</link> or <literal>NULL</literal> if you don't want to supply such a function.</entry></row><row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> a new <link linkend="GTree">GTree</link>.</entry></row></tbody></tgroup></informaltable></refsect2><refsect2><title><anchor id="g-tree-insert">g_tree_insert ()</title><programlisting>void g_tree_insert (<link linkend="GTree">GTree</link> *tree, <link linkend="gpointer">gpointer</link> key, <link linkend="gpointer">gpointer</link> value);</programlisting><para>Inserts a key/value pair into a <link linkend="GTree">GTree</link>. If the given key already exists in the <link linkend="GTree">GTree</link> its corresponding value is set to the new value. If you supplied a value_destroy_func when creating the <link linkend="GTree">GTree</link>, the old value is freed using that function. If you supplied a <parameter>key_destroy_func</parameter> when creating the <link linkend="GTree">GTree</link>, the passed key is freed using that function.</para><para>The tree is automatically 'balanced' as new key/value pairs are added,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -