Sri Devaraj Urs Educational Trust (R.
)
R. L. JALAPPA INSTITUTE OF TECHNOLOGY
(Approved by AICTE, New Delhi, Affiliated to VTU, Belagavi & Accredited by NAAC “A”
Grade)
Kodigehalli, Doddaballapur- 561 203
Subject Code: MCS103 Department of CS&E - PG
Subject Name: DATA STRUCTURES AND APPLICATIONS
Module Number: 01
Name of the Module: SEARCH TREES Scheme: 2024
Prepared by: Dr. Mamatha C M
Professor
Institute Vision
To be a premier Institution by imparting quality Technical education, Professional Training and Research.
Institute Mission
M1:To provide an outstanding Teaching, Learning and Research environment through Innovative Practices in
Quality Education.
M2: Develop Leaders with high level of Professionalism to have career in the Industry, Zeal for Higher
Education, focus on Entrepreneurial and Societal activities.
Department Vision- PG
To nurture students with advanced expertise and research-oriented skills in Computer Science and Engineering,
empowering them to drive technological innovation and thrive in an evolving global landscape.
Department Mission- PG
M1: To foster advanced skills in specialized domains of Computer Science and Engineering, equipping students
with the necessary expertise to address contemporary challenges and meet the evolving demands of the global
industry.
M2: To promote cutting-edge research and technological innovation, while cultivating entrepreneurship and
consultancy skills that empower students to contribute to the technological needs of industries, governments, and
society.
PROGRAMME SPECIFIC OUTCOMES (PSOs)
PSO1: Students will have a knowledge of Adva nc e d Software, Hardware, Network Models, Algorithms
PSO2: Students will be able to develop applications in the areas related to Artificial Intelligence, Machine
Learning, Data Science and IoT for efficient design of computer-based systems.
PROGRAMME EDUCATIONAL OBJECTIVES (PEOs)
PEO1: Our Graduates will have prospective careers in the IT Industry.
PEO2: Our Graduates will exhibit a high level of Professionalism and Leadership skills in work Environment.
PEO3: Our Graduates will pursue Research, and focus on Entrepreneurship.
Module-2
Tree Structures for Sets of Intervals. Interval Trees. Segment Trees. Trees for the Union of
Intervals. Trees for Sums of Weighted Interval. Trees for Interval-Restricted Maximum Sum
Queries. Orthogonal Range Trees. Higher-Dimensional Segment Trees. Other Systems of
Building Blocks. Range-Counting and the Semigroup Model. Kd-Trees and Related Structures.
Interval Trees
An Interval Tree is a specialized binary search tree (BST) used to efficiently store and manage intervals (or ranges), allowing you to
quickly query which intervals overlap with a given point or another interval.
Key Concepts:
Interval Representation: An interval is usually represented by a pair of numbers [l,r][l, r], where:
ll is the start (left endpoint).
rr is the end (right endpoint).
Operations on Interval Trees:
Insertion: Insert an interval into the tree while maintaining balance and the tree’s properties.
Deletion: Remove an interval from the tree.
Query: Given a point or an interval, find all intervals that overlap with it.
Interval Overlap:
Two intervals [l1,r1][l_1, r_1] and [l2,r2][l_2, r_2] overlap if and only if:
max(l1,l2)≤min(r1,r2)\text{max}(l_1, l_2) \leq \text{min}(r_1, r_2)
This means that the starting point of one interval is less than or equal to the ending point of the other.
Structure of an Interval Tree:
An interval tree is typically implemented as a balanced binary search tree (like AVL or Red-Black tree) with some modifications to
support efficient querying. Each node in the tree represents an interval, and it has additional information to help in quick queries:
Node Structure: Each node contains:
Interval: A pair [l,r][l, r] representing the interval.
Max: The maximum right endpoint (max) of any interval in the subtree rooted at this node. This value helps in pruning during
queries. The max value is updated during insertions and deletions.
Binary Search Tree Property: The tree is ordered by the start of the intervals. Specifically:
The left child of a node contains intervals whose starting point is less than the node’s interval's start.
The right child of a node contains intervals whose starting point is greater than or equal to the node's interval's start.
The ordering is based on the left endpoint of the intervals, but the max value is updated to help with overlap queries.
Interval Tree Operations:
1. Insertion:
Insert intervals into the tree as you would in a regular binary search tree, maintaining the tree's ordering property (sorted by left
endpoint).
After insertion, update the max value for each node. The max value for a node is the maximum right endpoint of its interval, along
with the max values from its left and right subtrees.
The update rule for the max value at each node:
max(node)=max(right endpoint of the node’s interval,max of left subtree,max of right subtree)\text{max}(\text{node}) = \max(\
text{right endpoint of the node's interval}, \text{max of left subtree}, \text{max of right subtree})
2. Query:
To find all intervals that overlap a given interval [l,r][l, r] or a point pp, you can use the following procedure:
Start at the root of the tree and recursively check the following:
If the current node's interval overlaps with the query interval [l,r][l, r], add it to the result list.
If the left child exists and its maximum right endpoint is greater than or equal to ll (the left endpoint of the query interval),
recursively query the left subtree.
Always query the right subtree because the intervals in the right subtree could have a start point greater than or equal to ll but an
endpoint that overlaps with the query interval.
By using the max values, the algorithm can efficiently prune branches that cannot possibly overlap with the query interval, thus
improving the efficiency of the search.
3. Deletion:
Deletion involves the standard process of removing a node from a binary search tree. After deletion, you need to:
Rebalance the tree if necessary (in the case of an unbalanced tree like AVL or Red-Black).
Update the max values along the path from the deleted node’s parent to the root to ensure that the tree remains consistent.
Time Complexity:
Insertion: O(logn)O(\log n) for a balanced tree, because you insert an interval just like a regular binary search tree and perform
rotations if necessary.
Query: O(logn+m)O(\log n + m), where mm is the number of intervals that overlap with the query. The logn\log n term comes from
the depth of the tree, and mm is the size of the result set.
Deletion: O(logn)O(\log n) for a balanced tree, similar to insertion.
Applications of Interval Trees:
Interval trees are useful in a variety of computational problems where you need to manage and query intervals efficiently. Some
common applications include:
Scheduling: Finding available time slots for appointments or tasks.
Computational Geometry: Solving problems like finding overlapping intervals or ranges.
Genome Sequence Analysis: Finding regions of interest in genome sequences that overlap with other regions.
Database Queries: Efficiently querying overlapping date ranges or time intervals.
Example:
Let's consider an interval tree with the following intervals:
[1,5][1, 5]
[3,7][3, 7]
[4,6][4, 6]
[8,10][8, 10]
[9,11][9, 11]
The tree might be structured as follows (assuming the intervals are balanced):
[4,6]
/ \
[1,5] [8,10]
\ / \
[3,7] [9,11]
The root of the tree is the interval [4,6][4,6], and the max value at this node would be 11 because it's the maximum right endpoint of
the intervals in the subtree. If we query for intervals overlapping with [5,9][5, 9], the tree will traverse the appropriate subtrees and
return the intervals [3,7][3, 7], [4,6][4, 6], and [8,10][8, 10] since they overlap with the query interval.
The nodes have the following structure:
typedef struct ls_n_t { key_t key;
struct ls_n_t *next;
object_t *object;
} list_node_t;
typedef struct tr_n_t { key_t key;
struct tr_n_t *left;
struct tr_n_t *right;
list_node_t *left_list;
list_node_t *right_list;
/* balancing information */
} tree_node_t;