Week Week 22 22: Radix Search Trees
Week Week 22 22: Radix Search 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
Difference: Branch in the tree by comparing the keys bits, not the keys as a whole
10
11
12
13
14
15
16
17
18
19
20
21
1 S 1 X
1 H 0 1 N 0 P R
23
A 0 E 1 H 0 1 N 0 P
1 S 0 R 1 X
27
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
32
35
A (00001)
0 A 1
36
S (10011)
0 A 1
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
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
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
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
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
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
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).
11
C E