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 __FBSDID("$FreeBSD$");
20 #define _SEARCH_PRIVATE
24 typedef void (*cmp_fn_t)(const posix_tnode *, VISIT, int);
26 /* Walk the nodes of a tree */
28 trecurse(const posix_tnode *root, cmp_fn_t action, int level)
31 if (root->llink == NULL && root->rlink == NULL)
32 (*action)(root, leaf, level);
34 (*action)(root, preorder, level);
35 if (root->llink != NULL)
36 trecurse(root->llink, action, level + 1);
37 (*action)(root, postorder, level);
38 if (root->rlink != NULL)
39 trecurse(root->rlink, action, level + 1);
40 (*action)(root, endorder, level);
44 /* Walk the nodes of a tree */
46 twalk(const posix_tnode *vroot, cmp_fn_t action)
48 if (vroot != NULL && action != NULL)
49 trecurse(vroot, action, 0);