OSDN Git Service

0c681e5ead79c728136f13ae10127a4bced69974
[mingw/mingw-org-wsl.git] / mingwrt / mingwex / twalk.c
1 /* $NetBSD: twalk.c,v 1.2 1999/09/16 11:45:37 lukem Exp $
2  *
3  *
4  * Tree search generalized from Knuth (6.2.2) Algorithm T just like
5  * the AT&T man page says.
6  *
7  * Written by reading the System V Interface Definition, not the code.
8  *
9  * Totally public domain.
10  *
11  */
12 #define _SEARCH_PRIVATE
13 #include <search.h>
14 #include <stdlib.h>
15
16 static __MINGW_ATTRIB_NONNULL(1) __MINGW_ATTRIB_NONNULL(2)
17 void trecurse (const node_t *, void (*)(const void *, VISIT, int), int);
18
19 static void trecurse
20 ( const node_t *root, void (*action)(const void *, VISIT, int), int level)
21 {
22   /* Recursively walk the nodes of a tree, performing the specified
23    * action as each node is traversed, and as appropriate for each
24    * phase of traversal.
25    */
26   if( (root->llink == NULL) && (root->rlink == NULL) )
27     (*action) (root, leaf, level);
28
29   else
30   { (*action) (root, preorder, level);
31     if( root->llink != NULL )
32       trecurse (root->llink, action, level + 1);
33     (*action) (root, postorder, level);
34     if( root->rlink != NULL)
35       trecurse (root->rlink, action, level + 1);
36     (*action) (root, endorder, level);
37   }
38 }
39
40 void twalk (const void *root, void (*action)(const void *, VISIT, int))
41 {
42   /* Walk the nodes of a tree, delegating to the local recursive
43    * helper function, to invoke the specified action on each node
44    * in turn, as appropriate in each phase of traversal.
45    */
46   if( (root != NULL) && (action != NULL) )
47     trecurse (root, action, 0);
48 }