|
| 1 | +// This file is part of SmallBASIC |
| 2 | +// |
| 3 | +// Tree search generalized from Knuth (6.2.2) Algorithm T just like |
| 4 | +// the AT&T man page says. |
| 5 | +// |
| 6 | +// The node_t structure is for internal use only, lint doesn't grok it. |
| 7 | +// |
| 8 | +// Written by reading the System V Interface Definition, not the code. |
| 9 | +// |
| 10 | +// Totally public domain. |
| 11 | +// |
| 12 | + |
| 13 | +#include <sys/cdefs.h> |
| 14 | +#include <assert.h> |
| 15 | +#include <stdlib.h> |
| 16 | + |
| 17 | +#include "search.h" |
| 18 | + |
| 19 | +typedef struct node { |
| 20 | + void *key; |
| 21 | + struct node *left, *right; |
| 22 | +} node_t; |
| 23 | + |
| 24 | +/* Walk the nodes of a tree */ |
| 25 | +tdestroy_recurse(node_t *root, tdestroy_cb freefct) { |
| 26 | + if (root->left != NULL) { |
| 27 | + tdestroy_recurse(root->left, freefct); |
| 28 | + } |
| 29 | + if (root->right != NULL) { |
| 30 | + tdestroy_recurse(root->right, freefct); |
| 31 | + } |
| 32 | + (*freefct) ((void *) root->key); |
| 33 | + free(root); |
| 34 | +} |
| 35 | + |
| 36 | +/* Walk the nodes of a tree */ |
| 37 | +void trecurse(const node_t *root, twalk_cb action, int level) { |
| 38 | + if (root->left == NULL && root->right == NULL) { |
| 39 | + action(root, leaf, level); |
| 40 | + } else { |
| 41 | + action(root, preorder, level); |
| 42 | + if (root->left != NULL) { |
| 43 | + trecurse(root->left, action, level + 1); |
| 44 | + } |
| 45 | + action(root, postorder, level); |
| 46 | + if (root->right != NULL) { |
| 47 | + trecurse(root->right, action, level + 1); |
| 48 | + } |
| 49 | + action(root, endorder, level); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/* find a node, or return 0 */ |
| 54 | +void *tfind(const void *vkey, void **vrootp, tcompare_cb compar) { |
| 55 | + node_t **rootp = (node_t **)vrootp; |
| 56 | + |
| 57 | + while (rootp != NULL && *rootp != NULL) { |
| 58 | + int r = (*compar)(vkey, (*rootp)->key); |
| 59 | + if (r == 0) { |
| 60 | + // found |
| 61 | + return *rootp; |
| 62 | + } |
| 63 | + rootp = (r < 0) ? &(*rootp)->left : &(*rootp)->right; |
| 64 | + } |
| 65 | + return NULL; |
| 66 | +} |
| 67 | + |
| 68 | +/* find or insert datum into search tree */ |
| 69 | +void *tsearch(const void *vkey, void **vrootp, tcompare_cb compar) { |
| 70 | + node_t **rootp = (node_t **) tfind(vkey, vrootp, compar); |
| 71 | + node_t *q; |
| 72 | + |
| 73 | + if (rootp != NULL) { |
| 74 | + q = *rootp; |
| 75 | + } else { |
| 76 | + // key not found. make new node and link to old |
| 77 | + q = malloc(sizeof(node_t)); |
| 78 | + if (q != 0) { |
| 79 | + *rootp = q; |
| 80 | + q->key = (void *)vkey; |
| 81 | + q->left = q->right = NULL; |
| 82 | + } |
| 83 | + } |
| 84 | + return q; |
| 85 | +} |
| 86 | + |
| 87 | +void tdestroy(void *vroot, tdestroy_cb freefct) { |
| 88 | + node_t *root = (node_t *)vroot; |
| 89 | + if (root != NULL) { |
| 90 | + tdestroy_recurse(root, freefct); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/* Walk the nodes of a tree */ |
| 95 | +void twalk(const void *vroot, twalk_cb action) { |
| 96 | + if (vroot != NULL && action != NULL) { |
| 97 | + trecurse(vroot, action, 0); |
| 98 | + } |
| 99 | +} |
0 commit comments