1 /* $NetBSD: twalk.c,v 1.4 2012/03/20 16:38:45 matt Exp $ */
4 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
5 * the AT&T man page says.
7 * Written by reading the System V Interface Definition, not the code.
9 * Totally public domain.
12 #include <sys/cdefs.h>
14 #if defined(LIBC_SCCS) && !defined(lint)
15 __RCSID("$NetBSD: twalk.c,v 1.4 2012/03/20 16:38:45 matt Exp $");
16 #endif /* LIBC_SCCS and not lint */
18 #define _SEARCH_PRIVATE
22 typedef void (*cmp_fn_t)(const posix_tnode *, VISIT, int);
24 /* Walk the nodes of a tree */
26 trecurse(const posix_tnode *root, cmp_fn_t action, int level)
29 if (root->llink == NULL && root->rlink == NULL)
30 (*action)(root, leaf, level);
32 (*action)(root, preorder, level);
33 if (root->llink != NULL)
34 trecurse(root->llink, action, level + 1);
35 (*action)(root, postorder, level);
36 if (root->rlink != NULL)
37 trecurse(root->rlink, action, level + 1);
38 (*action)(root, endorder, level);
42 /* Walk the nodes of a tree */
44 twalk(const posix_tnode *vroot, cmp_fn_t action)
46 if (vroot != NULL && action != NULL)
47 trecurse(vroot, action, 0);