Makeup Assignment B:  File I/O and Binary Search Trees

Due: Last day of class

100 points, +60 possible extra credit.  Extra credit points may be applied to partially make up for a second assignment.

 

Write a program to count the number of times each word appears in the file 'story.txt' ---

 

Once upon a time there were three little pigs who lived all alone in the deep forest where they amused themselves by building little houses  One day a wolf came to the forest and blew down their little houses and ate the little pigs

 

Your program should write the results to a file with the following format:

 

A 2

ALL 1

ALONE 1

AND 2

AMUSED 1

ATE 1

BUILDING 1

BLEW 1

BY 1

CAME 1

DAY 1

DEEP 1

DOWN 1

FOREST 2

HOUSES 2

IN 1

LITTLE 4

LIVED 1

          ---  Etc…

 

You should use a Binary Tree to keep track of the words. Each node should have a 'word' field and a 'count' field.  A word is only entered into the tree once.  If it appears again, its count is simply incremented. 

 

Extra Credit 1:(20 points) Implement 2 additional functions to your binary search tree: 

            deleteMin(Node h):  Delete the alphabetically smallest node in the tree rooted at node h.  For the above example, the node with word field "A" would be deleted. 

            deleteMax(Node h).  Delete the alphabetically largest node in the tree rooted at node h.

 

Extra Credit 2:(20 points)  Implement

            deleteWord(string x):  Delete the node from the BST whose word field is x.

To do this, you may want to make use of deleteMin() and deleteMax() in some clever way.

 

Extra Credit 3:(20 points)  How does your tree perform on a file of dictionary words?  (answer:  very slowly).  Why?  Fix this by designing an improved tree that performs fast regardless of the input.