30-Generalization-Of-Trees

30-Generalization-Of-Trees - CS106X Autumn 2010 Handout 30...

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

View Full Document Right Arrow Icon
CS106X Handout 30 Autumn 2010 November 8 th , 2010 Generalization Of Trees Handout written by Jerry. I don’t want to leave you with the impression that all trees need to be binary, much less binary search. You’ve already seen the PQueue modeled as an array-backed binary tree, although binary search had nothing to do with the way elements were stored. I want you to see another example that generalizes trees in a novel way to implement the Lexicon class you’ve come to adore. You’re all reading Chapter 13 on trees, but this particular example isn’t in there, so you’ll want to give this handout a good read. It’s also another example that’s complicated enough that it requires some advanced pointer work, so it’s all good fun for memory management enthusiasts as well. Implementing the Lexicon Trees can be used as the underlying implementation of a Lexicon data type—one which stores a large collection of words and provide very efficient enter and lookup times. The resulting structure, first developed by Edward Fredkin in 1960, is called a trie —over time, the pronunciation of this word has evolved to the point that it is now pronounced try , even though the name comes from the extraction of the central letters of retrieval . The trie-based implementation of the Lexicon makes it possible to determine whether a word is in the dictionary or not much more quickly than you can using a hash table. At one level, a trie is simply a tree in which each node branches in as many as 26 different directions, one for each letter of the alphabet. When using a trie to represent a lexicon, the words of the lexicon are stored implicitly in the structure of the tree and are represented as a chain of links moving downward from the root. The root of the trie corresponds to the empty string, and each successive level of the trie corresponds to the prefix of the entire word list formed by adding one more letter to the string represented by its parent. For example, the A link descending from the root leads to the subtrie containing all of the words beginning with A , the B link from that node leads to the subtrie containing all of the words beginning with AB , etc. Each node stores a Boolean flag that is true whenever the substring that ends at that particular point is a legitimate word. If we pretend that the English alphabet only has 7 letters (
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.

Page1 / 6

30-Generalization-Of-Trees - CS106X Autumn 2010 Handout 30...

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