This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Radix Trees (Tries) Suppose we have data items whose keys are binary strings, not necessarily of the same length. We can organize our data into a dictionary using a binary tree based on bitwise comparisons of strings. This data structure is called a radix tree or trie (pronounced “try”). Its main features are as follows: 1. a left child corresponds to a 0 bit, 2. a right child corresponds to a 1 bit, 3. items (or, alternatively, links to items) are stored only in leaf nodes of the tree. Note: property 3 implies that no string should be a prefix of another string (e.g., 011 is a prefix of 01101). We will always make this assumption unless otherwise stated. 156/ 193 Operations on Tries Search: Suppose X = x 1 x 2 ...x ‘ . We can use a binary search technique to locate an item with key X . There are four possible outcomes: 1. A successful search which ends at the correct leaf node. 2. an unsuccessful search that ends at a leaf node storing a key Y where Y is a prefix of X . 3. an unsuccessful search that ends at a nonleaf node at level ‘ of the trie (in this case, X is a prefix of a string Y in the trie). 4. an unsuccessful search that ends at a nonleaf node at some level k < ‘ of the trie. The complexity of Search is O (  X  ), where  X  denotes the length of the string X . 157/ 193 Trie Search Algorithm: TrieSearch ( T,X ) N ← Root ( T ); ‘ ← Length ( X ); i ← 1; case ← while i ≤ ‘ and case = 0 do if N is a leaf node then case ← 2 else if x i = 0 then N ← LeftChild ( N ) else N ← RightChild ( N ) if N = nil then case ← 4 else i ← i + 1 if i = ‘ + 1 then if N is a leaf node then case ← 1 else case ← 3 return ( case ,N ) 158/ 193 Operations on Tries (cont.) Insert: Suppose X = x 1 x 2 ...x ‘ . To insert a node containing the (new) key X , we first do an unsuccessful search for X . If we terminate in case 2 or 3, then we cannot insert X into the tree because it will violate the “prefixfree” property. So assume we end up in case 4. Then the path from the root node to N corresponds to a prefix of X , say x 1 x 2 ...x k , where k < ‘ . Now create ‘ k new nodes in the trie to “complete” this prefix to X . The complexity of Insert is O (  X  ). 159/ 193 Operations on Tries (cont.) Delete: Suppose X = x 1 x 2 ...x ‘ . To delete the node containing the key X , we first do a successful search for X that terminates at a leaf node N . We delete N and all ancestors of N until we reach an ancestor that has two children ; then stop. The complexity is O (  X  ). 160/ 193 Compressed Tries A compressed trie eliminates nodes with only one child, which decreases the storage requirement of the data structure. Roughly speaking, we compress every path of onechild nodes to a single edge . Each node stores an index indicating the next bit to be tested during a search....
View
Full Document
 Winter '09
 Ennis
 Psychology, String searching algorithm, leaf node

Click to edit the document details