Week 22
Radix Search Trees
Radix Search Trees
Radix Searching Digital Search Trees Radix Search Tries Multi-Way Radix Trees
Radix Searching
Examine the search keys one bit at a time Advantages:
reasonable worst-case performance easy way to handle variable length keys some savings in space by storing part of the key within the search structure competitive with both binary search trees and hashing
3
Idea:
Radix Searching
Disadvantages:
biased data can lead to degenerate trees with bad performance for some methods use of space is inefficient dependent on computers architecture difficult to do efficient implementations in some high-level languages
4
Radix Searching Methods
Digital Search Trees Radix Search Tries Multiway Radix Searching
Digital Search Trees (1)
Similar to binary search tree
Order of Keys is important. If keys are inserted in different order, it will result in a different tree.
Difference: Branch in the tree by comparing the keys bits, not the keys as a whole
Digital Search Trees (2)
Example
E X A M P L 00101 11000 00001 01101 10000 01100 E 0 A 1 M 0 L P 0 1 X
Digital Search Trees (2)
inserting Z = 11010 go right twice external node attach Z to the right of X
A 1 M 0 L P 0 Z E 0 1 X 1
Digital Search Trees (3)
Insert the keys A, S, E, R, C, H, I, N, G, X, M, P, L into an initially empty digital search tree
First convert keys to binary A S E R C H I N G X M P L (1) (19) (5) (18) (3) (8) (9) (14) (7) (24) (13) (16) (12) 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
Digital Search Trees (3)
A
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
A
10
Digital Search Trees (3)
S
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
A 1 S
11
Digital Search Trees (3)
E
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
0 E A 1 S
12
Digital Search Trees (3)
R
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
0 E R A 1 S
13
Digital Search Trees (3)
C
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
0 0 C E R A 1 S 0
14
Digital Search Trees (3)
H
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
0 0 C E 1 H R A 1 S 0
15
Digital Search Trees (3)
I
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
0 0 C 0 I E 1 H R A 1 S 0
16
Digital Search Trees (3)
N
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
A 0 0 C 0 I E 1 H 1 N R 1 S 0
17
Digital Search Trees (3)
G
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
A 0 0 C 1 G I 0 E 1 H 1 N R 1 S 0
18
Digital Search Trees (3)
X
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
A 0 0 C 1 G I 0 E 1 H 1 N R 1 S 0 1 X
19
Digital Search Trees (3)
M
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
A 0 0 C 1 G I 0 M 0 E 1 H 1 N R 1 S 0 1 X
20
Digital Search Trees (3)
P
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
A 0 0 C 1 G I 0 M 0 E 1 H 1 N 0 P R 1 S 0 1 X
21
Digital Search Trees (3)
L
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
A 0 0 C 1 G I 0 M 0 L
22
1 S 1 X
1 H 0 1 N 0 P R
Digital Search Trees (4)
To search in a digital search tree
We branch according to bits (starting from leftmost bit) We compare at each level (when be branch) We keep branding down the tree until the key is found or we reach an external node (i.e. key is not found)
23
Digital Search Trees (4)
Search Example: Search for M (01101)
1. 2. 3. 4. 5. Compare with root Branch to left and compare Branch right and compare Branch right and compare Branch left and compare Key is found 0 C 1 G I 0 M 0 L
24
A 0 E 1 H 0 1 N 0 P
1 S 0 R 1 X
Digital Search Trees (5)
Things to remember about digital search trees: Equal keys are anathema must be kept in separate data structures, linked to the nodes. Worst case better than for binary search trees the length of the longest path is equal to the longest match in the leading bits between any two keys. 25
Digital Search Trees (6)
Things to remember about digital search trees: Equal keys are anathema must be kept in separate data structures, linked to the nodes. Worst case better than for binary search trees the length of the longest path is equal to the longest match in the leading bits between any two keys. 26
Digital Search Trees (7)
Search or insertion requires about log(N) comparisons on the average and b comparisons in the worst case in a tree built from N random b-bit keys. No path will ever be longer than the number of bits in the keys
27
Digital Search Trees (8)
Tnode digitalSearch(int v, Tnode x) { int b; while((v!=x.get_key())&& (x!=null)) { if(bits(v,b,1)==0) x=x.get_left(); else x=x.get_right(); b=b-1; } return x; }
28
Tnode digitalInsert(int v, Tnode x) { int b; Tnode p=x; while(x!=NULL) { p=x; if(bits(v,b,1)==0) x=x.get_left(); else x=x.get_right(); b=b-1; } x=new Tnode(v); if(bits(v,b+1,1)==0) p.set_left(x); else p.set_right(x); return x; }
29
Radix Search Tries
If the keys are long digital search trees have low efficiency. Radix search tries : do not store keys in the tree at all, the keys are in the external nodes of the tree. Called tries (try-ee) from retrieval
30
Radix Search Tries
Two types of nodes
Internal: contain only links to other nodes ( ) External: contain keys and no Key links ( )
31
Radix Search Tries
Example
E X A M P L 00101 11000 00001 01101 10000 01100 A 0 0 0 1 E 0 L 1 P 1 0 1 M 0 1 1 X
32
Radix Search Tries
To insert a key
1. Go along the path described by the leading bit pattern of the key until an external node is reached. 2. If the external node is empty, store there the new key. If the external node contains a key, replace it by an internal node linked to the new key and the old key. If the keys have several bits equal, more internal nodes are necessary.
NOTE: insertion does not depend on the order of the keys.
33
Radix Search Tries
To search for a key 1. Branch according to its bits, 2. Dont compare it to anything, until we get to an external node. 3. One full key comparison there completes the search.
34
Radix Search Tries
A S E R C H I N G X M P L 00001 10011 00101 10010 00011 01000 01001 01110 00111 11000 01101 10000 01100
Insert the keys A, S, E, R, C, H, I, N, G, X, M, P, L into an initially empty radix search trie
First convert keys to binary
35
A (00001)
0 A 1
36
S (10011)
0 A 1
There is an empty null node, insert S
37
S (10011)
0 A 1 S
38
E (00101)
0 A 1 S
A (00001) E (00101)
There is A on the path Compare A and E They are similar for the first 2 leftmost bits They disagree on third bit (from left) Insert 2 internal nodes for the bits on which A and E agree and then place A in new position and E in its position 39
E (00101)
0 0 0 A 1 E 1 1 S
A (00001) E (00101)
40
R (10010)
0 0 0 A 1 E S (10011) R (10010) 1 1 S
There is S on the path Compare S and R They are similar for the first 4 leftmost bits They disagree on last bit (from left) Insert 4 internal nodes for the bits on which S and R agree and then place S in new position and R in its position 41
R (10010)
0 0 0 A 1 E 0 0 R 1 0 1 1 S 1 0 1 1
S (10011) R (10010)
42
C (00011)
0 0 0 A 1 E 0 0 R 1 0 1 1 S 1 0 1 1
A (00001) C (00011)
43
C (00011)
0 0 0 0 A 1 C R 1 E 0 0 1 0 1 1 S 1 0 1 1
A (00001) C (00011)
44
H (01000)
0 0 0 0 A 1 C R 1 E 0 0 1 0 1 1 S 1 0 1 1
There is an empty external node
45
H (01000)
0 0 0 0 A 1 C R 1 E 1 0 H 0 0 0 1
H (01000) I (01001)
1 1
1 S
46
I (01001)
0 0 0 0 A 1 C R 1 E 1 0 H 0 0 0 1
H (01000) I (01001)
1 1
1 S
47
I (01001)
0 0 0 0 A 1 C H 1 E 0 0 1 I R 1 0 1 1 0 0 0 1 1 S 1 1
1 0
48
N (01110)
0 0 0 0 A 1 C H 1 E 0 0 1 I R 1 0 1 1 0 0 0 1
There is an empty external node
1 0
1 1
1 S
49
N (01110)
0 0 0 0 A 1 C H 1 E 0 0 1 I R 1 0 1 1 N 0 0 0 1 1 S 1 1
1 0
50
G (00111)
0 0 0 0 A 1 C H 1 E 0 0 1 I R 1 0 1 1 N 0 0 0 1
E (00101) G (00111)
1 0
1 1
1 S
51
G (00111)
0 0 0 0 A 1 C 0 E 1 1 G H 0 0 1 I R 1 0 1 1 N 0 0 0 1
E (00101) G (00111)
0 1 1 1 S
52
X (11000)
0 0 0 0 A 1 C 0 E 1 1 G H 0 0 1 I R 1 0 1 1 N 0 0 0 1
There is an empty external node
0 1 1 1 S
53
X (11000)
0 0 0 0 A 1 C 0 E 1 1 G H 0 0 1 I R 1 0 1 1 N 0 0 0 1 1 S 1 0 1 1 X
54
M (01101)
0 0 0 0 A 1 C 0 E 1 1 G H 0 0 1 I R 1 0 1 1 N 0 0 0 1
N (01110) M (01101)
0 1 1 1 S
1 X
55
N (01110) M (01101)
0 0 0 0 A 1 C 0 E 1 1 G H 0 0 1 I 1 0 1 1 0 M 1 N R 0 0 0 1 1 S 1 0 1 1 X
56
P (10000)
0 0 0 0 A 1 C 0 E 1 1 G H 0 0 1 I 1 0 1 1 0 M 1 N 0 1
There is an empty external node
0 0 1 0 R 1 S 1
1 X
57
P (10000)
0 0 0 0 A 1 C 0 E 1 1 G H 0 0 1 I 1 0 1 1 0 M 1 N P R 0 0 0 1 1 S 1 0 1 1 X
58
L (01100)
0 0 0 0 A 1 C 0 E 1 1 G H 0 0 1 I 1 0 1 1 0 M 1 N P 0 1
M (01101) L (0110 0)
0 0 1 0 R 1 S 1
1 X
59
L (01100)
0 0 0 0 A 1 C 0 E 1 1 G 0 H 0 1 I L 1 0 1 1 0 0 1 M 1 N P 0 1
M (01101) L (0110 0)
0 0 1 0 R 1 S 1
1 X
60
Radix Search Tries - summary
Program implementation Necessity to maintain two types of nodes
Low-level implementation
Complexity: about logN bit comparisons in average case and b bit comparisons in the worst case in a tree built from N random b-bit keys. Annoying feature: One-way branching for keys with a large number of common leading bits :
The number of the nodes may exceed the number of the keys. 61 On average N/ln2 = 1.44N nodes
Multi-Way Radix Tries
The height of the tree is limited by the number of the bits in the keys If we have larger keys the height increases. One way to overcome this deficiency is using a multi-way radix trie searching. The branching is not according to 1 bit, but rather according to several bits (most often 2) If m bits are examined at a time the search is speeded up by a factor of 2m have 2m links, may result in considerable amount of wasted space due to unused links.
62
Problem: if m bits at a time, the nodes will
Search take left, right or middle links according to the first two bits. Insert replace external node by the key
(E.G. insert T 10100).
Multi-Way Radix Tries example
Nodes with 4 links 00, 01, 10, 11
00 01 10 X N P T H I L M R S
63
11
C E
Multi-Way Radix Tries
Wasted space due to the large number of unused links. Worse if M - the number of bits considered, gets higher. The running time: logMN very efficient. Hybrid method: Large M at the top, Small M at the bottom
64