Octree | Insertion and Searching
Octree is a tree data structure in which each internal node can have at most 8 children. Like Binary tree which divides the space two segments, Octree divides the space into at most eight-part which is called as octanes. It is used to store the 3-D point which takes a large amount of space. if all the internal node of the Octree contains exactly 8 children is called full Octree. It is also useful for high-resolution graphics like 3D computer graphics.
The Octree can be formed form 3D volume by doing the following steps:
- Divide the current 3D volume into eight boxes
- If any box has more than one point then divide it further into boxes
- Do not divide the box which has one or zero points in it
- Do this process repeatedly util all the box contains one or zero point in it
The above steps are shown in figure.
If S is the number of points in each dimension then the number of nodes that are formed in Octree is given by this formula .
Insertion in Octree:
- To insert a node in Octree, first of all, we check if a node exists or not if a node exists then return otherwise we go recursively
- First, we start with the root node and mark it as current
- Then we find the child node in which we can store the point
- If the node is empty the replace with the node we want to insert and make it a leaf node
- If the node is the leaf node then make it an internal node and if it is an internal node then go to the child node. do this process recursively till an empty node is not found
- The time complexity of this function is where N is the number of nodes
Search in Octree:
- This function is used to search the point exist is the tree or not
- Start with the root node and search recursively if the node with given point found then return true if an empty node or boundary point or empty point is encountered then return false
- If an internal node is found go that node. The time complexity of this function is also O(Log N) where N is the number of nodes
Below is the implementation of the above approach
Point already exist in the tree Point is out of bound found not found found
- It is used in 3D computer graphics games
- It is also used to find nearest neighboring objects in 3D space
- It is also used for color quantization
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.