Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / stdlib / twalk.c
blobc7a615e7ab69131d728cb7e7810c8c84a0404589
1 /* $NetBSD: twalk.c,v 1.1 1999/02/22 10:33:16 christos Exp $ */
3 /*
4 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
5 * the AT&T man page says.
7 * The node_t structure is for internal use only, lint doesn't grok it.
9 * Written by reading the System V Interface Definition, not the code.
11 * Totally public domain.
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 */
19 #include <assert.h>
20 #define _SEARCH_PRIVATE
21 #include <search.h>
22 #include <stdlib.h>
24 static void trecurse __P((const node_t *,
25 void (*action)(const void *, VISIT, int), int level));
27 /* Walk the nodes of a tree */
28 static void
29 trecurse(root, action, level)
30 const node_t *root; /* Root of the tree to be walked */
31 void (*action) __P((const void *, VISIT, int));
32 int level;
34 _DIAGASSERT(root != NULL);
35 _DIAGASSERT(action != NULL);
37 if (root->llink == NULL && root->rlink == NULL)
38 (*action)(root, leaf, level);
39 else {
40 (*action)(root, preorder, level);
41 if (root->llink != NULL)
42 trecurse(root->llink, action, level + 1);
43 (*action)(root, postorder, level);
44 if (root->rlink != NULL)
45 trecurse(root->rlink, action, level + 1);
46 (*action)(root, endorder, level);
50 /* Walk the nodes of a tree */
51 void
52 twalk(vroot, action)
53 const void *vroot; /* Root of the tree to be walked */
54 void (*action) __P((const void *, VISIT, int));
56 if (vroot != NULL && action != NULL)
57 trecurse(vroot, action, 0);