module08

# module08 - Module 8 Tries and String Matching CS 240 Data...

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

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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.

Unformatted text preview: Module 8: Tries and String Matching CS 240 - Data Structures and Data Management Arne Storjohann Based on lecture notes by R. Dorrigiv and D. Roche School of Computer Science, University of Waterloo Fall 2010 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 1 / 35 Tries Trie (Radix Tree) : A dictionary for binary strings I Comes from retrieval, but pronounced “try” I A binary tree based on bitwise comparisons I Similar to radix sort : use individual bits, not the whole key Structure of trie: I Items (keys) are stored only in the leaf nodes I A left child corresponds to a 0 bit I A right child corresponds to a 1 bit Keys can have different number of bits prefix-free : no key is a prefix of another key A prefix of a string S [0 .. n- 1]: a substring S [0 .. i ] of S for some 0 ≤ i ≤ n- 1 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 2 / 35 Tries Example: A trie for S = { 00 , 110 , 111 , 01010 , 01011 } 1 01010 01011 00 110 111 1 1 1 1 1 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 3 / 35 Tries: Search Search : start from the root, follow the relevant path using bitwise comparisons Example: Search(01010) 1 01010 01011 00 110 111 1 1 1 1 1 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 4 / 35 Tries: Search Search : start from the root, follow the relevant path using bitwise comparisons Example: Search(01010) successful 1 01010 01011 00 110 111 1 1 1 1 1 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 4 / 35 Tries: Search Search : start from the root, follow the relevant path using bitwise comparisons Example: Search(0100) 1 01010 01011 00 110 111 1 1 1 1 1 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 4 / 35 Tries: Search Search : start from the root, follow the relevant path using bitwise comparisons Example: Search(0100) unsuccessful 1 01010 01011 00 110 111 1 1 1 1 1 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 4 / 35 Tries: Insert Insert( x ) I First search for x I If we finish at a leaf with key x , then x is already in trie: do nothing I If we finish at a leaf with a key y 6 = x , then y is a prefix of x : not possible because our keys are prefix-free I If we finish at an internal node and there are no extra bits : not possible because our keys are prefix-free I If we finish at an internal node and there are extra bits: expand trie by adding necessary nodes that correspond to extra bits Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 5 / 35 Tries: Insert Insert( x ) Example: Insert(01000) 1 01010 01011 00 110 111 1 1 1 1 1 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 5 / 35 Tries: Insert Insert( x ) Search(01000) unsuccessful Extra bits: 00 1 01010 01011 00 110 111 1 1 1 1 1 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 5 / 35 Tries: Insert Insert( x ) 01010 1 01011 00 110 111 1 1 1 1 1 01000 Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 5 / 35 Tries: Delete Delete( x ) I Search for x to find the leaf v x I Delete v x and all ancestors of v x until we reach an ancestor that has two children Arne Storjohann (CS, UW) CS240 - Module 8 Fall 2010 6 / 35 Tries: Delete...
View Full Document

{[ snackBarMessage ]}

### Page1 / 59

module08 - Module 8 Tries and String Matching CS 240 Data...

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

View Full Document
Ask a homework question - tutors are online