Multidimensional Range Search: Static Collection of Records
Multidimensional Range Search: Static Collection of Records
Performance Measures
P(n,k).
Preprocessing time to construct search structure for n records, each has k key fields. For many applications, this time needs only to be reasonable.
S(n,k).
Total space needed by the search structure.
Q(n,k).
Time needed to answer a query.
k-d Tree
Binary tree. At each node of tree, pick a key field to partition records in that subtree into two approximately equal groups.
Pick field i with max spread in values. Select median key value, m.
Node stores i and m. Records with ki <= m in left subtree. Records with ki > m in right subtree. Stop when partition size <= 8 or 16 (say).
2-d Example
e
a
b d e f c g
Performance
a
b
d e f
c
g
Performance
a
b
d e f
c
g
S(n,k) = O(n).
O(n) needed for the n records. Tree takes O(n) space.
Performance
Q(n,k) depends on shape of query.
O(n1-1/k + s), where s is number of records that satisfy the query. Bound on worst-case query time. O(log n + s), average time when query is almost cubical and a small fraction of the n records satisfy the query. O(s), average time when query is almost cubical and a large fraction of the n records satisfy the query.
Range Treesk=1
Sorted array on single key.
10 12 15 20 24 26 27 29 35 40 50 55
Range Treesk=2
Let the two key fields be x and y. Binary search tree on x. x value used in a node is the median x value for all records in that subtree. Records with x value <= median are in left subtree. Records with larger x value in right subtree.
Range Treesk=2
Each node has a sorted array on y of all records in the subtree.
Root has sorted array of all n records. Left and right subtrees, each have a sorted array of about n/2 records.
Example
a b c
SA
a-g are x values. x-range of a node begins at min x value in subtree and ends at max x value in subtree.
ExampleSearch
a b c
SA
If x-range of root is contained in x-range of query, search SA for records that satisfy y-range of query. Done.
query x-range root x-range
ExampleSearch
a b c
SA
If entire x-range of query <= x (> x)value in root, recursively search left (right) subtree.
query x-range root x-value
ExampleSearch
a b c
SA
If x-range of query contains value in root, recursively search left and right subtrees.
query x-range root x-value
Performance
a b c
SA
Performance
a b c
SA
Performance
a b c
SA
Performance
a b c
SA
Range Treesk=3
Let the three key fields be w, x and y. Binary search tree on w. w value used in a node is the median w value for all records in that subtree. Records with w value <= median in left subtree. Records with larger w value in right subtree.
Range Treesk=3
Each node has a 2-d range tree on x and y of all records in the subtree. Stop partitioning when # records in a partition is small enough (say 8).
Example
a b 2-d c
a-g are w values. w-range of a node begins at min w value in subtree and ends at max w value in subtree.
ExampleSearch
a b 2-d c
If w-range of root is contained in w-range of query, search 2-d range tree in root for records that satisfy x- and y-ranges of query. Done. If entire w-range of query <= w (> w) value in root, recursively search left (right) subtree.
ExampleSearch
a b 2-d c
If w-range of query contains value in root, recursively search left and right subtrees.
P(n,k) = O(n logk-1 n), k > 1. S(n,k) = O(n logk-1 n). Q(n,k) = O(logk n + s).