1*44551472Smatt /* $NetBSD: twalk.c,v 1.4 2012/03/20 16:38:45 matt Exp $ */
27975455dSchristos
37975455dSchristos /*
47975455dSchristos * Tree search generalized from Knuth (6.2.2) Algorithm T just like
57975455dSchristos * the AT&T man page says.
67975455dSchristos *
77975455dSchristos * The node_t structure is for internal use only, lint doesn't grok it.
87975455dSchristos *
97975455dSchristos * Written by reading the System V Interface Definition, not the code.
107975455dSchristos *
117975455dSchristos * Totally public domain.
127975455dSchristos */
137975455dSchristos
147975455dSchristos #include <sys/cdefs.h>
157975455dSchristos #if defined(LIBC_SCCS) && !defined(lint)
16*44551472Smatt __RCSID("$NetBSD: twalk.c,v 1.4 2012/03/20 16:38:45 matt Exp $");
177975455dSchristos #endif /* LIBC_SCCS and not lint */
187975455dSchristos
19b48252f3Slukem #include <assert.h>
207975455dSchristos #define _SEARCH_PRIVATE
217975455dSchristos #include <search.h>
227975455dSchristos #include <stdlib.h>
237975455dSchristos
24*44551472Smatt typedef void (*cmp_fn_t)(const void *, VISIT, int);
257975455dSchristos
267975455dSchristos /* Walk the nodes of a tree */
277975455dSchristos static void
trecurse(const node_t * root,cmp_fn_t action,int level)28*44551472Smatt trecurse(const node_t *root, /* Root of the tree to be walked */
29*44551472Smatt cmp_fn_t action, int level)
307975455dSchristos {
31b48252f3Slukem _DIAGASSERT(root != NULL);
32b48252f3Slukem _DIAGASSERT(action != NULL);
33b48252f3Slukem
347975455dSchristos if (root->llink == NULL && root->rlink == NULL)
357975455dSchristos (*action)(root, leaf, level);
367975455dSchristos else {
377975455dSchristos (*action)(root, preorder, level);
387975455dSchristos if (root->llink != NULL)
397975455dSchristos trecurse(root->llink, action, level + 1);
407975455dSchristos (*action)(root, postorder, level);
417975455dSchristos if (root->rlink != NULL)
427975455dSchristos trecurse(root->rlink, action, level + 1);
437975455dSchristos (*action)(root, endorder, level);
447975455dSchristos }
457975455dSchristos }
467975455dSchristos
477975455dSchristos /* Walk the nodes of a tree */
487975455dSchristos void
twalk(const void * vroot,cmp_fn_t action)49*44551472Smatt twalk(const void *vroot, cmp_fn_t action) /* Root of the tree to be walked */
507975455dSchristos {
517975455dSchristos if (vroot != NULL && action != NULL)
527975455dSchristos trecurse(vroot, action, 0);
537975455dSchristos }
54