xref: /dragonfly/lib/libc/stdlib/twalk.c (revision 1ab20d67)
1 /*
2  * Tree search generalized from Knuth (6.2.2) Algorithm T just like
3  * the AT&T man page says.
4  *
5  * The node_t structure is for internal use only, lint doesn't grok it.
6  *
7  * Written by reading the System V Interface Definition, not the code.
8  *
9  * Totally public domain.
10  *
11  * $NetBSD: twalk.c,v 1.1 1999/02/22 10:33:16 christos Exp $
12  * $FreeBSD: src/lib/libc/stdlib/twalk.c,v 1.1.2.1 2000/08/17 07:38:39 jhb Exp $
13  * $DragonFly: src/lib/libc/stdlib/twalk.c,v 1.3 2003/09/06 08:19:16 asmodai Exp $
14  */
15 
16 #include <sys/cdefs.h>
17 
18 #include <assert.h>
19 #define _SEARCH_PRIVATE
20 #include <search.h>
21 #include <stdlib.h>
22 
23 static void trecurse (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) (const void *, VISIT, int);
31 	int level;
32 {
33 
34 	if (root->llink == NULL && root->rlink == NULL)
35 		(*action)(root, leaf, level);
36 	else {
37 		(*action)(root, preorder, level);
38 		if (root->llink != NULL)
39 			trecurse(root->llink, action, level + 1);
40 		(*action)(root, postorder, level);
41 		if (root->rlink != NULL)
42 			trecurse(root->rlink, action, level + 1);
43 		(*action)(root, endorder, level);
44 	}
45 }
46 
47 /* Walk the nodes of a tree */
48 void
49 twalk(vroot, action)
50 	const void *vroot;	/* Root of the tree to be walked */
51 	void (*action) (const void *, VISIT, int);
52 {
53 	if (vroot != NULL && action != NULL)
54 		trecurse(vroot, action, 0);
55 }
56