140 likes | 236 Views
15-211 Fundamental Data Structures and Algorithms (Spring ’05). Recitation Notes: Tries Slides prepared by Uri Dekel, udekel@cs Based on recitation notes by Will Haines. Radix Sort. A bucket-based sorting algorithm Input: elements with digits in longest key Algorithm:
E N D
15-211 Fundamental Data Structures and Algorithms(Spring ’05) Recitation Notes: Tries Slides prepared by Uri Dekel, udekel@cs Based on recitation notes by Will Haines
Radix Sort • A bucket-based sorting algorithm • Input: elements with digits in longest key • Algorithm: • From least-significant digit to most significant • Place into bins by current digit while preserving original order (“stable sort”) • Append bins into a sequence • Sort time complexity: 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Radix sort example • Input: {115, 364,112, 003, 087, 006, 091, 911} • Phase 1: {091, 911, 112, 003, 364, 115, 006, 087} • Phase 2: {003, 006, 911, 112, 115, 364, 087, 091} • Phase 3: {003, 006, 087, 091, 112, 115, 364, 911} 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Tries • Based on the same principle as radix sort • Create an -level tree • Support search in 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Multiway Tries with values at predefined depth • Every internal node has |A| pointers • |A| is the size of the “alphabet” • |A|=10 for decimal digits 0..9 • |A|=26 for the English alphabet • The -th level nodes point to leafs that contain actual value of key • Limitations: • All keys must have exactly digits • Search time is • One key cannot be a prefix of another key • Significant time/space tradeoff • Lots of wasted space but the tree is very shallow 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Multiway Tries with values at unknown depth • Allows searching for -digit key in • Even if longest key is digits • Add one “terminal” signal cell to each node • Indicates that the key entered so far is a member of the collection • Allows us to go on searching for longer key 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Digital Trie example • Input sequence: {0, 2, 10, 011, 01, 20, 21} • We use three digit buckets and one for signal 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Digital Trie example • Search for “2”: Hit! 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Digital Trie example • Search for “01”: Hit! 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Digital Trie example • Search for “011”: Hit! 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Digital Trie example • Search for “1”: Miss (within trie) 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Digital Trie example • Search for “22”: Miss (runs off of tree) 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Hashmap Tries • What if we don’t know the digits of our alphabet? • e.g., in HW2 we will have individual words as “digits” of the search tree • We will associate them with final words to complete phrases • We use a hashmap at each node • Amortized constant lookup at each node • Amortized constant insert at each node 15-211 Recitation notes on tries, Uri Dekel and Will Haines
Hashmap Trie Example • Store frequency of all 3-word sequence • Input sentence: “in at the ox at the in the in at ox at ox in the at the in at in ox the ox at the ox” 15-211 Recitation notes on tries, Uri Dekel and Will Haines