diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2018-11-19 17:31:18 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2018-11-19 17:31:18 +1300 |
commit | da93a3c6ac37fed028cceb49fa11bc6233535c59 (patch) | |
tree | 201211cf4ac7d0781965fd7d2cad15502ae22e23 | |
parent | Copy the arg strings rather than sharing them (diff) | |
download | btree-da93a3c6ac37fed028cceb49fa11bc6233535c59.tar.gz btree-da93a3c6ac37fed028cceb49fa11bc6233535c59.zip |
Refactor/adjust a lot
-rw-r--r-- | Makefile | 13 | ||||
-rw-r--r-- | btree-int.c | 66 | ||||
-rw-r--r-- | btree-str.c | 76 |
3 files changed, 98 insertions, 57 deletions
@@ -1,9 +1,6 @@ +.POSIX: .PHONY: all clean - -CC = clang -CFLAGS = -std=c90 -Weverything - -all : btree-int btree-str - -clean : - rm -f btree-int btree-str +ALL = btree-int btree-str +all: $(ALL) +clean: + rm -f -- $(ALL) diff --git a/btree-int.c b/btree-int.c index 592c9c2..7394b92 100644 --- a/btree-int.c +++ b/btree-int.c @@ -1,29 +1,47 @@ #include <stdio.h> #include <stdlib.h> -typedef struct Node { +struct node { long v; - struct Node *l; - struct Node *r; -} Node; + struct node *l; + struct node *r; +}; -void an(Node **, Node *); -void pn(Node *); -void fn(Node *); +struct node *cn(long); +void an(struct node *, struct node *); +void pn(struct node *); +void fn(struct node *); -void an(Node **r, Node *n) { - if (!*r) { - *r = n; +struct node *cn(long v) { + struct node *n; + + n = malloc(sizeof *n); + if (!n) + return NULL; + n->v = v; + n->l = n->r = NULL; + return n; +} + +void an(struct node *r, struct node *n) { + if (!r || !n) return; + if (n->v > r->v) { + if (r->r) + an(r->r, n); + else + r->r = n; + } + else { + if (r->l) + an(r->l, n); + else + r->l = n; } - if (n->v > (*r)->v) - an(&((*r)->r), n); - else - an(&((*r)->l), n); return; } -void pn(Node *n) { +void pn(struct node *n) { if (!n) return; if (n->l) @@ -34,7 +52,7 @@ void pn(Node *n) { return; } -void fn(Node *n) { +void fn(struct node *n) { if (!n) return; if (n->l) @@ -46,13 +64,17 @@ void fn(Node *n) { } int main(int argc, char **argv) { - Node *r = NULL; + struct node *r, *n; - for (argv++, argc--; argc; argv++, argc--) { - Node *n = malloc(sizeof(Node)); - n->v = atoi(*argv); - n->l = n->r = NULL; - an(&r, n); + r = NULL; + for (argv++; *argv; argv++) { + n = cn(atoi(*argv)); + if (!n) + break; + if (r) + an(r, n); + else + r = n; } pn(r); diff --git a/btree-str.c b/btree-str.c index ff2338e..8ba42a5 100644 --- a/btree-str.c +++ b/btree-str.c @@ -2,29 +2,52 @@ #include <stdlib.h> #include <string.h> -typedef struct Node { +struct node { char *v; - struct Node *l; - struct Node *r; -} Node; + struct node *l; + struct node *r; +}; -void an(Node **, Node *); -void pn(Node *); -void fn(Node *); +struct node *cn(char *); +void an(struct node *, struct node *); +void pn(struct node *); +void fn(struct node *); -void an(Node **r, Node *n) { - if (!*r) { - *r = n; +struct node *cn(char *s) { + struct node *n; + char *d; + + n = malloc(sizeof *n); + if (!n) + return NULL; + d = malloc(strlen(s) + 1); + if (!d) + return NULL; + strcpy(d, s); + n->v = d; + n->l = n->r = NULL; + return n; +} + +void an(struct node *r, struct node *n) { + if (!r || !n) return; + if (strcmp(n->v, r->v) > 0) { + if (r->r) + an(r->r, n); + else + r->r = n; + } + else { + if (r->l) + an(r->l, n); + else + r->l = n; } - if (strcmp(n->v, (*r)->v) > 0) - an(&((*r)->r), n); - else - an(&((*r)->l), n); return; } -void pn(Node *n) { +void pn(struct node *n) { if (!n) return; if (n->l) @@ -35,7 +58,7 @@ void pn(Node *n) { return; } -void fn(Node *n) { +void fn(struct node *n) { if (!n) return; if (n->l) @@ -48,18 +71,17 @@ void fn(Node *n) { } int main(int argc, char **argv) { - Node *r = NULL; - - for (argv++, argc--; argc; argv++, argc--) { - unsigned long l = 0; - Node *n = malloc(sizeof(Node)); - - l = strlen(*argv) + 1; - n->v = malloc(l); - strncpy(n->v, *argv, l); + struct node *r, *n; - n->l = n->r = NULL; - an(&r, n); + r = NULL; + for (argv++; *argv; argv++) { + n = cn(*argv); + if (!n) + break; + if (r) + an(r, n); + else + r = n; } pn(r); |