1 /*****************************************************************************
2 * tfind.c : implement every t* fuctions
3 *****************************************************************************/
9 /** search.h is not present so every t* functions has to be implemented */
17 struct node
*llink
, *rlink
;
20 /* $NetBSD: tdelete.c,v 1.4 2006/03/19 01:12:08 christos Exp $ */
23 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
24 * the AT&T man page says.
26 * The node_t structure is for internal use only, lint doesn't grok it.
28 * Written by reading the System V Interface Definition, not the code.
30 * Totally public domain.
33 /* delete node with given key */
35 tdelete(const void* vkey
, void** vrootp
, int (*compar
)(const void*, const void*))
37 node_t
**rootp
= (node_t
**)vrootp
;
42 assert(compar
!= NULL
);
44 if (rootp
== NULL
|| (p
= *rootp
) == NULL
)
47 while ((cmp
= (*compar
)(vkey
, (*rootp
)->key
)) != 0) {
50 &(*rootp
)->llink
: /* follow llink branch */
51 &(*rootp
)->rlink
; /* follow rlink branch */
53 return NULL
; /* key not found */
55 r
= (*rootp
)->rlink
; /* D1: */
56 if ((q
= (*rootp
)->llink
) == NULL
) /* Left NULL? */
58 else if (r
!= NULL
) { /* Right link is NULL? */
59 if (r
->llink
== NULL
) { /* D2: Find successor */
62 } else { /* D3: Find NULL link */
63 for (q
= r
->llink
; q
->llink
!= NULL
; q
= r
->llink
)
66 q
->llink
= (*rootp
)->llink
;
67 q
->rlink
= (*rootp
)->rlink
;
71 free(*rootp
); /* D4: Free node */
72 *rootp
= q
; /* link parent to new node */
77 /* $NetBSD: tdestroy.c,v 1.2 1999/09/16 11:45:37 lukem Exp $ */
80 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
81 * the AT&T man page says.
83 * The node_t structure is for internal use only, lint doesn't grok it.
85 * Written by reading the System V Interface Definition, not the code.
87 * Totally public domain.
90 /* Walk the nodes of a tree */
92 tdestroy_recurse(node_t
* root
, void (*free_action
)(void *))
94 if (root
->llink
!= NULL
)
95 tdestroy_recurse(root
->llink
, free_action
);
96 if (root
->rlink
!= NULL
)
97 tdestroy_recurse(root
->rlink
, free_action
);
99 (*free_action
) ((void *) root
->key
);
104 tdestroy(void *vrootp
, void (*freefct
)(void*))
106 node_t
*root
= (node_t
*) vrootp
;
109 tdestroy_recurse(root
, freefct
);
113 /* $NetBSD: tfind.c,v 1.5 2005/03/23 08:16:53 kleink Exp $ */
116 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
117 * the AT&T man page says.
119 * The node_t structure is for internal use only, lint doesn't grok it.
121 * Written by reading the System V Interface Definition, not the code.
123 * Totally public domain.
126 /* find a node, or return 0 */
128 tfind(const void* vkey
, void* const *vrootp
, int (*compar
) (const void *, const void *))
130 node_t
* const *rootp
= (node_t
* const*)vrootp
;
132 assert(vkey
!= NULL
);
133 assert(compar
!= NULL
);
138 while (*rootp
!= NULL
) { /* T1: */
141 if ((r
= (*compar
)(vkey
, (*rootp
)->key
)) == 0) /* T2: */
142 return *rootp
; /* key found */
144 &(*rootp
)->llink
: /* T3: follow left branch */
145 &(*rootp
)->rlink
; /* T4: follow right branch */
151 /* $NetBSD: tsearch.c,v 1.5 2005/11/29 03:12:00 christos Exp $ */
154 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
155 * the AT&T man page says.
157 * The node_t structure is for internal use only, lint doesn't grok it.
159 * Written by reading the System V Interface Definition, not the code.
161 * Totally public domain.
164 /* find or insert datum into search tree */
166 tsearch(const void* vkey
, void** vrootp
, int (*compar
)(const void*, const void*))
169 node_t
**rootp
= (node_t
**)vrootp
;
171 assert(vkey
!= NULL
);
172 assert(compar
!= NULL
);
177 while (*rootp
!= NULL
) { /* Knuth's T1: */
180 if ((r
= (*compar
)(vkey
, (*rootp
)->key
)) == 0) /* T2: */
181 return *rootp
; /* we found it! */
184 &(*rootp
)->llink
: /* T3: follow left branch */
185 &(*rootp
)->rlink
; /* T4: follow right branch */
188 q
= malloc(sizeof(node_t
)); /* T5: key not found */
189 if (q
!= 0) { /* make new node */
190 *rootp
= q
; /* link new node to old */
191 q
->key
= (void*)vkey
; /* initialize new node */
192 q
->llink
= q
->rlink
= NULL
;
198 /* $NetBSD: twalk.c,v 1.2 1999/09/16 11:45:37 lukem Exp $ */
201 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
202 * the AT&T man page says.
204 * The node_t structure is for internal use only, lint doesn't grok it.
206 * Written by reading the System V Interface Definition, not the code.
208 * Totally public domain.
211 /* Walk the nodes of a tree */
213 twalk_recurse(const node_t
* root
, void (*action
)(const void*, VISIT
, int), int level
)
215 assert(root
!= NULL
);
216 assert(action
!= NULL
);
218 if (root
->llink
== NULL
&& root
->rlink
== NULL
)
219 (*action
)(root
, leaf
, level
);
221 (*action
)(root
, preorder
, level
);
222 if (root
->llink
!= NULL
)
223 twalk_recurse(root
->llink
, action
, level
+ 1);
224 (*action
)(root
, postorder
, level
);
225 if (root
->rlink
!= NULL
)
226 twalk_recurse(root
->rlink
, action
, level
+ 1);
227 (*action
)(root
, endorder
, level
);
231 /* Walk the nodes of a tree */
233 twalk(const void* vroot
, void (*action
)(const void*, VISIT
, int))
235 if (vroot
!= NULL
&& action
!= NULL
)
236 twalk_recurse(vroot
, action
, 0);
239 #endif // HAVE_SEARCH_H