32S-Section-Solution

# 32S-Section-Solution - CS106X Autumn 2010 Handout 32S...

This preview shows pages 1–2. Sign up to view the full content.

CS106X Handout 32S Autumn 2010 November 17 th – - 19 th, 2010 Section Solution Solution 1: Dictionaries and Ternary Search Trees a.) void Dictionary::add(string word, string definition) { add(root, word, definition); } void Dictionary::add(node *& curr, string suffix, string definition) { // if we’re here, we’re here for a reason and need to make sure // that curr actually points to something if it doesn’t already if (curr == NULL) { curr = createNode(suffix[0]); // pretend it was here all along } // if the leading letter of the suffix doesn’t match the one held // by the node, then we need to branch left or right without // consuming any characters if (suffix[0] != curr->letter) { node *& pointer = (suffix[0] < curr->letter) ? curr->less : curr->greater; add(pointer, suffix, definition); return; } // if we got this far, then the leading character of the suffix // matches the one within the node. If the suffix happens to be // of length 1, then this is the node that gets the definition. if (suffix.size() == 1) {

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 2

32S-Section-Solution - CS106X Autumn 2010 Handout 32S...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online