BST Abuse!
(How to Get Your Output to Be Sorted by Frequency)
If you would like your output to be sorted by frequency for extra
credit on Project 2, follow these steps:
-
Write your BSTs as template classes so that the keys can be any class on
which order operators <, <=, ==, >=,
and > are defined, and the values can be any class at all.
-
Use your BSTs to store the word/frequencies as key/value pairs as you parse
through the input file.
-
Ok, now you want to resort the data by frequency. Create a class
called IntStringPair which holds a string and an integer and over
load the order operators so that they have the following semantics:
-
If A.integerPart < B.integerPart then A < B.
-
If A.integerPart == B.integerPart, then sort by the string part. (Note: This
takes care of words which may have the same frequency.)
-
Now instantiate a totally new BST.
-
Traverse the old BST, extracting key/value pairs and insert them as keys
of type IntStringPair into your new BST, you can leave the values
NULL in the new BST. (Note: This time we are using the BST as a
set of ordered items, instead of as a map.)
-
Traverse the new BST in order to get at the words sorted by frequency!