1 /* $NetBSD: tsearch.c,v 1.2 2017/01/28 21:31:50 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 * NetBSD: tsearch.c,v 1.3 1999/09/16 11:45:37 lukem Exp
14 * NetBSD: twalk.c,v 1.1 1999/02/22 10:33:16 christos Exp
15 * NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp
16 * NetBSD: tfind.c,v 1.2 1999/09/16 11:45:37 lukem Exp
17 */
18
19 #include <config.h>
20 #include <krb5/roken.h>
21 #include "search.h"
22 #include <stdlib.h>
23
24 typedef struct node {
25 char *key;
26 struct node *llink, *rlink;
27 } node_t;
28
29 #ifndef __DECONST
30 #define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
31 #endif
32
33 /*
34 * find or insert datum into search tree
35 *
36 * Parameters:
37 * vkey: key to be located
38 * vrootp: address of tree root
39 */
40
41 ROKEN_LIB_FUNCTION void *
rk_tsearch(const void * vkey,void ** vrootp,int (* compar)(const void *,const void *))42 rk_tsearch(const void *vkey, void **vrootp,
43 int (*compar)(const void *, const void *))
44 {
45 node_t *q;
46 node_t **rootp = (node_t **)vrootp;
47
48 if (rootp == NULL)
49 return NULL;
50
51 while (*rootp != NULL) { /* Knuth's T1: */
52 int r;
53
54 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
55 return *rootp; /* we found it! */
56
57 rootp = (r < 0) ?
58 &(*rootp)->llink : /* T3: follow left branch */
59 &(*rootp)->rlink; /* T4: follow right branch */
60 }
61
62 q = malloc(sizeof(node_t)); /* T5: key not found */
63 if (q != 0) { /* make new node */
64 *rootp = q; /* link new node to old */
65 /* LINTED const castaway ok */
66 q->key = __DECONST(void *, vkey); /* initialize new node */
67 q->llink = q->rlink = NULL;
68 }
69 return q;
70 }
71
72 /*
73 * Walk the nodes of a tree
74 *
75 * Parameters:
76 * root: Root of the tree to be walked
77 */
78 static void
trecurse(const node_t * root,void (* action)(const void *,VISIT,int),int level)79 trecurse(const node_t *root, void (*action)(const void *, VISIT, int),
80 int level)
81 {
82
83 if (root->llink == NULL && root->rlink == NULL)
84 (*action)(root, leaf, level);
85 else {
86 (*action)(root, preorder, level);
87 if (root->llink != NULL)
88 trecurse(root->llink, action, level + 1);
89 (*action)(root, postorder, level);
90 if (root->rlink != NULL)
91 trecurse(root->rlink, action, level + 1);
92 (*action)(root, endorder, level);
93 }
94 }
95
96 /*
97 * Walk the nodes of a tree
98 *
99 * Parameters:
100 * vroot: Root of the tree to be walked
101 */
102 ROKEN_LIB_FUNCTION void
rk_twalk(const void * vroot,void (* action)(const void *,VISIT,int))103 rk_twalk(const void *vroot,
104 void (*action)(const void *, VISIT, int))
105 {
106 if (vroot != NULL && action != NULL)
107 trecurse(vroot, action, 0);
108 }
109
110 /*
111 * delete node with given key
112 *
113 * vkey: key to be deleted
114 * vrootp: address of the root of the tree
115 * compar: function to carry out node comparisons
116 */
117 ROKEN_LIB_FUNCTION void *
rk_tdelete(const void * vkey,void ** vrootp,int (* compar)(const void *,const void *))118 rk_tdelete(const void * vkey, void ** vrootp,
119 int (*compar)(const void *, const void *))
120 {
121 node_t **rootp = (node_t **)vrootp;
122 node_t *q, *r;
123 int cmp;
124
125 if (rootp == NULL || *rootp == NULL)
126 return NULL;
127
128 while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
129 rootp = (cmp < 0) ?
130 &(*rootp)->llink : /* follow llink branch */
131 &(*rootp)->rlink; /* follow rlink branch */
132 if (*rootp == NULL)
133 return NULL; /* key not found */
134 }
135 r = (*rootp)->rlink; /* D1: */
136 if ((q = (*rootp)->llink) == NULL) /* Left NULL? */
137 q = r;
138 else if (r != NULL) { /* Right link is NULL? */
139 if (r->llink == NULL) { /* D2: Find successor */
140 r->llink = q;
141 q = r;
142 } else { /* D3: Find NULL link */
143 for (q = r->llink; q->llink != NULL; q = r->llink)
144 r = q;
145 r->llink = q->rlink;
146 q->llink = (*rootp)->llink;
147 q->rlink = (*rootp)->rlink;
148 }
149 }
150 free(*rootp); /* D4: Free node */
151 *rootp = q; /* link parent to new node */
152 return *rootp;
153 }
154
155 /*
156 * find a node, or return 0
157 *
158 * Parameters:
159 * vkey: key to be found
160 * vrootp: address of the tree root
161 */
162 ROKEN_LIB_FUNCTION void *
rk_tfind(const void * vkey,void * const * vrootp,int (* compar)(const void *,const void *))163 rk_tfind(const void *vkey, void * const *vrootp,
164 int (*compar)(const void *, const void *))
165 {
166 node_t **rootp = (node_t **)vrootp;
167
168 if (rootp == NULL)
169 return NULL;
170
171 while (*rootp != NULL) { /* T1: */
172 int r;
173
174 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
175 return *rootp; /* key found */
176 rootp = (r < 0) ?
177 &(*rootp)->llink : /* T3: follow left branch */
178 &(*rootp)->rlink; /* T4: follow right branch */
179 }
180 return NULL;
181 }
182