Download Range Trees, Priority Search Trees, and Range Priority Trees for Range Queries and more Study notes Data Structures and Algorithms in PDF only on Docsity!
rt
Copyright © 1998 Hanan Samet
These notes may not be reproduced by any means (mechanical or elec- tronic or any other) without the express written permission of Hanan Samet
RANGE TREES AND PRIORITY SEARCH TREES
Hanan Samet
Computer Science Department and Center for Automation Research and Institute for Advanced Computer Studies University of Maryland College Park, Maryland 20742 e-mail: [email protected]
rt
RANGE TREES
- Balanced binary search tree
- All data stored in the leaf nodes
- Leaf nodes linked in sorted order by a doubly-linked list
- Searching for [B : E]
- find node with smallest value ≥B or largest ≤B
- follow links until reach node with value >E
- O (log 2 N +F ) time to search,O (N · log 2 N ) to build, and O (N ) space forN points andF answers
- Ex: sort points in 2-d on theirx coordinate value
(^15 43 70 )
30 83
5 25 35 50 60 80 85 90
Denver (5,45)
Omaha (25,35)
Chicago (35,40)
Mobile (50,10)
Toronto (60,75)
Buffalo (80,65)
Atlanta (85,15)
Miami (90,5)
55 ≤ ≥
rt
SEARCHING 2-D RANGE TREES ([ BX : EX],[ BY : EY])
- Search tree T for nodes BX and EX
- find node LX with a minimum value ≥ BX
- find node RX with a maximum value ≤ EX
- Find their nearest common ancestor Q
- Compute {Li } and {Ri }, the sequences of nodes forming
the paths from Q to LX and RX , respectively (including LX and RX but excluding Q )
- LEFT ( P ) and RIGHT ( P ) are sons of P
- MIDRANGE ( P ) discriminates onx coordinate value
- RANGE _TREE ( P ) denotes the 1-d range tree stored at P
1 b
- For each element in the sequences {Li } and { Ri } do
T
Q
L 1
L 2
L (^3) B
A E
F R 3
R 2
R 1
R 4
RX
C G
L 4
LX D H
2 r
- if P and LEFT ( P ) are in {Li }, then look for [ BY, EY ] in RANGE _TREE ( RIGHT( P ))
3 z
- if P and RIGHT ( P ) are in {Ri }, then look for [ BY , EY ] in RANGE _ TREE ( LEFT ( P ))
4 g
- Check if LX and RX are in ([BX : EX ],[BY : EY ])
5 v
time to search and O (N · log 2 N ) space and time to build for N points and F answers
(^1) rt b EXAMPLE OF SEARCH IN A 2-D RANGE TREE
- Find all points in ([25:85],[8:16]) A
(90,5)(50,10)(85,15)(25,35)(35,40)(5,45)(80,65)(60,75)
A'
55
38
13 55
8 25 43 70
(90,5)(85,15)(80,65)(60,75)
10 70
5 15 65 75
(85,15) (90,5)
(90,5) (85,15)
88 G
10 G'
5 15
85 N^ O 90 (60,75) (80,65)
70 F
(80,65)(60,75)
70 F'
65 75
60 L^ M 80
83 C
C'
40
(50,10)(25,35)(35,40)(5,45)
23 43
10 35 40 45
(35,40) (50,10)
(50,10)(35,40)
43 E
25 E'
10 40
35 J^ K 50 (5,45) (25,35)
15 D
(25,35) (5,45)
40 D'
35 45
5 H^ I 25
30 B
B'
38
5 10 15 35 40 45 65 75
2 r
- Find nearest common ancestor — i.e., A
3 z
- Find paths to LX =25 and RX=
4 g
- Look in subtrees
- B and B ’s left son D are in path, so search range tree of B ’s right son E and report (50,10)
5 v
- C and C ’s right son G are in path, so search range tree of (^) C ’s left son F and report none
6 z
- Check boundaries ofx range (i.e., (25,35) and (85,15)) and report (85,15)
rt SEMI-INFINITE RANGE QUERY ON A
PRIORITY SEARCH TREE ([BX : EX ],[BY :∞])
- Procedure
- Descend tree looking for the nearest common ancestor of BX and EX — i.e., Q
- associated with each examined node T is a point P
- exit if P does not exist as all points in the subtrees have been examined and/or reported
- exit if Py < BY as P is point with maximumy coordinate value in T
- otherwise, output P if Px is in [BX: EX ]
- Once Q has been found, process left and right subtrees applying the tests above to their root nodes T
- T in left (right) subtree of Q : a. check if BX ( EX ) in LEFT( T ) ( RIGHT ( T )) b. yes: all points in RIGHT ( T) ( LEFT ( T)) are inx range - check if iny range - recursively apply to LEFT ( T ) (RIGHT ( T )) c. no: recursively apply to RIGHT( T ) ( LEFT( T ))
- O (log 2 N +F ) time to search forN points andF answers
- Ex:
1 b
2 r
Find all points in ([35:80],[50:∞])
rt
EXAMPLE OF A SEARCH IN A PRIORITY SEARCH TREE
- Find all points in ([35:80],[50:∞])
1 b
35 40 —^15
45 65
(^5 25 35 50 60 80 85 ) Denver (5,45)
Omaha (25,35)
Chicago (35,40)
Mobile (50,10)
Toronto (60,75)
Buffalo (80,65)
Atlanta (85,15)
Miami (90,5)
75
Omaha
Denver
Toronto
Buffalo
Chicago Atlanta D E F G
B C
A
— — — 10 — — — 5
(^15 43) Mobile Miami 70 88
30
55
83
2 r
- Find nearest common ancestor — i.e., A
- output Toronto (60,75) since 60 is in [35:80] and 75≥ 50
3 z
- Process left subtree of A (i.e., B )
4 g
- Process right subtree of A (i.e., C )
- output (80,65) as 65 ≥ 50 and 80 is in [35:80]
5 v
- Examine midrange value of C which is 83 and descend left subtree of (^) C (i.e., F)
- cease processing since no point is associated with F meaning all nodes in the subtree have been examined
rt
SEARCHING A RANGE PRIORITY TREE ([ BX : EX ],[ BY : EY ])
- Procedure
- find nearest common ancestor of BY and EY — i.e., Q
1 b
2 r
(50,10) Mobile
(25,35) Omaha
(85,15) Atlanta (90,5) Miami
- all points in LEFT ( Q) havey coordinate values < EY
- want to retrieve just the ones ≥BY
- find them with ([ BX : EX],[ BY :∞]) on priority tree of LEFT ( Q)
- priority tree is good for retrieving all points with a specific lower bound as it stores an upper bound and hence irrelevant values can be easily pruned
3 z
(60,75) Toronto
(80,65) Buffalo
(35,40) Chicago
(5,45) Denver
- all points in RIGHT( Q ) havey coordinate values > BY
- want to retrieve just the ones ≤EY
- find them with ([ BX : EX],[–∞:EY ]) on the inverse priority tree of (^) RIGHT (Q )
- inverse priority tree is good for retrieving all points with a specific upper bound as it stores a lower bound and hence irrelevant values can be easily pruned
4 g
- O (log 2 N +F ) time to search forN points andF answers
rt
G
(^5 1015 35 40 45 65 ) Miami (90,5)
Mobile (50,10)
Atlanta (85,15)
Omaha (25,35)
Chicago (35,40)
Denver (5,45)
Buffalo (80,65)
Toronto (60,75)
(25,35) (50,10) (85,15) (90,5) (5,45) (35,40) (60,75) (80,65)
35
10 15
40
(^3888)
68
25 50 85 90
45 65
48
5 35 60 80
D 8 E 25 F (^4370)
(50,10) (90,5) (25,35) (85,15) (60,75) (80,65)
65
(5,45) (35,40)
45
20
5 35
75 –
70
60 80
15
35 –
55
25 85
10
70
50 90
B 13 C (^55)
38 A
EXAMPLE OF A SEARCH IN A RANGE PRIORITY TREE
- Find all points in ([25:60],[15:45])
1 b
2 r
- Find nearest common ancestor of 15 and 45 — i.e., A
3 z
- Search for ([25:60],[15:∞]) in priority tree hanging from left son of A — i.e., B (all withy ≤45 since a range tree iny and in left subtree of a node withy midrange value of 38)
4 r
- output (25,35) as in range
5 g
- reject left subtree as 10 < lower limit ofy range
6 v
- reject items in right subtree as out ofx range
7 g
- Search for ([25:60],[–∞:45]) in inverse priority tree hanging from right son of (^) A — i.e., C (all withy ≥15 since in right subtree of a node withy midrange value of 38)
8 r
- output (35,40) as in range
9 v
- reject unreported items in left subtree as out ofx range
10 z
- reject right subtree as 65 > upper limit ofy range