1. 建立Trie树,记录每颗树的出现次数,O(n*le); le:平均查找长度
2. 维护一个10的小顶堆,O(n*lg10);
3. 总复杂度: O(n*le) + O(n*lg10);
增加一个问题,假如要找到并列排名前十的单词呢?
cheng@debian:~$ gcc -Wall -g -o show_top_ten show_top_ten.c
cheng@debian:~$ ./show_top_ten word.txt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_NUM 256
struct trie_node {
struct trie_node *node[WORD_NUM];
int value;
int count;
};
struct heap_node {
char *word;
int count;
};
void adjust_heap(struct heap_node *h, int len, int pos);
void create_heap(struct heap_node *h, int len);
void heap_sort(struct heap_node *h, int len);
int heap_insert_node(struct heap_node *h, int len, struct heap_node n);
void heap_insert_word(struct heap_node *h, int len, char *word