Open In App

Exploring Range Trees

Last Updated : 13 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

While learning data structures we come across various types of data structures ranging from data structures like arrays to slightly difficult data structures like graphs and their various subcategories. One of which is the type of data structure that we are going to discuss today “Range Trees”.

What are range queries and range trees?

Before moving on with the Range Trees let us first discuss the range queries that are addressed in a very efficient manner by the range trees.

Range Queries:

  • A range query in a very basic sense is a query that demands all the points inside a set for a given interval it may involve searching as well.
  • For such types of queries, a recursive transversal of the tree is done by comparing each node and query interval.
  • The time complexity of a range query is given as O(log n + k) and storage of O(n*logd-1 n), with d, indicating the dimension of the space, n being the number of points in the tree and k being the number of points retrieved for the query.

Range Trees:

  • Now moving on to the range trees, range trees are versatile solutions to range queries. The range trees very efficiently solve the multi-dimensional range queries.
  • Two well-known range trees are the Segment Tree and Fenwick Tree.
Range-Tree-Example

An example of a 1-Dimensional range tree. Every node other than the leaf node stores the highest value in its left subtree.

In the above example the range queries can be addressed, since at each node the value of the next left subtree is either equal or less than and the values in the right sub-tree is greater than the node value. The complete illustration of range tree is given the Illustration of Range Tree section below.

Working of a Range Tree:

A range tree is made recursively. A BST(Binary Search Tree) on a dimension of points constitutes one level of a range tree and this is done until all the points are addressed which means the first level of the range tree is the BST of the first dimension of points and so on.

Each node of the tree has two information in it: 

  • The range of points for which the node is responsible.
  • The maximum value of a point in the range of node.

To address a range query, recursive traversal of the tree is done. The interval of query is compared to the range of each node in the tree. If the  interval intersects the range of node, then the maximum value of the node’s range is reported and if this is not the case then the traversal is continued on next dimension of the query interval which is the child node.

Operations on Range Trees:

The various operations of the range trees are-

1. Construction of Range Trees:

  • The construction of range trees is done from a set of multi-dimensional data points. The construction involves the augmentation of the BSTs which are built by recursive division of the data points, In more simple terms the BSTs are constructed by recursively dividing the data points and then the range tree is built from the BSTs.
  • The 1-Dimensional range tree on a set of n points is a binary search tree, Range trees in higher dimensions are built recursively by constructing a balanced binary search tree on the first coordinate of the points, and after that, for each vertex v in this tree, building a (d−1)-dimensional range tree on the points contained in the subtree of v. Building a range tree this way would require O(n logdn) time.

2. Insertion in a Range Trees:

  • Insertion in a range tree is done simply by the addition of the leaf node which is responsible for the point’s range .The time complexity of the operation is O(log n) where n is the number of data points.

3. Deletion from Range Trees:

  • In performing the deletion the point is removed from the leaf node which is responsible for the range of the point and then the tree is rebalanced. The time complexity of the operation is O(log n) where n is the number of data points.

4. Range Query:

  • A range query is performed by traversing the tree recursively and reporting the maximum value of the nodes that intersect the interval of the query. This range query utilizes the structure of the tree and is very efficient. The time complexity of the operation is O(log n) where n is the number of data points.

5. Range Minimum/Maximum Query:

  • This refers to the query that demands the minimum or maximum value of a set. This can be done by extracting all the points in the range and then taking maximum or the minimum values .The time complexity of the operation is O(log n + k) where n is the number of data points and k is the number of data points reported .

6. Range Count Operation:

  • This is a similar operation like range query but this is used for counting the number of occurrences of the data points within a specified range instead of finding the data point itself. The time complexity of the operation is O(log n + k) where n is the number of data points and k is the number of data points reported.

Illustration of Range Tree:

A Range tress is a balanced BST in which at any node, the left sub-tree will have the values less than or equal to the node value and in the right tree the value is greater than the node value and with this the tree is traversed and the range query is addressed

Example:

  • If we have to address the range query of [15, 78], then the nodes in the light colour will be traversed and after each step the value of the node will be compared to the desired value range and depending upon that left or right sub-tree will be traversed.
  • The complete of the subtree inside the box will be the output and the nodes traversed may or may not present in the output depending upon the value.
Illustration-of-Range-Trees-(1)

Illustration of Range Tree

Applications of Range Trees:

  • Spatial Indexing: Range trees can be employed in indexing of points in space and solving various spatial problems like closest pair query, window query.
  • Computational Geometry: Range Trees can be used to address various types of computational geometry related problems like finding MSTs of a set of data points.
  • Statistical Analysis: Range trees can also be used for statistical problems which may include finding the minimum or maximum of a data set or computing the average of a set of points.

Limitation of Range Trees:

  • Range Trees can only be employed for range queries.
  • Range Trees are not efficient in case of some queries like nearest neighbours queries.
  • Range Trees are difficult to implement in comparison to other data structures.

Conclusion:

The Range Trees are a very powerful and versatile data structure which can be used in handling multi-dimensional data queries in a very efficient manner which makes it an essential tool and with time these data structures are also evolving and for enhanced performance Fractional Cascading and Higher Dimensional Range Trees are also introduced. These type of data structures can help software developers to solve multi-dimensional, spatial and computational problems with ease.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads