1 /* Copyright (C) 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Tree search generalized from Knuth (6.2.2) Algorithm T just like
20 the AT&T man page says.
22 The node_t structure is for internal use only, lint doesn't grok it.
24 Written by reading the System V Interface Definition, not the code.
26 Totally public domain. */
32 /* This routine is not very bad. It makes many assumptions about
33 the compiler. It assumpts that the first field in node must be
34 the "key" field, which points to the datum. It is a very trick
45 /* Prototype fpr local function. */
46 static void trecurse
__P ((const void *vroot
, __action_fn_t action
, int level
));
49 /* find or insert datum into search tree.
50 char *key; key to be located
51 node **rootp; address of tree root
52 int (*compar)(); ordering function
55 tsearch (key
, vrootp
, compar
)
61 node
**rootp
= (node
**) vrootp
;
66 while (*rootp
!= NULL
) /* Knuth's T1: */
70 r
= (*compar
) (key
, (*rootp
)->key
);
72 return *rootp
; /* we found it! */
74 ? &(*rootp
)->left
/* T3: follow left branch */
75 : &(*rootp
)->right
; /* T4: follow right branch */
78 q
= (node
*) malloc (sizeof (node
)); /* T5: key not found */
79 if (q
!= NULL
) /* make new node */
81 *rootp
= q
; /* link new node to old */
82 q
->key
= key
; /* initialize new node */
83 q
->left
= q
->right
= NULL
;
91 tfind (key
, vrootp
, compar
)
96 node
**rootp
= (node
**) vrootp
;
101 while (*rootp
!= NULL
) /* Knuth's T1: */
105 r
= (*compar
)(key
, (*rootp
)->key
);
106 if (r
== 0) /* T2: */
107 return *rootp
; /* we found it! */
110 ? &(*rootp
)->left
/* T3: follow left branch */
111 : &(*rootp
)->right
; /* T4: follow right branch */
117 /* delete node with given key
118 char *key; key to be deleted
119 node **rootp; address of the root of tree
120 int (*compar)(); comparison function
123 tdelete (key
, vrootp
, compar
)
126 __compar_fn_t compar
;
132 node
**rootp
= (node
**) vrootp
;
134 if (rootp
== NULL
|| (p
= *rootp
) == NULL
)
137 while ((cmp
= (*compar
) (key
, (*rootp
)->key
)) != 0)
141 ? &(*rootp
)->left
/* follow left branch */
142 : &(*rootp
)->right
; /* follow right branch */
144 return NULL
; /* key not found */
147 r
= (*rootp
)->right
; /* D1: */
149 if (q
== NULL
) /* Left NULL? */
151 else if (r
!= NULL
) /* Right link is NULL? */
153 if (r
->left
== NULL
) /* D2: Find successor */
159 { /* D3: Find (struct node_t *)0 link */
160 for (q
= r
->left
; q
->left
!= NULL
; q
= r
->left
)
163 q
->left
= (*rootp
)->left
;
164 q
->right
= (*rootp
)->right
;
167 free ((struct node_t
*) *rootp
); /* D4: Free node */
168 *rootp
= q
; /* link parent to new node */
173 /* Walk the nodes of a tree
174 node *root; Root of the tree to be walked
175 void (*action)(); Function to be called at each node
179 trecurse (vroot
, action
, level
)
181 __action_fn_t action
;
184 node
*root
= (node
*) vroot
;
186 if (root
->left
== NULL
&& root
->right
== NULL
)
187 (*action
) (root
, leaf
, level
);
190 (*action
) (root
, preorder
, level
);
191 if (root
->left
!= NULL
)
192 trecurse (root
->left
, action
, level
+ 1);
193 (*action
) (root
, postorder
, level
);
194 if (root
->right
!= NULL
)
195 trecurse (root
->right
, action
, level
+ 1);
196 (*action
) (root
, endorder
, level
);
201 /* void twalk(root, action) Walk the nodes of a tree
202 node *root; Root of the tree to be walked
203 void (*action)(); Function to be called at each node
207 twalk (vroot
, action
)
209 __action_fn_t action
;
211 const node
*root
= (node
*) vroot
;
213 if (root
!= NULL
&& action
!= NULL
)
214 trecurse (root
, action
, 0);