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