format tsearch

This commit is contained in:
Emile Clark-Boman 2025-09-27 18:39:44 +10:00
parent 81e34dc630
commit 58be2d1ee8
2 changed files with 92 additions and 104 deletions

View file

@ -11,8 +11,8 @@
* Totally public domain.
*/
#include <stdlib.h>
#include "tsearch.h"
#include <stdlib.h>
typedef struct node_t {
char *key;
@ -20,10 +20,8 @@ typedef struct node_t {
} node;
/* find or insert datum into search tree */
void *
tsearch(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *))
{
void *tsearch(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *)) {
node *q;
char *key = (char *)vkey;
node **rootp = (node **)vrootp;
@ -35,8 +33,7 @@ tsearch(const void *vkey, void **vrootp,
if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
return ((void *)*rootp); /* we found it! */
rootp = (r < 0) ?
&(*rootp)->left : /* T3: follow left branch */
rootp = (r < 0) ? &(*rootp)->left : /* T3: follow left branch */
&(*rootp)->right; /* T4: follow right branch */
}
q = malloc(sizeof(node)); /* T5: key not found */
@ -49,10 +46,8 @@ tsearch(const void *vkey, void **vrootp,
}
/* delete node with given key */
void *
tdelete(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *))
{
void *tdelete(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *)) {
node **rootp = (node **)vrootp;
char *key = (char *)vkey;
node *p = (node *)1;
@ -64,8 +59,7 @@ tdelete(const void *vkey, void **vrootp,
return ((struct node_t *)0);
while ((cmp = (*compar)(key, (*rootp)->key)) != 0) {
p = *rootp;
rootp = (cmp < 0) ?
&(*rootp)->left : /* follow left branch */
rootp = (cmp < 0) ? &(*rootp)->left : /* follow left branch */
&(*rootp)->right; /* follow right branch */
if (*rootp == (struct node_t *)0)
return ((void *)0); /* key not found */
@ -85,15 +79,14 @@ tdelete(const void *vkey, void **vrootp,
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 */
return(p);
return (p);
}
/* Walk the nodes of a tree */
static void
trecurse(node *root, void (*action)(const void *, VISIT, int), int level)
{
static void trecurse(node *root, void (*action)(const void *, VISIT, int),
int level) {
if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
(*action)(root, leaf, level);
else {
@ -108,9 +101,7 @@ trecurse(node *root, void (*action)(const void *, VISIT, int), int level)
}
/* 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)) {
node *root = (node *)vroot;
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 $ */
/* find a node, or return 0 */
void *
tfind(const void *vkey, void * const *vrootp,
int (*compar)(const void *, const void *))
{
void *tfind(const void *vkey, void *const *vrootp,
int (*compar)(const void *, const void *)) {
char *key = (char *)vkey;
node **rootp = (node **)vrootp;
@ -133,8 +122,7 @@ tfind(const void *vkey, void * const *vrootp,
int r;
if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
return (*rootp); /* key found */
rootp = (r < 0) ?
&(*rootp)->left : /* T3: follow left branch */
rootp = (r < 0) ? &(*rootp)->left : /* T3: follow left branch */
&(*rootp)->right; /* T4: follow right branch */
}
return (node *)0;

View file

@ -9,14 +9,14 @@ void *tsearch(const void *vkey, void **vrootp,
int (*compar)(const void *, const void *));
/* 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 *));
/* Walk the nodes of a tree */
void twalk(const void *vroot, void (*action)(const void *, VISIT, int));
/* 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 *));
#else