L17-18 QuadTrees PDF
L17-18 QuadTrees PDF
L17-18 QuadTrees PDF
CMSC 420
Applications of Geometric / Spatial Data Structs.
• Virtual reality
Circle
center = C
R.high[1]
Dimension 1
R.low[1]
R.low[0] R.high[0]
Question: how do you
compute the distance
from circle center to the
Dimension 0 rectangle?
Intersection of Circle & Rectangle
R.high[1]
Instead of a lot of
special cases, break the
distance down by
R.low[1] dimension (component)
R.low[0] R.high[0]
distance(C, R):
dist = 0
for i = 0 to DIM:
if C[i] < R.low[i]:
dist += square(R.low[i] - C[i])
return sqrt(dist)
Why are geometric (spatial) data different?
No natural ordering...
• In 1-d:
- we usually had a natural ordering on the keys (integers,
alphabetical order, ...)
- But how do you order a set of points?
• So:
- Instead of explicitly ordering and implicitly partitioning,
we usually: explicitly partition.
- Partitioning is very natural in geometric spaces.
Why are geometric (spatial) data different?
Static case also interesting...
• In 1-d:
- usually the static case (all data known at start) is not very
interesting
- can be solved by sorting the data
(heaps => sorted lists, balanced trees => binary search)
NW NE
NW SE
NE SW
SW SE
PR Quadtrees
L
M Q
N
M Q
N P R
P R
NW SE
NE SW
Find in PR Quadtrees
L
M Q
N
M Q
N P R
P R
Insert in PR Quadtrees
• insert(P):
- find(P)
- if cell where P would go is empty, then add P to it
(change from to )
- If cell where P would go has a point Q in it, repeatedly
split until P is separated from Q. Then add P to correct
(empty) cell.
• delete(P):
- find(P)
- If cell that would contain P is empty, return not found!
- Else, remove P (change to ).
- If at most 1 siblings of the cell has a point, merge siblings
into a single cell. Repeat until at least two siblings
contain a point.
• if
- c is the smallest distance between any two points
- s is the side length of the initial square containing all the
points
• Then
- the depth of a quadtree is ≤ log(s/c) + 3/2
north neighbor of a SW or
SE node is the NW or NE
node respectively
North neighbor of a NE or
NW node is a child of the
north neighbor of its parent.
u = NorthNeighbor(parent(v), Q)
if u is None or is_leaf(u): return u
• Can be inefficient:
- two closely spaced points may require a lot of levels in
the tree to split them
- Have to divide up space finely enough so that they end
up in different cells
In d d = 20 =>
dimensions, nodes will ~
each node 1 million
has 2d children
pointers!
Split & Merge Decomposition
Subdivide into
uniform blocks
Split & Merge Decomposition
Subdivide into
uniform blocks
Merge similar
brothers
Split & Merge Decomposition
Subdivide into
uniform blocks
Merge similar
brothers
Subdivide non-
homogenous cells
Split & Merge Decomposition
Subdivide into
uniform blocks
Merge similar
brothers
Subdivide non-
homogenous cells
Group identical
blocks to get regions
MX Quadtrees
• Insert(P):
- Find the region that would contain the point P.
- If P is encountered during the search, report Duplicate!
- Add point where you fall off the tree.
NW NE
35,40
(35,40)
SW SE
NW NE SW SE
Point Quadtree Demo
Deletion from Point Quadtrees
• Can be expensive.
• Point-based quadtrees
- data points in internal nodes
- often have fewer nodes
- harder deletion
- shape depends on insertion order
Problems with Point Quadtrees
• Size is bounded in n.
- Partitioning key space rather than geometric space.
- Because each node contains a point, you have at most n
nodes.
• Solution is kd-trees.