Computer Science II

advertisement
Computer Science II
Program #4
Due October 9, 2014
Write a C++ program to create a binary search tree. The program will read in data from an input file, and store
each word in the tree, as well as keep a count of how many times this word occurred in the input file. Thus, each
node in the tree will have the following data: a string (for the word) and an integer, Count. The output will be an
alphabetical list of the words that occurred in the input file, with a count of how many times they were used.
Notes:
1) A word is a grouping of alphabetic characters. Punctuation and spaces should be ignored/eliminated, and
cases should be ignored. The final tree should ignore capitalization as well.
2) The first time a node is created, the count should be initialized to 1. As words are read in, a search
should be undertaken to find that word. If it is found, increment the count. If it is not, call a function to
insert the node into the tree.
3) The data field of the root node should be initialized to the first word in the input file.
4) The input file will have the following quote (Arnold, David O., 1991. Computers and Society: Impact!
Mitchell McGraw - Hill, New York, p. 372):
Horse race politics.
The first is the rating game. We seem to have become number happy. In this computerized era, it
is all too easy to measure quantity and think we are measuring quality. We judge education by grade
point averages and movies by the number of asterisks (or stars or some other symbols) assigned to
them. The future of television shows, beauty contest entrants, novels, and even corporations rests
on the ratings. We have even been known to judge each other on a 10-point scale made famous in a
Bo Derek movie a few years back. During the 1988 presidential campaign, many observers felt that
both the media and the electorate were more concerned with ratings than with issues, with polls
than with policies. As in a horse race, the main question had become “who is winning?”
5) Record the inorder traversal of the tree using the recursive version of the traversal to the output file,
in the form “Word #” on a line. To save on paper, print 4 per row (to make this easier, I will allow a
single global variable to be used).
Sample output:
Word
#
Word
#
Word
#
Word
#
the
7
these
1
was
2
youth
1
6) Extra Credit possibility #1: Write the code to do an iterative preorder traversal of the tree.
7) Extra Credit Possibility #2: Write the code to do an iterative inorder traversal of the tree.
Download