Download presentation
Presentation is loading. Please wait.
1
Searching with Structured Keys Objectives
To look at search structures for variable length and multidimensional keys. To understand several basic radix search techniques, their advantages and disadvantages To look at range queries and partial match searches in multidimensional structures.
2
Radix Techniques ……. You remember the card sorter
Basic operation: examine a particular column (field with finite range) and put card into a bin based on it. To sort k field deck: * Make k passes from least to most significant column. * Each pass sorts on current column keeping “ties” in previous order. Cost: k passes n cards per pass. Linear in number of characters in file
3
Radix Sorting Technique
Dog Dog Dap Bip Boy Bog Map Bog Top Top Bip Bop Bop Bop Dip Boy Bip Bip Dog Dap Bog Dip Bog Dip Dip Dap Top Dog Dap Map Bop Map Map Boy Boy Top pass 1 pass 2 pass 3
4
Radix Searching Techniques
We will treat keys as consisting of a sequence of small fields … or simple bit patterns to develop useful search structures Underlying assumption about keys: - They are long and/or of variable length. - A comparison between keys (as opposed to parts of keys) is costly or of variable cost.
5
Digital Search Trees Same as a binary search tree, except that the decision at each node is either: - A match (key matches) or - Branch on next bit
6
Digital Search Tree S E A R C H I N G S R E G A C I N H 1
7
Digital Search Tree Updates
Insert : Search and hang it on. Delete : Remove and pull up any leaf descendant
8
Digital Search Tree Updates
N C R I G 1 Insert T Delete N,E
9
Digital Search Trees - Costs
The number of nodes inspected n a search is, at most, the number of leading bits needed to distinguish it from other keys For “randomly distributed” keys this should be Θ(log n), but if keys agree on lead bits problems arise But, visiting each node involves a key comparison, this may be the bulk of the cost
10
Radix Search Tries “trie” as in “retrieval” is pronounced “try”
Keep keys at leaves only … that way only one full key comparison is needed per search S R H E A C G I N 1
11
Radix Search Trie Updates
Insert: Search to a leaf or fall out at a single branch node If you “fall out”, hang a new node on there If you come to a leaf continue the path in the tree with the two elements until the bits differ S R H E A C G I N 1 Insert Y M Deletions are similar
12
A real fix: Leave out 1 way branch nodes The Search Structure
Patricia (Practical Algorithm To Retrieve Information Coded In Alphanumeric) Permits searches for n arbitrarily long keys with Only n nodes of storage Just one full key compare No “useless” tests
13
Patricia: Step 1 Leave out 1 way branch nodes
Add to each node the position of the bit to be tested (we will still move left to right) Bit # S E A R C E A C S R 1 2 4
14
Patricia: Step 2 Add the “dummy value” (which is either not stored or stored in the header). Store keys in internal nodes that are ancestors of their leaf positions in the tree A header 1 2 3 4 I H E C R S Since we still examine bits from left to right, we know we have reached a “key test” when the bit field is not less than that of previous node.
15
Patricia Insertions Insertions can be made in the natural way.
Follow the search path of new leaf. Determine the highest number bit where new key and leaft key differ. Retrace search path to the point whre a test on this bit could occur. Insert new node there with the node on the path to leaf node as one child and a new leaf containing the new key
16
Patricia Insertions: Insert H 0 1 0 0 0 I 0 1 0 0 1
A header 1 2 C 4 R S E Observe that the structure must contain one more leaf than internal node.
17
Patricia Insertions: If new key differs from those along its search path in an untested bit : put in a new node with new key and pointer to itself 4 5 7 old new 4 7 old
18
….Otherwise search ends at an upward branch: Insert new node with new key and pointers to itself and node of upward branch. The left pointer refers to the one with 0 in the differing bit old new old bit
19
Insertion Example: A I H E C R S N G Insert N 0 1 1 1 0 G 0 0 1 1 1 A
A header 1 2 3 4 I H E C R S N G Insert N G A header 1 2 3 4 I H E C R S Deletions are similar
20
Radix Techniques Insensitive to order of insertions Sensitive to particular value Very useful for long or variable length keys
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.