32S-Section-Solution

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

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

View Full Document Right Arrow Icon
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) {
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2011 for the course CS 106X taught by Professor Cain,g during the Fall '08 term at Stanford.

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 Right Arrow Icon
Ask a homework question - tutors are online