Function tskit::bindings::tsk_tree_preorder
source · pub unsafe extern "C" fn tsk_tree_preorder(
self_: *const tsk_tree_t,
nodes: *mut tsk_id_t,
num_nodes: *mut tsk_size_t
) -> c_int
Expand description
@brief Fill an array with the nodes of this tree in preorder.
@rst
Populate an array with the nodes in this tree in preorder. The array
must be pre-allocated and be sufficiently large to hold the array
of nodes visited. The recommended approach is to use the
:c:func:tsk_tree_get_size_bound
function, as in the following example:
.. code-block:: c
static void print_preorder(tsk_tree_t *tree) { int ret; tsk_size_t num_nodes, j; tsk_id_t *nodes = malloc(tsk_tree_get_size_bound(tree) * sizeof(*nodes));
if (nodes == NULL) { errx(EXIT_FAILURE, “Out of memory”); } ret = tsk_tree_preorder(tree, nodes, &num_nodes); check_tsk_error(ret); for (j = 0; j < num_nodes; j++) { printf(“Visit preorder %lld\n”, (long long) nodes[j]); } free(nodes); }
.. seealso::
See the :ref:sec_c_api_examples_tree_traversals
section for
more examples.
@endrst
@param self A pointer to a tsk_tree_t object. @param nodes The tsk_id_t array to store nodes in. See notes above for details. @param num_nodes A pointer to a tsk_size_t value where we store the number of nodes in the traversal. @return 0 on success or a negative value on failure.