Happy last week of section! This final section serves as a final exam review session. We hope today’s section helps you feel a little more prepared for the final :) You’ve got this! No starter code today. We’re suggesting one problem that will help review many important 106B concepts that may show up on the final: class implementation, tree traversal, dynamic memory and pointers, recursive problem-solving, to name a few. We are leaving the rest of the time to you and your SL to decide what would be the most helpful final review for you. Thanks for a great quarter!
1. Implementing Lexicon
On your second midterm you implemented a pretty straight forward implementation of the Lexicon ADT, using two Sets — of prefixes and strings — since the Lexicon is very similar in function to a Set. But a Lexicon isn’t just a Set, and, in reality, our Stanford libraries’ implementation actually uses a different data structure behind the scenes optimized for storing type string. That optimization is to use a special tree data structure where each node can have up to 26 children (!), one for each letter a-z (actual Lexicon can handle uppercase and other characters, but we will just do strings of a-z). Below is the lexicon.h file showing the planned interface (you will not need to implement functions marked n/a on the exam, but imagine someone else is writing them).
class Lexicon {
public:
Lexicon();
~Lexicon();
bool contains(string word); // n/a
void add(string word);
int size(); // n/a
private:
struct Node {
bool isWordEnd;
Map<char, Node*> children;
};
void destructorHelper(Node* cur);
Node* _root; // root of the tree
int _size; // number of words stored
};
The tree works as follows:
- Each word is represented as a tree Node where the isWordEnd field is true, and the letters of the word are the sequence of child pointers on the path from _root to that node.
- Since each node can have between 0 and 26 children, our Node struct avoids needing a separate field for each child pointer by using a Map where key:value pairs map characters to child Node pointers.
- We only store keys in a children Map for children that actually exist. So the size of the children Map for the root node in the diagram above is 3 (the keys are ‘a’, ‘c’, and ’d’).
- Examples: We can see “cat” is stored in this tree by this process: (1) start at the _root, and look up children[‘c’] to find a pointer to the ‘c’ child and follow it, (2) go to that node’s ‘a’ child, and (3) go to that node’s ’t’ child, and see that its isWordEnd field is true. We can see that “ca” is not a stored word, because the node we reach by following the path _root to ‘c’ to ‘a’ has isWordEnd set to false. (See two grey arrows indicating these observations.) This tree also stores both “do” and “dog”.
Before you start, answer the following questions to test your understanding of the data structure:
- What should _size be for the tree shown above?
- What is the shortest word stored in the tree above?
Here are some tips to help you think about your class design as you move through implementing the methods!
- We begin with the constructor. Your tree should start by allocating one new Node to be the root. Your tree doesn’t store any words yet, so you shouldn’t add any keys to the root’s children Map yet, and you should set the root’s isWordEnd and the tree’s _size accordingly for no words.
- Now we will add words to our tree. Assume the word is a string of a-z. This will initially follow a similar process to the cat example above. Start at _root, and follow the sequence of children dictated by each letter of the word. If, at some point along the path, you find that children does not contain the key that you need (the next letter of the word), so there is no next child pointer to follow, then you will need to allocate new Node(s) as needed to complete the path to the word. When you arrive at the node corresponding to the completed word, if its isWordEnd is false then set it to true and update _size (if its isWordEnd is already true, we return without changing anything).
- Finally, we implement our destructor. It should ensure that there are no memory leaks by deleting all dynamically allocated memory. Your solution should take a recursive tree traversal approach by making a recursive helper function, as shown.
The answers to the intro questions are 8 and “a,” respectively.
Lexicon::Lexicon() {
_size = 0;
_root = new Node;
_root->isWordEnd = false;
}
void destructorHelper::helper(Node* cur) {
// recursive calls for each child
for (char c : cur->children) {
destructorHelper(cur->children[c]);
}
// post-order so we don't touch deleted memory
delete cur;
}
Lexicon::~Lexicon() {
destructorHelper(_root);
}
void Lexicon::add(string word) {
Node* cur = _root;
// iterate over characters to follow path
for (char c : word) {
// add new node on path if needed
if (!cur->children.containsKey(c)) {
cur->children[c] = new Node;
cur->children[c]->isWordEnd = false;
}
cur = cur->children[c];
}
// only update if word is not already stored
if (!cur->_isWordEnd) {
cur->_isWordEnd = true;
_size++;
}
}
Compare this implementation to the Set-based one you coded up on Midterm 2. How do their runtimes differ? What do they depend on? Which do you think would be faster at scale? We discuss “space complexity” — that is, how much memory a program uses as the size of its input increases — much less than “time complexity” in 106B, but consider which of these implementations would yield a more memory-intensive Lexicon.
2. Students’ Choice!
With whatever time remains, please feel free to choose another practice problem (from section, lecture, IGs, anything!) on a subject you’d like to review. This bank of 106B problems might be a helpful place to look.
3. Say Goodbye!
Thanks for a great quarter! We hope you all learned a lot and had fun while doing so. You’ve come so far since the start of the course! This is the last time you will see your SL, so make sure you say goodbye :’(