1 /*****************************************************************************
2 * tdestroy.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(vkey
, vrootp
, compar
)
36 const void *vkey
; /* key to be deleted */
37 void **vrootp
; /* address of the root of tree */
38 int (*compar
) (const void *, const void *);
40 node_t
**rootp
= (node_t
**)vrootp
;
45 assert(compar
!= NULL
);
47 if (rootp
== NULL
|| (p
= *rootp
) == NULL
)
50 while ((cmp
= (*compar
)(vkey
, (*rootp
)->key
)) != 0) {
53 &(*rootp
)->llink
: /* follow llink branch */
54 &(*rootp
)->rlink
; /* follow rlink branch */
56 return NULL
; /* key not found */
58 r
= (*rootp
)->rlink
; /* D1: */
59 if ((q
= (*rootp
)->llink
) == NULL
) /* Left NULL? */
61 else if (r
!= NULL
) { /* Right link is NULL? */
62 if (r
->llink
== NULL
) { /* D2: Find successor */
65 } else { /* D3: Find NULL link */
66 for (q
= r
->llink
; q
->llink
!= NULL
; q
= r
->llink
)
69 q
->llink
= (*rootp
)->llink
;
70 q
->rlink
= (*rootp
)->rlink
;
74 free(*rootp
); /* D4: Free node */
75 *rootp
= q
; /* link parent to new node */
80 /* $NetBSD: tdestroy.c,v 1.2 1999/09/16 11:45:37 lukem Exp $ */
83 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
84 * the AT&T man page says.
86 * The node_t structure is for internal use only, lint doesn't grok it.
88 * Written by reading the System V Interface Definition, not the code.
90 * Totally public domain.
93 /* Walk the nodes of a tree */
95 tdestroy_recurse(node_t
* root
, void (*free_action
)(void *))
97 if (root
->llink
!= NULL
)
98 tdestroy_recurse(root
->llink
, free_action
);
99 if (root
->rlink
!= NULL
)
100 tdestroy_recurse(root
->rlink
, free_action
);
102 (*free_action
) ((void *) root
->key
);
107 tdestroy(vrootp
, freefct
)
109 void (*freefct
)(void *);
111 node_t
*root
= (node_t
*) vrootp
;
114 tdestroy_recurse(root
, freefct
);
118 /* $NetBSD: tfind.c,v 1.5 2005/03/23 08:16:53 kleink Exp $ */
121 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
122 * the AT&T man page says.
124 * The node_t structure is for internal use only, lint doesn't grok it.
126 * Written by reading the System V Interface Definition, not the code.
128 * Totally public domain.
131 /* find a node, or return 0 */
133 tfind(vkey
, vrootp
, compar
)
134 const void *vkey
; /* key to be found */
135 const void **vrootp
; /* address of the tree root */
136 int (*compar
) (const void *, const void *);
138 node_t
* const *rootp
= (node_t
* const*)vrootp
;
140 assert(vkey
!= NULL
);
141 assert(compar
!= NULL
);
146 while (*rootp
!= NULL
) { /* T1: */
149 if ((r
= (*compar
)(vkey
, (*rootp
)->key
)) == 0) /* T2: */
150 return *rootp
; /* key found */
152 &(*rootp
)->llink
: /* T3: follow left branch */
153 &(*rootp
)->rlink
; /* T4: follow right branch */
159 /* $NetBSD: tsearch.c,v 1.5 2005/11/29 03:12:00 christos Exp $ */
162 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
163 * the AT&T man page says.
165 * The node_t structure is for internal use only, lint doesn't grok it.
167 * Written by reading the System V Interface Definition, not the code.
169 * Totally public domain.
172 /* find or insert datum into search tree */
174 tsearch(vkey
, vrootp
, compar
)
175 const void *vkey
; /* key to be located */
176 void **vrootp
; /* address of tree root */
177 int (*compar
) (const void *, const void *);
180 node_t
**rootp
= (node_t
**)vrootp
;
182 assert(vkey
!= NULL
);
183 assert(compar
!= NULL
);
188 while (*rootp
!= NULL
) { /* Knuth's T1: */
191 if ((r
= (*compar
)(vkey
, (*rootp
)->key
)) == 0) /* T2: */
192 return *rootp
; /* we found it! */
195 &(*rootp
)->llink
: /* T3: follow left branch */
196 &(*rootp
)->rlink
; /* T4: follow right branch */
199 q
= malloc(sizeof(node_t
)); /* T5: key not found */
200 if (q
!= 0) { /* make new node */
201 *rootp
= q
; /* link new node to old */
202 q
->key
= (void*)vkey
; /* initialize new node */
203 q
->llink
= q
->rlink
= NULL
;
209 /* $NetBSD: twalk.c,v 1.2 1999/09/16 11:45:37 lukem Exp $ */
212 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
213 * the AT&T man page says.
215 * The node_t structure is for internal use only, lint doesn't grok it.
217 * Written by reading the System V Interface Definition, not the code.
219 * Totally public domain.
222 /* Walk the nodes of a tree */
224 twalk_recurse(root
, action
, level
)
225 const node_t
*root
; /* Root of the tree to be walked */
226 void (*action
) (const void *, VISIT
, int);
229 assert(root
!= NULL
);
230 assert(action
!= NULL
);
232 if (root
->llink
== NULL
&& root
->rlink
== NULL
)
233 (*action
)(root
, leaf
, level
);
235 (*action
)(root
, preorder
, level
);
236 if (root
->llink
!= NULL
)
237 twalk_recurse(root
->llink
, action
, level
+ 1);
238 (*action
)(root
, postorder
, level
);
239 if (root
->rlink
!= NULL
)
240 twalk_recurse(root
->rlink
, action
, level
+ 1);
241 (*action
)(root
, endorder
, level
);
245 /* Walk the nodes of a tree */
248 const void *vroot
; /* Root of the tree to be walked */
249 void (*action
) (const void *, VISIT
, int);
251 if (vroot
!= NULL
&& action
!= NULL
)
252 twalk_recurse(vroot
, action
, 0);
255 #endif // HAVE_SEARCH_H