📄 z29.html
字号:
#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> g_node_append</tt></code>(<tt class= "PARAMETER"><i>parent</i></tt>, <tt class= "PARAMETER"><i>node</i></tt>);</code> </p> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> g_node_insert_data</tt></code>(<tt class= "PARAMETER"><i>parent</i></tt>, <tt class= "PARAMETER"><i>position</i></tt>, <tt class= "PARAMETER"><i>data</i></tt>);</code> </p> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> g_node_insert_data_before</tt></code>(<tt class= "PARAMETER"><i>parent</i></tt>, <tt class= "PARAMETER"><i>sibling</i></tt>, <tt class= "PARAMETER"><i>data</i></tt>);</code> </p> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> g_node_prepend_data</tt></code>(<tt class= "PARAMETER"><i>parent</i></tt>, <tt class= "PARAMETER"><i>data</i></tt>);</code> </p> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> g_node_append_data</tt></code>(<tt class= "PARAMETER"><i>parent</i></tt>, <tt class= "PARAMETER"><i>data</i></tt>);</code> </p> </div> <p> <b>Figure 24. Building a <span class="STRUCTNAME"> GNode</span></b> </p> </div> <p> To remove a node from the tree, there are two functions shown in <a href="z29.html#FL-NODEDESTROY">Figure 25</a>. <tt class="FUNCTION">g_node_destroy()</tt> removes the node from a tree, destroying it and all its children. <tt class="FUNCTION">g_node_unlink()</tt> removes a node and makes it into a root node; i.e., it converts a subtree into an independent tree. </p> <div class="FIGURE"> <a name="FL-NODEDESTROY"></a> <div class="FUNCSYNOPSIS"> <a name="FL-NODEDESTROY.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">g_node_destroy</tt></code>(GNode* <tt class="PARAMETER"><i>root</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">g_node_unlink</tt></code>(GNode* <tt class="PARAMETER"><i>node</i></tt>);</code> </p> </div> <p> <b>Figure 25. Destroying a <span class="STRUCTNAME"> GNode</span></b> </p> </div> <p> There are two macros for detecting the top and bottom of a <span class="STRUCTNAME">GNode</span> tree, shown in <a href="z29.html#ML-NODEEXTREMA">Figure 26</a>. A root node is defined as a node with no parent or siblings. A leaf node has no children. </p> <div class="FIGURE"> <a name="ML-NODEEXTREMA"></a> <div class="FUNCSYNOPSIS"> <a name="ML-NODEEXTREMA.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> G_NODE_IS_ROOT</tt></code>(<tt class= "PARAMETER"><i>node</i></tt>);</code> </p> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> G_NODE_IS_LEAF</tt></code>(<tt class= "PARAMETER"><i>node</i></tt>);</code> </p> </div> <p> <b>Figure 26. Predicates for <span class= "STRUCTNAME">GNode</span></b> </p> </div> <p> You can ask glib to report useful information about a <span class="STRUCTNAME">GNode</span>, including the number of nodes it contains, its root node, its depth, and the node containing a particular data pointer. These functions are shown in <a href= "z29.html#FL-NODEPROPERTIES">Figure 27</a>. </p> <p> <span class="STRUCTNAME">GTraverseType</span> was introduced earlier, with respect to <span class= "STRUCTNAME">GTree</span>; here are the possible values for <span class="STRUCTNAME">GNode</span>: </p> <ul> <li> <p> <span class="STRUCTNAME">G_IN_ORDER</span> first recurses the leftmost child of the node, then visits the node itself, then recurses the rest of the node's children. This isn't very useful; mostly it is intended for use with <span class= "STRUCTNAME">GTree</span>. </p> </li> <li> <p> <span class="STRUCTNAME">G_PRE_ORDER</span> visits the current node, then recurses each child in turn. </p> </li> <li> <p> <span class="STRUCTNAME">G_POST_ORDER</span> recurses each child in order, then visits the current node. </p> </li> <li> <p> <span class="STRUCTNAME">G_LEVEL_ORDER</span> first visits the node itself; then each of the node's children; then the children of the children; then the children of the children of the children; and so on. That is, it visits each node of depth 0, then each node of depth 1, then each node of depth 2, etc. </p> </li> </ul> <p> <span class="STRUCTNAME">GNode</span>'s tree-traversal functions have a <span class="STRUCTNAME"> GTraverseFlags</span> argument. This is a bitfield used to change the nature of the traversal. Currently there are only three flags---you can visit only leaf nodes, only non-leaf nodes, or all nodes: </p> <ul> <li> <p> <span class="STRUCTNAME">G_TRAVERSE_LEAFS</span> means to traverse only leaf nodes. </p> </li> <li> <p> <span class="STRUCTNAME"> G_TRAVERSE_NON_LEAFS</span> means to traverse only non-leaf nodes. </p> </li> <li> <p> <span class="STRUCTNAME">G_TRAVERSE_ALL</span> is simply a shortcut for <span class="STRUCTNAME"> (G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS)</span>. </p> </li> </ul> <div class="FIGURE"> <a name="FL-NODEPROPERTIES"></a> <div class="FUNCSYNOPSIS"> <a name="FL-NODEPROPERTIES.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">guint <tt class= "FUNCTION">g_node_n_nodes</tt></code>(GNode* <tt class="PARAMETER"><i>root</i></tt>, GTraverseFlags <tt class="PARAMETER"><i>flags</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">GNode* <tt class= "FUNCTION">g_node_get_root</tt></code>(GNode* <tt class="PARAMETER"><i>node</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gboolean <tt class= "FUNCTION">g_node_is_ancestor</tt></code>(GNode* <tt class="PARAMETER"><i>node</i></tt>, GNode* <tt class="PARAMETER"><i>descendant</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">guint <tt class= "FUNCTION">g_node_depth</tt></code>(GNode* <tt class="PARAMETER"><i>node</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">GNode* <tt class= "FUNCTION">g_node_find</tt></code>(GNode* <tt class="PARAMETER"><i>root</i></tt>, GTraverseType <tt class="PARAMETER"><i>order</i></tt>, GTraverseFlags <tt class="PARAMETER"><i> flags</i></tt>, gpointer <tt class="PARAMETER"><i> data</i></tt>);</code> </p> </div> <p> <b>Figure 27. <span class="STRUCTNAME">GNode</span> Properties</b> </p> </div> <p> The remaining <span class="STRUCTNAME">GNode</span> functions are straightforward; most of them are simply operations on the node's list of children. <a href= "z29.html#FL-NODEACCESSORS">Figure 28</a> lists them. There are two function typedefs unique to <span class= "STRUCTNAME">GNode</span>: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> typedef gboolean (*GNodeTraverseFunc) (GNode* node, gpointer data);typedef void (*GNodeForeachFunc) (GNode* node, gpointer data); </pre> </td> </tr> </table> <p> These are called with a pointer to the node being visited, and the user data you provide. A <span class= "STRUCTNAME">GNodeTraverseFunc</span> can return <span class="STRUCTNAME">TRUE</span> to stop whatever traversal is in progress; thus you can use <span class= "STRUCTNAME">GNodeTraverseFunc</span> in combination with <tt class="FUNCTION">g_node_traverse()</tt> to search the tree by value. </p> <div class="FIGURE"> <a name="FL-NODEACCESSORS"></a> <div class="FUNCSYNOPSIS"> <a name="FL-NODEACCESSORS.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">g_node_traverse</tt></code>(GNode* <tt class="PARAMETER"><i>root</i></tt>, GTraverseType <tt class="PARAMETER"><i>order</i></tt>, GTraverseFlags <tt class="PARAMETER"><i> flags</i></tt>, gint <tt class="PARAMETER"><i> max_depth</i></tt>, GNodeTraverseFunc <tt class= "PARAMETER"><i>func</i></tt>, gpointer <tt class= "PARAMETER"><i>data</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">guint <tt class= "FUNCTION">g_node_max_height</tt></code>(GNode* <tt class="PARAMETER"><i>root</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> g_node_children_foreach</tt></code>(GNode* <tt class="PARAMETER"><i>node</i></tt>, GTraverseFlags <tt class="PARAMETER"><i>flags</i></tt>, GNodeForeachFunc <tt class="PARAMETER"><i> func</i></tt>, gpointer <tt class="PARAMETER"><i> data</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> g_node_reverse_children</tt></code>(GNode* <tt class="PARAMETER"><i>node</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">guint <tt class=
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -