6
* inserts the key into the tree if it isn't there already
7
* @param key - the key we are inserting (input)
8
* @param nodep - what the new node's nodep should be (optional input)
9
* @param rootp - the address of the tree's root
10
* NOTE: tinsert cannot report failure
15
const u_long key, /* I */
16
struct pbsnode *nodep, /* I (optional) */
24
/* invalid tree address - failure */
29
while (*rootp != NULL)
33
if (key == (*rootp)->key) /* T2: */
35
/* key already exists */
37
return; /* we found it! */
40
rootp = (key < (*rootp)->key) ?
42
&(*rootp)->left : /* T3: follow left branch */
43
&(*rootp)->right; /* T4: follow right branch */
46
/* create new tree node */
48
q = (tree *)malloc(sizeof(tree)); /* T5: key not found */
52
/* cannot allocate memory - failure */
58
/* link new node to old */
60
/* initialize new tree node */
64
/* if nodep is null then this doesn't do anything */
76
* delete the node with the given key
77
* @param key - the key of the node to be deleted
78
* @param rootp - the address of the root of the tree
83
const u_long key, /* I */
93
sprintf(log_buffer, "deleting key %lu",
98
PBS_EVENTCLASS_REQUEST,
103
if ((rootp == NULL) || ((p = *rootp) == NULL))
108
while (key != (*rootp)->key)
112
rootp = (key < (*rootp)->key) ?
113
&(*rootp)->left : /* left branch */
114
&(*rootp)->right; /* right branch */
118
return(NULL); /* key not found */
122
r = (*rootp)->right; /* D1: */
124
if ((q = (*rootp)->left) == NULL) /* Left */
134
/* D2: Find successor */
141
/* D3: Find (struct tree_t *)0 link */
143
for (q = r->left;q->left != NULL;q = r->left)
150
q->left = (*rootp)->left;
151
q->right = (*rootp)->right;
155
free((struct tree_t *)*rootp); /* D4: Free node */
157
*rootp = q; /* link parent to new node */
160
} /* END tdelete() */
164
* Tree search generalized from Knuth (6.2.2) Algorithm T just like
165
* the AT&T man page says.
167
* The tree_t structure is for internal use only, lint doesn't grok it.
169
* Written by reading the System V Interface Definition, not the code.
171
* Totally public domain.
176
* finds a pointer to the specified node in the tree
177
* @param key - the key of the node to find
178
* @param rootp - the address of the tree's root
179
* @return a pointer to the node with the specified key or NULL if not found
181
* NOTE: on the mom nodep is never used, but it expects a non-NULL value or
182
* else it thinks the key wasn't found
183
* WARNING: this means that pointer can't be used on the mom and that you
187
struct pbsnode *tfind(
189
const u_long key, /* I */
190
tree **rootp) /* I / O */
198
while (*rootp != NULL)
202
if (key == (*rootp)->key) /* T2: */
206
/* this if logic prevents mom errors, see above note */
207
if ((*rootp)->nodep != NULL)
208
return((*rootp)->nodep);
210
return((struct pbsnode *)1);
213
rootp = (key < (*rootp)->key) ?
215
&(*rootp)->left : /* T3: follow left branch */
216
&(*rootp)->right; /* T4: follow right branch */
226
* lists all of the keys in the tree
227
* NOTE: recursive. Buf not initialized
228
* @param rootp - the root node of the tree
229
* @param Buf - the output buffer
230
* @param BufSize - the size of the output buffer
231
* @return 1 if rootp or Buf are NULL, -1 if the buffer is too small, -1 otherwise
237
char *Buf, /* O (modified) */
245
/* check for bad inputs */
246
if ((rootp == NULL) || (Buf == NULL))
248
/* empty tree - failure */
255
/* inadequate space to append data */
262
if (rootp->left != NULL)
264
tlist(rootp->left, Buf, BSize);
266
BSize -= strlen(Buf);
269
if (rootp->right != NULL)
271
tlist(rootp->right, Buf, BSize);
273
BSize -= strlen(Buf);
278
/* inadequate space to append data */
283
sprintf(tmpLine, "%ld.%ld.%ld.%ld",
285
(rootp->key & 0xff000000) >> 24,
286
(rootp->key & 0x00ff0000) >> 16,
287
(rootp->key & 0x0000ff00) >> 8,
288
(rootp->key & 0x000000ff));
290
if ((Buf[0] != '\0') && (BSize > 1))
297
if (BSize > (int)strlen(tmpLine))
299
strcat(Buf, tmpLine);
309
* frees the tree from memory
310
* @param rootp - the address of the root of the tree
314
tree **rootp) /* I */
317
if (rootp == NULL || *rootp == NULL)
322
tfree(&(*rootp)->left);
324
tfree(&(*rootp)->right);