I'm doing K&R exercise 6-4, which is:
6-4. Write a program that prints the distinct words in its input sorted into decreasing order of frequency of occurrence. Precede each word by its count.
What I decided to do is create a struct called dstncFreqNode6_4:
struct dstncFreqNode6_4
{
char *word;
int count;
struct dstncFreqNode6_4 *left;
struct dstncFreqNode6_4 *right;
};
I then parsed the input for words, and for each distinct word created one "dstncFreqNode6_4" node and two pointers to it: one to insert in to a BST (to add new words/update the counts of already encountered words), and one to insert in to a global array of "dsntFredNode6_4" pointers.
This was done so that I could update the counts of words (nodes) by traversing the BST (which contains pointers to all the words encountered so far). After the entire input has been parsed, the array of pointers would be sorted by the members' "count" variables, and then printed. Since the
The code the adding of new words/updating the counts is here:(I don't think anything is wrong with it,since the BST and array appear to be populated correctly, so you may ignore this):
//"wordCounts" is originally a global dstncFreqNode6_4** declared outside the function, numWords is the current length of the array
struct dstncFreqNode6_4 *addFreqNode6_4(struct dstncFreqNode6_4 *root, char *word)
{
if(root == NULL)
{
root = (struct dstncFreqNode6_4 *) malloc(sizeof(struct dstncFreqNode6_4));
root -> word = word;
root -> count = 1;
root -> left = NULL;
root -> right = NULL;
struct dstncFreqNode6_4 **p;
wordCounts = (struct dstncFreqNode6_4 **) realloc(wordCounts, sizeof(struct dstncFreqNode6_4*) * (numWords +1));
p = wordCounts + numWords++;
(*p) = root;
}
else if(strcmp(word, root -> word) < 0)
root -> left = addFreqNode6_4(root -> left, word);
else if(strcmp(word, root -> word) > 0)
root -> right = addFreqNode6_4(root -> right, word);
else
root -> count++;
return root;
}
So I've got everything working right except the sorting; it simply won't sort the array. That is to say... the order of the elements remains unchanged EDIT: I get a segmentation error. EDIT #2: No segmentation error now; the original problem still exists.
I'm using stlib.h's qsort method; the compare function i'm using is:
int qcmp6_4(const void *a, const void *b)
{
return (*(struct dstncFreqNode6_4 **)a) -> count - (*(struct dstncFreqNode6_4 **)b) -> count;
}
I can't seem to figure out why it won't sort correctly. I actually implemented my own quicksort algorithm and am getting the same results. I really don't know at this point.
It would be nice to get some fresh, and expert eyes to point me in the right direction. Thanks.
EDIT
Sorry, here's the call to qsort:
qsort(wordCounts, numWords, sizeof(struct dstncFreqNode6_4 *), qcmp6_4);
EDIT #2:
Following the suggestion, I've made "wordCounts" an array of pointers to nodes (all the code in this post has been updated to reflect this). So in essence, the BST and the array contain identical information (in fact the array pointers are initialized to the corresponding pointer in the BST), but their uses are different. The BST is used to add new words/update the counts of already encountered ones, and the array is sorted at the end (by the each of the words' counts) and printed. However, i'm running in to the same problem I originally ran in to: the order of the array remains the same after the call to qsort.