Hi,
I'm trying to compare the processing times of a linked list versus a
binary tree. I've previously coded the linked list for this task.
I have begun coding the binary tree, but am stuck as I am new to
binary trees. I have coded the outline, but
the core parts of the program to make the binary tree work are
missing. Can anyone help (I've commented where I need help at and
what needs to be inserted. I know what needs to be put there, just
don't know the syntax or format)? The program is meant to take input
from a text file and provide word counts such as total words, most
frequent words, etc.
Note: I'm self-teaching myself C and none of my co-workers could
help me, except for the basic layout and I was hoping I could find
someone more knowledgable here. Once I finish this, I can have a
definitive example to teach myself with other than basic tutorials
that aren't helping me much.
The code is below (please do not create new variables if possible,
these are similar to my original linked list program and would help me
understand better).
Thank you for your help.
#include <stdio.h>
#include <malloc.h>
struct nodes { // specify the "shape" of a nodes structure ...
struct nodes *left; // the left and right branch pointers
struct nodes *right;
int count; // word count
char *word; // a pointer to the word
} *root; // declare the root pointer variable
struct nodes **search(struct nodes **, char *);
void statistics(struct nodes *);
int get_word(char *);
int total_of_nodes, total_words, high;
struct nodes *most_frequent;
int main(int argc, char *argv[]) {
struct nodes **tpp;
char word_buff[100]; // the reusable word buffer
int i;
while(get_word(word_buff)) {
tpp = search(&root, word_buff);
/****Code here to add any new nodes and count the repeats***/
}
statistics(root);
printf("total_of_nodes %d\n", total_of_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 = 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 a hit
struct nodes **search(struct nodes **tpp, char *w) {
/***** Need code here to search the binary tree***/
return(tpp);
}
void statistics(struct nodes *tp) {
/***Need recursive code that will get all of the statistics *****/
}
#include <ctype.h>
/* Leave this, as it's from a previous program that I used*/
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);
} |