0% found this document useful (0 votes)
73 views

Quad Tree: Insert Function

Quadtrees are trees used to store two-dimensional spatial data, with each node having at most four children representing quadrants of the space. They are constructed by recursively subdividing space into four boxes and creating child nodes for boxes containing points. Quadtrees allow efficient searching and compression of spatial data like images. The insert and search functions run in O(log N) time by recursively comparing points to boundaries and traversing child nodes.

Uploaded by

satya Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Quad Tree: Insert Function

Quadtrees are trees used to store two-dimensional spatial data, with each node having at most four children representing quadrants of the space. They are constructed by recursively subdividing space into four boxes and creating child nodes for boxes containing points. Quadtrees allow efficient searching and compression of spatial data like images. The insert and search functions run in O(log N) time by recursively comparing points to boundaries and traversing child nodes.

Uploaded by

satya Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Quad Tree

Quadtrees are trees used to efficiently store data of points on a two-dimensional


space. In this tree, each node has at most four children.
We can construct a quadtree from a two-dimensional area using the following steps:
1. Divide the current two dimensional space into four boxes.
2. If a box contains one or more points in it, create a child object, storing in it the
two dimensional space of the box
3. If a box does not contain any points, do not create a child for it
4. Recurse for each of the children.
Quadtrees are used in image compression, where each node contains the average
colour of each of its children. The deeper you traverse in the tree, the more the detail
of the image.
Quadtrees are also used in searching for nodes in a two-dimensional area. For
instance, if you wanted to find the closest point to given coordinates, you can do it
using quadtrees.
Insert Function
The insert functions is used to insert a node into an existing Quad Tree. This function
first checks whether the given node is within the boundaries of the current quad. If it
is not, then we immediately cease the insertion. If it is within the boundaries, we
select the appropriate child to contain this node based on its location.
This function is O(Log N) where N is the size of distance.
Search Function
The search function is used to locate a node in the given quad. It can also be modified to
return the closest node to the given point. This function is implemented by taking the given
point, comparing with the boundaries of the child quads and recursing.
This function is O(Log N) where N is size of distance.
Octree is a tree data structure where each internal node has 8 children. An octree is generally used to
represent relation between objects in a 3-dimensional space. It is used in 3D computer graphics. Octrees
are also used for nearest neighbor search which can be done easily in logarithmic time.
Like a binary tree divides a 1-dimensional space into two segments, an octree subdivides the 3D
space into 8 octants where each octant is represented by a node.
An octree save a large amount of space when storing points in a 3D space, especially if the space
is sparsely populated. If there are k points in a 3D cube of dimension N, then space used by the tree
is O(klog2N)
The image below shows how an octree represents points in a space.
Algorithm
 Three types of nodes are used in octree:
1. Point node: Used to represent of a point. Is always a leaf node.
2. Empty node: Used as a leaf node to represent that no point exists in the region it represent.
3. Region node: This is always an internal node. It is used to represent a 3D region or a cuboid.
A region node always have 4 children nodes that can either be a point node or empty node.

Insertion
 Insertion: This is a recursive function used to store a point in the octree.

1. Start with root node as current node.


2. If the given point is not in cuboid represented by current node, stop insertion
with error.
3. Determine the appropriate child node to store the point.
4. If the child node is empty node, replace it with a point node representing the
point. Stop insertion.
5. If the child node is a point node, replace it with a region node. Call insert for
the point that just got replaced. Set current node as the newly formed
region
node.
6. If selected child node is a region node, set the child node as current node.
Goto step 2.

Search
 Search: This is a boolean function used to determine weather a point exists in 3D space or not.

1. Start with root node as current node.


2. If the given point is not in boundary represented by current node, stop search
with error.
3. Determine the appropriate child node to store the point.
4. If the child node is empty node, return FALSE.
5. If the child node is a point node and it matches the given point return
TRUE, otherwise return FALSE.
6. If the child node is a
Note that a 3D cuboid can be represented by 2 points of any of its diagonal.

Complexity
 Time complexity:

1. Find: O(log2N)
2. Insert: O(log2N)
3. Search: O(log2N)
 Space complexity: O(k log2N)
Where k is count of points in the space and space is of dimension N x M x O, N >= M, O.

You might also like