📄 glib-n-ary-trees.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>N-ary Trees</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-data-types.html" title="GLib Data Types"><link rel="prev" href="glib-Balanced-Binary-Trees.html" title="Balanced Binary Trees"><link rel="next" href="glib-Quarks.html" title="Quarks"><meta name="generator" content="GTK-Doc V1.9 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"><link rel="index" href="ix09.html" title="Index of new symbols in 2.14"><link rel="index" href="ix10.html" title="Index of new symbols in 2.16"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Balanced-Binary-Trees.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-data-types.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Quarks.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr><tr><td colspan="5" class="shortcuts"><nobr><a href="#id3336423" class="shortcut">Top</a>  |  <a href="#id3337478" class="shortcut">Description</a></nobr></td></tr></table><div class="refentry" lang="en"><a name="glib-N-ary-Trees"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><a name="id3336423"></a><span class="refentrytitle">N-ary Trees</span></h2><p>N-ary Trees — trees of data with any number of branches</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>;<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-new">g_node_new</a> (<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-copy">g_node_copy</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> (<a class="link" href="glib-N-ary-Trees.html#GCopyFunc">*GCopyFunc</a>) (<a class="link" href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> src, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-copy-deep">g_node_copy_deep</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-N-ary-Trees.html#GCopyFunc">GCopyFunc</a> copy_func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-insert">g_node_insert</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *parent, <a class="link" href="glib-Basic-Types.html#gint">gint</a> position, <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-insert-before">g_node_insert_before</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *parent, <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *sibling, <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-insert-after">g_node_insert_after</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *parent, <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *sibling, <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);#define <a class="link" href="glib-N-ary-Trees.html#g-node-append">g_node_append</a> (parent, node)<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-prepend">g_node_prepend</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *parent, <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);#define <a class="link" href="glib-N-ary-Trees.html#g-node-insert-data">g_node_insert_data</a> (parent, position, data)#define <a class="link" href="glib-N-ary-Trees.html#g-node-insert-data-before">g_node_insert_data_before</a> (parent, sibling, data)#define <a class="link" href="glib-N-ary-Trees.html#g-node-append-data">g_node_append_data</a> (parent, data)#define <a class="link" href="glib-N-ary-Trees.html#g-node-prepend-data">g_node_prepend_data</a> (parent, data)void <a class="link" href="glib-N-ary-Trees.html#g-node-reverse-children">g_node_reverse_children</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);void <a class="link" href="glib-N-ary-Trees.html#g-node-traverse">g_node_traverse</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *root, <a class="link" href="glib-Balanced-Binary-Trees.html#GTraverseType">GTraverseType</a> order, <a class="link" href="glib-N-ary-Trees.html#GTraverseFlags">GTraverseFlags</a> flags, <a class="link" href="glib-Basic-Types.html#gint">gint</a> max_depth, <a class="link" href="glib-N-ary-Trees.html#GNodeTraverseFunc">GNodeTraverseFunc</a> func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);enum <a class="link" href="glib-N-ary-Trees.html#GTraverseFlags">GTraverseFlags</a>;<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> (<a class="link" href="glib-N-ary-Trees.html#GNodeTraverseFunc">*GNodeTraverseFunc</a>) (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);void <a class="link" href="glib-N-ary-Trees.html#g-node-children-foreach">g_node_children_foreach</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-N-ary-Trees.html#GTraverseFlags">GTraverseFlags</a> flags, <a class="link" href="glib-N-ary-Trees.html#GNodeForeachFunc">GNodeForeachFunc</a> func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);void (<a class="link" href="glib-N-ary-Trees.html#GNodeForeachFunc">*GNodeForeachFunc</a>) (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-get-root">g_node_get_root</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-find">g_node_find</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *root, <a class="link" href="glib-Balanced-Binary-Trees.html#GTraverseType">GTraverseType</a> order, <a class="link" href="glib-N-ary-Trees.html#GTraverseFlags">GTraverseFlags</a> flags, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-find-child">g_node_find_child</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-N-ary-Trees.html#GTraverseFlags">GTraverseFlags</a> flags, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a class="link" href="glib-Basic-Types.html#gint">gint</a> <a class="link" href="glib-N-ary-Trees.html#g-node-child-index">g_node_child_index</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);<a class="link" href="glib-Basic-Types.html#gint">gint</a> <a class="link" href="glib-N-ary-Trees.html#g-node-child-position">g_node_child_position</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *child);#define <a class="link" href="glib-N-ary-Trees.html#g-node-first-child">g_node_first_child</a> (node)<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-last-child">g_node_last_child</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-nth-child">g_node_nth_child</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-Basic-Types.html#guint">guint</a> n);<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-first-sibling">g_node_first_sibling</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);#define <a class="link" href="glib-N-ary-Trees.html#g-node-next-sibling">g_node_next_sibling</a> (node)#define <a class="link" href="glib-N-ary-Trees.html#g-node-prev-sibling">g_node_prev_sibling</a> (node)<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a>* <a class="link" href="glib-N-ary-Trees.html#g-node-last-sibling">g_node_last_sibling</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);#define <a class="link" href="glib-N-ary-Trees.html#G-NODE-IS-LEAF:CAPS">G_NODE_IS_LEAF</a> (node)#define <a class="link" href="glib-N-ary-Trees.html#G-NODE-IS-ROOT:CAPS">G_NODE_IS_ROOT</a> (node)<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-N-ary-Trees.html#g-node-depth">g_node_depth</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-N-ary-Trees.html#g-node-n-nodes">g_node_n_nodes</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *root, <a class="link" href="glib-N-ary-Trees.html#GTraverseFlags">GTraverseFlags</a> flags);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-N-ary-Trees.html#g-node-n-children">g_node_n_children</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-N-ary-Trees.html#g-node-is-ancestor">g_node_is_ancestor</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node, <a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *descendant);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-N-ary-Trees.html#g-node-max-height">g_node_max_height</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *root);void <a class="link" href="glib-N-ary-Trees.html#g-node-unlink">g_node_unlink</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *node);void <a class="link" href="glib-N-ary-Trees.html#g-node-destroy">g_node_destroy</a> (<a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *root);void <a class="link" href="glib-N-ary-Trees.html#g-node-push-allocator">g_node_push_allocator</a> (<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> dummy);void <a class="link" href="glib-N-ary-Trees.html#g-node-pop-allocator">g_node_pop_allocator</a> (void);</pre></div><div class="refsect1" lang="en"><a name="id3337478"></a><h2>Description</h2><p>The <a class="link" href="glib-N-ary-Trees.html#GNode"><span class="type">GNode</span></a> struct and its associated functions provide a N-ary tree datastructure, where nodes in the tree can contain arbitrary data.</p><p>To create a new tree use <a class="link" href="glib-N-ary-Trees.html#g-node-new"><code class="function">g_node_new()</code></a>.</p><p>To insert a node into a tree use <a class="link" href="glib-N-ary-Trees.html#g-node-insert"><code class="function">g_node_insert()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-insert-before"><code class="function">g_node_insert_before()</code></a>,<a class="link" href="glib-N-ary-Trees.html#g-node-append"><code class="function">g_node_append()</code></a> and <a class="link" href="glib-N-ary-Trees.html#g-node-prepend"><code class="function">g_node_prepend()</code></a>.</p><p>To create a new node and insert it into a tree use <a class="link" href="glib-N-ary-Trees.html#g-node-insert-data"><code class="function">g_node_insert_data()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-insert-data-before"><code class="function">g_node_insert_data_before()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-append-data"><code class="function">g_node_append_data()</code></a> and <a class="link" href="glib-N-ary-Trees.html#g-node-prepend-data"><code class="function">g_node_prepend_data()</code></a>.</p><p>To reverse the children of a node use <a class="link" href="glib-N-ary-Trees.html#g-node-reverse-children"><code class="function">g_node_reverse_children()</code></a>.</p><p>To find a node use <a class="link" href="glib-N-ary-Trees.html#g-node-get-root"><code class="function">g_node_get_root()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-find"><code class="function">g_node_find()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-find-child"><code class="function">g_node_find_child()</code></a>,<a class="link" href="glib-N-ary-Trees.html#g-node-child-index"><code class="function">g_node_child_index()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-child-position"><code class="function">g_node_child_position()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-first-child"><code class="function">g_node_first_child()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-last-child"><code class="function">g_node_last_child()</code></a>,<a class="link" href="glib-N-ary-Trees.html#g-node-nth-child"><code class="function">g_node_nth_child()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-first-sibling"><code class="function">g_node_first_sibling()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-prev-sibling"><code class="function">g_node_prev_sibling()</code></a>,<a class="link" href="glib-N-ary-Trees.html#g-node-next-sibling"><code class="function">g_node_next_sibling()</code></a> or <a class="link" href="glib-N-ary-Trees.html#g-node-last-sibling"><code class="function">g_node_last_sibling()</code></a>.</p><p>To get information about a node or tree use <a class="link" href="glib-N-ary-Trees.html#G-NODE-IS-LEAF:CAPS"><code class="function">G_NODE_IS_LEAF()</code></a>,<a class="link" href="glib-N-ary-Trees.html#G-NODE-IS-ROOT:CAPS"><code class="function">G_NODE_IS_ROOT()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-depth"><code class="function">g_node_depth()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-n-nodes"><code class="function">g_node_n_nodes()</code></a>, <a class="link" href="glib-N-ary-Trees.html#g-node-n-children"><code class="function">g_node_n_children()</code></a>,<a class="link" href="glib-N-ary-Trees.html#g-node-is-ancestor"><code class="function">g_node_is_ancestor()</code></a> or <a class="link" href="glib-N-ary-Trees.html#g-node-max-height"><code class="function">g_node_max_height()</code></a>.</p><p>To traverse a tree, calling a function for each node visited in thetraversal, use <a class="link" href="glib-N-ary-Trees.html#g-node-traverse"><code class="function">g_node_traverse()</code></a> or <a class="link" href="glib-N-ary-Trees.html#g-node-children-foreach"><code class="function">g_node_children_foreach()</code></a>.</p><p>To remove a node or subtree from a tree use <a class="link" href="glib-N-ary-Trees.html#g-node-unlink"><code class="function">g_node_unlink()</code></a> or<a class="link" href="glib-N-ary-Trees.html#g-node-destroy"><code class="function">g_node_destroy()</code></a>.</p></div><div class="refsect1" lang="en"><a name="id3337884"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3337894"></a><h3><a name="GNode"></a>GNode</h3><a class="indexterm" name="id3337906"></a><pre class="programlisting">typedef struct { gpointer data; GNode *next; GNode *prev; GNode *parent; GNode *children;} GNode;</pre><p>The <span class="structname">GNode</span> struct represents one node in a<a class="link" href="glib-N-ary-Trees.html" title="N-ary Trees">N-ary Tree</a>.fields </p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> <em class="structfield"><code>data</code></em>;</span></p></td><td>contains the actual data of the node.</td></tr><tr><td><p><span class="term"><a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *<em class="structfield"><code>next</code></em>;</span></p></td><td>points to the node's next sibling (a sibling is another <span class="structname">GNode</span> with the same parent).</td></tr><tr><td><p><span class="term"><a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *<em class="structfield"><code>prev</code></em>;</span></p></td><td>points to the node's previous sibling.</td></tr><tr><td><p><span class="term"><a class="link" href="glib-N-ary-Trees.html#GNode">GNode</a> *<em class="structfield"><code>parent</code></em>;</span></p></td><td>points to the parent of the <span class="structname">GNode</span>, or is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> if the <span class="structname">GNode</span> is the root of the tree.</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -