format tsearch
This commit is contained in:
parent
81e34dc630
commit
58be2d1ee8
2 changed files with 92 additions and 104 deletions
|
|
@ -11,8 +11,8 @@
|
||||||
* Totally public domain.
|
* Totally public domain.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "tsearch.h"
|
#include "tsearch.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
typedef struct node_t {
|
typedef struct node_t {
|
||||||
char *key;
|
char *key;
|
||||||
|
|
@ -20,10 +20,8 @@ typedef struct node_t {
|
||||||
} node;
|
} node;
|
||||||
|
|
||||||
/* find or insert datum into search tree */
|
/* find or insert datum into search tree */
|
||||||
void *
|
void *tsearch(const void *vkey, void **vrootp,
|
||||||
tsearch(const void *vkey, void **vrootp,
|
int (*compar)(const void *, const void *)) {
|
||||||
int (*compar)(const void *, const void *))
|
|
||||||
{
|
|
||||||
node *q;
|
node *q;
|
||||||
char *key = (char *)vkey;
|
char *key = (char *)vkey;
|
||||||
node **rootp = (node **)vrootp;
|
node **rootp = (node **)vrootp;
|
||||||
|
|
@ -35,8 +33,7 @@ tsearch(const void *vkey, void **vrootp,
|
||||||
|
|
||||||
if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
|
if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
|
||||||
return ((void *)*rootp); /* we found it! */
|
return ((void *)*rootp); /* we found it! */
|
||||||
rootp = (r < 0) ?
|
rootp = (r < 0) ? &(*rootp)->left : /* T3: follow left branch */
|
||||||
&(*rootp)->left : /* T3: follow left branch */
|
|
||||||
&(*rootp)->right; /* T4: follow right branch */
|
&(*rootp)->right; /* T4: follow right branch */
|
||||||
}
|
}
|
||||||
q = malloc(sizeof(node)); /* T5: key not found */
|
q = malloc(sizeof(node)); /* T5: key not found */
|
||||||
|
|
@ -49,10 +46,8 @@ tsearch(const void *vkey, void **vrootp,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* delete node with given key */
|
/* delete node with given key */
|
||||||
void *
|
void *tdelete(const void *vkey, void **vrootp,
|
||||||
tdelete(const void *vkey, void **vrootp,
|
int (*compar)(const void *, const void *)) {
|
||||||
int (*compar)(const void *, const void *))
|
|
||||||
{
|
|
||||||
node **rootp = (node **)vrootp;
|
node **rootp = (node **)vrootp;
|
||||||
char *key = (char *)vkey;
|
char *key = (char *)vkey;
|
||||||
node *p = (node *)1;
|
node *p = (node *)1;
|
||||||
|
|
@ -64,8 +59,7 @@ tdelete(const void *vkey, void **vrootp,
|
||||||
return ((struct node_t *)0);
|
return ((struct node_t *)0);
|
||||||
while ((cmp = (*compar)(key, (*rootp)->key)) != 0) {
|
while ((cmp = (*compar)(key, (*rootp)->key)) != 0) {
|
||||||
p = *rootp;
|
p = *rootp;
|
||||||
rootp = (cmp < 0) ?
|
rootp = (cmp < 0) ? &(*rootp)->left : /* follow left branch */
|
||||||
&(*rootp)->left : /* follow left branch */
|
|
||||||
&(*rootp)->right; /* follow right branch */
|
&(*rootp)->right; /* follow right branch */
|
||||||
if (*rootp == (struct node_t *)0)
|
if (*rootp == (struct node_t *)0)
|
||||||
return ((void *)0); /* key not found */
|
return ((void *)0); /* key not found */
|
||||||
|
|
@ -85,15 +79,14 @@ tdelete(const void *vkey, void **vrootp,
|
||||||
q->right = (*rootp)->right;
|
q->right = (*rootp)->right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free((struct node_t *) *rootp); /* D4: Free node */
|
free((struct node_t *)*rootp); /* D4: Free node */
|
||||||
*rootp = q; /* link parent to new node */
|
*rootp = q; /* link parent to new node */
|
||||||
return(p);
|
return (p);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Walk the nodes of a tree */
|
/* Walk the nodes of a tree */
|
||||||
static void
|
static void trecurse(node *root, void (*action)(const void *, VISIT, int),
|
||||||
trecurse(node *root, void (*action)(const void *, VISIT, int), int level)
|
int level) {
|
||||||
{
|
|
||||||
if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
|
if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
|
||||||
(*action)(root, leaf, level);
|
(*action)(root, leaf, level);
|
||||||
else {
|
else {
|
||||||
|
|
@ -108,9 +101,7 @@ trecurse(node *root, void (*action)(const void *, VISIT, int), int level)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Walk the nodes of a tree */
|
/* Walk the nodes of a tree */
|
||||||
void
|
void twalk(const void *vroot, void (*action)(const void *, VISIT, int)) {
|
||||||
twalk(const void *vroot, void (*action)(const void *, VISIT, int))
|
|
||||||
{
|
|
||||||
node *root = (node *)vroot;
|
node *root = (node *)vroot;
|
||||||
|
|
||||||
if (root != (node *)0 && action != (void (*)(const void *, VISIT, int))0)
|
if (root != (node *)0 && action != (void (*)(const void *, VISIT, int))0)
|
||||||
|
|
@ -120,10 +111,8 @@ twalk(const void *vroot, void (*action)(const void *, VISIT, int))
|
||||||
/* $OpenBSD: tfind.c,v 1.6 2014/03/16 18:38:30 guenther Exp $ */
|
/* $OpenBSD: tfind.c,v 1.6 2014/03/16 18:38:30 guenther Exp $ */
|
||||||
|
|
||||||
/* find a node, or return 0 */
|
/* find a node, or return 0 */
|
||||||
void *
|
void *tfind(const void *vkey, void *const *vrootp,
|
||||||
tfind(const void *vkey, void * const *vrootp,
|
int (*compar)(const void *, const void *)) {
|
||||||
int (*compar)(const void *, const void *))
|
|
||||||
{
|
|
||||||
char *key = (char *)vkey;
|
char *key = (char *)vkey;
|
||||||
node **rootp = (node **)vrootp;
|
node **rootp = (node **)vrootp;
|
||||||
|
|
||||||
|
|
@ -133,8 +122,7 @@ tfind(const void *vkey, void * const *vrootp,
|
||||||
int r;
|
int r;
|
||||||
if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
|
if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
|
||||||
return (*rootp); /* key found */
|
return (*rootp); /* key found */
|
||||||
rootp = (r < 0) ?
|
rootp = (r < 0) ? &(*rootp)->left : /* T3: follow left branch */
|
||||||
&(*rootp)->left : /* T3: follow left branch */
|
|
||||||
&(*rootp)->right; /* T4: follow right branch */
|
&(*rootp)->right; /* T4: follow right branch */
|
||||||
}
|
}
|
||||||
return (node *)0;
|
return (node *)0;
|
||||||
|
|
|
||||||
|
|
@ -9,14 +9,14 @@ void *tsearch(const void *vkey, void **vrootp,
|
||||||
int (*compar)(const void *, const void *));
|
int (*compar)(const void *, const void *));
|
||||||
|
|
||||||
/* delete node with given key */
|
/* delete node with given key */
|
||||||
void * tdelete(const void *vkey, void **vrootp,
|
void *tdelete(const void *vkey, void **vrootp,
|
||||||
int (*compar)(const void *, const void *));
|
int (*compar)(const void *, const void *));
|
||||||
|
|
||||||
/* Walk the nodes of a tree */
|
/* Walk the nodes of a tree */
|
||||||
void twalk(const void *vroot, void (*action)(const void *, VISIT, int));
|
void twalk(const void *vroot, void (*action)(const void *, VISIT, int));
|
||||||
|
|
||||||
/* find a node, or return 0 */
|
/* find a node, or return 0 */
|
||||||
void *tfind(const void *vkey, void * const *vrootp,
|
void *tfind(const void *vkey, void *const *vrootp,
|
||||||
int (*compar)(const void *, const void *));
|
int (*compar)(const void *, const void *));
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue