c12b600dc3630607437a76dd7b5710af954a6f91
[dragonfly.git] / lib / libc / stdlib / twalk.c
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.5 2005/11/24 17:18:30 swildner 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  *
28  * Parameters:
29  *      root:   Root of the tree to be walked
30  */
31 static void
32 trecurse(const node_t *root, void (*action)(const void *, VISIT, int),
33          int level)
34 {
35
36         if (root->llink == NULL && root->rlink == NULL)
37                 (*action)(root, leaf, level);
38         else {
39                 (*action)(root, preorder, level);
40                 if (root->llink != NULL)
41                         trecurse(root->llink, action, level + 1);
42                 (*action)(root, postorder, level);
43                 if (root->rlink != NULL)
44                         trecurse(root->rlink, action, level + 1);
45                 (*action)(root, endorder, level);
46         }
47 }
48
49 /* Walk the nodes of a tree
50  *
51  * Parameters:
52  *      vroot:  Root of the tree to be walked
53  */
54 void
55 twalk(const void *vroot,
56       void (*action)(const void *, VISIT, int))
57 {
58         if (vroot != NULL && action != NULL)
59                 trecurse(vroot, action, 0);
60 }