CSE 326 Multi-Dimensional Search Trees - PowerPoint PPT Presentation

About This Presentation
Title:

CSE 326 Multi-Dimensional Search Trees

Description:

CSE 326 Multi-Dimensional Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001 Outline Multidimensional search ADT Range Queries k-D Trees ... – PowerPoint PPT presentation

Number of Views:100
Avg rating:3.0/5.0
Slides: 37
Provided by: coursesCs66
Category:

less

Transcript and Presenter's Notes

Title: CSE 326 Multi-Dimensional Search Trees


1
CSE 326Multi-Dimensional Search Trees
  • David Kaplan
  • Dept of Computer Science Engineering
  • Autumn 2001

2
Outline
  • Multidimensional search ADT
  • Range Queries
  • k-D Trees
  • Quad Trees

3
Multi-D Search ADT
  • Dictionary operations
  • create
  • destroy
  • find
  • insert
  • delete
  • range queries
  • Each item has k keys for a k-dimensional search
    tree
  • Searches can be performed on one, some, or all of
    the keys or on ranges of the keys

4
Applications of Multi-D Search
  • Astronomy (simulation of galaxies) - 3 dimensions
  • Protein folding in molecular biology - 3
    dimensions
  • Lossy data compression - 4 to 64 dimensions
  • Image processing - 2 dimensions
  • Graphics - 2 or 3 dimensions
  • Animation - 3 to 4 dimensions
  • Geographical databases - 2 or 3 dimensions
  • Web searching - 200 or more dimensions

5
Range Query
  • Range query
  • search in which exact key may not be entirely
    specified
  • primary interface to multi-D data structures
  • Examples
  • All stars in the Milky Way within 1000 light
    years of the Sun.
  • All customers named Smith who spent from
    100-500 with us last year.

6
Two-DimensionalRange Query Examples
  • Search for items based on

single key range
multiple key ranges
function of multiple keys
7
Range Querying in 1-D
x
Find everything in the rectangle
8
Range Querying in 1-D with a BST
Find everything in the rectangle
x
9
1-D Range Querying in 2-D
y
x
10
2-D Range Querying in 2-D
y
x
11
k-D Trees
  • Split on the next dimension at each succeeding
    level
  • If building in batch choose the median along the
    current dimension at each level
  • guarantees logarithmic height and balanced tree
  • common scenario large, (relatively) static data
    sets
  • In general, add as in a BST

k-D tree node
The dimension that this node splits on
dimension
12
Building a 2-D Tree (1/4)
x
13
Building a 2-D Tree (2/4)
y
x
14
Building a 2-D Tree (3/4)
y
x
15
Building a 2-D Tree (4/4)
y
x
16
k-D Tree Data Structure
a
d
b
c
e
f
e
h
i
g
j
m
k
l
17
2-D Range Querying in 2-D Trees
Search every partition that intersects the
rectangle. Check whether each node (including
leaves) falls into the range.
18
Other Shapes for Range Querying
y
x
Search every partition that intersects the shape
(circle). Check whether each node (including
leaves) falls into the shape.
19
Find in a k-D Tree
  • Finds the node which has the given set of keys in
    it or returns null if there is no such node

Node find(const keyVector keys, Node
root) int dim root-gtdimension if (root
NULL) return root else if (root-gtkeys
keys) return root else if (keysdim lt
root-gtkeysdim) return find(keys,
root-gtleft) else return find(keys,
root-gtright)
Runtime
20
Breathalyzer Testk-D Trees Can Lose Their
Balance1
  • insert(lt5,0gt)
  • insert(lt6,9gt)
  • insert(lt9,3gt)
  • insert(lt6,5gt)
  • insert(lt7,7gt)
  • insert(lt8,6gt)

5,0
6,9
9,3
6,5
Worst-case imbalance?
7,7
1but not when built in batch!
8,6
21
k-D Find Example
5,2
  • Actual Find
  • find(lt3,6gt)
  • Insert (first, do a Find)
  • find(lt0,10gt)

8,4
2,5
5,7
8,2
1,9
4,4
4,2
3,6
9,1
22
Quad Trees
  • Split on all (two) dimensions at each level
  • Split key space into equal size partitions
    (quadrants)
  • Add a new node by adding to a leaf, and, if the
    leaf is already occupied, split until only one
    node per leaf

quadrants
quad tree node
0,1
1,1
Center
0,0
1,0
Center
0,0
1,0
0,1
1,1
Quadrants
23
Building a Quad Tree (1/5)
y
x
24
Building a Quad Tree (2/5)
y
x
25
Building a Quad Tree (3/5)
y
x
26
Building a Quad Tree (4/5)
y
x
27
Building a Quad Tree (5/5)
y
x
28
Quad Tree Data Structure
g
a
f
e
d
c
b
29
2-D Range Querying in Quad Trees
y
x
30
Find in a Quad Tree
  • Finds the node which has the given pair of keys
    in it or returns quadrant where the point should
    be if there is no such node

Node find(Key x, Key y, Node root) if
(root NULL) return root // Empty tree
if (root-gtisLeaf) return root // Key may
not actually be here // Compare vs. center
always make same choice on ties int quad
getQuadrant(x, y, root) return find(x, y,
root-gtquadrantsquad)
Runtime?
31
Walk this straight line, please Quad Trees Can
Lose Their Balance1
  • Worst-case imbalance?
  • O(log(1/dmin))

a
b
1but no batch bail-out why not?
32
Find Example
find(lt10,2gt) (i.e., c) find(lt5,6gt) (i.e., d)
g
a
f
e
d
c
b
33
Insert Example
insert(lt10,7gt,x)
g
a

  • Find the spot where the node should go.
  • If the space is unoccupied, insert the node.
  • If it is occupied, split until the existing
    node separates from the new one.

g
x
34
Delete Example
delete(lt10,2gt)(i.e., c)
a
b
c
g
a
d
f
e
d
e
g
f
  • Find and delete the node.
  • If its parent has just one child, delete it.
  • Propagate!

c
b
35
Nearest Neighbor Search
getNearestNeighbor(lt1,4gt)
b
a
c
g
a
d
e
f
e
d
g
f
  • Find a nearby node (do a find).
  • Do a circular range query.
  • As you get results, tighten the circle.
  • Continue until no closer node in query.

c
b
Works on k-D Trees, too!
36
Quad Trees vs. k-D Trees
  • k-D Trees
  • Density balanced trees
  • Number of nodes is O(n) where n is the number of
    points
  • Height of the tree is O(log n) with batch
    insertion
  • Supports insert, find, nearest neighbor, range
    queries
  • Quad Trees
  • Number of nodes is O(n(1 log(?/n))) where n is
    the number of points and ? is the ratio of the
    width (or height) of the key space and the
    smallest distance between two points
  • Height of the tree is O(log n log ?)
  • Supports insert, delete, find, nearest neighbor,
    range queries
  • Also many hybrids and variants
Write a Comment
User Comments (0)
About PowerShow.com