blob: c58da5372bc663248cf121ebf6e81db5425de28f (
plain) (
tree)
|
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include "tree.h"
struct tnode *talloc(void);
struct tnode *addtree(struct tnode *p, char *w, int line)
{
int cond;
if (p == NULL) {
p = talloc();
p->word = strdup(w);
p->lc = 0;
p->lines[p->lc++] = line;
p->left = p->right = NULL;
} else if (!(cond = strcasecmp(w, p->word))) {
if (p->lc < MAXLINES)
p->lines[p->lc++] = line;
else
error(EXIT_FAILURE, ENOBUFS, "addtree");
} else if (cond < 0) {
p->left = addtree(p->left, w, line);
} else if (cond > 0) {
p->right = addtree(p->right, w, line);
}
return p;
}
void tprint(struct tnode *p)
{
int lc;
if (p != NULL) {
tprint(p->left);
printf("---> %s\n", p->word);
for (lc = 0; lc < p->lc; ++lc)
printf((lc == p->lc-1) ? "%d" : "%d, ", p->lines[lc]);
putchar('\n');
tprint(p->right);
}
}
void tfree(struct tnode *p)
{
if (p != NULL) {
tfree(p->left);
tfree(p->right);
free(p->word);
free(p);
}
}
struct tnode *talloc(void)
{
return (struct tnode *) malloc(sizeof(struct tnode));
}
|