资 源 简 介
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,