: Hi again,
:
: Yeah, the getword is from the book.
:
: Anyways, I've made a lot of progress, but now I'm getting a seg. fault. I have no idea why. I've commented where it's happening at the location that says:
:
: //*** SEG FAULT HAPPENING BELOW WHEN CMP IS <, >, OR == 0 ********
:
: As a refresher, this is the binary tree that counts words from a text file on the command line.
:
: Thank you in advance,
: Jim
:
:
:
: #include <stdio.h>
: #include <malloc.h>
: struct tnode { // specify the "shape" of a tnode structure
: struct tnode *left; // the left and right branch pointers
: struct tnode *right;
: int count; // the word count
: char *word; // a pointer to the word
: } *root; // declare the root pointer variable
:
: struct tnode **tree_search(struct tnode **, char *);
: void tree_stats(struct tnode *);
: int get_word(char *);
:
: int total_nodes, total_words, high;
: struct tnode *most_frequent;
:
: int main(int argc, char *argv[]) {
: struct tnode **tpp;
: char word_buff[100]; // the reusable word buffer
: int i;
: while(get_word(word_buff)) {
: tpp = tree_search(&root, word_buff);
: /****CODE TO ADD NEW NODES AND COUNT REPEATS *****/
:
: if(*tpp!=NULL){
: (*tpp)->count++;
: }
: else{
: (*tpp)=malloc(sizeof(struct tnode));
: (*tpp)->left=NULL;
: (*tpp)->right=NULL;
: (*tpp)->count=1;
: (*tpp)->word=strdup(word_buff);
: }
:
: }
: tree_stats(root);
: printf("total_nodes %d\n", total_nodes);
: printf("total_words %d\n", total_words);
: if(most_frequent)
: printf("most frequent word <%s> count is %d\n",
: most_frequent->word, most_frequent->count);
: for(i = 1; i < argc; i++) {
: tpp = tree_search(&root, argv[i]);
: if((*tpp) == NULL)
: printf("%s is NOT in the tree\n", argv[i]);
: else
: printf("<%s> count is %d\n", argv[i], (*tpp)-
: >count);
: }
: return(0);
: }
:
: // binary tree search returning a pointer to the pointer leading to hit
: struct tnode **tree_search(struct tnode **tpp, char *w) {
: /***** CODE TO DO THE BINARY SRCH *****/
: int cmp;
:
: while(*tpp){
: cmp=strcmp(w,(*tpp)->word);
: //*****SEG FAULT HAPPENING BELOW WHEN CMP IS <, >, OR == 0 ********
: if (cmp>0) {
: tpp=(*tpp)->right;
: }
: else if (cmp<0){
: tpp=(*tpp)->left;
: }
: else if (cmp==0){
: break;
: }
: }
:
:
: return(tpp);
: }
:
: // gather stats to check tree correctness
: void tree_stats(struct tnode *tp) {
: /***** RECURSIVE CODE TO COLLECT ALL OF THE STATISTICS *****/
: if(tp=NULL)
: return;
: total_words+=tp->count;
: total_nodes++;
: if(tp->count > high){
: high=tp->count;
: most_frequent=tp;
: }
: }
:
: #include <ctype.h>
: /* Leave this routine EXACTLY as it stands */
: int get_word(char *s) {
: int c;
: do {
: c = getchar();
: if(c == EOF)
: return(0);
: } while(!isalpha(c) && !isdigit(c));
: do {
: if(isupper(c))
: c = tolower(c);
: *s++ = c;
: c = getchar();
: } while(isalpha(c) || isdigit(c));
: *s = 0;
: return(1);
: }
:
:
I do not have time to read your whole code; however I noticed a mistake here:
: // binary tree search returning a pointer to the pointer leading to hit
: struct tnode **tree_search(struct tnode **tpp, char *w) {
: /***** CODE TO DO THE BINARY SRCH *****/
: int cmp;
:
: while(*tpp){
: cmp=strcmp(w,(*tpp)->word);
: //*****SEG FAULT HAPPENING BELOW WHEN CMP IS <, >, OR == 0 ********
: if (cmp>0) {
: tpp=(*tpp)->right;
tpp is of type struct tnode ** but you're assigning a
struct tnode * value to it
: }
: else if (cmp<0){
: tpp=(*tpp)->left;
same thing
: }
: else if (cmp==0){
: break;
: }
: }
:
:
: return(tpp);
: }
Take care, Steph.