<s>
A	O
quadtree	B-Data_Structure
is	O
a	O
tree	B-Application
data	I-Application
structure	I-Application
in	O
which	O
each	O
internal	O
node	O
has	O
exactly	O
four	O
children	O
.	O
</s>
<s>
Quadtrees	B-Data_Structure
are	O
the	O
two-dimensional	O
analog	O
of	O
octrees	B-Data_Structure
and	O
are	O
most	O
often	O
used	O
to	O
partition	O
a	O
two-dimensional	O
space	O
by	O
recursively	O
subdividing	O
it	O
into	O
four	O
quadrants	O
or	O
regions	O
.	O
</s>
<s>
This	O
data	O
structure	O
was	O
named	O
a	O
quadtree	B-Data_Structure
by	O
Raphael	O
Finkel	O
and	O
J.L.	O
</s>
<s>
All	O
forms	O
of	O
quadtrees	B-Data_Structure
share	O
some	O
common	O
features	O
:	O
</s>
<s>
The	O
tree	O
directory	O
follows	O
the	O
spatial	O
decomposition	O
of	O
the	O
quadtree	B-Data_Structure
.	O
</s>
<s>
A	O
tree-pyramid	O
(	O
T-pyramid	B-Data_Structure
)	O
is	O
a	O
"	O
complete	O
"	O
tree	O
;	O
every	O
node	O
of	O
the	O
T-pyramid	B-Data_Structure
has	O
four	O
child	O
nodes	O
except	O
leaf	B-Application
nodes	I-Application
;	O
all	O
leaves	O
are	O
on	O
the	O
same	O
level	O
,	O
the	O
level	O
that	O
corresponds	O
to	O
individual	O
pixels	O
in	O
the	O
image	O
.	O
</s>
<s>
The	O
data	O
in	O
a	O
tree-pyramid	O
can	O
be	O
stored	O
compactly	O
in	O
an	O
array	O
as	O
an	O
implicit	B-Data_Structure
data	I-Data_Structure
structure	I-Data_Structure
similar	O
to	O
the	O
way	O
a	O
complete	O
binary	O
tree	O
can	O
be	O
stored	O
compactly	O
in	O
an	O
array	O
.	O
</s>
<s>
Quadtrees	B-Data_Structure
may	O
be	O
classified	O
according	O
to	O
the	O
type	O
of	O
data	O
they	O
represent	O
,	O
including	O
areas	O
,	O
points	O
,	O
lines	O
and	O
curves	O
.	O
</s>
<s>
Quadtrees	B-Data_Structure
may	O
also	O
be	O
classified	O
by	O
whether	O
the	O
shape	O
of	O
the	O
tree	O
is	O
independent	O
of	O
the	O
order	O
in	O
which	O
data	O
is	O
processed	O
.	O
</s>
<s>
The	O
following	O
are	O
common	O
types	O
of	O
quadtrees	B-Data_Structure
.	O
</s>
<s>
The	O
region	O
quadtree	B-Data_Structure
represents	O
a	O
partition	O
of	O
space	O
in	O
two	O
dimensions	O
by	O
decomposing	O
the	O
region	O
into	O
four	O
equal	O
quadrants	O
,	O
subquadrants	O
,	O
and	O
so	O
on	O
with	O
each	O
leaf	O
node	O
containing	O
data	O
corresponding	O
to	O
a	O
specific	O
subregion	O
.	O
</s>
<s>
The	O
height	O
of	O
quadtrees	B-Data_Structure
that	O
follow	O
this	O
decomposition	O
strategy	O
(	O
i.e.	O
</s>
<s>
The	O
region	O
quadtree	B-Data_Structure
is	O
a	O
type	O
of	O
trie	B-General_Concept
.	O
</s>
<s>
A	O
region	O
quadtree	B-Data_Structure
with	O
a	O
depth	O
of	O
n	O
may	O
be	O
used	O
to	O
represent	O
an	O
image	O
consisting	O
of	O
2n	O
×	O
2n	O
pixels	O
,	O
where	O
each	O
pixel	O
value	O
is	O
0	O
or	O
1	O
.	O
</s>
<s>
The	O
root	B-Application
node	I-Application
represents	O
the	O
entire	O
image	O
region	O
.	O
</s>
<s>
Rather	O
than	O
store	O
a	O
big	O
2-D	O
array	O
of	O
every	O
pixel	O
in	O
the	O
image	O
,	O
a	O
quadtree	B-Data_Structure
can	O
capture	O
the	O
same	O
information	O
potentially	O
many	O
divisive	O
levels	O
higher	O
than	O
the	O
pixel-resolution	O
sized	O
cells	O
that	O
we	O
would	O
otherwise	O
require	O
.	O
</s>
<s>
A	O
region	O
quadtree	B-Data_Structure
may	O
also	O
be	O
used	O
as	O
a	O
variable	O
resolution	O
representation	O
of	O
a	O
data	O
field	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
temperatures	O
in	O
an	O
area	O
may	O
be	O
stored	O
as	O
a	O
quadtree	B-Data_Structure
,	O
with	O
each	O
leaf	O
node	O
storing	O
the	O
average	O
temperature	O
over	O
the	O
subregion	O
it	O
represents	O
.	O
</s>
<s>
The	O
point	O
quadtree	B-Data_Structure
is	O
an	O
adaptation	O
of	O
a	O
binary	O
tree	O
used	O
to	O
represent	O
two-dimensional	O
point	O
data	O
.	O
</s>
<s>
It	O
shares	O
the	O
features	O
of	O
all	O
quadtrees	B-Data_Structure
but	O
is	O
a	O
true	O
tree	O
as	O
the	O
center	O
of	O
a	O
subdivision	O
is	O
always	O
on	O
a	O
point	O
.	O
</s>
<s>
Point	O
quadtrees	B-Data_Structure
are	O
worth	O
mentioning	O
for	O
completeness	O
,	O
but	O
they	O
have	O
been	O
surpassed	O
by	O
k-d	B-Data_Structure
trees	I-Data_Structure
as	O
tools	O
for	O
generalized	O
binary	O
search	O
.	O
</s>
<s>
Point	O
quadtrees	B-Data_Structure
are	O
constructed	O
as	O
follows	O
.	O
</s>
<s>
A	O
node	O
of	O
a	O
point	O
quadtree	B-Data_Structure
is	O
similar	O
to	O
a	O
node	O
of	O
a	O
binary	O
tree	O
,	O
with	O
the	O
major	O
difference	O
being	O
that	O
it	O
has	O
four	O
pointers	O
(	O
one	O
for	O
each	O
quadrant	O
)	O
instead	O
of	O
two	O
(	O
"	O
left	O
"	O
and	O
"	O
right	O
"	O
)	O
as	O
in	O
an	O
ordinary	O
binary	O
tree	O
.	O
</s>
<s>
Point-region	O
(	O
PR	O
)	O
quadtrees	B-Data_Structure
are	O
very	O
similar	O
to	O
region	O
quadtrees	B-Data_Structure
.	O
</s>
<s>
In	O
a	O
region	O
quadtree	B-Data_Structure
,	O
a	O
uniform	O
value	O
is	O
stored	O
that	O
applies	O
to	O
the	O
entire	O
area	O
of	O
the	O
cell	O
of	O
a	O
leaf	O
.	O
</s>
<s>
The	O
cells	O
of	O
a	O
PR	O
quadtree	B-Data_Structure
,	O
however	O
,	O
store	O
a	O
list	O
of	O
points	O
that	O
exist	O
within	O
the	O
cell	O
of	O
a	O
leaf	O
.	O
</s>
<s>
Like	O
the	O
point	O
quadtree	B-Data_Structure
,	O
the	O
PR	O
quadtree	B-Data_Structure
may	O
also	O
have	O
a	O
linear	O
height	O
when	O
given	O
a	O
"	O
bad	O
"	O
set	O
.	O
</s>
<s>
Edge	O
quadtrees	B-Data_Structure
(	O
much	O
like	O
PM	O
quadtrees	B-Data_Structure
)	O
are	O
used	O
to	O
store	O
lines	O
rather	O
than	O
points	O
.	O
</s>
<s>
Near	O
corners/vertices	O
,	O
edge	O
quadtrees	B-Data_Structure
will	O
continue	O
dividing	O
until	O
they	O
reach	O
their	O
maximum	O
level	O
of	O
decomposition	O
.	O
</s>
<s>
The	O
polygonal	O
map	O
quadtree	B-Data_Structure
(	O
or	O
PM	O
Quadtree	B-Data_Structure
)	O
is	O
a	O
variation	O
of	O
quadtree	B-Data_Structure
which	O
is	O
used	O
to	O
store	O
collections	O
of	O
polygons	O
that	O
may	O
be	O
degenerate	O
(	O
meaning	O
that	O
they	O
have	O
isolated	O
vertices	O
or	O
edges	O
)	O
.	O
</s>
<s>
A	O
big	O
difference	O
between	O
PM	O
quadtrees	B-Data_Structure
and	O
edge	O
quadtrees	B-Data_Structure
is	O
that	O
the	O
cell	O
under	O
consideration	O
is	O
not	O
subdivided	O
if	O
the	O
segments	O
meet	O
at	O
a	O
vertex	O
in	O
the	O
cell	O
.	O
</s>
<s>
There	O
are	O
three	O
main	O
classes	O
of	O
PM	O
Quadtrees	B-Data_Structure
,	O
which	O
vary	O
depending	O
on	O
what	O
information	O
they	O
store	O
within	O
each	O
black	O
node	O
.	O
</s>
<s>
PM3	O
quadtrees	B-Data_Structure
can	O
store	O
any	O
amount	O
of	O
non-intersecting	O
edges	O
and	O
at	O
most	O
one	O
point	O
.	O
</s>
<s>
PM2	O
quadtrees	B-Data_Structure
are	O
the	O
same	O
as	O
PM3	O
quadtrees	B-Data_Structure
except	O
that	O
all	O
edges	O
must	O
share	O
the	O
same	O
end	O
point	O
.	O
</s>
<s>
Finally	O
PM1	O
quadtrees	B-Data_Structure
are	O
similar	O
to	O
PM2	O
,	O
but	O
black	O
nodes	O
can	O
contain	O
a	O
point	O
and	O
its	O
edges	O
or	O
just	O
a	O
set	O
of	O
edges	O
that	O
share	O
a	O
point	O
,	O
but	O
you	O
cannot	O
have	O
a	O
point	O
and	O
a	O
set	O
of	O
edges	O
that	O
do	O
not	O
contain	O
the	O
point	O
.	O
</s>
<s>
We	O
can	O
cut	O
down	O
on	O
the	O
size	O
of	O
such	O
sparse	O
trees	O
by	O
only	O
storing	O
subtrees	B-Application
whose	O
leaves	O
have	O
interesting	O
data	O
(	O
i.e.	O
</s>
<s>
"	O
important	O
subtrees	B-Application
"	O
)	O
.	O
</s>
<s>
When	O
we	O
only	O
keep	O
important	O
subtrees	B-Application
,	O
the	O
pruning	O
process	O
may	O
leave	O
long	O
paths	O
in	O
the	O
tree	O
where	O
the	O
intermediate	O
nodes	O
have	O
degree	O
two	O
(	O
a	O
link	O
to	O
one	O
parent	O
and	O
one	O
child	O
)	O
.	O
</s>
<s>
It	O
turns	O
out	O
that	O
we	O
only	O
need	O
to	O
store	O
the	O
node	O
at	O
the	O
beginning	O
of	O
this	O
path	O
(	O
and	O
associate	O
some	O
meta-data	O
with	O
it	O
to	O
represent	O
the	O
removed	O
nodes	O
)	O
and	O
attach	O
the	O
subtree	B-Application
rooted	O
at	O
its	O
end	O
to	O
.	O
</s>
<s>
Although	O
we	O
trim	O
a	O
lot	O
of	O
the	O
tree	O
when	O
we	O
perform	O
this	O
compression	O
,	O
it	O
is	O
still	O
possible	O
to	O
achieve	O
logarithmic-time	O
search	O
,	O
insertion	O
,	O
and	O
deletion	O
by	O
taking	O
advantage	O
of	O
Z-order	B-Algorithm
curves	I-Algorithm
.	O
</s>
<s>
The	O
Z-order	B-Algorithm
curve	I-Algorithm
maps	O
each	O
cell	O
of	O
the	O
full	O
quadtree	B-Data_Structure
(	O
and	O
hence	O
even	O
the	O
compressed	O
quadtree	B-Data_Structure
)	O
in	O
time	O
to	O
a	O
one-dimensional	O
line	O
(	O
and	O
maps	O
it	O
back	O
in	O
time	O
too	O
)	O
,	O
creating	O
a	O
total	O
order	O
on	O
the	O
elements	O
.	O
</s>
<s>
Therefore	O
,	O
we	O
can	O
store	O
the	O
quadtree	B-Data_Structure
in	O
a	O
data	O
structure	O
for	O
ordered	O
sets	O
(	O
in	O
which	O
we	O
store	O
the	O
nodes	O
of	O
the	O
tree	O
)	O
.	O
</s>
<s>
We	O
also	O
assume	O
that	O
we	O
can	O
compute	O
in	O
time	O
the	O
lowest	O
common	O
ancestor	O
of	O
two	O
points/cells	O
in	O
the	O
quadtree	B-Data_Structure
and	O
establish	O
their	O
relative	O
Z-ordering	O
,	O
and	O
we	O
can	O
compute	O
the	O
floor	O
function	O
in	O
time	O
.	O
</s>
<s>
Find	O
the	O
existing	O
cell	O
in	O
the	O
compressed	O
tree	O
that	O
comes	O
before	O
in	O
the	O
Z-order	B-Algorithm
.	O
</s>
<s>
Else	O
,	O
find	O
what	O
would	O
have	O
been	O
the	O
lowest	O
common	O
ancestor	O
of	O
the	O
point	O
and	O
the	O
cell	O
in	O
an	O
uncompressed	O
quadtree	B-Data_Structure
.	O
</s>
<s>
Find	O
the	O
existing	O
cell	O
in	O
the	O
compressed	O
tree	O
that	O
comes	O
before	O
in	O
the	O
Z-order	B-Algorithm
and	O
return	O
it	O
.	O
</s>
<s>
Quadtrees	B-Data_Structure
,	O
particularly	O
the	O
region	O
quadtree	B-Data_Structure
,	O
have	O
lent	O
themselves	O
well	O
to	O
image	O
processing	O
applications	O
.	O
</s>
<s>
We	O
will	O
limit	O
our	O
discussion	O
to	O
binary	O
image	O
data	O
,	O
though	O
region	O
quadtrees	B-Data_Structure
and	O
the	O
image	O
processing	O
operations	O
performed	O
on	O
them	O
are	O
just	O
as	O
suitable	O
for	O
colour	O
images	O
.	O
</s>
<s>
One	O
of	O
the	O
advantages	O
of	O
using	O
quadtrees	B-Data_Structure
for	O
image	O
manipulation	O
is	O
that	O
the	O
set	O
operations	O
of	O
union	O
and	O
intersection	O
can	O
be	O
done	O
simply	O
and	O
quickly	O
.	O
</s>
<s>
Rather	O
than	O
do	O
the	O
operation	O
pixel	O
by	O
pixel	O
,	O
we	O
can	O
compute	O
the	O
union	O
more	O
efficiently	O
by	O
leveraging	O
the	O
quadtree	B-Data_Structure
's	O
ability	O
to	O
represent	O
multiple	O
pixels	O
with	O
a	O
single	O
node	O
.	O
</s>
<s>
For	O
the	O
purposes	O
of	O
discussion	O
below	O
,	O
if	O
a	O
subtree	B-Application
contains	O
both	O
black	O
and	O
white	O
pixels	O
we	O
will	O
say	O
that	O
the	O
root	O
of	O
that	O
subtree	B-Application
is	O
coloured	O
grey	O
.	O
</s>
<s>
The	O
algorithm	O
works	O
by	O
traversing	O
the	O
two	O
input	O
quadtrees	B-Data_Structure
(	O
and	O
)	O
while	O
building	O
the	O
output	O
quadtree	B-Data_Structure
.	O
</s>
<s>
If	O
only	O
one	O
of	O
them	O
is	O
black	O
and	O
the	O
other	O
is	O
gray	O
,	O
the	O
gray	O
node	O
will	O
contain	O
a	O
subtree	B-Application
underneath	O
.	O
</s>
<s>
This	O
subtree	B-Application
need	O
not	O
be	O
traversed	O
.	O
</s>
<s>
If	O
(	O
respectively	O
,	O
)	O
is	O
white	O
,	O
(	O
respectively	O
,	O
)	O
and	O
the	O
subtree	B-Application
underneath	O
it	O
(	O
if	O
any	O
)	O
is	O
copied	O
to	O
.	O
</s>
<s>
While	O
this	O
algorithm	O
works	O
,	O
it	O
does	O
not	O
by	O
itself	O
guarantee	O
a	O
minimally	O
sized	O
quadtree	B-Data_Structure
.	O
</s>
<s>
The	O
result	O
is	O
a	O
giant	O
black	O
square	O
which	O
should	O
be	O
represented	O
by	O
a	O
quadtree	B-Data_Structure
with	O
just	O
the	O
root	B-Application
node	I-Application
(	O
coloured	O
black	O
)	O
,	O
but	O
instead	O
the	O
algorithm	O
produces	O
a	O
full	O
4-ary	O
tree	O
of	O
depth	O
.	O
</s>
<s>
To	O
fix	O
this	O
,	O
we	O
perform	O
a	O
bottom-up	O
traversal	O
of	O
the	O
resulting	O
quadtree	B-Data_Structure
where	O
we	O
check	O
if	O
the	O
four	O
children	O
nodes	O
have	O
the	O
same	O
colour	O
,	O
in	O
which	O
case	O
we	O
replace	O
their	O
parent	O
with	O
a	O
leaf	O
of	O
the	O
same	O
colour	O
.	O
</s>
<s>
Using	O
the	O
quadtree	B-Data_Structure
representation	O
of	O
images	O
,	O
Samet	O
showed	O
how	O
we	O
can	O
find	O
and	O
label	O
these	O
connected	O
components	O
in	O
time	O
proportional	O
to	O
the	O
size	O
of	O
the	O
quadtree	B-Data_Structure
.	O
</s>
<s>
To	O
simplify	O
the	O
discussion	O
,	O
let	O
us	O
assume	O
the	O
children	O
of	O
a	O
node	O
in	O
the	O
quadtree	B-Data_Structure
follow	O
the	O
Z-order	B-Algorithm
(	O
SW	O
,	O
NW	O
,	O
SE	O
,	O
NE	O
)	O
.	O
</s>
<s>
Since	O
we	O
can	O
count	O
on	O
this	O
structure	O
,	O
for	O
any	O
cell	O
we	O
know	O
how	O
to	O
navigate	O
the	O
quadtree	B-Data_Structure
to	O
find	O
the	O
adjacent	O
cells	O
in	O
the	O
different	O
levels	O
of	O
the	O
hierarchy	O
.	O
</s>
<s>
Step	O
one	O
is	O
accomplished	O
with	O
a	O
post-order	O
traversal	O
of	O
the	O
quadtree	B-Data_Structure
.	O
</s>
<s>
Since	O
the	O
tree	O
is	O
organized	O
in	O
Z-order	B-Algorithm
,	O
we	O
have	O
the	O
invariant	O
that	O
the	O
Southern	O
and	O
Western	O
neighbours	O
have	O
already	O
been	O
taken	O
care	O
of	O
and	O
accounted	O
for	O
.	O
</s>
<s>
Step	O
two	O
can	O
be	O
accomplished	O
using	O
the	O
union-find	B-Algorithm
data	I-Algorithm
structure	I-Algorithm
.	O
</s>
<s>
This	O
time	O
,	O
for	O
each	O
black	O
node	O
we	O
use	O
the	O
union-find	B-Algorithm
'	O
s	O
find	O
operation	O
(	O
with	O
the	O
old	O
label	O
of	O
)	O
to	O
find	O
and	O
assign	O
its	O
new	O
label	O
(	O
associated	O
with	O
the	O
connected	O
component	O
of	O
which	O
is	O
part	O
)	O
.	O
</s>
<s>
Mesh	B-Architecture
generation	I-Architecture
is	O
essentially	O
the	O
triangulation	B-Algorithm
of	I-Algorithm
a	I-Algorithm
point	I-Algorithm
set	I-Algorithm
for	O
which	O
further	O
processing	O
may	O
be	O
performed	O
.	O
</s>
<s>
Quadtrees	B-Data_Structure
built	O
on	O
the	O
point	O
set	O
can	O
be	O
used	O
to	O
create	O
meshes	O
with	O
these	O
desired	O
properties	O
.	O
</s>
<s>
Consider	O
a	O
leaf	O
of	O
the	O
quadtree	B-Data_Structure
and	O
its	O
corresponding	O
cell	O
.	O
</s>
<s>
We	O
say	O
is	O
balanced	O
(	O
for	O
mesh	B-Architecture
generation	I-Architecture
)	O
if	O
the	O
cell	O
's	O
sides	O
are	O
intersected	O
by	O
the	O
corner	O
points	O
of	O
neighbouring	O
cells	O
at	O
most	O
once	O
on	O
each	O
side	O
.	O
</s>
<s>
This	O
means	O
that	O
the	O
quadtree	B-Data_Structure
levels	O
of	O
leaves	O
adjacent	O
to	O
differ	O
by	O
at	O
most	O
one	O
from	O
the	O
level	O
of	O
.	O
</s>
<s>
When	O
this	O
is	O
true	O
for	O
all	O
leaves	O
,	O
we	O
say	O
the	O
whole	O
quadtree	B-Data_Structure
is	O
balanced	O
(	O
for	O
mesh	B-Architecture
generation	I-Architecture
)	O
.	O
</s>
<s>
We	O
say	O
the	O
quadtree	B-Data_Structure
is	O
well-balanced	O
if	O
it	O
is	O
balanced	O
,	O
and	O
for	O
every	O
leaf	O
that	O
contains	O
a	O
point	O
of	O
the	O
point	O
set	O
,	O
its	O
extended	O
cluster	O
is	O
also	O
in	O
the	O
quadtree	B-Data_Structure
and	O
the	O
extended	O
cluster	O
contains	O
no	O
other	O
point	O
of	O
the	O
point	O
set	O
.	O
</s>
<s>
Build	O
a	O
quadtree	B-Data_Structure
on	O
the	O
input	O
points	O
.	O
</s>
<s>
Ensure	O
the	O
quadtree	B-Data_Structure
is	O
balanced	O
.	O
</s>
<s>
Transform	O
the	O
quadtree	B-Data_Structure
into	O
a	O
triangulation	O
.	O
</s>
<s>
At	O
the	O
end	O
of	O
it	O
all	O
,	O
we	O
have	O
a	O
nice	O
triangulated	O
mesh	O
of	O
our	O
point	O
set	O
built	O
from	O
a	O
quadtree	B-Data_Structure
.	O
</s>
<s>
The	O
following	O
pseudo	O
code	O
shows	O
one	O
means	O
of	O
implementing	O
a	O
quadtree	B-Data_Structure
which	O
handles	O
only	O
points	O
.	O
</s>
<s>
This	O
class	O
represents	O
both	O
one	O
quad	B-Data_Structure
tree	I-Data_Structure
and	O
the	O
node	O
where	O
it	O
is	O
rooted	O
.	O
</s>
<s>
QuadTree*	O
northWest	O
;	O
</s>
<s>
QuadTree*	O
northEast	O
;	O
</s>
<s>
QuadTree*	O
southWest	O
;	O
</s>
<s>
QuadTree*	O
southEast	O
;	O
</s>
<s>
The	O
following	O
method	O
inserts	O
a	O
point	O
into	O
the	O
appropriate	O
quad	O
of	O
a	O
quadtree	B-Data_Structure
,	O
splitting	O
if	O
necessary	O
.	O
</s>
