2014-07-15 03:28:37 +00:00
|
|
|
/* $NetBSD: twalk.c,v 1.4 2012/03/20 16:38:45 matt Exp $ */
|
2000-07-01 06:55:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tree search generalized from Knuth (6.2.2) Algorithm T just like
|
|
|
|
* the AT&T man page says.
|
|
|
|
*
|
|
|
|
* Written by reading the System V Interface Definition, not the code.
|
|
|
|
*
|
|
|
|
* Totally public domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2002-03-22 21:53:29 +00:00
|
|
|
#if 0
|
2000-07-01 06:55:11 +00:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2014-07-15 03:28:37 +00:00
|
|
|
__RCSID("$NetBSD: twalk.c,v 1.4 2012/03/20 16:38:45 matt Exp $");
|
2000-07-01 06:55:11 +00:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
2002-03-22 21:53:29 +00:00
|
|
|
#endif
|
|
|
|
__FBSDID("$FreeBSD$");
|
2000-07-01 06:55:11 +00:00
|
|
|
|
|
|
|
#define _SEARCH_PRIVATE
|
|
|
|
#include <search.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-10-13 18:25:40 +00:00
|
|
|
typedef void (*cmp_fn_t)(const posix_tnode *, VISIT, int);
|
2000-07-01 06:55:11 +00:00
|
|
|
|
|
|
|
/* Walk the nodes of a tree */
|
|
|
|
static void
|
2016-10-13 18:25:40 +00:00
|
|
|
trecurse(const posix_tnode *root, cmp_fn_t action, int level)
|
2000-07-01 06:55:11 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (root->llink == NULL && root->rlink == NULL)
|
|
|
|
(*action)(root, leaf, level);
|
|
|
|
else {
|
|
|
|
(*action)(root, preorder, level);
|
|
|
|
if (root->llink != NULL)
|
|
|
|
trecurse(root->llink, action, level + 1);
|
|
|
|
(*action)(root, postorder, level);
|
|
|
|
if (root->rlink != NULL)
|
|
|
|
trecurse(root->rlink, action, level + 1);
|
|
|
|
(*action)(root, endorder, level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Walk the nodes of a tree */
|
|
|
|
void
|
2016-10-13 18:25:40 +00:00
|
|
|
twalk(const posix_tnode *vroot, cmp_fn_t action)
|
2000-07-01 06:55:11 +00:00
|
|
|
{
|
|
|
|
if (vroot != NULL && action != NULL)
|
|
|
|
trecurse(vroot, action, 0);
|
|
|
|
}
|