The word frequency counter, as previously implemented, takes several minutes to count the frequency of words in the author’s manuscript for this textbook on a Raspberry Pi. Most of the time
is spent building the list of words and re-sorting the list in order of word frequency. Most of the
time for both of these operations is spent in searching for the word in the list before incrementing its count or inserting it in the list. There are more efficient ways to build ordered lists of data.
Since the code is well modularized using an ADT, the internal mechanism of the list can be
modified without affecting the main program. A major improvement can be made by changing
the data structure from a linked list to a binary tree. Fig. 6.1 shows an example binary tree
storing word frequency counts. The time required to insert into a linked list is O(N), but the
time required to insert into a binary tree is O(log2 N)。 To give some perspective, the author’s
manuscript for this textbook contains about 125,000 words. Since log2(125,000) 《 17, we
would expect the linked list implementation to require about 125,000
17 ≈ 7353 times as long as a
binary tree implementation to process the author’s manuscript for this textbook. In reality,
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !