twalk() — Binary tree walk
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4
XPG4.2 Single UNIX Specification, Version 3 |
both |
Format
#define _XOPEN_SOURCE
#include <search.h>
void
*twalk(const void *root, void (*action)(const void *, VISIT, int));
General description
The twalk()
function traverses a binary search tree. The root argument
is a pointer to the root node of the tree to be traversed. (Any node
in a tree may be used as the root for a walk below that node.) The
argument action is the name of a routine
to be invoked at each node. This routine is, in turn, called with
three arguments. The first argument is the address of the node being
visited. The structure pointed to by this argument is unspecified
and must not be modified by the application, but is guaranteed that
a pointer-to-node can be converted to pointer-to-pointer-to-element
to access the element stored in the node. The second argument is a
value from an enumeration data type:
typedef enum {preorder, postorder, endorder, leaf } VISIT;
(defined
in the <search.h> header), depending on whether this is the
first, second or third time that the node is visited (during a depth-first,
left-to-right traversal of the tree), or whether the node is a leaf.
The third argument is the level of the node in the tree, with the
root being level zero.Threading Behavior: see tsearch() — Binary tree search.
Special behavior for C++: Because
C and C++ linkage
conventions are incompatible, twalk() cannot receive a C++ function
pointer as the argument. If you attempt to pass a C++ function
pointer to twalk(), the compiler will flag it as an error. You can
pass a C or C++ function
to twalk() by declaring it as extern C
.
Returned value
twalk() returns no values.
No errors are defined.