Search Radix search trie RST Rway trie RT

  • Slides: 17
Download presentation
Search Radix search trie (RST) R-way trie (RT) De la Briandias trie (DLB)

Search Radix search trie (RST) R-way trie (RT) De la Briandias trie (DLB)

Binary search tree (BST) • Left branch is less than • Right branch is

Binary search tree (BST) • Left branch is less than • Right branch is larger than • Create a tree with 0, 1, 2, 3 (in order)

Create BST 0 1 0 < < 1 2 < 3 < 2 <

Create BST 0 1 0 < < 1 2 < 3 < 2 < < 3 Can we do better?

Radix search trie • Using a pair <Key, Value> instead of only Value (BST)

Radix search trie • Using a pair <Key, Value> instead of only Value (BST) • Key is parsed along the tree edges • Value is stored at a node • Assume each Value is linked with only one Key • Create a tree with 0, 1, 2, 3 (in order)

“binary” or 2 -way tree each node links to 2 children Create RST Key

“binary” or 2 -way tree each node links to 2 children Create RST Key Value 0 001 1 010 2 011 3 Binary representation Consider a lower case alphabet string: each node (character) links to (followed by) 26 children (characters) 26 -way tree 0 0 1 1 0 1 Worst case bounds by binary representation length (log n), not by n as in BST Can we apply to string?

R-way trie (lecture example) • she, case sells, sea, shells, the, sea, length shore

R-way trie (lecture example) • she, case sells, sea, shells, the, sea, length shore (Keys Worst bounds by theby, character of theare string on nodes, not on edges) false false true How can we indicate “she” is a complete word? Using a flag variable in each node? false true false true

Create R-way trie • shells, shel l s Is this approach good? Special node

Create R-way trie • shells, shel l s Is this approach good? Special node (starting of any string) s false h false e false true l false s true

The ugly truth she, sea s ? ? a c 1 link for letter

The ugly truth she, sea s ? ? a c 1 link for letter “e” e false 1 link for letter “h” h 1 node = 26 links + 1 flag variable The same prefix? Impossible combinations? Can we do it better?

De la Briandais (DLB) • Replace the fixed link array by a flexible linked

De la Briandais (DLB) • Replace the fixed link array by a flexible linked list s a b c d e f g h i j k l mn o p q r s t u v w x y z Linked-list head next s e h

Trade off • Save a lot of space, especially when the real case has

Trade off • Save a lot of space, especially when the real case has sparse strings • Increase searching time. Why? • R-way trie: Directly go to a child in the array • DLB: linearly go the child in the linked list s e h

Create a DLB • shells, she, seat shel l s s ea t false

Create a DLB • shells, she, seat shel l s s ea t false s false e false h false true a false l true t false l true s

Delete a word in DLB shells false h A true node stop true e

Delete a word in DLB shells false h A true node stop true e false true a False, no children delete false l true t False, no children delete false l false true s sea Change to false no children: delete false e If the path does not belong to other words, remove. Otherwise, leave it alone. Change to false, has a child stop

Exercises (on paper) • RST: • Create a tree for: 2, 3, 4, 6

Exercises (on paper) • RST: • Create a tree for: 2, 3, 4, 6 • Delete values: 4, 6 • DLB: • Create a tree for: baby, bad, bank, box, dad, dance • Delete words: bad, bank, dance

Exercises RST (creation) 0 1 1 0 2 0 0 1 3 1 4

Exercises RST (creation) 0 1 1 0 2 0 0 1 3 1 4 0 6

Exercises RST (deletion) 0 1 0 2 1 3

Exercises RST (deletion) 0 1 0 2 1 3

Exercise DLB (creation) d b a b o d n true y k x

Exercise DLB (creation) d b a b o d n true y k x true a d true n c true e true

Exercise DLB (deletion) d b true a o b x y true a d

Exercise DLB (deletion) d b true a o b x y true a d true