static struct tnode *treeadd(struct tnode *p, char *w, char *uw, int line) { int cond; if (p == NULL) { // Create new tnode p = talloc(); p->word = mystrdup(w); p->words = lwalloc(); p->words->word = mystrdup(uw); p->words->next = NULL; p->wordcount = 1; p->lines = lalloc(); p->lines->linenum = line; p->lines->next = NULL; p->right = NULL; p->left = NULL; } else if ((cond = strncmp(w, p->word, MAXWORDSIZE)) == 0) { // Update existing tnode addword(p, uw); addline(p, line); } else if (cond < 0) // Traverse left-hand side of tree p->left = treeadd(p->left, w, uw, line); else // Traverse right-hand side of tree p->right = treeadd(p->right, w, uw, line); return p; }
NODE *treeadd(NODE *n, const char *k, int vlen, int offt) { int cmp; if (n == NULL) { init(n, NODE); if (n == NULL) fprintf(stderr, "ERR:failed to init node\n"); init(n->idx, IDX); if (n->idx == NULL) fprintf(stderr, "ERR:failed to init node\n"); n->idx->k = strdup(k); if (n->idx->k == NULL) fprintf(stderr, "ERR:failed to copy key\n"); n->idx->vlen = vlen; n->idx->offt = offt; n->ln = n->rn = NULL; } else if ((cmp = strcmp(k, n->idx->k)) > 0) n->rn = treeadd(n->rn, k, vlen, offt); else if (cmp < 0) n->ln = treeadd(n->ln, k, vlen, offt); else fprintf(stderr, "WARN: DUPLICATE key: %s\n", k); return n; }
void htput(char *k, int vlen, int offt) { unsigned int h = hash(k); HT[h].n = treeadd(HT[h].n, k, vlen, offt); ++HT[h].len; CC[cc_len].key = strdup(k); ++cc_len; }
void treeadd(struct node **pp, int val) { /* create new node if *p is null */ if (*pp == (struct node *)0) { *pp = (struct node *)malloc(sizeof(struct node)); (*pp)->value = val; } else if ((*pp)->value > val) { treeadd(&(*pp)->left, val); } else if ((*pp)->value < val) { treeadd(&(*pp)->right, val); } else /* (*pp)->value == val */ { /* do nothing */ } }
int main(int argc, char **argv) { struct node *rootp; int i; int array[15] = {50, 12, 18, 70, 41, 19, 91, 1, 7, 6, 81, 65, 55, 20, 0}; for (i = 0; i < 15; i++) { treeadd(&rootp, array[i]); } treewalk(rootp); printf("\n"); treefree(rootp); return 0; }
int main(void) { struct tnode *root = NULL; char word[MAXWORDSIZE] = ""; char unstemmed[MAXWORDSIZE] = ""; int line = 1; while (getword(word, MAXWORDSIZE) != EOF) if (isalpha(word[0])) { lowerstr(word); if (isnotstopword(word)) { strncpy(unstemmed, word, MAXWORDSIZE); squeezechar(word, '\''); word[stem(word, 0, strlen(word) - 1) + 1] = '\0'; root = treeadd(root, word, unstemmed, line); } } else if (word[0] == '\n') line++; treeprint(root); return 0; }