Exercise 1 Warmup: Completing Frequency.cc
We’ll use the Frequency ADT to store a character, e.g., ‘a’, and a count of how many times it appears in some text, e.g., 7. The frequency record will be stored in the Huffman trees.
The Frequency interface is complete, and defined in Frequency.h. It’s basically a storage unit for characters and integers. The implementation is almost complete, but the constructor, createFrequency() is incomplete. Here’s what is in the Frequency.cc file:
1 | // CONSTRUCTOR |
It currently does nothing. Complete this operation, by allocating a record, and storing the appropriate data.
Exercise Summary
- Complete createFrequency() as indicated.
- Build the testADT app, and run it.
- If your code does the right things, the first 4 tests should all report “Passed.”
What to hand in:
The file Frequency.cc containing your code for createFrequency() described above.
Grading
5 marks: createFrequency() builds a Frequency record and correctly initializes it with the given data.
Exercise 2 Warmup: Completing TreeNode.cc
We’ll use the TreeNode ADT to help us build Huffman trees. In our application, a TreeNode will store a reference to a Frequency record; see TreeNode.h and TreeElement.h. Most of the TreeNode implementation is complete. There are two operations that are currently incomplete: the constructor, createTreeNode(), and the operation height().
The constructor currently looks like this:
1 | // CONSTRUCTOR |
Complete it so that it allocates a correctly initialize TreeNode record.
The height operation currently looks like this:
1 | // height(n) |
Complete this function so that it returns the height of a tree whose root is given as the parameter n.
Hints:
- This function must be recursive.
- The height of a NULL tree is 0.
- There is a function called max(a,b) you can use. It returns the larger of a or b. This function is defined in a C++ library called “algorithm” which we’ve already #included for you.
Exercise Summary
- Complete createTreeNode() as indicated.
- Complete height() as indicated.
- Build the testADT app, and run it.
If your code does the right things, tests 5-16 should all report “Passed.”
What to hand in:
The file TreeNode.cc containing your code for the two operations described above.