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