我正在尝试计算文本中每个单词的出现次数。 所以我将所有单词和计数存储在二叉树中:
typedef struct Node{
char* word;
int count;
struct Node *left;
struct Node *right;
struct Node *parent;
} Node;
现在我需要按计数对树进行排序。 我不能只做 while 循环并对其进行排序,所以我想知道我可以用哪种方式来做到这一点?
这是我现在拥有的示例:
The - 3
/ \
Project - 1 of - 3
/ \ / \
.... .... .... ....
我需要打印文本中的前 N 个单词。
最佳答案
遍历树并将单词及其计数提取到以下数组中:
struct WordAndCount {
char * word;
int count;
};
然后使用qsort
对数组进行排序。您将需要一个比较 WordAndCount.count 的自定义比较函数;
关于c - 如何通过其他参数对二叉树进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49801489/