The quadtree data structure stores points by recursively partitioning a 2-dimensional region.
Let
At all times, every subtree rooted at node
We represent a quadtree node by the following sum type:
type quadtree_node = NoPoint
| Point of int * int
| QNode of quadtree_node (* bottom left *)
* quadtree_node (* top left *)
* quadtree_node (* bottom right *)
* quadtree_node (* top right *)
and the entire quadtree with region quadtree
type:
type quadtree = {width:int; height:int; root:quadtree_node}
Note, that we do not store the region inside the individual nodes, but just in the tree itself, so node regions must be computed during tree traversal.
- insert
Implement the functioninsert : point -> quadtree -> quadtree
that inserts a point into the tree.
Hint: You may assume that region sizes are always a power of two and that insert is only called for points
Hint: The function print_quadtree
is provided to save an svg-image of your tree.