xref: /netbsd-src/lib/libc/stdlib/twalk.c (revision 7975455d4528eaa09f0c561d2fde4088681231ab)
1 /*	$NetBSD: twalk.c,v 1.1 1999/02/22 10:33:16 christos 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 <sys/cdefs.h>
15 #if defined(LIBC_SCCS) && !defined(lint)
16 __RCSID("$NetBSD: twalk.c,v 1.1 1999/02/22 10:33:16 christos Exp $");
17 #endif /* LIBC_SCCS and not lint */
18 
19 #define _SEARCH_PRIVATE
20 #include <search.h>
21 #include <stdlib.h>
22 
23 static void trecurse __P((const node_t *,
24     void  (*action)(const void *, VISIT, int), int level));
25 
26 /* Walk the nodes of a tree */
27 static void
28 trecurse(root, action, level)
29 	const node_t *root;	/* Root of the tree to be walked */
30 	void (*action) __P((const void *, VISIT, int));
31 	int level;
32 {
33 	if (root->llink == NULL && root->rlink == NULL)
34 		(*action)(root, leaf, level);
35 	else {
36 		(*action)(root, preorder, level);
37 		if (root->llink != NULL)
38 			trecurse(root->llink, action, level + 1);
39 		(*action)(root, postorder, level);
40 		if (root->rlink != NULL)
41 			trecurse(root->rlink, action, level + 1);
42 		(*action)(root, endorder, level);
43 	}
44 }
45 
46 /* Walk the nodes of a tree */
47 void
48 twalk(vroot, action)
49 	const void *vroot;	/* Root of the tree to be walked */
50 	void (*action) __P((const void *, VISIT, int));
51 {
52 	if (vroot != NULL && action != NULL)
53 		trecurse(vroot, action, 0);
54 }
55