F
Foodbank
Hi all,
I'm trying to do a binary search and collect some stats from a text
file in order to compare the processing times of this program (binary
searching) versus an old program using linked lists. I'm totally new
to binary searches by the way. Can anyone help me with the commented
sections below? Much of the code such as functions and printfs has
already been completed. Any help is greatly appreciated.
Thanks,
James
I'm trying to do a binary search and collect some stats from a text
file in order to compare the processing times of this program (binary
searching) versus an old program using linked lists. I'm totally new
to binary searches by the way. Can anyone help me with the commented
sections below? Much of the code such as functions and printfs has
already been completed. Any help is greatly appreciated.
Thanks,
James
Code:
#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 as before
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 *****/
}
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 a
hit
struct tnode **tree_search(struct tnode **tpp, char *w) {
/***** CODE TO DO THE BINARY SRCH *****/
return(tpp);
}
// gather stats to check tree correctness
void tree_stats(struct tnode *tp) {
/***** RECURSIVE CODE TO COLLECT ALL OF THE STATISTICS *****/
}
#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);
}