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  * The node_t structure is for internal use only, lint doesn't grok it.
8  *
9  * Written by reading the System V Interface Definition, not the code.
10  *
11  * Totally public domain.
12  */
13 
14 #include <assert.h>
15 #define _SEARCH_PRIVATE
16 #include <search.h>
17 #include <stdlib.h>
18 
19 static void trecurse (const node_t *, void (*action)(const void *, VISIT, int),
20 	              int level)  __MINGW_ATTRIB_NONNULL (1)
21 				  __MINGW_ATTRIB_NONNULL (2);
22 /* Walk the nodes of a tree */
23 static void
trecurse(const node_t * root,void (* action)(const void *,VISIT,int),int level)24 trecurse (const node_t *root,	/* Root of the tree to be walked */
25 	  void (*action)(const void *, VISIT, int),
26 	  int level)
27 {
28   if (root->llink == NULL && root->rlink == NULL)
29     (*action)(root, leaf, level);
30   else
31     {
32       (*action)(root, preorder, level);
33       if (root->llink != NULL)
34         trecurse (root->llink, action, level + 1);
35       (*action)(root, postorder, level);
36       if (root->rlink != NULL)
37 	      trecurse(root->rlink, action, level + 1);
38       (*action)(root, endorder, level);
39     }
40 }
41 
42 /* Walk the nodes of a tree */
43 void
twalk(const void * vroot,void (* action)(const void *,VISIT,int))44 twalk (const void *vroot,	/* Root of the tree to be walked */
45        void (*action) (const void *, VISIT, int))
46 {
47   if (vroot != NULL && action != NULL)
48     trecurse(vroot, action, 0);
49 }
50